You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2007/07/21 21:13:08 UTC

svn commit: r558369 - in /incubator/tuscany/java/sca/modules/interface-wsdl-xml/src: main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/ test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/ test/resources/wsdl/ test/resources/xsd/

Author: rfeng
Date: Sat Jul 21 12:13:07 2007
New Revision: 558369

URL: http://svn.apache.org/viewvc?view=rev&rev=558369
Log:
Improve the import/include support for WSDL/XSD

Added:
    incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelper.java   (with props)
    incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessorTestCase.java   (with props)
    incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/
    incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/helloworld-interface.wsdl
    incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/helloworld-service.wsdl
    incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/
    incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/greeting.xsd   (with props)
    incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/name.xsd   (with props)
Modified:
    incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java
    incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XSDDocumentProcessor.java

Modified: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java?view=diff&rev=558369&r1=558368&r2=558369
==============================================================================
--- incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java (original)
+++ incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java Sat Jul 21 12:13:07 2007
@@ -23,14 +23,23 @@
 import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;
-import java.util.HashMap;
+import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Vector;
 
+import javax.wsdl.Binding;
+import javax.wsdl.BindingOperation;
 import javax.wsdl.Definition;
+import javax.wsdl.Fault;
 import javax.wsdl.Import;
+import javax.wsdl.Message;
+import javax.wsdl.Operation;
+import javax.wsdl.Port;
+import javax.wsdl.PortType;
+import javax.wsdl.Service;
 import javax.wsdl.Types;
 import javax.wsdl.WSDLException;
 import javax.wsdl.extensions.ExtensionRegistry;
@@ -46,14 +55,14 @@
 import org.apache.tuscany.sca.contribution.service.ContributionRuntimeException;
 import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition;
 import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory;
+import org.apache.tuscany.sca.interfacedef.wsdl.xml.XMLDocumentHelper.URIResolverImpl;
 import org.apache.ws.commons.schema.XmlSchemaCollection;
-import org.apache.ws.commons.schema.resolver.URIResolver;
 import org.w3c.dom.Element;
 import org.xml.sax.InputSource;
 
 /**
  * An ArtifactProcessor for WSDL documents.
- *
+ * 
  * @version $Rev$ $Date$
  */
 public class WSDLDocumentProcessor implements URLArtifactProcessor<WSDLDefinition> {
@@ -61,7 +70,7 @@
     private javax.wsdl.factory.WSDLFactory wsdlFactory;
     private ExtensionRegistry wsdlExtensionRegistry;
     private WSDLFactory factory;
-    
+
     private Map<String, WSDLDefinition> loadedDefinitions = new Hashtable<String, WSDLDefinition>();
 
     /**
@@ -78,10 +87,19 @@
         }
 
         public void close() {
+            try {
+                inputStream.close();
+            } catch (IOException e) {
+                // Ignore
+            }
         }
 
         public InputSource getBaseInputSource() {
-            return new InputSource(inputStream);
+            try {
+                return XMLDocumentHelper.getInputSource(base, inputStream);
+            } catch (IOException e) {
+                throw new IllegalArgumentException(e);
+            }
         }
 
         public String getBaseURI() {
@@ -90,9 +108,12 @@
 
         public InputSource getImportInputSource(String parentLocation, String importLocation) {
             try {
+                if (importLocation == null || importLocation.startsWith("/")) {
+                    return null;
+                }
                 URL url = new URL(new URL(parentLocation), importLocation);
                 latestImportURI = url.toString();
-                return new InputSource(url.openStream());
+                return XMLDocumentHelper.getInputSource(url);
             } catch (Exception e) {
                 throw new ContributionRuntimeException(e);
             }
@@ -104,26 +125,9 @@
 
     }
 
-    /**
-     * URI resolver implementation for xml schema
-     */
-    private class URIResolverImpl implements URIResolver {
-
-        public org.xml.sax.InputSource resolveEntity(java.lang.String targetNamespace,
-                                                     java.lang.String schemaLocation,
-                                                     java.lang.String baseUri) {
-            try {
-                URL url = new URL(new URL(baseUri), schemaLocation);
-                return new InputSource(url.openStream());
-            } catch (IOException e) {
-                return null;
-            }
-        }
-    }
-
     public WSDLDocumentProcessor(WSDLFactory factory, javax.wsdl.factory.WSDLFactory wsdlFactory) {
         this.factory = factory;
-        
+
         if (wsdlFactory != null) {
             this.wsdlFactory = wsdlFactory;
         } else {
@@ -133,10 +137,10 @@
                 throw new ContributionRuntimeException(e);
             }
         }
-        
+
         wsdlExtensionRegistry = this.wsdlFactory.newPopulatedExtensionRegistry();
     }
