practicalxml-commits Mailing List for Practical XML (Page 12)
Brought to you by:
kdgregory
You can subscribe to this list here.
| 2008 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
(6) |
Nov
(4) |
Dec
(35) |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2009 |
Jan
(5) |
Feb
|
Mar
|
Apr
(7) |
May
|
Jun
|
Jul
(12) |
Aug
(24) |
Sep
(39) |
Oct
(16) |
Nov
(4) |
Dec
(7) |
| 2010 |
Jan
(10) |
Feb
|
Mar
(2) |
Apr
(1) |
May
|
Jun
|
Jul
|
Aug
(1) |
Sep
|
Oct
|
Nov
(4) |
Dec
(3) |
| 2011 |
Jan
(1) |
Feb
|
Mar
(1) |
Apr
(1) |
May
(3) |
Jun
|
Jul
|
Aug
(1) |
Sep
(10) |
Oct
(1) |
Nov
(1) |
Dec
(7) |
| 2012 |
Jan
(1) |
Feb
|
Mar
|
Apr
(1) |
May
(9) |
Jun
|
Jul
(5) |
Aug
(6) |
Sep
|
Oct
(1) |
Nov
|
Dec
|
| 2013 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
(16) |
Jul
|
Aug
(6) |
Sep
(10) |
Oct
|
Nov
(2) |
Dec
|
| 2014 |
Jan
(5) |
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
(2) |
Aug
|
Sep
|
Oct
(6) |
Nov
|
Dec
|
| 2015 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
(5) |
| 2016 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
(1) |
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
|
From: Auto-Generated S. C. M. <pra...@li...> - 2008-11-18 01:51:49
|
Revision: 32
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=32&view=rev
Author: kdgregory
Date: 2008-11-18 01:51:39 +0000 (Tue, 18 Nov 2008)
Log Message:
-----------
newSchema() - remove println()
Modified Paths:
--------------
trunk/src/main/java/net/sf/practicalxml/SchemaUtil.java
Modified: trunk/src/main/java/net/sf/practicalxml/SchemaUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/SchemaUtil.java 2008-10-12 16:57:10 UTC (rev 31)
+++ trunk/src/main/java/net/sf/practicalxml/SchemaUtil.java 2008-11-18 01:51:39 UTC (rev 32)
@@ -67,8 +67,6 @@
combineSchema(combined, source);
}
- System.out.println(OutputUtil.indentedString(combined.getOwnerDocument(), 4));
-
try
{
synchronized (factory)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: Auto-Generated S. C. M. <pra...@li...> - 2008-10-12 16:57:17
|
Revision: 31
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=31&view=rev
Author: kdgregory
Date: 2008-10-12 16:57:10 +0000 (Sun, 12 Oct 2008)
Log Message:
-----------
ParseUtil: add schema validation
Modified Paths:
--------------
trunk/src/main/java/net/sf/practicalxml/ParseUtil.java
trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java
Modified: trunk/src/main/java/net/sf/practicalxml/ParseUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/ParseUtil.java 2008-10-12 13:05:05 UTC (rev 30)
+++ trunk/src/main/java/net/sf/practicalxml/ParseUtil.java 2008-10-12 16:57:10 UTC (rev 31)
@@ -6,6 +6,7 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.validation.Schema;
import net.sf.practicalxml.misc.ExceptionErrorHandler;
@@ -119,10 +120,40 @@
public static Document validatingParse(
InputSource source, ErrorHandler errHandler)
{
- return validatingParse(source, null, errHandler);
+ return validatingParse(source, (EntityResolver)null, errHandler);
}
+ /**
+ * Parses the supplied source with a namespace-aware, XSD-validating
+ * parser, using a caller-supplied error handler and entity resolver.
+ * Both of these objects may be <code>null</code>, to use the built-in
+ * defaults.
+ *
+ * @throws XmlException for any configuration or fatal execution error.
+ */
+ public static Document validatingParse(
+ InputSource source, Schema schema, ErrorHandler errHandler)
+ {
+ DocumentBuilder db = newXSDDocumentBuilder(schema);
+ if (errHandler != null)
+ db.setErrorHandler(errHandler);
+
+ try
+ {
+ return db.parse(source);
+ }
+ catch (IOException e)
+ {
+ throw new XmlException("unable to parse", e);
+ }
+ catch (SAXException e)
+ {
+ throw new XmlException("unable to parse", e);
+ }
+ }
+
+
//----------------------------------------------------------------------------
// Internals
//----------------------------------------------------------------------------
@@ -134,6 +165,9 @@
private static DocumentBuilderFactory _dtdDbf;
+ /**
+ * Returns a namespace-aware, non-validating parser.
+ */
private static synchronized DocumentBuilder newNVDocumentBuilder()
{
if (_nvDbf == null)
@@ -155,6 +189,9 @@
}
+ /**
+ * Returns a namespace-aware, DTD-validating parser.
+ */
private static synchronized DocumentBuilder newDTDDocumentBuilder()
{
if (_dtdDbf == null)
@@ -174,4 +211,28 @@
throw new XmlException("unable to confiure parser", e);
}
}
+
+
+ /**
+ * Returns a namespace-aware, XSD-validating parser using the supplied
+ * schema. Note that we don't use a singleton factory, because the schema
+ * gets applied to the factory, not the parser.
+ */
+ private static synchronized DocumentBuilder newXSDDocumentBuilder(Schema schema)
+ {
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+ dbf.setValidating(false);
+ dbf.setCoalescing(true);
+ dbf.setSchema(schema);
+
+ try
+ {
+ return _dtdDbf.newDocumentBuilder();
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new XmlException("unable to confiure parser", e);
+ }
+ }
}
Modified: trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java 2008-10-12 13:05:05 UTC (rev 30)
+++ trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java 2008-10-12 16:57:10 UTC (rev 31)
@@ -5,6 +5,11 @@
import java.util.ArrayList;
import java.util.List;
+import javax.xml.XMLConstants;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
@@ -30,7 +35,43 @@
+ "<!ATTLIST foo name CDATA #REQUIRED>";
+ /** An XSD that replicates the DTD above */
+ private final static String BASIC_XSD
+ = "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
+ + "<xsd:element name=\"foo\" type=\"FooType\"/>"
+ + "<xsd:complexType name=\"FooType\">"
+ + "<xsd:sequence>"
+ + "<xsd:element name=\"bar\" type=\"xsd:string\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>"
+ + "<xsd:element name=\"baz\" minOccurs=\"1\" maxOccurs=\"unbounded\">"
+ + "<xsd:complexType>"
+ + "</xsd:complexType>"
+ + "</xsd:element>"
+ + "</xsd:sequence>"
+ + "<xsd:attribute name=\"name\" type=\"xsd:string\" use=\"required\"/>"
+ + "</xsd:complexType>"
+ + "</xsd:schema>";
+
+
/**
+ * Creates a <code>Schema</code> object from source XML. We could use
+ * {@link SchemaUtil}, but I'd like this test to be as self-contained
+ * as possible.
+ */
+ private static Schema createSchema(String xsd)
+ {
+ try
+ {
+ SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+ return sf.newSchema(new StreamSource(new StringReader(xsd)));
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ /**
* An ErrorHandler that records its invocations, and provides asserts
* on them.
*/
@@ -63,38 +104,8 @@
assertEquals("TestErrorHandler warnings", numWarnings, warnings.size());
}
}
-
-
- /**
- * An EntityResolver that will resolve a single entity.
- */
- private static class TestEntityResolver
- implements EntityResolver
- {
- private String _publicId;
- private String _systemId;
- private String _content;
-
- public TestEntityResolver(String publicId, String systemId, String content)
- {
- _publicId = publicId;
- _systemId = systemId;
- _content = content;
- }
- public InputSource resolveEntity(String publicId, String systemId)
- throws SAXException, IOException
- {
- if (((publicId == null) || publicId.equals(_publicId))
- && ((systemId == null) || systemId.equals(_systemId)))
- {
- return new InputSource(new StringReader(_content));
- }
- return null;
- }
- }
-
//----------------------------------------------------------------------------
// Test Cases
//----------------------------------------------------------------------------
@@ -229,13 +240,40 @@
+ "<baz/>"
+ "</foo>";
+ EntityResolver resolver = new EntityResolver()
+ {
+ public InputSource resolveEntity(String publicId, String systemId)
+ throws SAXException, IOException
+ {
+ return new InputSource(new StringReader(BASIC_DTD));
+ }
+ };
TestErrorHandler errHandler = new TestErrorHandler();
- TestEntityResolver resolver = new TestEntityResolver(null, "test", BASIC_DTD);
Document doc = ParseUtil.validatingParse(
new InputSource(new StringReader(xml)),
- resolver, errHandler);
+ resolver,
+ errHandler);
assertEquals("foo", doc.getDocumentElement().getTagName());
errHandler.assertResults(0, 0, 0);
}
+
+
+ public void testValidDocumentWithSchema() throws Exception
+ {
+ String xml
+ = "<foo name='zippy'>"
+ + "<bar>something here</bar>"
+ + "<baz/>"
+ + "</foo>";
+
+ TestErrorHandler errHandler = new TestErrorHandler();
+ Document doc = ParseUtil.validatingParse(
+ new InputSource(new StringReader(xml)),
+ createSchema(BASIC_XSD),
+ errHandler);
+
+ assertEquals("foo", doc.getDocumentElement().getTagName());
+ errHandler.assertResults(0, 0, 0);
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: Auto-Generated S. C. M. <pra...@li...> - 2008-10-12 13:05:15
|
Revision: 30
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=30&view=rev
Author: kdgregory
Date: 2008-10-12 13:05:05 +0000 (Sun, 12 Oct 2008)
Log Message:
-----------
TestParseUtil: lost a few test cases in previous revision
Modified Paths:
--------------
trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java
Modified: trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java 2008-10-08 01:43:42 UTC (rev 29)
+++ trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java 2008-10-12 13:05:05 UTC (rev 30)
@@ -1,5 +1,6 @@
package net.sf.practicalxml;
+import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
@@ -7,6 +8,7 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
+import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -61,8 +63,38 @@
assertEquals("TestErrorHandler warnings", numWarnings, warnings.size());
}
}
+
+
+ /**
+ * An EntityResolver that will resolve a single entity.
+ */
+ private static class TestEntityResolver
+ implements EntityResolver
+ {
+ private String _publicId;
+ private String _systemId;
+ private String _content;
+
+ public TestEntityResolver(String publicId, String systemId, String content)
+ {
+ _publicId = publicId;
+ _systemId = systemId;
+ _content = content;
+ }
+ public InputSource resolveEntity(String publicId, String systemId)
+ throws SAXException, IOException
+ {
+ if (((publicId == null) || publicId.equals(_publicId))
+ && ((systemId == null) || systemId.equals(_systemId)))
+ {
+ return new InputSource(new StringReader(_content));
+ }
+ return null;
+ }
+ }
+
//----------------------------------------------------------------------------
// Test Cases
//----------------------------------------------------------------------------
@@ -168,4 +200,42 @@
assertEquals("foo", doc.getDocumentElement().getTagName());
errHandler.assertResults(0, 2, 0);
}
+
+
+ public void testValidatingParseWithMissingDoctype() throws Exception
+ {
+ String xml
+ = "<foo name='zippy'>"
+ + "<bar>something here</bar>"
+ + "<baz/>"
+ + "</foo>";
+
+ TestErrorHandler errHandler = new TestErrorHandler();
+ Document doc = ParseUtil.validatingParse(
+ new InputSource(new StringReader(xml)),
+ errHandler);
+
+ assertEquals("foo", doc.getDocumentElement().getTagName());
+ errHandler.assertResults(0, 2, 0);
+ }
+
+
+ public void testValidatingParseWithResolvedDTD() throws Exception
+ {
+ String xml
+ = "<!DOCTYPE foo SYSTEM \"test\">"
+ + "<foo name='zippy'>"
+ + "<bar>something here</bar>"
+ + "<baz/>"
+ + "</foo>";
+
+ TestErrorHandler errHandler = new TestErrorHandler();
+ TestEntityResolver resolver = new TestEntityResolver(null, "test", BASIC_DTD);
+ Document doc = ParseUtil.validatingParse(
+ new InputSource(new StringReader(xml)),
+ resolver, errHandler);
+
+ assertEquals("foo", doc.getDocumentElement().getTagName());
+ errHandler.assertResults(0, 0, 0);
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: Auto-Generated S. C. M. <pra...@li...> - 2008-10-08 01:43:47
|
Revision: 29
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=29&view=rev
Author: kdgregory
Date: 2008-10-08 01:43:42 +0000 (Wed, 08 Oct 2008)
Log Message:
-----------
ParseUtil: rename "parseWithDTD" to "validatingParse"
Modified Paths:
--------------
trunk/src/main/java/net/sf/practicalxml/ParseUtil.java
trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java
Modified: trunk/src/main/java/net/sf/practicalxml/ParseUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/ParseUtil.java 2008-10-08 00:08:37 UTC (rev 28)
+++ trunk/src/main/java/net/sf/practicalxml/ParseUtil.java 2008-10-08 01:43:42 UTC (rev 29)
@@ -84,7 +84,7 @@
*
* @throws XmlException for any configuration or fatal execution error.
*/
- public static Document parseWithDTD(
+ public static Document validatingParse(
InputSource source, EntityResolver resolver,
ErrorHandler errHandler)
{
@@ -116,10 +116,10 @@
*
* @throws XmlException for any configuration or fatal execution error.
*/
- public static Document parseWithDTD(
+ public static Document validatingParse(
InputSource source, ErrorHandler errHandler)
{
- return parseWithDTD(source, null, errHandler);
+ return validatingParse(source, null, errHandler);
}
Modified: trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java 2008-10-08 00:08:37 UTC (rev 28)
+++ trunk/src/test/java/net/sf/practicalxml/TestParseUtil.java 2008-10-08 01:43:42 UTC (rev 29)
@@ -1,6 +1,5 @@
package net.sf.practicalxml;
-import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
@@ -8,7 +7,6 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
-import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -22,15 +20,24 @@
// Support Code
//----------------------------------------------------------------------------
+ /** A basic DTD, shared between validating parser tests */
+ private final static String BASIC_DTD
+ = "<!ELEMENT foo (bar*,baz+)>"
+ + "<!ELEMENT bar (#PCDATA)>"
+ + "<!ELEMENT baz EMPTY>"
+ + "<!ATTLIST foo name CDATA #REQUIRED>";
+
+
/**
- * An error handler that records its invocations.
+ * An ErrorHandler that records its invocations, and provides asserts
+ * on them.
*/
- private static class MockErrorHandler
+ private static class TestErrorHandler
implements ErrorHandler
{
- public List<SAXParseException> warnings = new ArrayList<SAXParseException>();
- public List<SAXParseException> errors = new ArrayList<SAXParseException>();
public List<SAXParseException> fatalErrors = new ArrayList<SAXParseException>();
+ public List<SAXParseException> errors = new ArrayList<SAXParseException>();
+ public List<SAXParseException> warnings = new ArrayList<SAXParseException>();
public void error(SAXParseException exception) throws SAXException
{
@@ -46,6 +53,13 @@
{
warnings.add(exception);
}
+
+ public void assertResults(int numFatal, int numErrors, int numWarnings)
+ {
+ assertEquals("TestErrorHandler fatal errors", numFatal, fatalErrors.size());
+ assertEquals("TestErrorHandler errors", numErrors, errors.size());
+ assertEquals("TestErrorHandler warnings", numWarnings, warnings.size());
+ }
}
@@ -86,23 +100,6 @@
}
- public void testMalformedStringParseWithErrorHandler() throws Exception
- {
- String xml = "<foo><bar>baz</foo>";
- MockErrorHandler handler = new MockErrorHandler();
- try
- {
- ParseUtil.parse(new InputSource(new StringReader(xml)), handler);
- fail("able to parse malformed XML");
- }
- catch (XmlException e)
- {
- // even though we're handling the error, the parser will abend
- }
- assertEquals(1, handler.fatalErrors.size());
- }
-
-
public void testNamespacedStringParse() throws Exception
{
String xml = "<foo xmlns:me=\"argle\">"
@@ -136,103 +133,39 @@
}
- public void testValidatingParseWithMissingDoctype() throws Exception
+ public void testValidDocumentWithInternalDoctype() throws Exception
{
- InputSource input = new InputSource(new StringReader(
- "<foo>bar</foo>"));
- MockErrorHandler errHandler = new MockErrorHandler();
+ String xml
+ = "<!DOCTYPE foo [" + BASIC_DTD + "]>"
+ + "<foo name='zippy'>"
+ + "<bar>something here</bar>"
+ + "<baz/>"
+ + "</foo>";
- Document dom = ParseUtil.parseWithDTD(input, errHandler);
- assertEquals("foo", dom.getDocumentElement().getNodeName());
- assertTrue(errHandler.errors.size() > 0);
- assertTrue(errHandler.fatalErrors.size() == 0);
- assertTrue(errHandler.warnings.size() == 0);
+ TestErrorHandler errHandler = new TestErrorHandler();
+ Document doc = ParseUtil.validatingParse(
+ new InputSource(new StringReader(xml)),
+ errHandler);
+
+ assertEquals("foo", doc.getDocumentElement().getTagName());
+ errHandler.assertResults(0, 0, 0);
}
- public void testValidatingParseWithInternalDTD() throws Exception
+ public void testInvalidDocumentWithInternalDoctype() throws Exception
{
- String dtd
- = "<!ELEMENT foo (bar*)>"
- + "<!ELEMENT bar (#PCDATA)>"
- + "<!ATTLIST foo baz CDATA #REQUIRED>";
+ String xml
+ = "<!DOCTYPE foo [" + BASIC_DTD + "]>"
+ + "<foo>"
+ + "<bar>something here</bar>"
+ + "</foo>";
- // first test will be valid
- InputSource in1 = new InputSource(new StringReader(
- "<!DOCTYPE foo [" + dtd + "]>"
- + "<foo baz=\"argle\">"
- + "<bar>argle</bar>"
- + "</foo>"));
- MockErrorHandler eh1 = new MockErrorHandler();
+ TestErrorHandler errHandler = new TestErrorHandler();
+ Document doc = ParseUtil.validatingParse(
+ new InputSource(new StringReader(xml)),
+ errHandler);
- Document dom1 = ParseUtil.parseWithDTD(in1, eh1);
- assertEquals("foo", dom1.getDocumentElement().getNodeName());
- assertTrue(eh1.errors.size() == 0);
- assertTrue(eh1.fatalErrors.size() == 0);
- assertTrue(eh1.warnings.size() == 0);
-
- // second won't -- missing required attribute
- InputSource in2 = new InputSource(new StringReader(
- "<!DOCTYPE foo [" + dtd + "]>\n"
- + "<foo>\n"
- + "<bar>argle</bar>\n"
- + "</foo>"));
- MockErrorHandler eh2 = new MockErrorHandler();
-
- Document dom2 = ParseUtil.parseWithDTD(in2, eh2);
- assertEquals("foo", dom2.getDocumentElement().getNodeName());
- assertTrue(eh2.errors.size() > 0);
- assertTrue(eh2.fatalErrors.size() == 0);
- assertTrue(eh2.warnings.size() == 0);
-
- assertEquals(2, eh2.errors.get(0).getLineNumber());
-// assertEquals(4, eh2.errors.get(0).getColumnNumber());
+ assertEquals("foo", doc.getDocumentElement().getTagName());
+ errHandler.assertResults(0, 2, 0);
}
-
-
- // note: this will fail if you don't have Internet access
- // ... also, unit tests shouldn't reach outside the JVM
-// public void testValidatingParseWithExternalDTD() throws Exception
-// {
-// InputSource input = new InputSource(new StringReader(
-// "<!DOCTYPE web-app SYSTEM \"http://java.sun.com/dtd/web-app_2_3.dtd\">"
-// + "<web-app>"
-// + "<display-name>"
-// + "Simple Web-App"
-// + "</display-name>"
-// + "</web-app>"));
-// MockErrorHandler errHandler = new MockErrorHandler();
-//
-// Document dom = ParseUtil.parseWithDTD(input, null, errHandler);
-// assertEquals("web-app", dom.getDocumentElement().getNodeName());
-// assertTrue(errHandler.errors.size() == 0);
-// assertTrue(errHandler.fatalErrors.size() == 0);
-// assertTrue(errHandler.warnings.size() == 0);
-// }
-
-
- public void testValidatingParseWithLocalResolver() throws Exception
- {
- InputSource input = new InputSource(new StringReader(
- "<!DOCTYPE foo SYSTEM \"http://java.sun.com/dtd/web-app_2_3.dtd\">"
- + "<foo><bar>baz</bar></foo>"));
- MockErrorHandler errHandler = new MockErrorHandler();
- EntityResolver resolver = new EntityResolver()
- {
- public InputSource resolveEntity(String publicId, String systemId)
- throws SAXException, IOException
- {
- // we don't care what they want; return what we want
- return new InputSource(new StringReader(
- "<!ELEMENT foo (bar*)>"
- + "<!ELEMENT bar (#PCDATA)>"));
- }
- };
-
- Document dom = ParseUtil.parseWithDTD(input, resolver, errHandler);
- assertEquals("foo", dom.getDocumentElement().getNodeName());
- assertTrue(errHandler.errors.size() == 0);
- assertTrue(errHandler.fatalErrors.size() == 0);
- assertTrue(errHandler.warnings.size() == 0);
- }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: Auto-Generated S. C. M. <pra...@li...> - 2008-10-08 00:08:43
|
Revision: 28
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=28&view=rev
Author: kdgregory
Date: 2008-10-08 00:08:37 +0000 (Wed, 08 Oct 2008)
Log Message:
-----------
SchemaUtil: initial revision
Added Paths:
-----------
trunk/src/main/java/net/sf/practicalxml/SchemaUtil.java
trunk/src/test/java/net/sf/practicalxml/TestSchemaUtil.java
Added: trunk/src/main/java/net/sf/practicalxml/SchemaUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/SchemaUtil.java (rev 0)
+++ trunk/src/main/java/net/sf/practicalxml/SchemaUtil.java 2008-10-08 00:08:37 UTC (rev 28)
@@ -0,0 +1,109 @@
+package net.sf.practicalxml;
+
+import javax.xml.XMLConstants;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+
+/**
+ * A collection of static utility methods for working with XML Schema
+ * documents. Since <code>javax.xml.validation.Schema</code> is an
+ * opaque object, most of these actually work with DOM documents that
+ * contain an XSD.
+ */
+public class SchemaUtil
+{
+ private final static String SCHEMA_NS = XMLConstants.W3C_XML_SCHEMA_NS_URI;
+ private final static String SCHEMA_PREFIX = "xsd";
+ private final static String SCHEMA_ROOT = "schema";
+
+
+ /**
+ * Creates a new <code>javax.xml.validation.SchemaFactory</code> instance
+ * for validation with XML Schema, which reports errors via the specified
+ * error handler.
+ */
+ public synchronized static SchemaFactory newFactory(ErrorHandler errHandler)
+ {
+ SchemaFactory fact = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+ fact.setErrorHandler(errHandler);
+ return fact;
+ }
+
+
+ /**
+ * A workaround for Sun bug <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6198705">
+ * 6198705</code>, also known as Xerces bug <a href="http://issues.apache.org/jira/browse/XERCESJ-1130">
+ * XERCESJ-1130</code>, which prevents the <code>SchemaFactory.ewSchema</code>
+ * method from combining multiple sources that have the same namespace.
+ * <p>
+ * This method works by parsing the children of the passed sources,
+ * verifying that their root element is <code><xsd:schema></code>,
+ * combining the children of this root into a new document, and calling
+ * <code>newSchema()</code> on the result.
+ *
+ * @param factory Used to create the schema object. This factory's
+ * <code>ErrorHandler</code> is also used to report
+ * errors when parsing the source documents.
+ * @param sources The source schema documents.
+ *
+ * @throws XmlException on any failure that is not handled by the
+ * factory's <code>ErrorHandler</code>. This includes parse
+ * errors and local validation of the source root element.
+ */
+ public static Schema newSchema(SchemaFactory factory, InputSource... sources)
+ {
+ Element combined = DomUtil.newDocument(SCHEMA_NS, SCHEMA_PREFIX + ":" + SCHEMA_ROOT);
+ for (InputSource source : sources)
+ {
+ combineSchema(combined, source);
+ }
+
+ System.out.println(OutputUtil.indentedString(combined.getOwnerDocument(), 4));
+
+ try
+ {
+ synchronized (factory)
+ {
+ return factory.newSchema(new DOMSource(combined.getOwnerDocument()));
+ }
+ }
+ catch (SAXException e)
+ {
+ throw new XmlException("unable to generate schema", e);
+ }
+ }
+
+
+//----------------------------------------------------------------------------
+// Internals
+//----------------------------------------------------------------------------
+
+ /**
+ * Parses, verifies, and combines a source document.
+ */
+ private static void combineSchema(Element newRoot, InputSource source)
+ {
+ Document doc = ParseUtil.parse(source);
+ Element root = doc.getDocumentElement();
+ if (!DomUtil.isNamed(root, SCHEMA_NS, SCHEMA_ROOT))
+ {
+ throw new XmlException("invalid root element name: {"
+ + root.getNamespaceURI() + "}"
+ + DomUtil.getLocalName(root));
+ }
+ for (Element child : DomUtil.getChildren(root))
+ {
+ Node tmp = newRoot.getOwnerDocument().importNode(child, true);
+ newRoot.appendChild(tmp);
+ }
+ }
+}
Added: trunk/src/test/java/net/sf/practicalxml/TestSchemaUtil.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/TestSchemaUtil.java (rev 0)
+++ trunk/src/test/java/net/sf/practicalxml/TestSchemaUtil.java 2008-10-08 00:08:37 UTC (rev 28)
@@ -0,0 +1,91 @@
+package net.sf.practicalxml;
+
+import java.io.Reader;
+import java.io.StringReader;
+
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+
+import net.sf.practicalxml.misc.ExceptionErrorHandler;
+
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+
+import junit.framework.TestCase;
+
+
+public class TestSchemaUtil extends TestCase
+{
+//----------------------------------------------------------------------------
+// Definitions for newSchema() testing
+//----------------------------------------------------------------------------
+
+ public final static String NEW_SCHEMA_START
+ = "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
+
+ public final static String NEW_SCHEMA_DEF_1
+ = "<xsd:element name=\"foo\" type=\"FooType\"/>";
+
+ public final static String NEW_SCHEMA_DEF_2
+ = "<xsd:complexType name=\"FooType\">"
+ + "<xsd:sequence>"
+ + "<xsd:element name=\"argle\" type=\"xsd:integer\"/>"
+ + "<xsd:element name=\"bargle\" type=\"BarType\"/>"
+ + "</xsd:sequence>"
+ + "</xsd:complexType>";
+
+ public final static String NEW_SCHEMA_DEF_3
+ = "<xsd:simpleType name=\"BarType\">"
+ + "<xsd:restriction base=\"xsd:string\">"
+ + "</xsd:restriction>"
+ + "</xsd:simpleType>";
+
+ public final static String NEW_SCHEMA_FINISH
+ = "</xsd:schema>";
+
+
+//----------------------------------------------------------------------------
+// Test Cases
+//----------------------------------------------------------------------------
+
+ public void testNewFactory() throws Exception
+ {
+ ErrorHandler errHandler = new ExceptionErrorHandler();
+ SchemaFactory fact = SchemaUtil.newFactory(errHandler);
+
+ assertNotNull(fact);
+ assertSame(errHandler, fact.getErrorHandler());
+ }
+
+
+ public void testNewSchemaWithSingleSource() throws Exception
+ {
+ Reader xsd = new StringReader(
+ NEW_SCHEMA_START
+ + NEW_SCHEMA_DEF_1 + NEW_SCHEMA_DEF_2 + NEW_SCHEMA_DEF_3
+ + NEW_SCHEMA_FINISH);
+
+ Schema schema = SchemaUtil.newSchema(
+ SchemaUtil.newFactory(new ExceptionErrorHandler()),
+ new InputSource(xsd));
+ assertNotNull(schema);
+ }
+
+
+ public void testNewSchemaWithMultipleSources() throws Exception
+ {
+ Reader xsd1 = new StringReader(
+ NEW_SCHEMA_START + NEW_SCHEMA_DEF_1 + NEW_SCHEMA_FINISH);
+ Reader xsd2 = new StringReader(
+ NEW_SCHEMA_START + NEW_SCHEMA_DEF_2 + NEW_SCHEMA_FINISH);
+ Reader xsd3 = new StringReader(
+ NEW_SCHEMA_START + NEW_SCHEMA_DEF_3 + NEW_SCHEMA_FINISH);
+
+ Schema schema = SchemaUtil.newSchema(
+ SchemaUtil.newFactory(new ExceptionErrorHandler()),
+ new InputSource(xsd1),
+ new InputSource(xsd2),
+ new InputSource(xsd3));
+ assertNotNull(schema);
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: Auto-Generated S. C. M. <pra...@li...> - 2008-10-07 23:50:08
|
Revision: 27
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=27&view=rev
Author: kdgregory
Date: 2008-10-07 23:50:03 +0000 (Tue, 07 Oct 2008)
Log Message:
-----------
minor change to test mailing list
Modified Paths:
--------------
trunk/pom.xml
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2008-10-07 23:38:14 UTC (rev 26)
+++ trunk/pom.xml 2008-10-07 23:50:03 UTC (rev 27)
@@ -14,9 +14,6 @@
to the JDK.
</description>
- <organization>
- </organization>
-
<mailingLists>
<mailingList>
<name>practicalxml-commits</name>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: Auto-Generated S. C. M. <pra...@li...> - 2008-10-07 13:43:20
|