Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes in support of Issue #175 #181 #208

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/main/java/com/ctc/wstx/msv/W3CMultiSchemaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;

import org.codehaus.stax2.validation.XMLValidationSchema;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.xml.sax.EntityResolver;
import org.xml.sax.Locator;

import com.sun.msv.grammar.ExpressionPool;
Expand All @@ -42,8 +43,6 @@
import com.sun.msv.reader.xmlschema.WSDLGrammarReaderController;
import com.sun.msv.reader.xmlschema.XMLSchemaReader;

import org.codehaus.stax2.validation.XMLValidationSchema;

/**
* This is a StAX2 schema factory that can parse and create schema instances
* for creating validators that validate documents to check their validity
Expand All @@ -66,7 +65,8 @@
public class W3CMultiSchemaFactory
{
private final SAXParserFactory parserFactory;

private EntityResolver entityResolver;

public W3CMultiSchemaFactory() {
parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
Expand Down Expand Up @@ -125,7 +125,7 @@ public void switchSource(Source source, State newState) {
}

}

/**
* Creates an XMLValidateSchema that can be used to validate XML instances against
* any of the schemas defined in the Map of schemaSources.
Expand All @@ -136,6 +136,20 @@ public void switchSource(Source source, State newState) {
public XMLValidationSchema createSchema(String baseURI,
Map<String, Source> schemaSources) throws XMLStreamException
{
return createSchema(baseURI, schemaSources, this.entityResolver);
}

/**
* Creates an XMLValidateSchema that can be used to validate XML instances against
* any of the schemas defined in the Map of schemaSources.
*
* @param baseURI Base URI for resolving dependant schemas
* @param schemaSources Map of schemas, namespace to Source
* @param entityResolver Entity resolver used to resolve path to unknown schemas
*/
public XMLValidationSchema createSchema(String baseURI,
Map<String, Source> schemaSources, EntityResolver entityResolver) throws XMLStreamException
{
Map<String, EmbeddedSchema> embeddedSources = new HashMap<String, EmbeddedSchema>();
for (Map.Entry<String, Source> source : schemaSources.entrySet()) {
if (source.getValue() instanceof DOMSource) {
Expand All @@ -150,7 +164,9 @@ public XMLValidationSchema createSchema(String baseURI,
}
}

WSDLGrammarReaderController ctrl = new WSDLGrammarReaderController(null, baseURI, embeddedSources);
final WSDLGrammarReaderController ctrl = new WSDLGrammarReaderController(null, baseURI, embeddedSources);
ctrl.setEntityResolver(entityResolver);

final RecursiveAllowedXMLSchemaReader xmlSchemaReader = new RecursiveAllowedXMLSchemaReader(ctrl, parserFactory);
final MultiSchemaReader multiSchemaReader = new MultiSchemaReader(xmlSchemaReader);
for (Source source : schemaSources.values()) {
Expand All @@ -164,4 +180,10 @@ public XMLValidationSchema createSchema(String baseURI,
return new W3CSchema(grammar);
}

public W3CMultiSchemaFactory setEntityResolver(EntityResolver entityResolver)
{
this.entityResolver = entityResolver;

return this;
}
}
13 changes: 13 additions & 0 deletions src/test/java/wstxtest/msv/Issue_175_ver1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<main:Issue175 xmlns:main="http://www.example.org/issue_175_main"
xmlns:imprt="http://www.example.org/issue_175_import"
main:XmlVersion="1"
imprt:XmlVersion="1" >
<main:Data imprt:Attrib1="Attribute #1 - Ver1"
imprt:Attrib2="Attribute #2 - Ver1" >
<main:Element1>Element #1 from Version 1</main:Element1>
<main:Element2>12345</main:Element2>
<main:Element3 imprt:Attrib3="true">Element #2 from Version 1</main:Element3>
<main:Element4>ENUM4</main:Element4>
</main:Data>
</main:Issue175>
14 changes: 14 additions & 0 deletions src/test/java/wstxtest/msv/Issue_175_ver2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<main:Issue175 xmlns:main="http://www.example.org/issue_175_main"
xmlns:imprt="http://www.example.org/issue_175_import"
main:XmlVersion="2"
imprt:XmlVersion="1" >
<main:Data imprt:Attrib1="Attribute #1 - Ver1"
imprt:Attrib2="Attribute #2 - Ver1" >
<main:Element1>Element #1 from Version 2</main:Element1>
<main:Element2>12345</main:Element2>
<main:Element3 imprt:Attrib3="false">Element #2 from Version 2</main:Element3>
<main:Element4>ENUM1</main:Element4>
<main:Element5>Element #2 from Version 2</main:Element5>
</main:Data>
</main:Issue175>
15 changes: 15 additions & 0 deletions src/test/java/wstxtest/msv/Issue_175_ver3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<main:Issue175 xmlns:main="http://www.example.org/issue_175_main"
xmlns:imprt="http://www.example.org/issue_175_import"
main:XmlVersion="3"
imprt:XmlVersion="2" >
<main:Data imprt:Attrib1="Attribute #1 - Ver1"
imprt:Attrib2="Attribute #2 - Ver1"
imprt:Attrib4="false">
<main:Element1>Element #1 from Main Version 1</main:Element1>
<main:Element2>12345</main:Element2>
<main:Element3 imprt:Attrib3="true">Element #2 from Main Version 3</main:Element3>
<main:Element4 imprt:Attrib5="New attrib Import Ver 2">ENUM2</main:Element4>
<main:Element5>Element #5 from Main Version 3</main:Element5>
</main:Data>
</main:Issue175>
Loading
Loading