-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Class to address issue of MSV being shaded making it hard/impossible … #106
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
src/main/java/com/ctc/wstx/msv/W3CMultiSchemaFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* Woodstox XML processor | ||
* | ||
* Copyright (c) 2004- Tatu Saloranta, tatu.saloranta@iki.fi | ||
* | ||
* Licensed under the License specified in the file LICENSE which is | ||
* included with the source code. | ||
* You may not use this file except in compliance with the License. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.ctc.wstx.msv; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import javax.xml.parsers.SAXParserFactory; | ||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.transform.Source; | ||
import javax.xml.transform.dom.DOMSource; | ||
|
||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
|
||
import org.xml.sax.InputSource; | ||
import org.xml.sax.Locator; | ||
|
||
import com.ctc.wstx.msv.BaseSchemaFactory; | ||
import com.ctc.wstx.msv.W3CSchema; | ||
import com.sun.msv.grammar.ExpressionPool; | ||
import com.sun.msv.grammar.xmlschema.XMLSchemaGrammar; | ||
import com.sun.msv.grammar.xmlschema.XMLSchemaSchema; | ||
import com.sun.msv.reader.GrammarReaderController; | ||
import com.sun.msv.reader.State; | ||
import com.sun.msv.reader.xmlschema.EmbeddedSchema; | ||
import com.sun.msv.reader.xmlschema.MultiSchemaReader; | ||
import com.sun.msv.reader.xmlschema.SchemaState; | ||
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 | ||
* against specific W3C Schema instances. It requires | ||
* Sun Multi-Schema Validator | ||
* (http://www.sun.com/software/xml/developers/multischema/) | ||
* to work, and acts as a quite thin wrapper layer, similar to | ||
* how matching RelaxNG validator works | ||
*/ | ||
public class W3CMultiSchemaFactory extends BaseSchemaFactory { | ||
|
||
private MultiSchemaReader multiSchemaReader; | ||
private SAXParserFactory parserFactory; | ||
private RecursiveAllowedXMLSchemaReader xmlSchemaReader; | ||
|
||
public W3CMultiSchemaFactory() { | ||
super(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA); | ||
} | ||
|
||
static class RecursiveAllowedXMLSchemaReader extends XMLSchemaReader { | ||
Set<String> sysIds = new TreeSet<String>(); | ||
RecursiveAllowedXMLSchemaReader(GrammarReaderController controller, SAXParserFactory parserFactory) { | ||
super(controller, parserFactory, new StateFactory() { | ||
public State schemaHead(String expectedNamespace) { | ||
return new SchemaState(expectedNamespace) { | ||
private XMLSchemaSchema old; | ||
protected void endSelf() { | ||
super.endSelf(); | ||
RecursiveAllowedXMLSchemaReader r = (RecursiveAllowedXMLSchemaReader)reader; | ||
r.currentSchema = old; | ||
} | ||
protected void onTargetNamespaceResolved(String targetNs, boolean ignoreContents) { | ||
|
||
RecursiveAllowedXMLSchemaReader r = (RecursiveAllowedXMLSchemaReader)reader; | ||
// sets new XMLSchemaGrammar object. | ||
old = r.currentSchema; | ||
r.currentSchema = r.getOrCreateSchema(targetNs); | ||
if (ignoreContents) { | ||
return; | ||
} | ||
if (!r.isSchemaDefined(r.currentSchema)) { | ||
r.markSchemaAsDefined(r.currentSchema); | ||
} | ||
} | ||
}; | ||
} | ||
}, new ExpressionPool()); | ||
} | ||
|
||
public void setLocator(Locator locator) { | ||
if (locator == null && getLocator() != null && getLocator().getSystemId() != null) { | ||
sysIds.add(getLocator().getSystemId()); | ||
} | ||
super.setLocator(locator); | ||
} | ||
public void switchSource(Source source, State newState) { | ||
String url = source.getSystemId(); | ||
if (url != null && sysIds.contains(url)) { | ||
return; | ||
} | ||
super.switchSource(source, newState); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Creates an XMLValidateSchema that can be used to validate XML instances against any of the shemas | ||
* defined in the Map of schemaSources. | ||
*/ | ||
public XMLValidationSchema createSchema(String baseURI, | ||
Map<String, Source> schemaSources) throws XMLStreamException { | ||
|
||
Map<String, EmbeddedSchema> embeddedSources = new HashMap<String, EmbeddedSchema>(); | ||
for (Map.Entry<String, Source> source : schemaSources.entrySet()) { | ||
if (source.getValue() instanceof DOMSource) { | ||
Node nd = ((DOMSource)source.getValue()).getNode(); | ||
Element el = null; | ||
if (nd instanceof Element) { | ||
el = (Element)nd; | ||
} else if (nd instanceof Document) { | ||
el = ((Document)nd).getDocumentElement(); | ||
} | ||
embeddedSources.put(source.getKey(), new EmbeddedSchema(source.getValue().getSystemId(), el)); | ||
} | ||
} | ||
parserFactory = getSaxFactory(); | ||
|
||
WSDLGrammarReaderController ctrl = new WSDLGrammarReaderController(null, baseURI, embeddedSources); | ||
xmlSchemaReader = new RecursiveAllowedXMLSchemaReader(ctrl, parserFactory); | ||
multiSchemaReader = new MultiSchemaReader(xmlSchemaReader); | ||
for (Source source : schemaSources.values()) { | ||
multiSchemaReader.parse(source); | ||
} | ||
|
||
XMLSchemaGrammar grammar = multiSchemaReader.getResult(); | ||
if (grammar == null) { | ||
throw new XMLStreamException("Failed to load schemas"); | ||
} | ||
return new W3CSchema(grammar); | ||
} | ||
|
||
@Override | ||
protected XMLValidationSchema loadSchema(InputSource src, Object sysRef) throws XMLStreamException { | ||
throw new XMLStreamException("W3CMultiSchemaFactory does not support the provider API."); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this does not actually really implement
XMLValidationSchemaFactory
, it might be better NOT to extendBaseSchemaFactory
(so users do not try to use it as pluggable factory that way)?Or does it require something from the base class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only thing it used from the base class was the saxParser. That's simple enough to to create on demand. I've updated the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok good. Thanks!