Author: chathura
Date: Thu Dec 13 23:19:09 2007
New Revision: 11108

Log:


Implementing WSDL media type support for the registry.
(Integrating the WSDL utility classes written by Amila).



Added:
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/utils/
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/utils/SchemaFileProcessor.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/utils/WSDLFileProcessor.java
Modified:
   trunk/registry/modules/core/pom.xml
   trunk/registry/pom.xml

Modified: trunk/registry/modules/core/pom.xml
==============================================================================
--- trunk/registry/modules/core/pom.xml (original)
+++ trunk/registry/modules/core/pom.xml Thu Dec 13 23:19:09 2007
@@ -127,6 +127,14 @@
             <artifactId>xml-apis</artifactId>
         </dependency>
         <dependency>
+                       <groupId>wsdl4j</groupId>
+                       <artifactId>wsdl4j</artifactId>
+               </dependency>
+        <dependency>
+                       <groupId>org.apache.ws.commons.schema</groupId>
+                       <artifactId>XmlSchema</artifactId>
+               </dependency>
+        <dependency>
             <groupId>org.mortbay.jetty</groupId>
             <artifactId>jetty</artifactId>
             <version>6.1.5</version>

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/utils/SchemaFileProcessor.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/utils/SchemaFileProcessor.java
  Thu Dec 13 23:19:09 2007
