Skip to content
Permalink
Browse files
[CXF-5367] Work around some issues with MSV with included schemas
git-svn-id: https://svn.apache.org/repos/asf/cxf/trunk@1537200 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
dkulp committed Oct 30, 2013
1 parent 78d67e6 commit 5d512079a047cd0c934164cc76b5c6663d5ca8b6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
@@ -141,7 +141,6 @@ private XMLValidationSchema getValidator(Endpoint endpoint, ServiceInfo serviceI
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri)) {
continue;
}
LOG.info(uri);

Element serialized = schemaInfo.getElement();
String schemaSystemId = sch.getSourceURI();
@@ -24,17 +24,26 @@
package org.apache.cxf.staxutils.validation;

import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;

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.MultiSchemaReader;
import com.sun.msv.reader.xmlschema.SchemaState;
import com.sun.msv.reader.xmlschema.XMLSchemaReader;

import org.codehaus.stax2.validation.XMLValidationSchema;
@@ -46,18 +55,69 @@ public class W3CMultiSchemaFactory extends BaseSchemaFactory {

private MultiSchemaReader multiSchemaReader;
private SAXParserFactory parserFactory;
private XMLSchemaReader xmlSchemaReader;
private RecursiveAllowedXMLSchemaReader xmlSchemaReader;

public W3CMultiSchemaFactory() {
super(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA);
}

static class RecursiveAllowedXMLSchemaReader extends XMLSchemaReader {
Set<String> sysIds = new TreeSet<String>();
public 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 final XMLSchemaGrammar getResultNoError() {
return grammar;
}

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);
}

}

public XMLValidationSchema loadSchemas(String baseURI,
Map<String, EmbeddedSchema> sources) throws XMLStreamException {
parserFactory = getSaxFactory();

ResolvingGrammarReaderController ctrl = new ResolvingGrammarReaderController(baseURI, sources);
xmlSchemaReader = new XMLSchemaReader(ctrl, parserFactory);
xmlSchemaReader = new RecursiveAllowedXMLSchemaReader(ctrl, parserFactory);
multiSchemaReader = new MultiSchemaReader(xmlSchemaReader);
for (EmbeddedSchema source : sources.values()) {
DOMSource domSource = new DOMSource(source.getSchemaElement());
@@ -66,6 +126,9 @@ public XMLValidationSchema loadSchemas(String baseURI,
}

XMLSchemaGrammar grammar = multiSchemaReader.getResult();
if (grammar == null) {
grammar = xmlSchemaReader.getResultNoError();
}
if (grammar == null) {
throw new XMLStreamException("Failed to load schemas");
}

0 comments on commit 5d51207

Please sign in to comment.