-    
+
     private void readInlineSchemas(Definition definition, WSDLDefinition wsdlDefinition) {
         Types types = definition.getTypes();
         if (types != null) {
@@ -145,13 +149,7 @@
                 if (ext instanceof Schema) {
                     Element element = ((Schema)ext).getElement();
 
-                    // TODO: fix to make includes in imported
-                    //       schema work. The XmlSchema library was crashing
-                    //       because the base uri was not set. This doesn't
-                    //       affect imports. Need to check that this
-                    //       is the right approach for XSDs included by a
-                    //       XSD which is itself imported inline in a WSDL
-                    XmlSchemaCollection schemaCollection = wsdlDefinition.getInlinedSchemas();            
+                    XmlSchemaCollection schemaCollection = wsdlDefinition.getInlinedSchemas();
                     schemaCollection.setBaseUri(((Schema)ext).getDocumentBaseURI());
 
                     wsdlDefinition.getInlinedSchemas().read(element, element.getBaseURI());
@@ -159,19 +157,7 @@
             }
         }
     }
-    
-    @SuppressWarnings("unchecked")
-    private void mergeDefinition(Definition existingDefn, Definition definition) {
-        if (existingDefn != null ) {
-            //merge into existing defn
-            existingDefn.getImports().putAll(definition.getImports());
-            existingDefn.getMessages().putAll(definition.getMessages());
-            existingDefn.getPortTypes().putAll(definition.getPortTypes());
-            existingDefn.getBindings().putAll(definition.getBindings());
-            existingDefn.getServices().putAll(definition.getServices());
-        } 
-    }
-    
+
     @SuppressWarnings("unchecked")
     public WSDLDefinition read(URL contributionURL, URI artifactURI, URL artifactURL) throws ContributionReadException {
         try {
@@ -180,55 +166,318 @@
             InputStream is = artifactURL.openStream();
             WSDLReader reader = wsdlFactory.newWSDLReader();
             reader.setFeature("javax.wsdl.verbose", false);
+            reader.setFeature("javax.wsdl.importDocuments", true);
+            // FIXME: We need to decide if we should disable the import processing by WSDL4J
+            // reader.setFeature("javax.wsdl.importDocuments", false);
             reader.setExtensionRegistry(wsdlExtensionRegistry);
 
             WSDLLocatorImpl locator = new WSDLLocatorImpl(artifactURL, is);
             Definition definition = reader.readWSDL(locator);
-            
+
             WSDLDefinition wsdlDefinition = loadedDefinitions.get(definition.getTargetNamespace());
-            if ( wsdlDefinition != null ) {
-                mergeDefinition(wsdlDefinition.getDefinition(), definition);
+            if (wsdlDefinition != null) {
+                merge(wsdlDefinition.getDefinition(), definition);
             } else {
                 wsdlDefinition = factory.createWSDLDefinition();
                 wsdlDefinition.setDefinition(definition);
                 loadedDefinitions.put(definition.getTargetNamespace(), wsdlDefinition);
             }
-            
+
             //Read inline schemas 
             readInlineSchemas(definition, wsdlDefinition);
-             
+
             //read the inline schemas for wsdl imports
-            if ( definition.getImports().size() > 0 ) {
+            if (definition.getImports().size() > 0) {
                 Iterator<Vector<Import>> importsIterator = definition.getImports().values().iterator();
                 Vector<Import> imports = null;
                 Import anImport = null;
                 while (importsIterator.hasNext()) {
                     imports = importsIterator.next();
-                    for ( int count = 0 ; count < imports.size() ; ++count ) {
+                    for (int count = 0; count < imports.size(); ++count) {
                         anImport = imports.elementAt(count);
-//                      Read inline schemas 
-                        readInlineSchemas(anImport.getDefinition(), wsdlDefinition);
+                        // Read inline schemas 
+                        if (anImport.getDefinition() != null) {
+                            readInlineSchemas(anImport.getDefinition(), wsdlDefinition);
+                        }
                     }
                 }
             }
-            
+
             return wsdlDefinition;
-            
+
         } catch (WSDLException e) {
             throw new ContributionReadException(e);
         } catch (IOException e) {
             throw new ContributionReadException(e);
         }
     }
-    
+
     public void resolve(WSDLDefinition model, ModelResolver resolver) throws ContributionResolveException {
+        Definition definition = model.getDefinition();
+        if (definition != null) {
+            for (Object imports : definition.getImports().values()) {
+                List importList = (List)imports;
+                for (Object i : importList) {
+                    Import imp = (Import)i;
+                    if (imp.getDefinition() != null) {
+                        continue;
+                    }
+                    if (imp.getLocationURI() == null) {
+                        // We need to resolve it by QName
+                        WSDLDefinition proxy = factory.createWSDLDefinition();
+                        proxy.setUnresolved(true);
+                        proxy.setNamespace(imp.getNamespaceURI());
+                        WSDLDefinition resolved = resolver.resolveModel(WSDLDefinition.class, proxy);
+                        if (resolved != null && !resolved.isUnresolved()) {
+                            imp.setDefinition(resolved.getDefinition());
+                        }
+                    } else {
+                        String location = imp.getLocationURI();
+                        URI uri = URI.create(location);
+                        if (uri.isAbsolute()) {
+                            WSDLDefinition resolved;
+                            try {
+                                resolved = read(null, uri, uri.toURL());
+                                imp.setDefinition(resolved.getDefinition());
+                            } catch (Exception e) {
+                                throw new ContributionResolveException(e);
+                            }
+                        } else {
+                            if (location.startsWith("/")) {
+                                // This is a relative URI against a contribution
+                                location = location.substring(1);
+                                // TODO: Need to resolve it against the contribution
+                            } else {
+                                // This is a relative URI against the WSDL document
+                                URI baseURI = URI.create(model.getDefinition().getDocumentBaseURI());
+                                URI locationURI = baseURI.resolve(location);
+                                WSDLDefinition resolved;
+                                try {
+                                    resolved = read(null, locationURI, locationURI.toURL());
+                                    imp.setDefinition(resolved.getDefinition());
+                                } catch (Exception e) {
+                                    throw new ContributionResolveException(e);
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
     }
-    
+
     public String getArtifactType() {
         return ".wsdl";
     }
-    
+
     public Class<WSDLDefinition> getModelType() {
         return WSDLDefinition.class;
     }
+
+    /**
+     * Merge a set of WSDLs into a facade Definition
+     * 
+     * @param definitions
+     * @return
+     */
+    private Definition merge(Definition target, Definition source) {
+        for (Iterator j = source.getImports().values().iterator(); j.hasNext();) {
+            List list = (List)j.next();
+            for (Iterator k = list.iterator(); k.hasNext();)
+                target.addImport((Import)k.next());
+        }
+
+        for (Iterator k = source.getBindings().values().iterator(); k.hasNext();) {
+            Binding binding = (Binding)k.next();
+            if (!binding.isUndefined())
+                target.getBindings().put(binding.getQName(), binding);
+        }
+
+        target.getExtensibilityElements().addAll(source.getExtensibilityElements());
+
+        for (Iterator k = source.getMessages().values().iterator(); k.hasNext();) {
+            Message msg = (Message)k.next();
+            if (!msg.isUndefined())
+                target.getMessages().put(msg.getQName(), msg);
+        }
+
+        target.getNamespaces().putAll(source.getNamespaces());
+
+        for (Iterator k = source.getPortTypes().values().iterator(); k.hasNext();) {
+            PortType portType = (PortType)k.next();
+            if (!portType.isUndefined())
+                target.getPortTypes().put(portType.getQName(), portType);
+        }
+
+        target.getServices().putAll(source.getServices());
+
+        if (target.getTypes() == null) {
+            target.setTypes(target.createTypes());
+        }
+        if (source.getTypes() != null)
+            target.getTypes().getExtensibilityElements().addAll(source.getTypes().getExtensibilityElements());
+        return target;
+
+    }
+
+    /**
+     * Resolve a definition by QName
+     * 
+     * @param name
+     * @return
+     */
+    private Definition resolveDefinition(QName name) {
+        return null;
+    }
+
+    /**
+     * Resolve the undefined elements in the WSDL definition
+     * 
+     * @param definition
+     */
+    private void resolveDefinition(Definition definition) {
+        if (definition == null)
+            return;
+        for (Iterator i = definition.getMessages().values().iterator(); i.hasNext();) {
+            resolveElement(definition, i.next());
+        }
+        for (Iterator i = definition.getPortTypes().values().iterator(); i.hasNext();) {
+            resolveElement(definition, i.next());
+        }
+        for (Iterator i = definition.getBindings().values().iterator(); i.hasNext();) {
+            resolveElement(definition, i.next());
+        }
+        for (Iterator i = definition.getServices().values().iterator(); i.hasNext();) {
+            resolveElement(definition, i.next());
+        }
+    }
+
+    /**
+     * @param elements
+     * @param location
+     */
+    private Object resolveElement(Definition definition, Object element) {
+        if (element == null)
+            return null;
+        QName name = null;
+        if (element instanceof Binding) {
+            Binding binding = (Binding)element;
+            if (binding.isUndefined()) {
+                name = binding.getQName();
+                Definition resolvedDefinition = resolveDefinition(name);
+                if (resolvedDefinition != null && resolvedDefinition != definition) {
+                    Binding resovledBinding = resolvedDefinition.getBinding(name);
+                    if (resovledBinding != null && resovledBinding != binding) {
+                        binding = resovledBinding;
+                        if (definition != null) {
+                            definition.getBindings().put(name, binding);
+                        }
+                    }
+                }
+            }
+            PortType portType = binding.getPortType();
+            PortType resolvedPortType = (PortType)resolveElement(null, portType);
+            if (resolvedPortType != null && resolvedPortType != portType) {
+                portType = resolvedPortType;
+                binding.setPortType(portType);
+            }
+
+            for (Iterator i = binding.getBindingOperations().iterator(); i.hasNext();) {
+                BindingOperation bindingOperation = (BindingOperation)i.next();
+                Operation operation = bindingOperation.getOperation();
+                if (operation != null && operation.isUndefined()) {
+                    String inputName =
+                        bindingOperation.getBindingInput() == null ? null : bindingOperation.getBindingInput()
+                            .getName();
+                    String outputName =
+                        bindingOperation.getBindingOutput() == null ? null : bindingOperation.getBindingOutput()
+                            .getName();
+                    Operation resolvedOperation =
+                        (Operation)portType.getOperation(operation.getName(), inputName, outputName);
+                    if (resolvedOperation != null && operation != resolvedOperation)
+                        bindingOperation.setOperation(resolvedOperation);
+                }
+            }
+            return binding;
+        } else if (element instanceof Message) {
+            Message message = (Message)element;
+            if (message.isUndefined()) {
+                name = message.getQName();
+                Definition resolvedDefinition = resolveDefinition(name);
+                if (resolvedDefinition != null && resolvedDefinition != definition) {
+                    Message resolvedMessage = resolvedDefinition.getMessage(name);
+                    if (resolvedMessage != null && resolvedMessage != message) {
+                        message = resolvedMessage;
+                        if (definition != null)
+                            definition.getMessages().put(name, message);
+                    }
+                }
+            }
+            return message;
+        } else if (element instanceof PortType) {
+            PortType portType = (PortType)element;
+            if (portType.isUndefined()) {
+                name = portType.getQName();
+                Definition resolvedDefinition = resolveDefinition(name);
+                if (resolvedDefinition != null && resolvedDefinition != definition) {
+                    PortType resolvedPortType = resolvedDefinition.getPortType(name);
+                    if (resolvedPortType != null && resolvedPortType != portType) {
+                        portType = resolvedPortType;
+                        if (definition != null)
+                            definition.getPortTypes().put(name, portType);
+                    }
+                }
+            }
+            List operations = new ArrayList(portType.getOperations());
+            for (Iterator i = operations.iterator(); i.hasNext();) {
+                Operation operation = (Operation)i.next();
+                Operation resolvedOperation = (Operation)resolveElement(null, operation);
+                if (resolvedOperation != null && resolvedOperation != operation) {
+                    int index = portType.getOperations().indexOf(operation);
+                    portType.getOperations().set(index, resolvedOperation);
+                }
+            }
+            return portType;
+        } else if (element instanceof Service) {
+            Service service = (Service)element;
+            name = service.getQName();
+            for (Iterator j = service.getPorts().values().iterator(); j.hasNext();) {
+                Port port = (Port)j.next();
+                Binding binding = port.getBinding();
+                Binding resolvedBinding = (Binding)resolveElement(null, binding);
+                if (resolvedBinding != null && resolvedBinding != binding) {
+                    port.setBinding(resolvedBinding);
+                }
+            }
+            return service;
+        } else if (element instanceof Operation) {
+            Operation operation = (Operation)element;
+            if (operation.getInput() != null) {
+                Message message = operation.getInput().getMessage();
+                Message resolvedMessage = (Message)resolveElement(null, message);
+                if (resolvedMessage != null && resolvedMessage != message)
+                    operation.getInput().setMessage(resolvedMessage);
+            }
+            if (operation.getOutput() != null) {
+                Message message = operation.getOutput().getMessage();
+                Message resolvedMessage = (Message)resolveElement(null, message);
+                if (resolvedMessage != null && resolvedMessage != message)
+                    operation.getOutput().setMessage(resolvedMessage);
+            }
+            for (Iterator j = operation.getFaults().values().iterator(); j.hasNext();) {
+                Fault fault = (Fault)j.next();
+                Message message = fault.getMessage();
+                Message resolvedMessage = (Message)resolveElement(null, message);
+                if (resolvedMessage != null && resolvedMessage != message)
+                    fault.setMessage(resolvedMessage);
+            }
+            if (operation.isUndefined())
+                operation.setUndefined(false);
+            return operation;
+        } else {
+            return element;
+        }
+
+    }
+
 }

Added: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelper.java?view=auto&rev=558369
==============================================================================
--- incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelper.java (added)
+++ incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelper.java Sat Jul 21 12:13:07 2007
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * 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 org.apache.tuscany.sca.interfacedef.wsdl.xml;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+
+import org.apache.ws.commons.schema.resolver.URIResolver;
+import org.xml.sax.InputSource;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class XMLDocumentHelper {
+
+   protected static final int BUFFER_SIZE = 256;
+
+    /**
+     * Detect the XML encoding of the WSDL document
+     * 
+     * @param is The input stream
+     * @return The encoding
+     * @throws IOException
+     */
+    public static String getEncoding(InputStream is) throws IOException {
+        if (!is.markSupported())
+            is = new BufferedInputStream(is);
+    
+        byte[] buffer = readBuffer(is);
+        return getXMLEncoding(buffer);
+    }
+
+    /**
+     * Searches the array of bytes to determine the XML encoding.
+     */
+    protected static String getXMLEncoding(byte[] bytes) {
+        String javaEncoding = null;
+    
+        if (bytes.length >= 4) {
+            if (((bytes[0] == -2) && (bytes[1] == -1)) || ((bytes[0] == 0) && (bytes[1] == 60)))
+                javaEncoding = "UnicodeBig";
+            else if (((bytes[0] == -1) && (bytes[1] == -2)) || ((bytes[0] == 60) && (bytes[1] == 0)))
+                javaEncoding = "UnicodeLittle";
+            else if ((bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65))
+                javaEncoding = "UTF8";
+        }
+    
+        String header = null;
+    
+        try {
+            if (javaEncoding != null)
+                header = new String(bytes, 0, bytes.length, javaEncoding);
+            else
+                header = new String(bytes, 0, bytes.length);
+        } catch (UnsupportedEncodingException e) {
+            return null;
+        }
+    
+        if (!header.startsWith("<?xml"))
+            return "UTF-8";
+    
+        int endOfXMLPI = header.indexOf("?>");
+        int encodingIndex = header.indexOf("encoding", 6);
+    
+        if ((encodingIndex == -1) || (encodingIndex > endOfXMLPI))
+            return "UTF-8";
+    
+        int firstQuoteIndex = header.indexOf("\"", encodingIndex);
+        int lastQuoteIndex;
+    
+        if ((firstQuoteIndex == -1) || (firstQuoteIndex > endOfXMLPI)) {
+            firstQuoteIndex = header.indexOf("'", encodingIndex);
+            lastQuoteIndex = header.indexOf("'", firstQuoteIndex + 1);
+        } else
+            lastQuoteIndex = header.indexOf("\"", firstQuoteIndex + 1);
+    
+        return header.substring(firstQuoteIndex + 1, lastQuoteIndex);
+    }
+
+    protected static byte[] readBuffer(InputStream is) throws IOException {
+        if (is.available() == 0) {
+            return new byte[0];
+        }
+    
+        byte[] buffer = new byte[BUFFER_SIZE];
+        is.mark(BUFFER_SIZE);
+        int bytesRead = is.read(buffer, 0, BUFFER_SIZE);
+        int totalBytesRead = bytesRead;
+    
+        while (bytesRead != -1 && (totalBytesRead < BUFFER_SIZE)) {
+            bytesRead = is.read(buffer, totalBytesRead, BUFFER_SIZE - totalBytesRead);
+    
+            if (bytesRead != -1)
+                totalBytesRead += bytesRead;
+        }
+    
+        if (totalBytesRead < BUFFER_SIZE) {
+            byte[] smallerBuffer = new byte[totalBytesRead];
+            System.arraycopy(buffer, 0, smallerBuffer, 0, totalBytesRead);
+            smallerBuffer = buffer;
+        }
+    
+        is.reset();
+        return buffer;
+    }
+
+    public static InputSource getInputSource(URL url) throws IOException {
+        InputStream is = url.openStream();
+        return getInputSource(url, is);
+    }
+
+    public static InputSource getInputSource(URL url, InputStream is) throws IOException {
+        is = new BufferedInputStream(is);
+        String encoding = getEncoding(is);
+        InputSource inputSource = new InputSource(is);
+        inputSource.setEncoding(encoding);
+        // [rfeng] Make sure we set the system id as it will be used as the base URI for nested import/include 
+        inputSource.setSystemId(url.toString());
+        return inputSource;
+    }
+    
+    /**
+     * URI resolver implementation for xml schema
+     */
+    public static class URIResolverImpl implements URIResolver {
+    
+        public org.xml.sax.InputSource resolveEntity(java.lang.String targetNamespace,
+                                                     java.lang.String schemaLocation,
+                                                     java.lang.String baseUri) {
+            try {
+                if (schemaLocation == null || schemaLocation.startsWith("/")) {
+                    return null;
+                }
+                URL url = new URL(new URL(baseUri), schemaLocation);
+                return getInputSource(url);
+            } catch (IOException e) {
+                return null;
+            }
+        }
+    }
+
+ 
+}

Propchange: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XSDDocumentProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XSDDocumentProcessor.java?view=diff&rev=558369&r1=558368&r2=558369
==============================================================================
--- incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XSDDocumentProcessor.java (original)
+++ incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XSDDocumentProcessor.java Sat Jul 21 12:13:07 2007
@@ -33,8 +33,6 @@
 import org.apache.tuscany.sca.interfacedef.wsdl.XSDefinition;
 import org.apache.ws.commons.schema.XmlSchema;
 import org.apache.ws.commons.schema.XmlSchemaCollection;
-import org.apache.ws.commons.schema.resolver.URIResolver;
-import org.xml.sax.InputSource;
 
 /**
  * An ArtifactProcessor for XSD documents.
@@ -45,23 +43,6 @@
 
     private WSDLFactory factory;
 
-    /**
-     * URI resolver implementation for xml schema
-     */
-    private class URIResolverImpl implements URIResolver {
-
-        public org.xml.sax.InputSource resolveEntity(java.lang.String targetNamespace,
-                                                     java.lang.String schemaLocation,
-                                                     java.lang.String baseUri) {
-            try {
-                URL url = new URL(new URL(baseUri), schemaLocation);
-                return new InputSource(url.openStream());
-            } catch (IOException e) {
-                return null;
-            }
-        }
-    }
-
     public XSDDocumentProcessor(WSDLFactory factory) {
         this.factory = factory;
     }
@@ -74,7 +55,7 @@
             try {
     
                 XmlSchemaCollection collection = new XmlSchemaCollection();
-                collection.setSchemaResolver(new URIResolverImpl());
+                collection.setSchemaResolver(new XMLDocumentHelper.URIResolverImpl());
                 XmlSchema schema = collection.read(new InputStreamReader(is), null);
     
                 XSDefinition xsDefinition = factory.createXSDefinition();

Added: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessorTestCase.java?view=auto&rev=558369
==============================================================================
--- incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessorTestCase.java (added)
+++ incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessorTestCase.java Sat Jul 21 12:13:07 2007
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * 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 org.apache.tuscany.sca.interfacedef.wsdl.xml;
+
+import java.net.URI;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.List;
+
+import javax.wsdl.Import;
+
+import junit.framework.Assert;
+
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.interfacedef.wsdl.DefaultWSDLFactory;
+import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class WSDLDocumentProcessorTestCase {
+    private WSDLDocumentProcessor processor;
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @Before
+    public void setUp() throws Exception {
+        processor = new WSDLDocumentProcessor(new DefaultWSDLFactory(), null);
+    }
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @After
+    public void tearDown() throws Exception {
+    }
+
+    @Test
+    public void testWSDL() throws Exception {
+        URL url = getClass().getResource("/wsdl/helloworld-service.wsdl");
+        WSDLDefinition definition = processor.read(null, URI.create("wsdl/helloworld-service.wsdl"), url);
+        Assert.assertNotNull(definition);
+        URL url1 = getClass().getResource("/wsdl/helloworld-interface.wsdl");
+        WSDLDefinition definition1 = processor.read(null, URI.create("wsdl/helloworld-interface.wsdl"), url1);
+        ModelResolver resolver = new MockResolver();
+        resolver.addModel(definition);
+        resolver.addModel(definition1);
+        processor.resolve(definition, resolver);
+        List imports = (List)definition.getDefinition().getImports().get("http://helloworld");
+        Assert.assertNotNull(imports);
+        Assert.assertNotNull(((Import)imports.get(0)).getDefinition());
+    }
+
+    private static class MockResolver extends HashMap implements ModelResolver {
+
+        /**
+         * @see org.apache.tuscany.sca.contribution.resolver.ModelResolver#addModel(java.lang.Object)
+         */
+        public void addModel(Object resolved) {
+            super.put(resolved, resolved);
+        }
+
+        /**
+         * @see org.apache.tuscany.sca.contribution.resolver.ModelResolver#removeModel(java.lang.Object)
+         */
+        public Object removeModel(Object resolved) {
+            return super.remove(resolved);
+        }
+
+        /**
+         * @see org.apache.tuscany.sca.contribution.resolver.ModelResolver#resolveModel(java.lang.Class,
+         *      java.lang.Object)
+         */
+        public <T> T resolveModel(Class<T> modelClass, T unresolved) {
+            return (T)super.get(unresolved);
+        }
+
+    }
+
+}

Propchange: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessorTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessorTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/helloworld-interface.wsdl
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/helloworld-interface.wsdl?view=auto&rev=558369
==============================================================================
--- incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/helloworld-interface.wsdl (added)
+++ incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/helloworld-interface.wsdl Sat Jul 21 12:13:07 2007
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    * Licensed to the Apache Software Foundation (ASF) under one
+    * or more contributor license agreements.  See the NOTICE file
+    * distributed with this work for additional information
+    * regarding copyright ownership.  The ASF licenses this file
+    * to you under the Apache License, Version 2.0 (the
+    * "License"); you may not use this file except in compliance
+    * with the License.  You may obtain a copy of the License at
+    * 
+    *   http://www.apache.org/licenses/LICENSE-2.0
+    * 
+    * 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.    
+-->
+<wsdl:definitions targetNamespace="http://helloworld" xmlns:tns="http://helloworld"
+    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
+    xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="helloworld">
+
+    <wsdl:types>
+        <schema elementFormDefault="qualified" targetNamespace="http://helloworld"
+            xmlns="http://www.w3.org/2001/XMLSchema" xmlns:g="http://greeting">
+
+            <import namespace="http://greeting" schemaLocation="../xsd/greeting.xsd" />
+
+            <element name="getGreetings">
+                <complexType>
+                    <sequence>
+                        <element name="name" type="g:Name" />
+                    </sequence>
+                </complexType>
+            </element>
+
+            <element name="getGreetingsResponse">
+                <complexType>
+                    <sequence>
+                        <element name="getGreetingsReturn" type="g:Greeting" />
+                    </sequence>
+                </complexType>
+            </element>
+
+        </schema>
+    </wsdl:types>
+
+    <wsdl:message name="getGreetingsRequest">
+        <wsdl:part element="tns:getGreetings" name="parameters" />
+    </wsdl:message>
+
+    <wsdl:message name="getGreetingsResponse">
+        <wsdl:part element="tns:getGreetingsResponse" name="parameters" />
+    </wsdl:message>
+
+    <wsdl:portType name="HelloWorld">
+        <wsdl:operation name="getGreetings">
+            <wsdl:input message="tns:getGreetingsRequest" name="getGreetingsRequest" />
+            <wsdl:output message="tns:getGreetingsResponse" name="getGreetingsResponse" />
+        </wsdl:operation>
+    </wsdl:portType>
+
+</wsdl:definitions>

Added: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/helloworld-service.wsdl
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/helloworld-service.wsdl?view=auto&rev=558369
==============================================================================
--- incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/helloworld-service.wsdl (added)
+++ incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/wsdl/helloworld-service.wsdl Sat Jul 21 12:13:07 2007
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    * Licensed to the Apache Software Foundation (ASF) under one
+    * or more contributor license agreements.  See the NOTICE file
+    * distributed with this work for additional information
+    * regarding copyright ownership.  The ASF licenses this file
+    * to you under the Apache License, Version 2.0 (the
+    * "License"); you may not use this file except in compliance
+    * with the License.  You may obtain a copy of the License at
+    * 
+    *   http://www.apache.org/licenses/LICENSE-2.0
+    * 
+    * 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.    
+-->
+<wsdl:definitions targetNamespace="http://helloworld" xmlns:tns="http://helloworld"
+    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
+    xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="helloworld">
+
+    <!-- 
+    <wsdl:import location="/wsdl/helloworld-interface.wsdl" namespace="http://helloworld"></wsdl:import>
+     -->
+    <wsdl:import namespace="http://helloworld"></wsdl:import>
+
+    <wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorld">
+        <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
+        <wsdl:operation name="getGreetings">
+            <wsdlsoap:operation soapAction="" />
+            <wsdl:input name="getGreetingsRequest">
+                <wsdlsoap:body use="literal" />
+            </wsdl:input>
+            <wsdl:output name="getGreetingsResponse">
+                <wsdlsoap:body use="literal" />
+            </wsdl:output>
+        </wsdl:operation>
+    </wsdl:binding>
+
+    <wsdl:service name="HelloWorldService">
+        <wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldSoapPort">
+            <wsdlsoap:address
+                location="http://localhost:8085/sample-helloworldws-1.0-SNAPSHOT/services/HelloWorldWebService" />
+        </wsdl:port>
+    </wsdl:service>
+
+</wsdl:definitions>

Added: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/greeting.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/greeting.xsd?view=auto&rev=558369
==============================================================================
--- incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/greeting.xsd (added)
+++ incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/greeting.xsd Sat Jul 21 12:13:07 2007
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+    http://www.apache.org/licenses/LICENSE-2.0
+    
+    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.
+-->
+<schema targetNamespace="http://greeting" xmlns="http://www.w3.org/2001/XMLSchema">
+    <include schemaLocation="name.xsd" />
+    <complexType name="Greeting">
+        <sequence>
+            <element name="message" type="string" />
+            <element name="name" type="Name" />
+        </sequence>
+    </complexType>
+
+</schema>
\ No newline at end of file

Propchange: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/greeting.xsd
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/greeting.xsd
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/name.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/name.xsd?view=auto&rev=558369
==============================================================================
--- incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/name.xsd (added)
+++ incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/name.xsd Sat Jul 21 12:13:07 2007
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+    http://www.apache.org/licenses/LICENSE-2.0
+    
+    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.
+-->
+<schema targetNamespace="http://greeting" xmlns="http://www.w3.org/2001/XMLSchema">
+
+    <complexType name="Name">
+        <sequence>
+            <element name="firstName" type="string" />
+            <element name="lastName" type="string" />
+        </sequence>
+    </complexType>
+
+</schema>
\ No newline at end of file

Propchange: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/name.xsd
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/interface-wsdl-xml/src/test/resources/xsd/name.xsd
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org