You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by bi...@apache.org on 2008/01/11 15:42:36 UTC

svn commit: r611188 - in /incubator/cxf/trunk: api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java

Author: bimargulies
Date: Fri Jan 11 06:42:26 2008
New Revision: 611188

URL: http://svn.apache.org/viewvc?rev=611188&view=rev
Log:
Improve esthetics of some of the code related to schema validation.

Modified:
    incubator/cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java

Modified: incubator/cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java?rev=611188&r1=611187&r2=611188&view=diff
==============================================================================
--- incubator/cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java (original)
+++ incubator/cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java Fri Jan 11 06:42:26 2008
@@ -21,6 +21,8 @@
 
 import java.io.InputStream;
 import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
@@ -62,7 +64,6 @@
 import org.apache.cxf.common.i18n.Message;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
-import org.apache.cxf.common.xmlschema.SchemaCollection;
 import org.apache.cxf.endpoint.EndpointResolverRegistry;
 import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.endpoint.ServerRegistry;
@@ -84,6 +85,70 @@
  */
 public final class EndpointReferenceUtils {
 
+    /**
+     * We want to load the schemas, including references to external schemas, into a SchemaFactory
+     * to validate. There seem to be bugs in resolving inter-schema references in Xerces, so even when we are 
+     * handing the factory all the schemas, interrelated with <import> elements, we need
+     * to also hand over extra copies (!) as character images when requested.
+     * 
+     * To do this, we use the DOM representation kept in the SchemaInfo. This has the bonus
+     * of benefiting from the use of the catalog resolver in there, which is missing from
+     * the code in here.
+     */
+    private static final class SchemaLSResourceResolver implements LSResourceResolver {
+        private final ServiceInfo si;
+
+        private SchemaLSResourceResolver(ServiceInfo serviceInfo) {
+            this.si = serviceInfo;
+        }
+        
+        private Reader getSchemaAsStream(Element schemaElement) {
+            DOMSource source = new DOMSource(schemaElement);
+            StringWriter writer = new StringWriter();
+            StreamResult result = new StreamResult(writer);
+            try {
+                TransformerFactory.newInstance().newTransformer().transform(source, result);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+            return new StringReader(writer.toString());
+        }
+
+        public LSInput resolveResource(String type, String namespaceURI, String publicId,
+                                       String systemId, String baseURI) {
+            for (SchemaInfo schemaInfo : si.getSchemas()) {
+                XmlSchema sch = schemaInfo.getSchema();
+                if (namespaceURI.equals(sch.getTargetNamespace())) {
+                    LSInputImpl impl = new LSInputImpl();
+                    Element schemaAsDom = schemaInfo.getElement();
+                    if (schemaAsDom != null) {
+                        impl.setCharacterStream(getSchemaAsStream(schemaAsDom));
+                        return impl;
+                    }
+                    // otherwise, go ahead and assume it's out there somewhere.
+                    // this needs catalog support, does it not?
+                    InputStream ins = null;
+                    try {
+                        URL url = new URL(sch.getSourceURI());
+                        ins = url.openStream();
+                    } catch (Exception e) {
+                        //ignore, we'll just use what we have.  (though
+                        //bugs in XmlSchema could make this less useful)
+                    }
+                    
+                    if (ins == null) {
+                        LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
+                        sch.write(out);
+                        ins = out.createInputStream();
+                    }
+                    impl.setByteStream(ins);
+                    return impl;
+                }
+            }
+            return null;
+        }
+    }
+
     public static final String ANONYMOUS_ADDRESS = "http://www.w3.org/2005/08/addressing/anonymous";
 
     private static final Logger LOG = LogUtils.getL7dLogger(EndpointReferenceUtils.class);
@@ -455,52 +520,26 @@
             SchemaFactory factory = SchemaFactory.newInstance(
                 XMLConstants.W3C_XML_SCHEMA_NS_URI);
             List<Source> schemaSources = new ArrayList<Source>();
-            final SchemaCollection sc = serviceInfo.getXmlSchemaCollection();
             for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
                 Source source = new DOMSource(schemaInfo.getElement());
-                if (source != null) {
-                    source.setSystemId(schemaInfo.getElement().getBaseURI());
-                    schemaSources.add(source);
+                String baseURI = schemaInfo.getElement().getBaseURI();
+                if (baseURI != null) {
+                    source.setSystemId(baseURI);
+                } else {
+                    source.setSystemId(schemaInfo.getSystemId());
                 }
+                schemaSources.add(source);
             }
             try {
-                factory.setResourceResolver(new LSResourceResolver() {
-                    public LSInput resolveResource(String type, String namespaceURI, String publicId,
-                                                   String systemId, String baseURI) {
-                        for (XmlSchema sch : sc.getXmlSchemas()) {
-                            if (namespaceURI.equals(sch.getTargetNamespace())) {
-                                LSInputImpl impl = new LSInputImpl();
-                                InputStream ins = null;
-                                try {
-                                    URL url = new URL(sch.getSourceURI());
-                                    ins = url.openStream();
-                                } catch (Exception e) {
-                                    //ignore, we'll just use what we have.  (though
-                                    //bugs in XmlSchema could make this less useful)
-                                }
-                                
-                                if (ins == null) {
-                                    LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
-                                    sch.write(out);
-                                    ins = out.createInputStream();
-                                }
-                                impl.setByteStream(ins);
-                                return impl;
-                            }
-                        }
-                        return null;
-                    }
-                    
-                });
-                schema = factory.newSchema(schemaSources.toArray(
-                    new Source[schemaSources.size()]));
+                factory.setResourceResolver(new SchemaLSResourceResolver(serviceInfo));
+                schema = factory.newSchema(schemaSources.toArray(new Source[schemaSources.size()]));
                 if (schema != null) {
                     serviceInfo.setProperty(Schema.class.getName(), schema);
                     LOG.log(Level.FINE, "Obtained schema from ServiceInfo");
                 }
             } catch (SAXException ex) {
                 // Something not right with the schema from the wsdl.
-                LOG.log(Level.WARNING, "SAXException for newSchema()", ex);
+                LOG.log(Level.WARNING, "SAXException for newSchema() on ", ex);
             }
             
         }

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java?rev=611188&r1=611187&r2=611188&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java (original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/databinding/source/AbstractDataBinding.java Fri Jan 11 06:42:26 2008
@@ -69,7 +69,7 @@
         }
         SchemaInfo schema = new SchemaInfo(serviceInfo, ns);
         schema.setSystemId(systemId);
-        XmlSchema xmlSchema = col.read(d.getDocumentElement());
+        XmlSchema xmlSchema = col.read(d, null);
         schema.setSchema(xmlSchema);
         serviceInfo.addSchema(schema);
         return xmlSchema;