/*
* ChainBuilder ESB
* Visual Enterprise Integration
*
* Copyright (C) 2007 Bostech Corporation
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* $Id: TypesInfo.java 11104 2007-12-24 08:17:04Z lzheng $
*/
package com.bostechcorp.cbesb.common.wsdl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.HashMap;
import java.util.Map;
import javax.wsdl.Definition;
import javax.wsdl.Types;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.UnknownExtensibilityElement;
import org.jdom.input.DOMBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.eclipse.xsd.*;
public class TypesInfo {
private Vector<org.jdom.Document> schemas;
private HashMap<String, String> namespaceMap;
private WsdlInfo wsdlInfo;
public TypesInfo()
{
schemas = new Vector<org.jdom.Document>();
namespaceMap = new HashMap<String, String>();
}
protected static TypesInfo load(WsdlInfo wsdlInfo)
{
TypesInfo typesInfo = new TypesInfo();
typesInfo.wsdlInfo = wsdlInfo;
// Get all schema elements from the WSDL definition
org.w3c.dom.Element schemaElement;
Definition wsdlDefinition = wsdlInfo.wsdlDefinition;
if (wsdlDefinition.getTypes() != null) {
Types types = wsdlDefinition.getTypes();
List extElems = types.getExtensibilityElements();
for (Iterator iter = extElems.iterator(); iter.hasNext();) {
ExtensibilityElement element = (ExtensibilityElement)iter.next();
schemaElement = null;
if (element.getElementType().getLocalPart().equalsIgnoreCase("schema"))
{
if (element instanceof UnknownExtensibilityElement) {
schemaElement = ((UnknownExtensibilityElement) element).getElement();
}
if (element instanceof com.ibm.wsdl.extensions.schema.SchemaImpl) {
schemaElement = ((com.ibm.wsdl.extensions.schema.SchemaImpl) element).getElement();
}
if (schemaElement != null)
{
// Convert from DOM to JDOM
DOMBuilder domBuilder = new DOMBuilder();
org.jdom.Element jdomSchemaElement = domBuilder.build(schemaElement);
if (jdomSchemaElement == null) {
System.err.println("Unable to read schema defined in WSDL");
return null;
}
org.jdom.Document jdomDoc = new org.jdom.Document();
jdomSchemaElement.detach();
jdomDoc.setRootElement(jdomSchemaElement);
typesInfo.schemas.add(jdomDoc);
}
}
}
}
return typesInfo;
}
public boolean containsSchemas()
{
return schemas.size() > 0;
}
public Map getSchemaMap()
{
return namespaceMap;
}
public String getSchemaNameFromNamespace(String namespace)
{
return namespaceMap.get(namespace);
}
public void saveSchemas(File destDir, String baseName) throws IOException
{
for (int i=0; i < schemas.size(); i++)
{
//Write out the schema
org.jdom.Document schemaDoc = schemas.elementAt(i);
String filename = baseName + "_Types" + i + ".xsd";
File destFile = new File(destDir, filename);
FileOutputStream os = new FileOutputStream(destFile);
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
xmlOut.output(schemaDoc, os);
//Now read it back into an XSDSchema object
XSDSchema typeSchema = SchemaUtil.loadSchema(destFile);
//Get the target namespace of the schema
String namespace = typeSchema.getTargetNamespace();
System.out.println(namespace);
namespaceMap.put(namespace, filename);
//Add this for bug 334
Map map=this.wsdlInfo.getWsdlDefinition().getNamespaces();
for (Object key : map.keySet()) {
// System.out.println(key);
typeSchema.getDocument().getDocumentElement().setAttribute("xmlns:"+(String)key, (String)map.get(key));
}
SchemaUtil.saveXSDSchema(typeSchema, destFile);
}
}
}
|