Author: chathura
Date: Thu Jan 10 03:31:41 2008
New Revision: 12114
Log:
Refactoring...
Added:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/utils/
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/utils/MediaTypeDetector.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/utils/SchemaFileProcessor.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/utils/WSDLFileProcessor.java
Added:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/utils/MediaTypeDetector.java
==============================================================================
--- (empty file)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/utils/MediaTypeDetector.java
Thu Jan 10 03:31:41 2008
@@ -0,0 +1,24 @@
+/*
+ * 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.utils;
+
+public class MediaTypeDetector {
+
+ public static String getMediaType(String sourceURL) {
+ return null;
+ }
+}
Added:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/utils/SchemaFileProcessor.java
==============================================================================
--- (empty file)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/utils/SchemaFileProcessor.java
Thu Jan 10 03:31:41 2008
@@ -0,0 +1,210 @@
+/*
+ * 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.utils;
+
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.apache.ws.commons.schema.XmlSchemaExternal;
+import org.apache.ws.commons.schema.XmlSchemaObjectCollection;
+import org.xml.sax.InputSource;
+import org.wso2.registry.jdbc.mediatypes.builtin.DefaultMediaTypeHandler;
+import org.wso2.registry.Resource;
+import org.wso2.registry.RegistryConstants;
+import org.wso2.registry.RegistryException;
+
+import javax.xml.transform.OutputKeys;
+import java.io.FileNotFoundException;
+import java.io.ByteArrayOutputStream;
+import java.util.*;
+
+public class SchemaFileProcessor {
+
+ private DefaultMediaTypeHandler defaultMediaTypeHandler;
+
+ // remove this when it is not needed
+ private int i;
+
+ public SchemaFileProcessor(DefaultMediaTypeHandler
defaultMediaTypeHandler) {
+ this.defaultMediaTypeHandler = defaultMediaTypeHandler;
+ }
+
+ /**
+ * 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, String
registryBasePath)
+ throws RegistryException {
+
+ saveSchemaFileToRegistry(location, new HashMap(), registryBasePath);
+ }
+
+ /**
+ * @param location - original schema location
+ * @param processedSchemaMap - this map contains schema source URI vs new
schema locaitons.
+ */
+ public void saveSchemaFileToRegistry(String location,
+ Map processedSchemaMap, String
registryBasePath)
+ throws RegistryException {
+
+ 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, registryBasePath);
+ }
+
+ /**
+ * 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,
+ String registryBasePath) throws
RegistryException {
+
+ // 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, registryBasePath);
+ }
+
+ // 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());
+ ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
+ xmlSchema.write(byteArrayOutputStream);
+ byte[] xsdContent = byteArrayOutputStream.toByteArray();
+
+ Resource xsdResource = new Resource();
+ xsdResource.setContent(xsdContent);
+ defaultMediaTypeHandler.put(registryBasePath +
RegistryConstants.PATH_SEPARATOR + fileNameToSave, xsdResource);
+
+ //xmlSchema.write(new FileOutputStream("repository/" +
fileNameToSave),
+ // getDefaultOptionMap());
+ // add this entry to the proccessed wsdl map
+ }
+ }
+
+ 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/utils/WSDLFileProcessor.java
==============================================================================
--- (empty file)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/utils/WSDLFileProcessor.java
Thu Jan 10 03:31:41 2008
@@ -0,0 +1,252 @@
+/*
+ * 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.utils;
+
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.wso2.registry.RegistryConstants;
+import org.wso2.registry.RegistryException;
+import org.wso2.registry.Resource;
+import org.wso2.registry.jdbc.mediatypes.builtin.DefaultMediaTypeHandler;
+
+import javax.wsdl.Definition;
+import javax.wsdl.Import;
+import javax.wsdl.Types;
+import javax.wsdl.WSDLException;
+import javax.wsdl.extensions.schema.Schema;
+import javax.wsdl.factory.WSDLFactory;
+import javax.wsdl.xml.WSDLReader;
+import javax.wsdl.xml.WSDLWriter;
+import java.io.ByteArrayOutputStream;
+import java.util.*;
+
+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";
+
+ DefaultMediaTypeHandler defaultMediaTypeHandler;
+
+ public WSDLFileProcessor(DefaultMediaTypeHandler defaultMediaTypeHandler) {
+ this.defaultMediaTypeHandler = defaultMediaTypeHandler;
+ }
+
+ /**
+ * saves a wsdl file including its imports and imported schemas.
+ *
+ * @param location
+ * @throws WSDLException
+ */
+ public void saveWSDLFileToRegistry(String location, String
registryBasePath)
+ throws RegistryException {
+ WSDLReader wsdlReader = null;
+
+ try {
+ wsdlReader = WSDLFactory.newInstance().newWSDLReader();
+ } catch (WSDLException e) {
+ String msg = "Could not initiate the wsdl reader. Caused by: " +
e.getMessage();
+ throw new RegistryException(msg);
+ }
+
+ wsdlReader.setFeature("javax.wsdl.importDocuments", true);
+ Definition wsdlDefinition = null;
+
+ try {
+ wsdlDefinition = wsdlReader.readWSDL(location);
+ } catch (WSDLException e) {
+ String msg = "Could not read the wsdl at location " + location +
". Caused by: " +
+ e.getMessage();
+ throw new RegistryException(msg);
+ }
+
+ Map processedWSDLMap = new HashMap();
+ calculateWSDLNamesAndChangeTypes(wsdlDefinition, processedWSDLMap, new
HashMap(),
+ new HashSet(), registryBasePath);
+ saveWSDLFileToRegistry(wsdlDefinition, processedWSDLMap, new
HashSet(), registryBasePath);
+ }
+
+ /**
+ * 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,
+ String registryBasePath)
throws RegistryException {
+ // 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, registryBasePath);
+ }
+ }
+ }
+
+ // 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(defaultMediaTypeHandler);
+ changedLocationMap = new HashMap();
+ schemaFileProcessor.calculateNewSchemaNames(
+ xmlSchema, processedScheamMap, new HashSet(),
true);
+ schemaFileProcessor.saveSchemaFileToRegistry(
+ xmlSchema, processedScheamMap, changedLocationMap,
new HashSet(), true, registryBasePath);
+ // 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,
+ String registryBasePath) throws
RegistryException {
+ // 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,
+ registryBasePath);
+ }
+ // 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 importedResourceName =
+
(String)processedWSDLMap.get(wsdlDefinition.getDocumentBaseURI());
+ try {
+ WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
+ ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
+ wsdlWriter.writeWSDL(wsdlDefinition, byteArrayOutputStream);
+ byte[] wsdlResourceContent = byteArrayOutputStream.toByteArray();
+
+ // create a resource this wsdlResourceContent and put it to the
registry with the name importedResourceName (in some path)
+
+ String wsdlPath =
+ registryBasePath + RegistryConstants.PATH_SEPARATOR +
importedResourceName;
+ Resource wsdlResource = new Resource();
+ wsdlResource.setContent(wsdlResourceContent);
+ defaultMediaTypeHandler.put(wsdlPath, wsdlResource);
+
+ //wsdlWriter.writeWSDL(wsdlDefinition, new
FileOutputStream("repository/" + importedResourceName));
+ } catch (WSDLException 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));
+ }
+ }
+ }
+ }
+
+}
_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev