summaryrefslogtreecommitdiffstats
path: root/sandbox/tools/xmlfromxsd/src/main/java/org/apache/tuscany/tools/xmlfromxsd/generate/XBbasedXMLGenerator.java
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/tools/xmlfromxsd/src/main/java/org/apache/tuscany/tools/xmlfromxsd/generate/XBbasedXMLGenerator.java')
-rw-r--r--sandbox/tools/xmlfromxsd/src/main/java/org/apache/tuscany/tools/xmlfromxsd/generate/XBbasedXMLGenerator.java239
1 files changed, 239 insertions, 0 deletions
diff --git a/sandbox/tools/xmlfromxsd/src/main/java/org/apache/tuscany/tools/xmlfromxsd/generate/XBbasedXMLGenerator.java b/sandbox/tools/xmlfromxsd/src/main/java/org/apache/tuscany/tools/xmlfromxsd/generate/XBbasedXMLGenerator.java
new file mode 100644
index 0000000000..03029bdf43
--- /dev/null
+++ b/sandbox/tools/xmlfromxsd/src/main/java/org/apache/tuscany/tools/xmlfromxsd/generate/XBbasedXMLGenerator.java
@@ -0,0 +1,239 @@
+package org.apache.tuscany.tools.xmlfromxsd.generate;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.wsdl.Definition;
+import javax.wsdl.WSDLException;
+import javax.wsdl.extensions.schema.Schema;
+import javax.wsdl.factory.WSDLFactory;
+import javax.wsdl.xml.WSDLReader;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.axis2.util.XMLUtils;
+import org.apache.xmlbeans.SchemaType;
+import org.apache.xmlbeans.SchemaTypeSystem;
+import org.apache.xmlbeans.XmlBeans;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
+import org.apache.xmlbeans.impl.xsd2inst.SchemaInstanceGenerator;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+import com.ibm.wsdl.util.xml.DOM2Writer;
+
+public class XBbasedXMLGenerator extends SchemaInstanceGenerator implements XMLGenerator
+{
+ private XMLfromXSDConfiguration config = null;
+
+ public XBbasedXMLGenerator(XMLfromXSDConfiguration config)
+ {
+ this.config = config;
+ // config.setXsdInputStream(getClass().getResource(conf).openStream());
+ }
+
+ public void generateXML() throws Exception
+ {
+ SchemaTypeSystem sts = processXSDSources();
+ SchemaType rootElementType = getRootElementSchemaType(sts);
+ if ( rootElementType != null )
+ {
+ TuscanySampleXmlUtil xmlUtil = new TuscanySampleXmlUtil();
+ xmlUtil.setGenerate_sample_data(config.isGenerateSampleData());
+ String result = xmlUtil.createSampleForType(rootElementType);
+ config.getXmlOutputStream().write(result.getBytes());
+ //System.out.println(result);
+ }
+ else
+ {
+ System.out.println("Could not find a global element with name \"" + config.getRootElementLocalName() + "\"");
+ }
+ }
+
+ protected SchemaType getRootElementSchemaType(SchemaTypeSystem sts)
+ {
+ SchemaType schemaType = null;
+
+ if (sts == null)
+ {
+ System.out.println("No Schemas to process.");
+ }
+ else
+ {
+ //first check in the global types
+ SchemaType[] globalTypes = sts.globalTypes();
+ for (int i = 0; i < globalTypes.length; i++)
+ {
+ if (config.getSchemaTypeName().equals(globalTypes[i].getName().getLocalPart()))
+ {
+ schemaType = globalTypes[i];
+ break;
+ }
+ }
+
+ //next check for anonymous types defined inline within elements
+ if ( schemaType == null )
+ {
+ SchemaType[] globalElems = sts.documentTypes();
+ for (int i = 0; i < globalElems.length; i++)
+ {
+ if (config.getSchemaTypeName().equals(globalElems[i].getDocumentElementName().getLocalPart()))
+ {
+ schemaType = globalElems[i];
+ break;
+ }
+ }
+ }
+ }
+ return schemaType;
+ }
+
+ public void generateXML(XMLfromXSDConfiguration config) throws Exception
+ {
+ this.config = config;
+ generateXML();
+ }
+
+ public XMLfromXSDConfiguration getConfig()
+ {
+ return config;
+ }
+
+ public void setConfig(XMLfromXSDConfiguration config)
+ {
+ this.config = config;
+ }
+
+ private Definition readInTheWSDLFile(InputStream wsdlStream) throws WSDLException
+ {
+ WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
+ reader.setFeature("javax.wsdl.importDocuments", true);
+
+ /*File file = new File(uri);
+ String baseURI;
+
+ if (uri.startsWith("http://")){
+ baseURI = uri;
+ } else{
+ if(file.getParentFile() == null){
+ try {
+ baseURI = new File(".").getCanonicalFile().toURI().toString();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ baseURI = file.getParentFile().toURI().toString();
+ }
+ }
+*/
+ Document doc;
+ try {
+ doc = XMLUtils.newDocument(wsdlStream);
+ } catch (ParserConfigurationException e) {
+ throw new WSDLException(WSDLException.PARSER_ERROR,
+ "Parser Configuration Error",
+ e);
+ } catch (SAXException e) {
+ throw new WSDLException(WSDLException.PARSER_ERROR,
+ "Parser SAX Error",
+ e);
+
+ } catch (IOException e) {
+ throw new WSDLException(WSDLException.INVALID_WSDL, "IO Error", e);
+ }
+
+ return reader.readWSDL(config.getRootElementNamespaceURI(), doc);
+ }
+
+ protected InputStream[] getXSDsFromWSDL(InputStream xsdInputStream) throws Exception
+ {
+ Definition defn = readInTheWSDLFile(xsdInputStream);
+ List list = defn.getTypes().getExtensibilityElements();
+
+ InputStream[] xsdFragments = new InputStream[list.size()];
+
+ Iterator iterator = list.iterator();
+ Schema schemaElement = null;
+
+ for ( int count = 0 ; count < list.size() ; ++count )
+ {
+ schemaElement = (Schema)iterator.next();
+ //System.out.println(" *** - " + element.getElementType().getNamespaceURI());
+ //System.out.println(" **** - " + element + " & " + element.getClass().getPackage().getName());
+ //System.out.println(DOM2Writer.nodeToString(element.getElement()));
+ xsdFragments[count] =
+ new ByteArrayInputStream(DOM2Writer.nodeToString(schemaElement.getElement()).getBytes());
+ }
+
+ return xsdFragments;
+ }
+
+ protected SchemaTypeSystem processXSDSources() throws Exception
+ {
+ SchemaTypeSystem sts = null;
+ if ( config.getXsdFileName().endsWith(XSD_FILE) )
+ {
+ sts = processXSDs(new InputStream[]{ config.getXsdInputStream()} );
+ }
+ else if ( config.getXsdFileName().endsWith(WSDL_FILE) )
+ {
+ sts = processXSDs(getXSDsFromWSDL(config.getXsdInputStream()));
+ }
+ return sts;
+ }
+
+ protected SchemaTypeSystem processXSDs(InputStream[] inputStreams)
+ {
+ List sdocs = new ArrayList();
+ for (int i = 0; i < inputStreams.length; i++)
+ {
+ try
+ {
+ sdocs.add(XmlObject.Factory.parse(inputStreams[i],
+ (new XmlOptions()).setLoadLineNumbers().setLoadMessageDigest()));
+ }
+ catch (Exception e)
+ {
+ System.err.println("Can not load schema file: " + inputStreams[i] + ": ");
+ e.printStackTrace();
+ }
+ }
+
+ XmlObject[] schemas = (XmlObject[]) sdocs.toArray(new XmlObject[sdocs.size()]);
+
+ SchemaTypeSystem sts = null;
+ if (schemas.length > 0)
+ {
+ Collection errors = new ArrayList();
+ XmlOptions compileOptions = new XmlOptions();
+ /*if (dl)
+ compileOptions.setCompileDownloadUrls();
+ if (nopvr)
+ compileOptions.setCompileNoPvrRule();
+ if (noupa)
+ compileOptions.setCompileNoUpaRule();
+ */
+ try
+ {
+ sts = XmlBeans.compileXsd(schemas, XmlBeans.getBuiltinTypeSystem(), compileOptions);
+ }
+ catch (Exception e)
+ {
+ if (errors.isEmpty() || !(e instanceof XmlException))
+ e.printStackTrace();
+
+ System.out.println("Schema compilation errors: ");
+ for (Iterator i = errors.iterator(); i.hasNext(); )
+ System.out.println(i.next());
+ }
+ }
+
+ return sts;
+ }
+}