cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bimargul...@apache.org
Subject svn commit: r592999 - in /incubator/cxf/trunk/rt/javascript/src: main/java/org/apache/cxf/javascript/ main/java/org/apache/cxf/javascript/service/ main/java/org/apache/cxf/javascript/types/ main/resources/org/apache/cxf/javascript/ test/java/org/apache...
Date Thu, 08 Nov 2007 02:28:31 GMT
Author: bimargulies
Date: Wed Nov  7 18:28:29 2007
New Revision: 592999

URL: http://svn.apache.org/viewvc?rev=592999&view=rev
Log:
More code generation. Modularity begins to get somewhere.

Added:
    incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/NamespacePrefixAccumulator.java
Modified:
    incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/JavascriptUtils.java
    incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/XmlSchemaUtils.java
    incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/service/ServiceJavascriptBuilder.java
    incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/types/SchemaJavascriptBuilder.java
    incubator/cxf/trunk/rt/javascript/src/main/resources/org/apache/cxf/javascript/cxf-utils.js
    incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/service/DocLitWrappedTest.java

Modified: incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/JavascriptUtils.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/JavascriptUtils.java?rev=592999&r1=592998&r2=592999&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/JavascriptUtils.java
(original)
+++ incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/JavascriptUtils.java
Wed Nov  7 18:28:29 2007
@@ -28,6 +28,9 @@
 import javax.xml.namespace.QName;
 
 import org.apache.cxf.wsdl.WSDLConstants;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.apache.ws.commons.schema.XmlSchemaComplexType;
+import org.apache.ws.commons.schema.XmlSchemaElement;
 import org.apache.ws.commons.schema.XmlSchemaSimpleType;
 import org.apache.ws.commons.schema.XmlSchemaType;
 
@@ -187,5 +190,84 @@
     
     public static String javaScriptNameToken(String token) {
         return token;
+    }
+    
+    public void generateCodeToSerializeElement(XmlSchemaElement element, 
+                                               String elementJavascriptName,
+                                               String elementXmlName,
+                                               XmlSchemaCollection xmlSchemaCollection,
+                                               String referencingURI,
+                                               XmlSchemaType containingType) {
+        boolean nillable = element.isNillable();
+        
+        XmlSchemaType elType = 
+            XmlSchemaUtils.getElementType(xmlSchemaCollection, referencingURI, element, containingType);
+        
+        // first question: optional?
+        if (XmlSchemaUtils.isParticleOptional(element)) {
+            startIf(elementJavascriptName + " != null");
+        }
+        
+        // nillable and optional would be very strange together.
+        // and nillable in the array case applies to the elements.
+        if (nillable && !XmlSchemaUtils.isParticleArray(element)) {
+            startIf(elementJavascriptName + " == null");
+            appendString("<" + elementXmlName + " " + XmlSchemaUtils.NIL_ATTRIBUTES +
"/>");
+            appendElse();
+        }
+        
+        if (XmlSchemaUtils.isParticleArray(element)) {
+            // protected against null in arrays.
+            startIf(elementJavascriptName + " != null");
+            startFor("var ax = 0", "ax < " +  elementJavascriptName + ".length", "ax ++");
+            elementJavascriptName = elementJavascriptName + "[ax]";
+            // we need an extra level of 'nil' testing here. Or do we, depending on the type
structure?
+            // Recode and fiddle appropriately.
+            startIf(elementJavascriptName + " == null");
+            appendString("<" + elementXmlName + " " + XmlSchemaUtils.NIL_ATTRIBUTES +
"/>");
+            appendElse();
+        }
+        
+        // now for the thing itself.
+        if (elType instanceof XmlSchemaComplexType) {
+            if (element.getMinOccurs() != 0) { // required
+                startIf(elementJavascriptName + " == null");
+                appendString("<" + elementXmlName + " " + XmlSchemaUtils.NIL_ATTRIBUTES
+ "/>");
+                appendElse();
+                appendExpression(elementJavascriptName + ".serialize(cxfjsutils, '" + elementXmlName
+ "')");
+                endBlock();
+            } else {
+                startIf(elementJavascriptName + " != null");
+                appendExpression(elementJavascriptName + ".serialize(cxfjsutils, '"  
+                                 + elementXmlName + "')");
+                endBlock();
+            }
+        } else {
+            QName typeName = elType.getQName();
+            appendString("<" + elementXmlName + ">");
+            // warning: this assumes that ordinary Javascript serialization is all we need.
+            // except for &gt; ad all of that.
+            if (isStringSimpleType(typeName)) {
+                appendExpression("cxfjsutils.escapeXmlEntities(" + elementJavascriptName
+ ")");
+            } else {
+                appendExpression(elementJavascriptName);
+            }
+            appendString("</" + elementXmlName + ">");
+        }
+        
+        if (XmlSchemaUtils.isParticleArray(element)) {
+            endBlock(); // for the extra level of nil checking, which might be wrong.
+            endBlock(); // for the for loop.
+            endBlock(); // the null protection.
+        }
+        
+        if (nillable && !XmlSchemaUtils.isParticleArray(element)) {
+            endBlock();
+        }
+        
+        if (XmlSchemaUtils.isParticleOptional(element)) {
+            endBlock();
+        }
+
     }
 }

Added: incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/NamespacePrefixAccumulator.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/NamespacePrefixAccumulator.java?rev=592999&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/NamespacePrefixAccumulator.java
(added)
+++ incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/NamespacePrefixAccumulator.java
Wed Nov  7 18:28:29 2007
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.cxf.javascript;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.cxf.service.model.SchemaInfo;
+import org.apache.ws.commons.schema.XmlSchemaElement;
+
+public class NamespacePrefixAccumulator {
+    private StringBuffer attributes;
+    private Set<String> prefixes;
+    private Map<String, String> fallbackNamespacePrefixMap;
+    private int nsCounter;
+    private SchemaInfo schemaInfo;
+
+    public NamespacePrefixAccumulator(SchemaInfo schemaInfo) {
+        attributes = new StringBuffer();
+        prefixes = new HashSet<String>();
+        fallbackNamespacePrefixMap = new HashMap<String, String>();
+        nsCounter = 0;
+        this.schemaInfo = schemaInfo;
+    }
+    
+    public void collect(String prefix, String uri) {
+        if (!prefixes.contains(prefix)) {
+            attributes.append("xmlns:" + prefix + "='" + uri + "' ");
+            prefixes.add(prefix);
+        }
+    }
+    
+    public String getAttributes() {
+        return attributes.toString();
+    }
+    
+    private String getPrefix(String namespaceURI) {
+        String schemaPrefix = schemaInfo.getSchema().getNamespaceContext().getPrefix(namespaceURI);
+        if (schemaPrefix == null || "tns".equals(schemaPrefix)) {
+            schemaPrefix = fallbackNamespacePrefixMap.get(namespaceURI);
+            if (schemaPrefix == null) {
+                schemaPrefix = "jns" + nsCounter;
+                nsCounter++;
+                fallbackNamespacePrefixMap.put(namespaceURI, schemaPrefix);
+            }
+        }
+        return schemaPrefix;
+    }
+    
+    /**
+     * This function obtains a name, perhaps namespace-qualified, for an element.
+     * It also maintains a Map that records all the prefixes used in the course
+     * of working on a single serializer function (and thus a single complex-type-element
+     * XML element) which is used for namespace prefix management.
+     * @param element
+     * @param namespaceMap
+     * @return
+     */
+    public String xmlElementString(XmlSchemaElement element) {
+        String namespaceURI = XmlSchemaUtils.getElementQualifier(schemaInfo, element);
+        if ("".equals(namespaceURI)) {
+            return element.getName(); // use the non-qualified name.
+        } else {
+            // What if there were a prefix in the element's qname? This is not apparently

+            // something that happens in this environment.
+            String prefix = getPrefix(namespaceURI);
+            collect(prefix, namespaceURI);
+            return prefix + ":" + element.getName();
+        }
+    }
+
+}
\ No newline at end of file

Modified: incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/XmlSchemaUtils.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/XmlSchemaUtils.java?rev=592999&r1=592998&r2=592999&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/XmlSchemaUtils.java
(original)
+++ incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/XmlSchemaUtils.java
Wed Nov  7 18:28:29 2007
@@ -25,9 +25,12 @@
 
 import org.apache.cxf.common.i18n.Message;
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.service.model.SchemaInfo;
+import org.apache.cxf.wsdl.WSDLConstants;
 import org.apache.ws.commons.schema.XmlSchemaCollection;
 import org.apache.ws.commons.schema.XmlSchemaComplexType;
 import org.apache.ws.commons.schema.XmlSchemaElement;
+import org.apache.ws.commons.schema.XmlSchemaForm;
 import org.apache.ws.commons.schema.XmlSchemaParticle;
 import org.apache.ws.commons.schema.XmlSchemaSequence;
 import org.apache.ws.commons.schema.XmlSchemaType;
