Author: mmerz
Date: Thu Feb 10 16:52:03 2005
New Revision: 153309
URL: http://svn.apache.org/viewcvs?view=rev&rev=153309
Log:
Fixed the wsdl generation for xmlbeans.
Contributor: Daryoush Mehrtash
Modified:
incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/axis/util/encoding/XmlBeanSerializer.java
incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/databinding/xmlbeans/XMLBeanTypeLookUp.java
Modified:
incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/axis/util/encoding/XmlBeanSerializer.java
URL:
http://svn.apache.org/viewcvs/incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/axis/util/encoding/XmlBeanSerializer.java?view=diff&r1=153308&r2=153309
==============================================================================
---
incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/axis/util/encoding/XmlBeanSerializer.java
(original)
+++
incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/axis/util/encoding/XmlBeanSerializer.java
Thu Feb 10 16:52:03 2005
@@ -20,72 +20,71 @@
package org.apache.beehive.wsm.axis.util.encoding;
import java.io.IOException;
-import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.HashSet;
import java.util.Set;
import javax.xml.namespace.QName;
+import org.apache.axis.AxisFault;
import org.apache.axis.Constants;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.encoding.Serializer;
import org.apache.axis.wsdl.fromJava.Types;
+import org.apache.beehive.wsm.databinding.xmlbeans.XMLBeanTypeLookUp;
+import org.apache.beehive.wsm.wsdl.Schema;
+import org.apache.beehive.wsm.wsdl.Utilities;
+import org.apache.xmlbeans.SchemaField;
import org.apache.xmlbeans.SchemaType;
+import org.apache.xmlbeans.SchemaTypeLoader;
import org.apache.xmlbeans.XmlBeans;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
-import org.apache.xmlbeans.XmlOptions;
-import org.w3.x2001.xmlSchema.SchemaDocument;
-import org.w3.x2001.xmlSchema.TopLevelComplexType;
-import org.w3.x2001.xmlSchema.TopLevelElement;
+import org.w3.x2001.xmlSchema.LocalElement;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
-import org.xmlsoap.schemas.wsdl.DefinitionsDocument;
-import org.xmlsoap.schemas.wsdl.TDefinitions;
-import org.xmlsoap.schemas.wsdl.TTypes;
/**
* ****************************************************************************
- *
+ *
* @author Jonathan Colwell
*/
public class XmlBeanSerializer implements Serializer {
/**
- * Serialize an element named name, with the indicated attributes
- * and value.
- *
- * @param name is the element name
- * @param attributes are the attributes...serialize is free to add more.
- * @param value is the value
- * @param context is the SerializationContext
+ * Serialize an element named name, with the indicated attributes and
value.
+ *
+ * @param name
+ * is the element name
+ * @param attributes
+ * are the attributes...serialize is free to add more.
+ * @param value
+ * is the value
+ * @param context
+ * is the SerializationContext
*/
- public void serialize(QName name, Attributes attributes,
- Object value, SerializationContext context)
- throws IOException {
+ public void serialize(QName name, Attributes attributes, Object value,
+ SerializationContext context) throws IOException {
if (!(value instanceof XmlObject)) {
- throw new IOException(((value != null) ?
- value.getClass().getName()
+ throw new IOException(((value != null)
+ ? value.getClass().getName()
: "null")
- + " is not an "
- + XmlObject.class.getName());
+ + " is not an " + XmlObject.class.getName());
} else {
context.setWriteXMLType(null);
context.startElement(name, attributes);
-
+
XmlCursor xCur = ((XmlObject) value).newCursor();
if (xCur.toFirstContentToken() == XmlCursor.TokenType.START) {
do {
Node n = xCur.getDomNode();
if (n.getNodeType() == Node.ELEMENT_NODE) {
- context.writeDOMElement((Element)n);
+ context.writeDOMElement((Element) n);
}
- }
- while (xCur.toNextSibling());
+ } while (xCur.toNextSibling());
}
context.endElement();
}
@@ -96,142 +95,139 @@
}
/**
- * Return XML schema for the specified type, suitable for insertion into
- * the <types> element of a WSDL document, or underneath an
+ * Return XML schema for the specified type, suitable for insertion into
the
+ * <types> element of a WSDL document, or underneath an
* <element> or <attribute> declaration.
- *
- * @param javaType the Java Class we're writing out schema for
- * @param types the Java2WSDL Types object which holds the context
- * for the WSDL being generated.
+ *
+ * @param javaType
+ * the Java Class we're writing out schema for
+ * @param types
+ * the Java2WSDL Types object which holds the context for the
+ * WSDL being generated.
* @return a type element containing a schema simpleType/complexType
* @see org.apache.axis.wsdl.fromJava.Types
*/
public Element writeSchema(Class javaType, Types types) throws Exception {
- if (XmlObject.class.isAssignableFrom(javaType)) {
+ try {
+ if (!XmlObject.class.isAssignableFrom(javaType)) {
+ throw new RuntimeException(
+ "Invalid Object type is assigned to the
XMLBeanSerialization Type: "
+ + javaType.getCanonicalName());
+ }
+
SchemaType docType = XmlBeans.typeForClass(javaType);
+ writeSchemaForDocType(docType, types);
+ // assume that the writeSchemaForDocType wrote the schema
+ // for the type and all the dependent types.
+ return null;
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw e;
+ }
+ }
- /*
- * NOTE [EMAIL PROTECTED] 2004-Oct-18 --
- * This is a hack to handle node adoption.
- * I don't like it but I need to avoid a
- * org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR
- * NOTE [EMAIL PROTECTED] 2004-Oct-21 --
- * since I already use the Document I'll use it to check
- * if a schema for the namspace is already in place.
- */
-
- Document doc = types.createElement("deleteme")
- .getOwnerDocument();
- XmlOptions opts = new XmlOptions()
- .setLoadReplaceDocumentElement(null);
- Element root = doc.getDocumentElement();
- String schemaSrc = docType.getSourceName();
- InputStream stream = docType.getTypeSystem()
- .getSourceAsStream(schemaSrc);
-
- SchemaDocument.Schema schema = null;
- if (schemaSrc.endsWith(".wsdl") || schemaSrc.endsWith(".WSDL")) {
- DefinitionsDocument defDoc =
- DefinitionsDocument.Factory.parse(stream);
- TTypes tt = defDoc.getDefinitions().getTypesArray(0);
- SchemaDocument.Schema[] schemas = selectChildren
- (tt,SchemaDocument.Schema.class);
- if (schemas.length == 1) {
- schema = schemas[0];
- }
- else {
- String stNS = docType.getName().getNamespaceURI();
- System.out.println("target NS " + stNS);
- for (SchemaDocument.Schema s : schemas) {
- System.out.println("comparing schema namespace "
- + s.getTargetNamespace());
- if (stNS.equals(s.getTargetNamespace())) {
- schema = s;
- break;
- }
- }
- }
- }
- else {
- SchemaDocument schemaDoc =
SchemaDocument.Factory.parse(stream);
- schema = schemaDoc.getSchema();
+ /**
+ * @param types
+ * @param docType
+ * @return
+ * @throws Exception
+ */
+ private void writeSchemaForDocType(SchemaType docType, Types types)
+ throws Exception {
+ Schema mySchema = Utilities.findtSchemaDocument(docType);
+
+ QName q = docType.getName();
+
+ XmlObject typeNodeInWSDL = mySchema.getTypeNode(q);
+
+ if (null == typeNodeInWSDL)
+ throw new RuntimeException(
+ "Type for object not found in the assigned WSDL file. "
+ + docType.getName() + " schema in: "
+ + docType.getSourceName());
+ // insertDependentTypes(typeNodeInWSDL, types);
+ Node n = typeNodeInWSDL.getDomNode();
+ Document doc = types.createElement(
+
"element_to_get_document_useless_otherwise").getOwnerDocument();
+ Element e = (Element) doc.importNode(n, true);
+ try {
+ types.writeSchemaElementDecl(q, e);
+ } catch (AxisFault e1) {
+ // this means the types was already in... fine!
+ // TBD: make sure there are other types of exceptions are at least
+ // reported
+ }
+ Set<QName> dependentTypes = new HashSet<QName>();
+ getAllDependentTypes(typeNodeInWSDL, dependentTypes);
+ for (QName nxtType : dependentTypes) {
+ Class nxtJavaType = null;
+ // add the class if it is an xml bean
+ if (null != (nxtJavaType = q2UserClass(nxtType))
+ && XmlObject.class.isAssignableFrom(nxtJavaType)) {
+ writeSchema(nxtJavaType, types);
}
+ }
+ return;
+ }
- /*
- FIXME [EMAIL PROTECTED] 2004-Oct-21 -- it would be great if
- the Types.loadInputSchema took an input source instead of a
- String so I could directly pass in the input stream instead of
- providing the schema elements individually.
- */
- DefinitionsDocument defDoc = DefinitionsDocument.Factory
- .newInstance();
- TDefinitions definitions = defDoc.addNewDefinitions();
- definitions.addNewService();
- Node defEl = definitions.newDomNode(new XmlOptions()
- .setSaveOuter());
- Document dDoc = defEl.getOwnerDocument();
- if (null == dDoc.getDocumentElement()) {
- dDoc.appendChild(defEl);
- }
- Set<String> existingNameSpaces = new HashSet<String>();
- if (dDoc != null) {
- types.insertTypesFragment(dDoc);
- Element e = (Element) dDoc.getFirstChild().getFirstChild()
- .getFirstChild();
- if (e != null) {
- String tn = e.getAttribute("targetNamespace");
- existingNameSpaces.add(tn);
- while (null != (e = (Element) e.getNextSibling())) {
- tn = e.getAttribute("targetNamespace");
- existingNameSpaces.add(tn);
- }
- }
- } else {
- throw new Exception("null document");
- }
- if (schema != null) {
- String targetNamespace = schema.getTargetNamespace();
- if (targetNamespace != null) {
-
- if (!existingNameSpaces.contains(targetNamespace)) {
- TopLevelComplexType[] schemaTypes = schema
- .getComplexTypeArray();
- for (TopLevelComplexType top : schemaTypes) {
- types.writeSchemaElement(targetNamespace,
- (Element) doc
- .importNode(top.newDomNode()
- .getFirstChild(),
- true));
- }
- TopLevelElement[] elements = schema
- .getElementArray();
- for (TopLevelElement element : elements) {
- types.writeSchemaElement(targetNamespace,
- (Element) doc
- .importNode(element.newDomNode()
- .getFirstChild(),
- true));
- }
- }
- return null;
- }
+ /**
+ * @param nxtType
+ * @return null for classes that are not found, or if they are primitive
types
+ * *
+ */
+ private Class q2UserClass(QName qname) {
+ SchemaTypeLoader stl = XmlBeans.getContextTypeLoader();
+ SchemaType st = stl.findType(qname);
+ if (st == null) {
+ SchemaField sf = stl.findElement(qname);
+ if (sf != null)
+ st = sf.getType();
+ }
+
+ if (st != null && !st.isBuiltinType())
+ return st.getJavaClass();
+ else
+ return null; // for classes that are not found, or are build in
+
+ }
+
+ /**
+ * @param nodeInWSDL
+ * @param dependentTypes
+ * @return
+ *
+ * Walk all the nodes under the nodeInWSDL if there is an 'element' type
the
+ * add its types or references to the dependent type.
+ */
+ private void getAllDependentTypes(XmlObject nodeInWSDL,
+ Set<QName> dependentTypes) {
+ // scan for any node under the type that has "type" or "ref" attribute
+ XmlCursor cursor = nodeInWSDL.newCursor();
+ if (cursor.toFirstChild()) { // has child
+ while (true) {
+ getAllDependentTypes(cursor.getObject(), dependentTypes);
+ if (!cursor.toNextSibling())
+ break;
}
- throw new Exception(javaType.getName()
- + "did not specify a target namespace");
}
- else {
- throw new Exception(javaType.getName()
- + " must be a subclass of XmlObject");
+ if (nodeInWSDL.schemaType().getName().getLocalPart().equals(
+ "localElement")) {
+ LocalElement e = (LocalElement) nodeInWSDL;
+
+ if (e.isSetType())
+ dependentTypes.add(e.getType());
+ else if (e.isSetRef())
+ dependentTypes.add(e.getRef());
}
+ return;
}
-
- // NOTE [EMAIL PROTECTED] 2004-Nov-15 --
+ // NOTE [EMAIL PROTECTED] 2004-Nov-15 --
// once the WSDLProcessor is changed to an interface, remove this function
// and use the one in the upcoming XmlBeanWSDLProcessor.
private static <T extends XmlObject> T[] selectChildren(XmlObject parent,
- Class<T>
childClass)
- throws IllegalAccessException, NoSuchFieldException {
+ Class<T> childClass) throws IllegalAccessException,
+ NoSuchFieldException {
// retrieve the SchemaType from the static type field
SchemaType st = (SchemaType) childClass.getField("type").get(null);
XmlObject[] kids = parent.selectChildren(st.getDocumentElementName());
Modified:
incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/databinding/xmlbeans/XMLBeanTypeLookUp.java
URL:
http://svn.apache.org/viewcvs/incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/databinding/xmlbeans/XMLBeanTypeLookUp.java?view=diff&r1=153308&r2=153309
==============================================================================
---
incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/databinding/xmlbeans/XMLBeanTypeLookUp.java
(original)
+++
incubator/beehive/trunk/wsm/src/runtime/org/apache/beehive/wsm/databinding/xmlbeans/XMLBeanTypeLookUp.java
Thu Feb 10 16:52:03 2005
@@ -35,7 +35,8 @@
public class XMLBeanTypeLookUp implements BindingLookupService {
static Logger logger = Logger.getLogger(XMLBeanTypeLookUp.class);
-
+ static XMLBeanTypeLookUp theLookup = new XMLBeanTypeLookUp();
+
public QName class2qname( Class cls) {
if (XmlObject.class.isAssignableFrom(cls)) {
return XmlBeans.typeForClass(cls).getName();
@@ -140,5 +141,13 @@
}
return null;
}
+
+ /**
+ * @param nxtType
+ * @return
+ */
+ public static Class q2Class(QName q) {
+ return theLookup.qname2class(q);
+ }
}