Author: bimargulies
Date: Wed Oct 31 18:35:49 2007
New Revision: 590874
URL: http://svn.apache.org/viewvc?rev=590874&view=rev
Log:
Pass an initial test of the javascript code that deserializes. This depends
on a do-it-ourself micro-DOM wrapper for Rhino.
Added:
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java
incubator/cxf/trunk/rt/javascript/src/test/resources/deserializationTests.js
incubator/cxf/trunk/rt/javascript/src/test/resources/serializationTests.js
- copied, changed from r590636,
incubator/cxf/trunk/rt/javascript/src/test/resources/serializationTest.js
Removed:
incubator/cxf/trunk/rt/javascript/src/test/resources/serializationTest.js
Modified:
incubator/cxf/trunk/rt/javascript/pom.xml
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/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/JavascriptTestUtilities.java
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/fortest/TestBean1.java
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/types/SerializationTests.java
incubator/cxf/trunk/systests/pom.xml
Modified: incubator/cxf/trunk/rt/javascript/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/pom.xml?rev=590874&r1=590873&r2=590874&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/javascript/pom.xml (original)
+++ incubator/cxf/trunk/rt/javascript/pom.xml Wed Oct 31 18:35:49 2007
@@ -140,7 +140,7 @@
<dependency>
<groupId>rhino</groupId>
<artifactId>js</artifactId>
- <version>1.6R6</version>
+ <version>1.6R7</version>
<scope>test</scope>
</dependency>
</dependencies>
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=590874&r1=590873&r2=590874&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 Oct 31 18:35:49 2007
@@ -28,6 +28,7 @@
import javax.xml.namespace.QName;
import org.apache.cxf.wsdl.WSDLConstants;
+import org.apache.ws.commons.schema.XmlSchemaSimpleType;
import org.apache.ws.commons.schema.XmlSchemaType;
/**
@@ -40,20 +41,35 @@
private String xmlStringAccumulatorVariable;
private Map<String, String> defaultValueForSimpleType;
private Set<String> nonStringSimpleTypes;
+ private Set<String> intTypes;
+ private Set<String> floatTypes;
public JavascriptUtils(StringBuffer code) {
this.code = code;
defaultValueForSimpleType = new HashMap<String, String>();
defaultValueForSimpleType.put("int", "0");
+ defaultValueForSimpleType.put("unsignedInt", "0");
defaultValueForSimpleType.put("long", "0");
+ defaultValueForSimpleType.put("unsignedLong", "0");
defaultValueForSimpleType.put("float", "0.0");
defaultValueForSimpleType.put("double", "0.0");
nonStringSimpleTypes = new HashSet<String>();
nonStringSimpleTypes.add("int");
nonStringSimpleTypes.add("long");
+ nonStringSimpleTypes.add("unsignedInt");
+ nonStringSimpleTypes.add("unsignedLong");
nonStringSimpleTypes.add("float");
nonStringSimpleTypes.add("double");
+ intTypes = new HashSet<String>();
+ intTypes.add("int");
+ intTypes.add("long");
+ intTypes.add("unsignedInt");
+ intTypes.add("unsignedLong");
+ floatTypes = new HashSet<String>();
+ floatTypes.add("float");
+ floatTypes.add("double");
+
prefixStack = new Stack<String>();
prefixStack.push(" ");
}
@@ -143,5 +159,29 @@
code.append(prefix());
code.append("for (" + start + ";" + test + ";" + increment + ") {" +
NL);
prefixStack.push(prefix() + " ");
+ }
+
+ public void startDo() {
+ code.append(prefix());
+ code.append("do {" + NL);
+ prefixStack.push(prefix() + " ");
+ }
+
+ // Given a js variable and a simple type object, correctly set the
variables simple type
+ public String javascriptParseExpression(XmlSchemaType type, String value) {
+ if (!(type instanceof XmlSchemaSimpleType)) {
+ return value;
+ }
+ assert
type.getQName().getNamespaceURI().equals(WSDLConstants.NU_SCHEMA_XSD);
+ String name = type.getName();
+ if (intTypes.contains(name)) {
+ return "parseInt(" + value + ")";
+ } else if (floatTypes.contains(name)) {
+ return "parseFloat(" + value + ")";
+ } else if ("boolean".equals(name)) {
+ return "(" + value + " == true)";
+ } else {
+ return value;
+ }
}
}
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=590874&r1=590873&r2=590874&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 Oct 31 18:35:49 2007
@@ -44,6 +44,7 @@
import org.apache.ws.commons.schema.XmlSchemaObjectTable;
import org.apache.ws.commons.schema.XmlSchemaParticle;
import org.apache.ws.commons.schema.XmlSchemaSequence;
+import org.apache.ws.commons.schema.XmlSchemaSimpleType;
import org.apache.ws.commons.schema.XmlSchemaType;
/**
@@ -131,17 +132,16 @@
}
/**
- * 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.
+ * Return an empty string if this element should be unqualified in XML
+ * or the namespace URI if it should be qualified.
* @param element
- * @param namespaceMap
* @return
*/
- private String xmlElementString(XmlSchemaElement element,
NamespacePrefixAccumulator accumulator) {
+ 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());
@@ -152,15 +152,31 @@
// 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)) {
- assert qname != null;
- String prefix = qname.getPrefix();
- if ("".equals(prefix)) { // this is not quite good enough.
- prefix = getPrefix(qname.getNamespaceURI());
- }
- accumulator.collect(prefix, qname.getNamespaceURI());
- return prefix + ":" + qname.getLocalPart();
+ 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();
}
}
@@ -203,6 +219,7 @@
XmlSchemaComplexType complexType =
(XmlSchemaComplexType)xmlSchemaObject;
code.append(complexTypeConstructorAndAccessors(complexType));
code.append(complexTypeSerializerFunction(complexType));
+ code.append(domDeserializerFunction(complexType));
} catch (UnsupportedSchemaConstruct usc) {
continue; // it could be empty, but the style checker
would complain.
}
@@ -435,5 +452,102 @@
utils.endBlock();
}
}
+ }
+ /**
+ * Generate a JavaScript function that takes an element for a complex type
and walks through
+ * its children using them to fill in the values for a JavaScript object.
+ * @param type schema type for the process
+ * @return the string contents of the JavaScript.
+ */
+ public String domDeserializerFunction(XmlSchemaComplexType type) {
+ StringBuffer code = new StringBuffer();
+ JavascriptUtils utils = new JavascriptUtils(code);
+ XmlSchemaParticle particle = type.getParticle();
+ XmlSchemaSequence sequence = null;
+
+ if (particle == null) {
+ unsupportedConstruct("NULL_PARTICLE", type);
+ }
+
+ try {
+ sequence = (XmlSchemaSequence) particle;
+ } catch (ClassCastException cce) {
+ unsupportedConstruct("NON_SEQUENCE_PARTICLE", type);
+ }
+
+ String typeObjectName = nameManager.getJavascriptName(type);
+ code.append("function " + typeObjectName + "_deserialize (cxfjsutils,
element) {\n");
+ // create the object we are deserializing into.
+ utils.appendLine("var newobject = new " + typeObjectName + "();");
+
+ utils.appendLine("var curElement =
cxfjsutils.getFirstElementChild(element);");
+ utils.appendLine("var item;");
+
+ for (int i = 0; i < sequence.getItems().getCount(); i++) {
+ utils.appendLine("cxfjsutils.trace('curElement: ' +
cxfjsutils.traceElementName(curElement));");
+ XmlSchemaObject thing = sequence.getItems().getItem(i);
+ if (!(thing instanceof XmlSchemaElement)) {
+ unsupportedConstruct("NON_ELEMENT_CHILD",
thing.getClass().getSimpleName(), type);
+ }
+
+ XmlSchemaElement elChild = (XmlSchemaElement)thing;
+ XmlSchemaType elType = getElementType(type, elChild);
+ boolean simple = elType instanceof XmlSchemaSimpleType;
+
+ String accessorName = "set" +
StringUtils.capitalize(elChild.getName());
+ // For optional or an array, we need to check if the element is
the
+ // one we want.
+
+ String elementName = elChild.getName();
+ utils.appendLine("cxfjsutils.trace('processing " + elementName +
"');");
+ String elementNamespaceURI = getElementQualifier(elChild);
+
+ String valueTarget = "item";
+
+ if (isParticleOptional(elChild) || isParticleArray(elChild)) {
+ utils.startIf("curElement != null &&
cxfjsutils.isNodeNamedNS(curElement, '"
+ + elementNamespaceURI
+ + "', '"
+ + elementName
+ + "')");
+ if (isParticleArray(elChild)) {
+ utils.appendLine("item = [];");
+ utils.startDo();
+ valueTarget = "arrayItem";
+ utils.appendLine("var arrayItem;");
+ }
+ }
+
+ utils.appendLine("var value = null;");
+ utils.startIf("!cxfjsutils.isElementNil(curElement)");
+ if (simple) {
+ utils.appendLine("value =
cxfjsutils.getNodeText(curElement);");
+ utils.appendLine(valueTarget
+ + " = " +
utils.javascriptParseExpression(elType, "value")
+ + ";");
+ } else {
+ String elTypeJsName =
nameManager.getJavascriptName((XmlSchemaComplexType)elType);
+ utils.appendLine(valueTarget + " = "
+ + elTypeJsName
+ + "_deserialize(cxfjsutils, curElement);");
+ }
+
+ utils.endBlock(); // the if for the nil.
+ if (isParticleArray(elChild)) {
+ utils.appendLine("item.push(arrayItem);");
+ utils.endBlock();
+ utils.appendLine("while(cfjsutils.isNodeNamedNS(curElement, '"
+ + elementNamespaceURI + "', '"
+ + elChild.getName() + "'));");
+ }
+ utils.appendLine("newobject." + accessorName + "(item);");
+ utils.appendLine("curElement =
cxfjsutils.getNextElementSibling(curElement);");
+ if (isParticleOptional(elChild) || isParticleArray(elChild)) {
+ utils.endBlock();
+ }
+ }
+ utils.appendLine("return newobject;");
+ code.append("}\n");
+ return code.toString() + "\n";
}
}
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=590874&r1=590873&r2=590874&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 Oct 31 18:35:49 2007
@@ -17,28 +17,50 @@
* under the License.
*/
- // We use a pseudo-class for name scoping here.
+
+function cxf_apache_org_util_null_trace(message)
+{
+}
function CxfApacheOrgUtil()
{
this.ELEMENT_NODE = 1;
+ if ("function" == typeof(org_apache_cxf_trace)) {
+ this.trace = org_apache_cxf_trace.trace;
+ } else {
+ this.trace = cxf_apache_org_util_null_trace;
+ }
}
// compensate for Microsoft's weakness here.
function org_apache_cxf_getNodeLocalName(node)
{
- if(node.localName)
+ if("localName" in node) {
return node.localName;
- else
+ } else {
return node.baseName;
+ }
}
-CxfApacheOrgUtil.prototype.getNodeLocalName = org_apache_cxf_getNodeLocalName;
+CxfApacheOrgUtil.prototype.getNodeLocalName = org_apache_cxf_getNodeLocalName;
+function org_apache_cxf_element_name_for_trace(node)
+{
+ if(node == null)
+ return "null";
+ else if(node == undefined)
+ return "undefined";
+ else {
+ var n = '';
+ if(node.namespaceURI != null && node.namespaceURI != '') {
+ n = n + "{" + node.namespaceURI + "}";
+ }
+ return n + this.getNodeLocalName(node);
+ }
+}
+
+CxfApacheOrgUtil.prototype.traceElementName =
org_apache_cxf_element_name_for_trace;
-//*************************************************
-// XML Utils
-//*************************************************
function org_apache_cxf_escapeXmlEntities(val) {
if(val == null)
return "";
@@ -49,6 +71,8 @@
CxfApacheOrgUtil.prototype.escapeXmlEntities =
org_apache_cxf_escapeXmlEntities;
function org_apache_cxf_isElementNil(node) {
+ if(node == null)
+ throw "null node passed to isElementNil";
// we need to look for an attribute xsi:nil, where xsi is
// http://www.w3.org/2001/XMLSchema-instance. we have the usual
// problem here with namespace-awareness.
@@ -64,15 +88,23 @@
CxfApacheOrgUtil.prototype.isElementNil = org_apache_cxf_isElementNil;
function org_apache_cxf_getFirstElementChild(node) {
+ if(node == undefined)
+ throw "undefined node to getFirstElementChild";
+
var n;
- for(n = node.firstChild; n != null && n.nodeType != this.ELEMENT_NODE;
n = n.nextSibling) {
+ for(n = node.firstChild; n != null && n.nodeType != this.ELEMENT_NODE;
n = n.nextSibling) {
}
+
return n;
}
CxfApacheOrgUtil.prototype.getFirstElementChild =
org_apache_cxf_getFirstElementChild;
function org_apache_cxf_getNextElementSibling(node) {
+ if(node == undefined)
+ throw "undefined node to getNextElementSibling";
+ if(node == null)
+ throw "null node to getNextElementSibling";
var n;
for(n = node.nextSibling; n != null && n.nodeType != this.ELEMENT_NODE;
n = n.nextSibling)
;
@@ -83,13 +115,16 @@
function org_apache_cxf_isNodeNamedNS(node, namespaceURI, localName)
{
+ if(node == undefined)
+ throw "undefined node to isNodeNamedNS";
+
if(namespaceURI == '' || namespaceURI == null) {
if(node.namespaceURI == '' || node.namespaceURI == null) {
- return localName == xml_getLocalName(node);
+ return localName == org_apache_cxf_getNodeLocalName(node);
} else
return false;
} else {
- return namespaceURI == node.namespaceURI && localName ==
xml_getLocalName(node);
+ return namespaceURI == node.namespaceURI && localName ==
org_apache_cxf_getNodeLocalName(node);
}
}
Modified:
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JavascriptTestUtilities.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JavascriptTestUtilities.java?rev=590874&r1=590873&r2=590874&view=diff
==============================================================================
---
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JavascriptTestUtilities.java
(original)
+++
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JavascriptTestUtilities.java
Wed Oct 31 18:35:49 2007
@@ -21,6 +21,10 @@
import java.io.IOException;
import java.io.Reader;
+import java.lang.reflect.InvocationTargetException;
+import java.util.logging.Logger;
+
+import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.test.TestUtilities;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
@@ -31,8 +35,47 @@
*/
public class JavascriptTestUtilities extends TestUtilities {
+ private static final Logger LOG =
LogUtils.getL7dLogger(JavascriptTestUtilities.class);
+
private ScriptableObject rhinoScope;
private Context rhinoContext;
+
+ public static class JavaScriptAssertionFailed extends RuntimeException {
+
+ public JavaScriptAssertionFailed(String what) {
+ super(what);
+ }
+ }
+
+ public static class Assert extends ScriptableObject {
+
+ public Assert() { }
+ public void jsConstructor(String exp) {
+ throw new JavaScriptAssertionFailed(exp);
+ }
+ @Override
+ public String getClassName() {
+ return "Assert";
+ }
+ }
+
+ public static class Trace extends ScriptableObject {
+
+ public Trace() {
+ }
+
+ @Override
+ public String getClassName() {
+ return "org_apache_cxf_trace";
+ }
+
+ //CHECKSTYLE:OFF
+ public static void jsStaticFunction_trace(String message) {
+ LOG.info(message);
+ }
+ //CHECKSTYLE:ON
+
+ }
public JavascriptTestUtilities(Class<?> classpathReference) {
super(classpathReference);
@@ -41,6 +84,18 @@
public void initializeRhino() {
rhinoContext = Context.enter();
rhinoScope = rhinoContext.initStandardObjects();
+ try {
+ ScriptableObject.defineClass(rhinoScope, Assert.class);
+ ScriptableObject.defineClass(rhinoScope, Trace.class);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (InstantiationException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ JsSimpleDomNode.register(rhinoScope);
+ JsSimpleDomParser.register(rhinoScope);
}
public void readResourceIntoRhino(String resourceClasspath) throws
IOException {
Added:
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java?rev=590874&view=auto
==============================================================================
---
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java
(added)
+++
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java
Wed Oct 31 18:35:49 2007
@@ -0,0 +1,169 @@
+/**
+ * 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.lang.reflect.InvocationTargetException;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ScriptableObject;
+
+/**
+ * A Rhino wrapper around org.w3c.dom.Node. Not comprehensive, but enough to
test CXF JavaScript.
+ */
+public class JsSimpleDomNode extends ScriptableObject {
+ private Node wrappedNode;
+ private boolean childrenWrapped;
+ private JsSimpleDomNode previousSibling;
+ private JsSimpleDomNode nextSibling;
+ private JsSimpleDomNode[] children;
+
+ /**
+ * Only exists to make Rhino happy. Should never be used.
+ */
+ public JsSimpleDomNode() {
+ }
+
+ public static void register(ScriptableObject scope) {
+ try {
+ ScriptableObject.defineClass(scope, JsSimpleDomNode.class);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (InstantiationException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public String getClassName() {
+ return "Node";
+ }
+
+ //CHECKSTYLE:OFF
+ public String jsGet_localName() {
+ String localName = wrappedNode.getLocalName();
+ return localName;
+ }
+
+ public String jsGet_namespaceURI() {
+ return wrappedNode.getNamespaceURI();
+ }
+
+ public Object jsGet_firstChild() {
+ establishChildren();
+ if (children.length > 0)
+ return children[0];
+ else
+ return null;
+ }
+
+ public Object jsGet_nextSibling() {
+ return nextSibling;
+ }
+
+ public Object jsGet_previousSibling() {
+ return previousSibling;
+ }
+
+ public int jsGet_nodeType() {
+ return wrappedNode.getNodeType();
+ }
+
+ public String jsGet_nodeValue() {
+ return wrappedNode.getNodeValue();
+ }
+
+ public Object[] jsGet_childNodes() {
+ establishChildren();
+ return children;
+ }
+
+ public String jsFunction_getAttributeNS(String namespaceURI, String
localName) {
+ NamedNodeMap attributes = wrappedNode.getAttributes();
+ Node attrNode = attributes.getNamedItemNS(namespaceURI, localName);
+ if(attrNode == null) {
+ return null;
+ } else {
+ Attr attribute = (Attr) attrNode;
+ return attribute.getValue();
+ }
+ }
+
+ public String jsFunction_getAttribute(String localName) {
+ NamedNodeMap attributes = wrappedNode.getAttributes();
+ Node attrNode = attributes.getNamedItem(localName);
+ if(attrNode == null) {
+ return null;
+ } else {
+ Attr attribute = (Attr) attrNode;
+ return attribute.getValue();
+ }
+ }
+
+ //CHECKSTYLE:ON
+
+ private JsSimpleDomNode newObject(Node node, JsSimpleDomNode prev) {
+ Context cx = Context.enter();
+ JsSimpleDomNode newObject =
(JsSimpleDomNode)cx.newObject(getParentScope(), "Node");
+ newObject.initialize(node, prev);
+ return newObject;
+ }
+
+ private void establishChildren() {
+ if (!childrenWrapped) {
+ if (wrappedNode.hasChildNodes()) {
+ NodeList nodeChildren = wrappedNode.getChildNodes();
+ children = new JsSimpleDomNode[nodeChildren.getLength()];
+ for (int x = 0; x < nodeChildren.getLength(); x++) {
+ JsSimpleDomNode prev = null;
+ if (x > 0) {
+ prev = (JsSimpleDomNode)children[x - 1];
+ }
+ children[x] = newObject(nodeChildren.item(x), prev);
+ if (x > 0) {
+ children[x - 1].setNext(children[x]);
+ }
+ }
+ } else {
+ children = new JsSimpleDomNode[0];
+ }
+ childrenWrapped = true;
+ }
+ }
+
+ //rhino won't let us use a constructor.
+ void initialize(Node node, JsSimpleDomNode prev) {
+ wrappedNode = node;
+ childrenWrapped = false;
+ previousSibling = prev;
+ }
+
+ void setNext(JsSimpleDomNode next) {
+ nextSibling = next;
+ }
+
+
+}
Added:
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java?rev=590874&view=auto
==============================================================================
---
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java
(added)
+++
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java
Wed Oct 31 18:35:49 2007
@@ -0,0 +1,97 @@
+/**
+ * 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.io.IOException;
+import java.io.StringReader;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Document;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ScriptableObject;
+
+/**
+ * A Rhino wrapper to define DOMParser.
+ */
+public class JsSimpleDomParser extends ScriptableObject {
+
+ private DocumentBuilder documentBuilder;
+
+ public JsSimpleDomParser() {
+ try {
+ DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
+ documentBuilderFactory.setNamespaceAware(true);
+ documentBuilderFactory.setCoalescing(true);
+ documentBuilder = documentBuilderFactory.newDocumentBuilder();
+ } catch (ParserConfigurationException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static void register(ScriptableObject scope) {
+ try {
+ ScriptableObject.defineClass(scope, JsSimpleDomParser.class);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (InstantiationException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public String getClassName() {
+ return "DOMParser";
+ }
+
+ //CHECKSTYLE:OFF
+
+ public Object jsFunction_parse(String xml, String mimeType) {
+ StringReader reader = new StringReader(xml);
+ InputSource inputSource = new InputSource(reader);
+ Document document;
+ try {
+ document = documentBuilder.parse(inputSource);
+ } catch (SAXException e) {
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ Context context = Context.enter();
+ JsSimpleDomNode domNode =
(JsSimpleDomNode)context.newObject(getParentScope(), "Node");
+ domNode.initialize(document.getDocumentElement(), null);
+ return domNode;
+ }
+
+
+
+ //CHECKSTYLE:ON
+
+}
Modified:
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/fortest/TestBean1.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/fortest/TestBean1.java?rev=590874&r1=590873&r2=590874&view=diff
==============================================================================
---
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/fortest/TestBean1.java
(original)
+++
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/fortest/TestBean1.java
Wed Oct 31 18:35:49 2007
@@ -20,11 +20,13 @@
package org.apache.cxf.javascript.fortest;
import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* Bean with a selection of elements suitable for testing the JavaScript
client.
*/
[EMAIL PROTECTED]
@XmlType(namespace = "uri:org.apache.cxf.javascript.testns")
public class TestBean1 {
//CHECKSTYLE:OFF
@@ -36,6 +38,8 @@
public byte[] base64Item;
@XmlElement(required = false)
public int optionalIntItem;
+ @XmlElement(defaultValue = "trip", required = false, namespace =
"uri:org.apache.cxf.javascript.testns2")
+ public String optionalStringItem;
@XmlElement(required = false)
public int[] optionalIntArrayItem;
Modified:
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/types/SerializationTests.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/types/SerializationTests.java?rev=590874&r1=590873&r2=590874&view=diff
==============================================================================
---
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/types/SerializationTests.java
(original)
+++
incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/types/SerializationTests.java
Wed Oct 31 18:35:49 2007
@@ -21,19 +21,24 @@
import java.io.IOException;
import java.io.StringReader;
+import java.io.StringWriter;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
import org.apache.cxf.Bus;
import org.apache.cxf.databinding.DataBinding;
import org.apache.cxf.databinding.DataReader;
+import org.apache.cxf.databinding.DataWriter;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.javascript.BasicNameManager;
import org.apache.cxf.javascript.JavascriptTestUtilities;
+import
org.apache.cxf.javascript.JavascriptTestUtilities.JavaScriptAssertionFailed;
import org.apache.cxf.javascript.NameManager;
import org.apache.cxf.javascript.fortest.TestBean1;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
@@ -47,6 +52,7 @@
public class SerializationTests extends
AbstractDependencyInjectionSpringContextTests {
private JavascriptTestUtilities testUtilities;
private XMLInputFactory xmlInputFactory;
+ private XMLOutputFactory xmlOutputFactory;
private Client client;
private List<ServiceInfo> serviceInfos;
private Collection<SchemaInfo> schemata;
@@ -57,6 +63,7 @@
testUtilities = new JavascriptTestUtilities(getClass());
testUtilities.addDefaultNamespaces();
xmlInputFactory = XMLInputFactory.newInstance();
+ xmlOutputFactory = XMLOutputFactory.newInstance();
}
@Override
@@ -64,11 +71,36 @@
return new String[] {"classpath:serializationTestBeans.xml"};
}
+ @Test
+ public void testDeserialization() throws Exception {
+ setupClientAndRhino("simple-dlwu-proxy-factory");
+ testUtilities.readResourceIntoRhino("/deserializationTests.js");
+ DataBinding dataBinding =
clientProxyFactory.getServiceFactory().getDataBinding();
+ assertNotNull(dataBinding);
+ try {
+ TestBean1 bean = new TestBean1();
+ bean.stringItem = "bean1>stringItem";
+ DataWriter<XMLStreamWriter> writer =
dataBinding.createWriter(XMLStreamWriter.class);
+ StringWriter stringWriter = new StringWriter();
+ XMLStreamWriter xmlStreamWriter =
xmlOutputFactory.createXMLStreamWriter(stringWriter);
+ writer.write(bean, xmlStreamWriter);
+ xmlStreamWriter.flush();
+ xmlStreamWriter.close();
+ testUtilities.rhinoCall("deserializeTestBean1_1",
stringWriter.toString());
+ } catch (JavaScriptAssertionFailed assertion) {
+ fail(assertion.getMessage());
+ } catch (RhinoException angryRhino) {
+ String trace = angryRhino.getScriptStackTrace();
+ Assert.fail("Javascript error: " + angryRhino.toString() + " " +
trace);
+ }
+
+ }
+
@Test
public void testSerialization() throws Exception {
setupClientAndRhino("simple-dlwu-proxy-factory");
- testUtilities.readResourceIntoRhino("/serializationTest.js");
+ testUtilities.readResourceIntoRhino("/serializationTests.js");
DataBinding dataBinding =
clientProxyFactory.getServiceFactory().getDataBinding();
assertNotNull(dataBinding);
Added:
incubator/cxf/trunk/rt/javascript/src/test/resources/deserializationTests.js
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/resources/deserializationTests.js?rev=590874&view=auto
==============================================================================
---
incubator/cxf/trunk/rt/javascript/src/test/resources/deserializationTests.js
(added)
+++
incubator/cxf/trunk/rt/javascript/src/test/resources/deserializationTests.js
Wed Oct 31 18:35:49 2007
@@ -0,0 +1,40 @@
+/**
+ * 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.
+ */
+
+var jsutils = new CxfApacheOrgUtil();
+
+function assertionFailed(explanation)
+{
+ var assert = new Assert(explanation); // this will throw out in Java.
+}
+
+function parseXml(xmlString)
+{
+ var parser = new DOMParser();
+ return parser.parse(xmlString, "text/xml");
+}
+
+function deserializeTestBean1_1(xml)
+{
+ var dom = parseXml(xml);
+ var bean =
org_apache_cxf_javascript_testns_testBean1_deserialize(jsutils, dom);
+ if(bean.getStringItem() != "bean1>stringItem")
+ assertionFailed("deserializeTestBean1_1 stringItem " +
bean.getStringItem());
+}
+
Copied:
incubator/cxf/trunk/rt/javascript/src/test/resources/serializationTests.js
(from r590636,
incubator/cxf/trunk/rt/javascript/src/test/resources/serializationTest.js)
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/resources/serializationTests.js?p2=incubator/cxf/trunk/rt/javascript/src/test/resources/serializationTests.js&p1=incubator/cxf/trunk/rt/javascript/src/test/resources/serializationTest.js&r1=590636&r2=590874&rev=590874&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/javascript/src/test/resources/serializationTest.js
(original)
+++ incubator/cxf/trunk/rt/javascript/src/test/resources/serializationTests.js
Wed Oct 31 18:35:49 2007
@@ -17,10 +17,15 @@
* under the License.
*/
- var jsutils = new CxfApacheOrgUtil();
+var jsutils = new CxfApacheOrgUtil();
- function serializeTestBean1_1()
- {
+function assertionFailed(explanation)
+{
+ var assert = new Assert(explanation); // this will throw out in Java.
+}
+
+function serializeTestBean1_1()
+{
var bean1 = new org_apache_cxf_javascript_testns_testBean1();
bean1.setStringItem("bean1<stringItem");
bean1.setIntItem(64);
Modified: incubator/cxf/trunk/systests/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/pom.xml?rev=590874&r1=590873&r2=590874&view=diff
==============================================================================
--- incubator/cxf/trunk/systests/pom.xml (original)
+++ incubator/cxf/trunk/systests/pom.xml Wed Oct 31 18:35:49 2007
@@ -384,7 +384,7 @@
<dependency>
<groupId>rhino</groupId>
<artifactId>js</artifactId>
- <version>1.6R6</version>
+ <version>1.6R7</version>
<scope>test</scope>
</dependency>
<dependency>