@@ -0,0 +1,190 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.registry.jdbc.mediatypes.builtin.utils;
+
+import org.apache.ws.commons.schema.XmlSchemaExternal;
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaObjectCollection;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.xml.sax.InputSource;
+
+import javax.xml.transform.OutputKeys;
+import java.util.*;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+
+public class SchemaFileProcessor {
+
+    // remove this when it is not needed
+    private int i;
+
+    /**
+     * saves the file to the reginstry after saving all its inclues and 
imports to the
+     * reginstry and updating the schema locations accordingly.
+     *
+     * @param location - original schema location.
+     * @throws FileNotFoundException
+     */
+
+    public void saveSchemaFileToRegistry(String location)
+            throws FileNotFoundException {
+
+        saveSchemaFileToRegistry(location, new HashMap());
+    }
+
+    /**
+     * @param location           - original schema location
+     * @param processedSchemaMap - this map contains schema source URI vs new 
schema locaitons.
+     */
+    public void saveSchemaFileToRegistry(String location,
+                                         Map processedSchemaMap) {
+        XmlSchemaCollection xmlSchemaCollection = new XmlSchemaCollection();
+        InputSource inputSource = new InputSource(location);
+        // here we assue schema is correct. schema validation is beyond 
registry scope.
+        // hence passes null to validator.
+        XmlSchema xmlSchema = xmlSchemaCollection.read(inputSource, null);
+        // this is not an inline wsdl schema. so pass null to change map.
+        calculateNewSchemaNames(xmlSchema, processedSchemaMap, new HashSet(), 
false);
+        saveSchemaFileToRegistry(xmlSchema, processedSchemaMap, null, new 
HashSet(), false);
+    }
+
+    /**
+     * calculate the new schema file names to save the schema. Here we can not 
save the
+     * schema file as it is since there can be recursive imports. So what we 
have to do
+     * is to first determine the schema names to be saved and then change the 
schema locations
+     * accordingly.
+     * In this method first we iterate through the imports and includes and 
find the names.
+     * have used the visitedSchemas variable to keep track of the visited 
schemas
+     * to avoid the recursion.
+     * @param xmlSchema - schema to change save into the registry
+     * @param processedSchemaMap - map to keep the source uri vs new schema 
location
+     * @param visitedShemas   - already visited schemas source uris
+     * @param isWsdlInlineSchema - this variable is used to check whether the 
given schema is
+     * an inline schema of a wsdl. in that case we do not need to calculate a 
name for that
+     */
+    public void calculateNewSchemaNames(XmlSchema xmlSchema,
+                                        Map processedSchemaMap,
+                                        Set visitedShemas,
+                                        boolean isWsdlInlineSchema) {
+
+        // first process the imports and includes
+        XmlSchemaObjectCollection includes = xmlSchema.getIncludes();
+        // set this as an visited schema to stop recursion
+        visitedShemas.add(xmlSchema.getSourceURI());
+        if (includes != null) {
+            Object externalComponet = null;
+            XmlSchemaExternal xmlSchemaExternal = null;
+            XmlSchema innerSchema = null;
+            for (Iterator iter = includes.getIterator(); iter.hasNext();) {
+                externalComponet = iter.next();
+                if (externalComponet instanceof XmlSchemaExternal) {
+                    xmlSchemaExternal = (XmlSchemaExternal) externalComponet;
+                    innerSchema = xmlSchemaExternal.getSchema();
+                    // we do not need to process it if it already process
+                    // when calling with a different import.
+                    if 
(!processedSchemaMap.containsKey(innerSchema.getSourceURI()) &&
+                            
!visitedShemas.contains(innerSchema.getSourceURI())) {
+                        calculateNewSchemaNames(xmlSchemaExternal.getSchema(),
+                                processedSchemaMap, visitedShemas, false);
+                    }
+                }
+            }
+        }
+
+        // after processing includes and imports save the xml schema
+        if (!isWsdlInlineSchema) {
+            String baseUri = xmlSchema.getSourceURI();
+            String xsdFileName = baseUri.substring(baseUri.lastIndexOf("/") + 
1);
+            String fileNameToSave = xsdFileName.substring(0, 
xsdFileName.indexOf(".")) + ".xsd";
+            while (processedSchemaMap.containsValue(fileNameToSave)) {
+                fileNameToSave = xsdFileName.substring(0, 
xsdFileName.indexOf(".")) + ++i + ".xsd";
+            }
+            // add this entry to the proccessed wsdl map
+            processedSchemaMap.put(baseUri, fileNameToSave);
+        }
+    }
+
+    /**
+     * save the schemas to the registry. used the calcualted names in the 
processedSchemaMap
+     * to change the schema locations.
+     * @param xmlSchema - xml scheam to save
+     * @param processedSchemaMap - already determined schema names
+     * @param changeSchemaNames - this contains the map between original 
schema names and the
+     * change uris. this is used to update the wsdl inline schema imports and 
inclueds when
+     * saving the wsdl file.
+     * @param visitedShemas  - keep track of visited schemas to avoid recursion
+     * @param isWsdlInlineSchema  - if called from a inline wsdl schema.
+     */
+    public void saveSchemaFileToRegistry(XmlSchema xmlSchema,
+                                         Map processedSchemaMap,
+                                         Map changeSchemaNames,
+                                         Set visitedShemas,
+                                         boolean isWsdlInlineSchema) {
+
+        // first process the imports and includes
+        XmlSchemaObjectCollection includes = xmlSchema.getIncludes();
+        // set this as an visited schema to stop recursion
+        visitedShemas.add(xmlSchema.getSourceURI());
+        if (includes != null) {
+            Object externalComponet = null;
+            XmlSchemaExternal xmlSchemaExternal = null;
+            XmlSchema innerSchema = null;
+            for (Iterator iter = includes.getIterator(); iter.hasNext();) {
+                externalComponet = iter.next();
+                if (externalComponet instanceof XmlSchemaExternal) {
+                    xmlSchemaExternal = (XmlSchemaExternal) externalComponet;
+                    innerSchema = xmlSchemaExternal.getSchema();
+                    if (!visitedShemas.contains(innerSchema.getSourceURI())) {
+                        saveSchemaFileToRegistry(xmlSchemaExternal.getSchema(),
+                                processedSchemaMap, null, visitedShemas, 
false);
+                    }
+
+                    // add the new name to changeschema map
+                    // have to do before change the schema location
+                    if (isWsdlInlineSchema) {
+                        
changeSchemaNames.put(xmlSchemaExternal.getSchemaLocation(), (String) 
processedSchemaMap.get(
+                                innerSchema.getSourceURI()));
+                    }
+                    // set the new location
+                    xmlSchemaExternal.setSchemaLocation((String) 
processedSchemaMap.get(
+                            innerSchema.getSourceURI()));
+
+                }
+            }
+        }
+
+        // after processing includes and imports save the xml schema
+        if (!isWsdlInlineSchema) {
+            String fileNameToSave = (String) 
processedSchemaMap.get(xmlSchema.getSourceURI());
+            try {
+                xmlSchema.write(new FileOutputStream("repository/" + 
fileNameToSave), getDefaultOptionMap());
+                // add this entry to the proccessed wsdl map
+            } catch (FileNotFoundException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    private Map getDefaultOptionMap() {
+        Map options = new HashMap();
+        options.put(OutputKeys.OMIT_XML_DECLARATION, "no");
+        options.put(OutputKeys.INDENT, "yes");
+
+        return options;
+    }
+
+}

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/utils/WSDLFileProcessor.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/utils/WSDLFileProcessor.java
    Thu Dec 13 23:19:09 2007
@@ -0,0 +1,213 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.registry.jdbc.mediatypes.builtin.utils;
+
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import javax.wsdl.Types;
+import javax.wsdl.Definition;
+import javax.wsdl.Import;
+import javax.wsdl.WSDLException;
+import javax.wsdl.xml.WSDLReader;
+import javax.wsdl.xml.WSDLWriter;
+import javax.wsdl.factory.WSDLFactory;
+import javax.wsdl.extensions.schema.Schema;
+import java.util.*;
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+
+public class WSDLFileProcessor {
+
+    // remove this when it is not needed
+    private int i;
+
+    public static final String IMPORT_TAG = "import";
+    public static final String INCLUDE_TAG = "include";
+
+    /**
+     * saves a wsdl file including its imports and imported schemas.
+     *
+     * @param location
+     * @throws WSDLException
+     */
+    public void saveWSDLFileToRegistry(String location)
+            throws WSDLException {
+        WSDLReader wsdlReader = WSDLFactory.newInstance().newWSDLReader();
+        wsdlReader.setFeature("javax.wsdl.importDocuments", true);
+        Definition wsdlDefinition = wsdlReader.readWSDL(location);
+        Map processedWSDLMap = new HashMap();
+        calculateWSDLNamesAndChangeTypes(wsdlDefinition, processedWSDLMap, new 
HashMap(), new HashSet());
+        saveWSDLFileToRegistry(wsdlDefinition, processedWSDLMap, new 
HashSet());
+    }
+
+    /**
+     * saves the given wsdl definition file with its imported wsdls and
+     * imported and included schemas.
+     *
+     * @param wsdlDefinition     - wsdl file to save
+     * @param processedWSDLMap   - map with original source URI vs new uris 
for wsdls
+     * @param processedScheamMap - map with orignal source URI vs new uris for 
schemas
+     */
+
+    public void calculateWSDLNamesAndChangeTypes(Definition wsdlDefinition,
+                                                 Map processedWSDLMap,
+                                                 Map processedScheamMap,
+                                                 Set visitedWSDLs) {
+        // first we have to process the imports and change the
+        // schema locations suite for the registry
+        Iterator iter = wsdlDefinition.getImports().values().iterator();
+        Vector values = null;
+        Import wsdlImport = null;
+        // add this to visited list to stop recursion
+        visitedWSDLs.add(wsdlDefinition.getDocumentBaseURI());
+        for (; iter.hasNext();) {
+            values = (Vector) iter.next();
+            for (Iterator valuesIter = values.iterator(); 
valuesIter.hasNext();) {
+                wsdlImport = (Import) valuesIter.next();
+                // process the types recuresiveilt
+                Definition innerDefinition = wsdlImport.getDefinition();
+                if 
(!visitedWSDLs.contains(innerDefinition.getDocumentBaseURI())) {
+                    // we have not process this wsdl file earlier
+                    calculateWSDLNamesAndChangeTypes(
+                            innerDefinition, processedWSDLMap, 
processedScheamMap, visitedWSDLs);
+                }
+            }
+        }
+
+        // change the schema names
+        Types types = wsdlDefinition.getTypes();
+        if (types != null) {
+            List extensibleElements = types.getExtensibilityElements();
+            Schema schemaExtension = null;
+            Object extensionObject = null;
+            XmlSchema xmlSchema = null;
+            XmlSchemaCollection xmlSchemaCollection = null;
+            SchemaFileProcessor schemaFileProcessor = null;
+            Map changedLocationMap = null;
+            String basuri = wsdlDefinition.getDocumentBaseURI();
+            basuri = basuri.substring(0, basuri.lastIndexOf("/") + 1);
+            for (Iterator extensibleElementsIter = 
extensibleElements.iterator();
+                 extensibleElementsIter.hasNext();) {
+                extensionObject = extensibleElementsIter.next();
+                if (extensionObject instanceof Schema) {
+                    // first get the schema object
+                    schemaExtension = (Schema) extensionObject;
+                    // create an xml schema object to be processed by 
SchemaFile procesor.
+                    xmlSchemaCollection = new XmlSchemaCollection();
+                    xmlSchemaCollection.setBaseUri(basuri);
+                    xmlSchema = 
xmlSchemaCollection.read(schemaExtension.getElement());
+                    schemaFileProcessor = new SchemaFileProcessor();
+                    changedLocationMap = new HashMap();
+                    schemaFileProcessor.calculateNewSchemaNames(
+                            xmlSchema, processedScheamMap, new HashSet(), 
true);
+                    schemaFileProcessor.saveSchemaFileToRegistry(
+                            xmlSchema, processedScheamMap, changedLocationMap, 
new HashSet(), true);
+                    // update the current schema locations with the generated 
ones.
+                    changeLocations(schemaExtension.getElement(), 
changedLocationMap);
+                }
+            }
+        }
+
+        // after processing the defintions save this to the registry
+        // TODO: save this to the registry for the moment save this to the
+        // folder and omit any exception occurs.
+        String baseUri = wsdlDefinition.getDocumentBaseURI();
+        String wsdlFileName = baseUri.substring(baseUri.lastIndexOf("/") + 1);
+        String fileNameToSave = wsdlFileName.substring(0, 
wsdlFileName.indexOf(".")) + ".wsdl";
+        while (processedWSDLMap.containsValue(fileNameToSave)) {
+            fileNameToSave = wsdlFileName.substring(0, 
wsdlFileName.indexOf(".")) + ++i + ".wsdl";
+        }
+        // add this entry to the proccessed wsdl map
+        processedWSDLMap.put(baseUri, fileNameToSave);
+
+    }
+
+    public void saveWSDLFileToRegistry(Definition wsdlDefinition,
+                                       Map processedWSDLMap,
+                                       Set visitedWSDLs) {
+        // first we have to process the imports and change the
+        // schema locations suite for the registry
+        Iterator iter = wsdlDefinition.getImports().values().iterator();
+        Vector values = null;
+        Import wsdlImport = null;
+        // add this to visited list to stop recursion
+        visitedWSDLs.add(wsdlDefinition.getDocumentBaseURI());
+        for (; iter.hasNext();) {
+            values = (Vector) iter.next();
+            for (Iterator valuesIter = values.iterator(); 
valuesIter.hasNext();) {
+                wsdlImport = (Import) valuesIter.next();
+                // process the types recuresiveilt
+                Definition innerDefinition = wsdlImport.getDefinition();
+                if 
(!visitedWSDLs.contains(innerDefinition.getDocumentBaseURI())) {
+                    // we have not process this wsdl file earlier
+                    saveWSDLFileToRegistry(innerDefinition, processedWSDLMap, 
visitedWSDLs);
+                }
+                // set the import location according to the new location
+                wsdlImport.setLocationURI((String) processedWSDLMap.get(
+                        innerDefinition.getDocumentBaseURI()));
+            }
+        }
+
+        // after processing the defintions save this to the registry
+        // TODO: save this to the registry for the moment save this to the
+        // folder and omit any exception occurs.
+        String fileNameToSave = (String) 
processedWSDLMap.get(wsdlDefinition.getDocumentBaseURI());
+        try {
+            WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
+            ByteArrayOutputStream byteArrayOutputStream = new 
ByteArrayOutputStream();
+            wsdlWriter.writeWSDL(wsdlDefinition, new 
FileOutputStream("repository/" + fileNameToSave));
+        } catch (WSDLException e) {
+            e.printStackTrace();
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void changeLocations(Element element, Map changedLocationMap) {
+        NodeList nodeList = element.getChildNodes();
+        String tagName;
+        for (int i = 0; i < nodeList.getLength(); i++) {
+            tagName = nodeList.item(i).getLocalName();
+            if (IMPORT_TAG.equals(tagName) || INCLUDE_TAG.equals(tagName)) {
+                processImport(nodeList.item(i), changedLocationMap);
+            }
+        }
+    }
+
+    private void processImport(Node importNode, Map changedLocationMap) {
+        NamedNodeMap nodeMap = importNode.getAttributes();
+        Node attribute;
+        String attributeValue;
+        for (int i = 0; i < nodeMap.getLength(); i++) {
+            attribute = nodeMap.item(i);
+            if (attribute.getNodeName().equals("schemaLocation")) {
+                attributeValue = attribute.getNodeValue();
+                if (changedLocationMap.get(attributeValue) != null) {
+                    attribute.setNodeValue(
+                            (String) changedLocationMap.get(attributeValue));
+                }
+            }
+        }
+    }
+
+}

Modified: trunk/registry/pom.xml
==============================================================================
--- trunk/registry/pom.xml      (original)
+++ trunk/registry/pom.xml      Thu Dec 13 23:19:09 2007
@@ -247,6 +247,16 @@
                 <artifactId>jetty</artifactId>
                 <version>${jetty.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.apache.ws.commons.schema</groupId>
+                <artifactId>XmlSchema</artifactId>
+                <version>${xmlschema.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>wsdl4j</groupId>
+                <artifactId>wsdl4j</artifactId>
+                <version>${wsdl4j.version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
@@ -345,6 +355,8 @@
         <jaxen.version>1.1.1</jaxen.version>
         <xmlapi.version>1.3.04</xmlapi.version>
         <jetty.version>6.1.5</jetty.version>
+        <wsdl4j.version>1.6.2</wsdl4j.version>
+        <xmlschema.version>SNAPSHOT</xmlschema.version>
     </properties>
 
     <repositories>

_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev

Reply via email to