cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1537200 - in /cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation: Stax2ValidationUtils.java W3CMultiSchemaFactory.java
Date Wed, 30 Oct 2013 17:47:19 GMT
Author: dkulp
Date: Wed Oct 30 17:47:18 2013
New Revision: 1537200

URL: http://svn.apache.org/r1537200
Log:
[CXF-5367] Work around some issues with MSV with included schemas

Modified:
    cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation/Stax2ValidationUtils.java
    cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation/W3CMultiSchemaFactory.java

Modified: cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation/Stax2ValidationUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation/Stax2ValidationUtils.java?rev=1537200&r1=1537199&r2=1537200&view=diff
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation/Stax2ValidationUtils.java
(original)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation/Stax2ValidationUtils.java
Wed Oct 30 17:47:18 2013
@@ -141,7 +141,6 @@ class Stax2ValidationUtils {
                     if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri)) {
                         continue;
                     }
-                    LOG.info(uri);
         
                     Element serialized = schemaInfo.getElement();
                     String schemaSystemId = sch.getSourceURI();

Modified: cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation/W3CMultiSchemaFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation/W3CMultiSchemaFactory.java?rev=1537200&r1=1537199&r2=1537200&view=diff
==============================================================================
--- cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation/W3CMultiSchemaFactory.java
(original)
+++ cxf/trunk/core/src/main/java/org/apache/cxf/staxutils/validation/W3CMultiSchemaFactory.java
Wed Oct 30 17:47:18 2013
@@ -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 exten
     
     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());
@@ -67,6 +127,9 @@ public class W3CMultiSchemaFactory exten
         
         XMLSchemaGrammar grammar = multiSchemaReader.getResult();
         if (grammar == null) {
+            grammar = xmlSchemaReader.getResultNoError();
+        }        
+        if (grammar == null) {
             throw new XMLStreamException("Failed to load schemas");
         }
         return new W3CSchema(grammar); 



Mime
View raw message