@@ -36,7 +39,13 @@
  * 
  */
 public final class XmlSchemaUtils {
+    public static final XmlSchemaForm QUALIFIED = new XmlSchemaForm(XmlSchemaForm.QUALIFIED);
+    public static final XmlSchemaForm UNQUALIFIED = new XmlSchemaForm(XmlSchemaForm.UNQUALIFIED);
     
+    public static final String XSI_NS_ATTR = WSDLConstants.NP_XMLNS + ":" 
+                    + WSDLConstants.NP_SCHEMA_XSI + "='" + WSDLConstants.NS_SCHEMA_XSI +
"'";
+    public static final String NIL_ATTRIBUTES = XSI_NS_ATTR + " xsi:nil='true'";
+
     private static final Logger LOG = LogUtils.getL7dLogger(XmlSchemaUtils.class);
     
     private XmlSchemaUtils() {
@@ -135,5 +144,55 @@
         }
         return element.getSchemaType();
     }
+    
+    public static boolean isComplexType(XmlSchemaType type) {
+        return type instanceof XmlSchemaComplexType;
+    }
+    
+    public static boolean isElementNameQualified(XmlSchemaElement element, SchemaInfo schemaInfo)
{
+        if (element.getForm().equals(QUALIFIED)) {
+            return true;
+        }
+        if (element.getForm().equals(UNQUALIFIED)) {
+            return false;
+        }
+        return schemaInfo.getSchema().getElementFormDefault().equals(QUALIFIED);
+    }
+    
+    /**
+     * Return an empty string if this element should be unqualified in XML
+     * or the namespace URI if it should be qualified. 
+     * @param element
+     * @return
+     */
+    public static String getElementQualifier(SchemaInfo schemaInfo, XmlSchemaElement element)
{
+        QName qname;
+        boolean forceQualification = false;
+        // JAXB ends up with no form='qualified', but we qualify anyway if the namespace
don't
+        // match.
+        if (element.getRefName() != null) {
+            qname = element.getRefName();
+            forceQualification = !qname.getNamespaceURI().equals(schemaInfo.getNamespaceURI());
+        } else {
+            qname = element.getQName();
+        }
+        // some elements don't have qnames, only local names.
+        // one hopes that we aren't called upon to produce a qualified form for such an element,
though
+        // perhaps we're supposed to pull the TNS out of a hat.
+        if (forceQualification || isElementNameQualified(element, schemaInfo)) {
+            return qname.getNamespaceURI();
+        } else {
+            return "";
+        }
+    }
+    
+    public static boolean isParticleArray(XmlSchemaParticle particle) {
+        return particle.getMaxOccurs() > 1;
+    }
+    
+    public static boolean isParticleOptional(XmlSchemaParticle particle) {
+        return particle.getMinOccurs() == 0 && particle.getMaxOccurs() == 1;
+    }
+      
 
 }

Modified: incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/service/ServiceJavascriptBuilder.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/service/ServiceJavascriptBuilder.java?rev=592999&r1=592998&r2=592999&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/service/ServiceJavascriptBuilder.java
(original)
+++ incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/service/ServiceJavascriptBuilder.java
Wed Nov  7 18:28:29 2007
@@ -35,6 +35,7 @@
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.javascript.JavascriptUtils;
 import org.apache.cxf.javascript.NameManager;
+import org.apache.cxf.javascript.NamespacePrefixAccumulator;
 import org.apache.cxf.javascript.UnsupportedConstruct;
 import org.apache.cxf.javascript.XmlSchemaUtils;
 import org.apache.cxf.service.ServiceModelVisitor;
@@ -44,6 +45,7 @@
 import org.apache.cxf.service.model.MessageInfo;
 import org.apache.cxf.service.model.MessagePartInfo;
 import org.apache.cxf.service.model.OperationInfo;
+import org.apache.cxf.service.model.SchemaInfo;
 import org.apache.cxf.service.model.ServiceInfo;
 import org.apache.cxf.transport.local.LocalTransportFactory;
 import org.apache.cxf.wsdl.WSDLConstants;
@@ -64,6 +66,8 @@
     private String currentInterfaceClassName;
     private Set<OperationInfo> operationsWithNameConflicts;
     private XmlSchemaCollection xmlSchemaCollection;
+    private SchemaInfo serviceSchemaInfo;
+    private XmlSchemaElement wrapperElement;
 
     public ServiceJavascriptBuilder(ServiceInfo serviceInfo, NameManager nameManager) {
         super(serviceInfo);
@@ -96,6 +100,7 @@
             }
             localNameMap.put(operation.getName().getLocalPart(), operation);
         }
+        serviceSchemaInfo = serviceInfo.getSchema(serviceInfo.getTargetNamespace());
     }
 
     @Override
@@ -105,7 +110,7 @@
     @Override
     public void begin(MessagePartInfo part) {
     }
-
+    
     @Override
     public void begin(OperationInfo op) {
         assert !isRPC;
@@ -124,34 +129,16 @@
         MessageInfo inputMessage = op.getInput();
         String wrapperClassName = null;
         StringBuilder parameterList = new StringBuilder();
-        XmlSchemaElement wrapperElement = null;
-
+        
+        // the message content is a set of elements. Perhaps they come from the parts,
+        // or perhaps we invent them.
+        List<XmlSchemaElement> elements = new ArrayList<XmlSchemaElement>();
+        List<MessagePartInfo> parts  = null;
+        
         if (inputMessage != null) {
-            List<MessagePartInfo> parts = inputMessage.getMessageParts();
+            parts = inputMessage.getMessageParts();
             if (isWrapped) {
-                // expect one input part.
-                assert parts.size() == 1;
-                MessagePartInfo wrapperPart = parts.get(0);
-                // we expect a type
-                assert wrapperPart.isElement();
-                wrapperElement = (XmlSchemaElement)wrapperPart.getXmlSchema();
-                XmlSchemaComplexType wrapperType = 
-                    (XmlSchemaComplexType)XmlSchemaUtils.getElementType(xmlSchemaCollection,

-                                                                        op.getName().getNamespaceURI(),

-                                                                        wrapperElement,
-                                                                        null);
-                wrapperClassName = nameManager.getJavascriptName(wrapperType);
-                XmlSchemaSequence wrapperTypeSequence = XmlSchemaUtils.getSequence(wrapperType);
-                for (int i = 0; i < wrapperTypeSequence.getItems().getCount(); i++) {
-                    XmlSchemaObject thing = wrapperTypeSequence.getItems().getItem(i);
-                    if (!(thing instanceof XmlSchemaElement)) {
-                        XmlSchemaUtils.unsupportedConstruct("NON_ELEMENT_CHILD", thing.getClass()
-                            .getSimpleName(), wrapperType);
-                    }
-
-                    XmlSchemaElement elChild = (XmlSchemaElement)thing;
-                    inputParameterNames.add(elChild.getName());
-                }
+                wrapperClassName = setupWrapperElement(op, inputParameterNames, elements,
parts);
             }
 
             for (String param : inputParameterNames) {
@@ -170,19 +157,96 @@
         utils.appendLine("function " + opFunctionName + "(" + parameterList
                          + "responseCallback, errorCallback) {");
         
-        // wrapped
-        if (wrapperClassName != null) {
-            utils.appendLine("var wrapper = new " + wrapperClassName + "();");
+        if (parts != null) {
+            for (MessagePartInfo mpi : parts) {
+                XmlSchemaElement element;
+                if (mpi.isElement()) {
+                    element = (XmlSchemaElement) mpi.getXmlSchema();
+                    if (element == null) {
+                        element = xmlSchemaCollection.getElementByQName(mpi.getElementQName());
+                    }
+                        
+                } else {
+                    // there is still an element in there, but it's not a very interesting
element
+                    element = new XmlSchemaElement();
+                    XmlSchemaElement dummyElement = (XmlSchemaElement)mpi.getXmlSchema();
+                    element.setMaxOccurs(dummyElement.getMaxOccurs());
+                    element.setMinOccurs(dummyElement.getMinOccurs());
+                    element.setNillable(dummyElement.isNillable());
+                    element.setSchemaType(xmlSchemaCollection.getTypeByQName(mpi.getTypeQName()));
+                    element.setQName(mpi.getName());
+                }
+                assert element != null;
+                assert element.getQName() != null;
+                elements.add(element);
+            }
+        }
+        
+        // if not wrapped, we already have parameter vars for each of the parts.
+
+        if (isWrapped) {
+            // this will need Javascript name cleanup
+            String partElementName = 
+                JavascriptUtils.javaScriptNameToken(elements.get(0).getQName().getLocalPart());
+            utils.appendLine("var " + partElementName + " = new " + wrapperClassName + "();");
             for (String param : inputParameterNames) {
-                utils.appendLine("wrapper.set" + StringUtils.capitalize(param) + "(" + param
+ ");");
+                utils.appendLine(partElementName + ".set" 
+                                 + StringUtils.capitalize(param) + "(" + param + ");");
             }
         }
         
+        utils.appendLine("var cxfutils = new CxfApacheOrgUtil();");
+        
         SoapVersion soapVersion = soapBindingInfo.getSoapVersion();
         assert soapVersion.getVersion() == 1.1;
-        // we could have less code cooked in 
+        utils.appendLine("var xml;");
+        utils.appendLine("xml = cxfutils.beginSoap11Message();");
+        utils.setXmlStringAccumulator("xml");
+        
+        NamespacePrefixAccumulator prefixAccumulator = new NamespacePrefixAccumulator(serviceSchemaInfo);
+        
+        for (XmlSchemaElement partElement : elements) {
+            String partElementName = 
+                JavascriptUtils.javaScriptNameToken(partElement.getQName().getLocalPart());
+            String elementXmlRef = prefixAccumulator.xmlElementString(partElement);
+            utils.generateCodeToSerializeElement(partElement, partElementName, elementXmlRef,

+                                                 xmlSchemaCollection, serviceSchemaInfo.getNamespaceURI(),

+                                                 null);
+        }
+  
+        utils.appendLine("xml = xml + cxfutils.endSoap11Message();");
 
         utils.appendLine("}");
+    }
+
+    private String setupWrapperElement(OperationInfo op, List<String> inputParameterNames,
+                                       List<XmlSchemaElement> elements, List<MessagePartInfo>
parts) {
+        String wrapperClassName;
+        // expect one input part.
+        assert parts.size() == 1;
+        MessagePartInfo wrapperPart = parts.get(0);
+        // we expect a type
+        assert wrapperPart.isElement();
+        wrapperElement = (XmlSchemaElement)wrapperPart.getXmlSchema();
+        elements.add(wrapperElement);
+        XmlSchemaComplexType wrapperType = 
+            (XmlSchemaComplexType)XmlSchemaUtils.getElementType(xmlSchemaCollection, 
+                                                                op.getName().getNamespaceURI(),

+                                                                wrapperElement,
+                                                                null);
+        wrapperClassName = nameManager.getJavascriptName(wrapperType);
+        XmlSchemaSequence wrapperTypeSequence = XmlSchemaUtils.getSequence(wrapperType);
+        for (int i = 0; i < wrapperTypeSequence.getItems().getCount(); i++) {
+            XmlSchemaObject thing = wrapperTypeSequence.getItems().getItem(i);
+            if (!(thing instanceof XmlSchemaElement)) {
+                XmlSchemaUtils.unsupportedConstruct("NON_ELEMENT_CHILD", thing.getClass()
+                    .getSimpleName(), wrapperType);
+            }
+
+            XmlSchemaElement elChild = (XmlSchemaElement)thing;
+            inputParameterNames.add(elChild.getName());
+        }
+        return wrapperClassName;
     }
 
     @Override

Modified: incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/types/SchemaJavascriptBuilder.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/types/SchemaJavascriptBuilder.java?rev=592999&r1=592998&r2=592999&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/types/SchemaJavascriptBuilder.java
(original)
+++ incubator/cxf/trunk/rt/javascript/src/main/java/org/apache/cxf/javascript/types/SchemaJavascriptBuilder.java
Wed Nov  7 18:28:29 2007
@@ -19,25 +19,20 @@
 
 package org.apache.cxf.javascript.types;
 
-import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
 
 import javax.xml.namespace.QName;
 
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.javascript.JavascriptUtils;
 import org.apache.cxf.javascript.NameManager;
+import org.apache.cxf.javascript.NamespacePrefixAccumulator;
 import org.apache.cxf.javascript.UnsupportedConstruct;
 import org.apache.cxf.javascript.XmlSchemaUtils;
 import org.apache.cxf.service.model.SchemaInfo;
-import org.apache.cxf.wsdl.WSDLConstants;
 import org.apache.ws.commons.schema.XmlSchemaCollection;
 import org.apache.ws.commons.schema.XmlSchemaComplexType;
 import org.apache.ws.commons.schema.XmlSchemaElement;
-import org.apache.ws.commons.schema.XmlSchemaForm;
 import org.apache.ws.commons.schema.XmlSchemaObject;
 import org.apache.ws.commons.schema.XmlSchemaObjectTable;
 import org.apache.ws.commons.schema.XmlSchemaParticle;
@@ -54,17 +49,10 @@
     
     //private static final Logger LOG = LogUtils.getL7dLogger(SchemaJavascriptBuilder.class);
     
-    private static final XmlSchemaForm QUALIFIED = new XmlSchemaForm(XmlSchemaForm.QUALIFIED);
-    private static final XmlSchemaForm UNQUALIFIED = new XmlSchemaForm(XmlSchemaForm.UNQUALIFIED);
     
-    private static final String XSI_NS_ATTR = WSDLConstants.NP_XMLNS + ":" 
-        + WSDLConstants.NP_SCHEMA_XSI + "='" + WSDLConstants.NS_SCHEMA_XSI + "'";
-    private static final String NIL_ATTRIBUTES = XSI_NS_ATTR + " xsi:nil='true'";
     private XmlSchemaCollection xmlSchemaCollection;
-    private SchemaInfo schemaInfo;
     private NameManager nameManager;
-    private Map<String, String> fallbackNamespacePrefixMap;
-    private int nsCounter;
+    private SchemaInfo schemaInfo;
     
     public SchemaJavascriptBuilder(XmlSchemaCollection schemaCollection,
                                    NameManager nameManager, 
@@ -72,110 +60,6 @@
         this.xmlSchemaCollection = schemaCollection;
         this.nameManager = nameManager;
         this.schemaInfo = schemaInfo;
-        fallbackNamespacePrefixMap = new HashMap<String, String>();
-    }
-    
-    // this class assumes that the rest of the code is not going to try to use the same prefix
twice
-    // for two different URIs.
-    private static class NamespacePrefixAccumulator {
-        private StringBuffer attributes;
-        private Set<String> prefixes;
-        
-        NamespacePrefixAccumulator() {
-            attributes = new StringBuffer();
-            prefixes = new HashSet<String>();
-        }
-        
-        void collect(String prefix, String uri) {
-            if (!prefixes.contains(prefix)) {
-                attributes.append("xmlns:" + prefix + "='" + uri + "' ");
-                prefixes.add(prefix);
-            }
-        }
-        
-        String getAttributes() {
-            return attributes.toString();
-        }
-    }
-    
-    public static boolean isParticleArray(XmlSchemaParticle particle) {
-        return particle.getMaxOccurs() > 1;
-    }
-    
-    public static boolean isParticleOptional(XmlSchemaParticle particle) {
-        return particle.getMinOccurs() == 0 && particle.getMaxOccurs() == 1;
-    }
-    
-    /**
-     * Return an empty string if this element should be unqualified in XML
-     * or the namespace URI if it should be qualified. 
-     * @param element
-     * @return
-     */
-    private String getElementQualifier(XmlSchemaElement element) {
-        QName qname;
-        boolean forceQualification = false;
-        // JAXB ends up with no form='qualified', but we qualify anyway if the namespace
don't
-        // match.
-        if (element.getRefName() != null) {
-            qname = element.getRefName();
-            forceQualification = !qname.getNamespaceURI().equals(schemaInfo.getNamespaceURI());
-        } else {
-            qname = element.getQName();
-        }
-        // some elements don't have qnames, only local names.
-        // one hopes that we aren't called upon to produce a qualified form for such an element,
though
-        // perhaps we're supposed to pull the TNS out of a hat.
-        if (forceQualification || isElementNameQualified(element)) {
-            return qname.getNamespaceURI();
-        } else {
-            return "";
-        }
-    }
-    
-    /**
-     * This function obtains a name, perhaps namespace-qualified, for an element.
-     * It also maintains a Map that records all the prefixes used in the course
-     * of working on a single serializer function (and thus a single complex-type-element
-     * XML element) which is used for namespace prefix management.
-     * @param element
-     * @param namespaceMap
-     * @return
-     */
-    private String xmlElementString(XmlSchemaElement element, NamespacePrefixAccumulator
accumulator) {
-        String namespaceURI = getElementQualifier(element);
-        if ("".equals(namespaceURI)) {
-            return element.getName(); // use the non-qualified name.
-        } else {
-            // What if there were a prefix in the element's qname? This is not apparently

-            // something that happens in this environment.
-            String prefix = getPrefix(namespaceURI);
-            accumulator.collect(prefix, namespaceURI);
-            return prefix + ":" + element.getName();
-        }
-    }
-    
-    private boolean isElementNameQualified(XmlSchemaElement element) {
-        if (element.getForm().equals(QUALIFIED)) {
-            return true;
-        }
-        if (element.getForm().equals(UNQUALIFIED)) {
-            return false;
-        }
-        return schemaInfo.getSchema().getElementFormDefault().equals(QUALIFIED);
-    }
-    
-    private String getPrefix(String namespaceURI) {
-        String schemaPrefix = schemaInfo.getSchema().getNamespaceContext().getPrefix(namespaceURI);
-        if (schemaPrefix == null || "tns".equals(schemaPrefix)) {
-            schemaPrefix = fallbackNamespacePrefixMap.get(namespaceURI);
-            if (schemaPrefix == null) {
-                schemaPrefix = "jns" + nsCounter;
-                nsCounter++;
-                fallbackNamespacePrefixMap.put(namespaceURI, schemaPrefix);
-            }
-        }
-        return schemaPrefix;
     }
     
     public String generateCodeForSchema(SchemaInfo schema) {
@@ -248,9 +132,10 @@
             accessors.append(typeObjectName 
                              + ".prototype.set" + accessorSuffix + " = " + accessorName +
";\n");
             
-            if (isParticleOptional(elChild) || (nillable && !isParticleArray(elChild)))
{
+            if (XmlSchemaUtils.isParticleOptional(elChild) 
+                || (nillable && !XmlSchemaUtils.isParticleArray(elChild))) {
                 utils.appendLine(elementName + " = null;");
-            } else if (isParticleArray(elChild)) {
+            } else if (XmlSchemaUtils.isParticleArray(elChild)) {
                 utils.appendLine(elementName + " = [];");
             } else if (elType instanceof XmlSchemaComplexType) {
                 // even for required complex elements, we leave them null. 
@@ -286,7 +171,7 @@
         JavascriptUtils bodyUtils = new JavascriptUtils(bodyCode);
         bodyUtils.setXmlStringAccumulator("xml");
 
-        NamespacePrefixAccumulator prefixAccumulator = new NamespacePrefixAccumulator();
+        NamespacePrefixAccumulator prefixAccumulator = new NamespacePrefixAccumulator(schemaInfo);
         complexTypeSerializerBody(type, "this._", bodyUtils, prefixAccumulator);
         
         StringBuilder code = new StringBuilder();
@@ -338,8 +223,6 @@
         // XML Schema, please meet Iterable (not).
         for (int i = 0; i < sequence.getItems().getCount(); i++) {
             XmlSchemaElement elChild = (XmlSchemaElement)sequence.getItems().getItem(i);
-            XmlSchemaType elType = XmlSchemaUtils.getElementType(xmlSchemaCollection, null,
elChild, type);
-            boolean nillable = elChild.isNillable();
             if (elChild.isAbstract()) {
                 XmlSchemaUtils.unsupportedConstruct("ABSTRACT_ELEMENT", elChild.getName(),
type);
             }
@@ -347,72 +230,10 @@
             // assume that no lunatic has created multiple elements that differ only by namespace.
             // or, perhaps, detect that when generating the parser?
             String elementName = elementPrefix + elChild.getName();
-            String elementXmlRef = xmlElementString(elChild, prefixAccumulator);
-            
-            // first question: optional?
-            if (isParticleOptional(elChild)) {
-                utils.startIf(elementName + " != null");
-            }
+            String elementXmlRef = prefixAccumulator.xmlElementString(elChild);
             
-            // nillable and optional would be very strange together.
-            // and nillable in the array case applies to the elements.
-            if (nillable && !isParticleArray(elChild)) {
-                utils.startIf(elementName + " == null");
-                utils.appendString("<" + elementXmlRef + " " + NIL_ATTRIBUTES + "/>");
-                utils.appendElse();
-            }
-            
-            if (isParticleArray(elChild)) {
-                // protected against null in arrays.
-                utils.startIf(elementName + " != null");
-                utils.startFor("var ax = 0", "ax < " +  elementName + ".length", "ax ++");
-                elementName = elementName + "[ax]";
-                // we need an extra level of 'nil' testing here. Or do we, depending on the
type structure?
-                // Recode and fiddle appropriately.
-                utils.startIf(elementName + " == null");
-                utils.appendString("<" + elementXmlRef + " " + NIL_ATTRIBUTES + "/>");
-                utils.appendElse();
-            }
-            
-            // now for the thing itself.
-            if (elType instanceof XmlSchemaComplexType) {
-                if (elChild.getMinOccurs() != 0) { // required
-                    utils.startIf(elementName + " == null");
-                    utils.appendString("<" + elementXmlRef + " " + NIL_ATTRIBUTES + "/>");
-                    utils.appendElse();
-                    utils.appendExpression(elementName + ".serialize(cxfjsutils, '" + elementXmlRef
+ "')");
-                    utils.endBlock();
-                } else {
-                    utils.startIf(elementName + " != null");
-                    utils.appendExpression(elementName + ".serialize(cxfjsutils, '" + elementXmlRef
+ "')");
-                    utils.endBlock();
-                }
-            } else {
-                QName typeName = elType.getQName();
-                utils.appendString("<" + elementXmlRef + ">");
-                // warning: this assumes that ordinary Javascript serialization is all we
need.
-                // except for &gt; ad all of that.
-                if (utils.isStringSimpleType(typeName)) {
-                    utils.appendExpression("cxfjsutils.escapeXmlEntities(" + elementName
+ ")");
-                } else {
-                    utils.appendExpression(elementName);
-                }
-                utils.appendString("</" + elementXmlRef + ">");
-            }
-            
-            if (isParticleArray(elChild)) {
-                utils.endBlock(); // for the extra level of nil checking, which might be
wrong.
-                utils.endBlock(); // for the for loop.
-                utils.endBlock(); // the null protection.
-            }
-            
-            if (nillable && !isParticleArray(elChild)) {
-                utils.endBlock();
-            }
-            
-            if (isParticleOptional(elChild)) {
-                utils.endBlock();
-            }
+            utils.generateCodeToSerializeElement(elChild, elementName, 
+                                                 elementXmlRef, xmlSchemaCollection, null,
type);
         }
     }
     /**
@@ -463,17 +284,17 @@
             
             String elementName = elChild.getName();
             utils.appendLine("cxfjsutils.trace('processing " + elementName + "');");
-            String elementNamespaceURI = getElementQualifier(elChild);
+            String elementNamespaceURI = XmlSchemaUtils.getElementQualifier(schemaInfo, elChild);
             
             String valueTarget = "item";
 
-            if (isParticleOptional(elChild) || isParticleArray(elChild)) {
+            if (XmlSchemaUtils.isParticleOptional(elChild) || XmlSchemaUtils.isParticleArray(elChild))
{
                 utils.startIf("curElement != null && cxfjsutils.isNodeNamedNS(curElement,
'" 
                               + elementNamespaceURI 
                               + "', '" 
                               + elementName
                               + "')");
-                if (isParticleArray(elChild)) {
+                if (XmlSchemaUtils.isParticleArray(elChild)) {
                     utils.appendLine("item = [];");
                     utils.startDo();
                     valueTarget = "arrayItem";
@@ -496,7 +317,7 @@
             }
              
             utils.endBlock(); // the if for the nil.
-            if (isParticleArray(elChild)) {
+            if (XmlSchemaUtils.isParticleArray(elChild)) {
                 utils.appendLine("item.push(arrayItem);");
                 utils.appendLine("curElement = cxfjsutils.getNextElementSibling(curElement);");
                 utils.endBlock();
@@ -505,12 +326,12 @@
                                   + elChild.getName() + "'));");
             }
             utils.appendLine("newobject." + accessorName + "(item);");
-            if (!isParticleArray(elChild)) {
+            if (!XmlSchemaUtils.isParticleArray(elChild)) {
                 utils.startIf("curElement != null");
                 utils.appendLine("curElement = cxfjsutils.getNextElementSibling(curElement);");
                 utils.endBlock();
             }
-            if (isParticleOptional(elChild) || isParticleArray(elChild)) {
+            if (XmlSchemaUtils.isParticleOptional(elChild) || XmlSchemaUtils.isParticleArray(elChild))
{
                 utils.endBlock();
             }
         }

Modified: incubator/cxf/trunk/rt/javascript/src/main/resources/org/apache/cxf/javascript/cxf-utils.js
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/main/resources/org/apache/cxf/javascript/cxf-utils.js?rev=592999&r1=592998&r2=592999&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/javascript/src/main/resources/org/apache/cxf/javascript/cxf-utils.js
(original)
+++ incubator/cxf/trunk/rt/javascript/src/main/resources/org/apache/cxf/javascript/cxf-utils.js
Wed Nov  7 18:28:29 2007
@@ -17,6 +17,9 @@
  * under the License.
  */
  
+// This code is structured on to require a 'new' of an object of type CxfApacheOrgUtil.
+// Alternative, it could be made 'static', but this allowed us to use this same object
+// to carry some state.
  
 function cxf_apache_org_util_null_trace(message)
 {
@@ -141,6 +144,9 @@
 }
 
 CxfApacheOrgUtil.prototype.getNodeText = org_apache_cxf_getNodeText;
+
+// The following could be parameterized using the SoapVersion class, but does anyone believe
that
+// there will ever be another soap version?
 
 function org_apache_cxf_begin_soap11_message()
 {

Modified: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/service/DocLitWrappedTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/service/DocLitWrappedTest.java?rev=592999&r1=592998&r2=592999&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/service/DocLitWrappedTest.java
(original)
+++ incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/service/DocLitWrappedTest.java
Wed Nov  7 18:28:29 2007
@@ -33,9 +33,11 @@
 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 import org.apache.cxf.service.model.SchemaInfo;
 import org.apache.cxf.service.model.ServiceInfo;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
 
+@Ignore
 public class DocLitWrappedTest extends AbstractDependencyInjectionSpringContextTests {
     private JavascriptTestUtilities testUtilities;
     private Client client;
@@ -57,7 +59,6 @@
     @Test 
     public void testDeserialization() throws Exception {
         setupClientAndRhino("simple-dlwu-proxy-factory");
-        testUtilities.readResourceIntoRhino("/deserializationTests.js");
         DataBinding dataBinding = clientProxyFactory.getServiceFactory().getDataBinding();
         assertNotNull(dataBinding);
     }
@@ -87,5 +88,7 @@
         ServiceJavascriptBuilder serviceBuilder = 
             new ServiceJavascriptBuilder(serviceInfo, nameManager);
         serviceBuilder.walk();
+        String serviceJavascript = serviceBuilder.getCode();
+        testUtilities.readStringIntoRhino(serviceJavascript, serviceInfo.toString() + ".js");
     }
 }



Mime
View raw message