Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attributes support in serialize/deserialize functions [wsdl2js] #256

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,11 @@ private void createInputSerializer() {
utils.appendLine("var wrapperObj = new " + inputWrapperClassName + "();");
int px = 0;
for (String param : inputParameterNames) {
utils.appendLine("wrapperObj.set" + StringUtils.capitalize(param) + "(args[" + px
+ "]);");
utils.startIf("typeof wrapperObj.set" + StringUtils.capitalize(param) + " === \"function\"");
utils.appendLine("wrapperObj.set" + StringUtils.capitalize(param) + "(args[" + px + "]);");
utils.appendElse();
utils.appendLine("wrapperObj.add" + StringUtils.capitalize(param) + "(args[" + px + "]);");
utils.endBlock();
px++;
}
} else if (isRPC) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.apache.ws.commons.schema.XmlSchema;
import org.apache.ws.commons.schema.XmlSchemaAnnotated;
import org.apache.ws.commons.schema.XmlSchemaAny;
import org.apache.ws.commons.schema.XmlSchemaAttributeOrGroupRef;
import org.apache.ws.commons.schema.XmlSchemaCollection;
import org.apache.ws.commons.schema.XmlSchemaComplexType;
import org.apache.ws.commons.schema.XmlSchemaElement;
Expand Down Expand Up @@ -220,9 +219,9 @@ private void constructOneItem(XmlSchemaComplexType type, final String elementPre

String accessorSuffix = StringUtils.capitalize(itemInfo.getJavascriptName());

String accessorName = typeObjectName + "_get" + accessorSuffix;
String getFunctionProperty = typeObjectName + ".prototype.get" + accessorSuffix;
String setFunctionProperty = typeObjectName + ".prototype.set" + accessorSuffix;
String setFunctionProperty = typeObjectName + ".prototype." + (itemInfo.isArray() ? "add" : "set")
+ accessorSuffix;
accessors.append("//\n");
accessors.append("// accessor is " + getFunctionProperty + "\n");
accessors.append("// element get for " + itemInfo.getJavascriptName() + "\n");
Expand All @@ -238,30 +237,52 @@ private void constructOneItem(XmlSchemaComplexType type, final String elementPre
accessors.append("// - optional element\n");
} else {
accessors.append("// - required element\n");

}

if (itemInfo.isArray()) {
accessors.append("// - array\n");

}

if (itemInfo.isNillable()) {
accessors.append("// - nillable\n");
}


String valueType = "value";
if (itemInfo.getType() != null) {
valueType = "value_" + itemInfo.getType().getName().toString();
}

accessors.append("//\n");
accessors.append("// element set for " + itemInfo.getJavascriptName() + "\n");
accessors.append("// setter function is is " + setFunctionProperty + "\n");
accessors.append("//\n");
accessors.append("function " + accessorName + "() { return this._" + itemInfo.getJavascriptName()
+ ";}\n\n");
accessors.append(getFunctionProperty + " = " + accessorName + ";\n\n");
accessorName = typeObjectName + "_set" + accessorSuffix;
accessors.append("function " + accessorName + "(value) { this._" + itemInfo.getJavascriptName()
+ " = value;}\n\n");
accessors.append(setFunctionProperty + " = " + accessorName + ";\n");

// Getter
accessors.append(getFunctionProperty + " = function () { return this._"
+ itemInfo.getJavascriptName() + "; };\n\n");

// Setter
if (itemInfo.isArray()) {
accessors.append(setFunctionProperty + " = function (" + valueType + ") {\n");
accessors.append(" var value = " + valueType + ";\n");
accessors.append(" if (value !== null && value !== \"undefined\") {\n");
accessors.append(" if (value instanceof Array) {\n");
accessors.append(" for(var i in value)\n");
accessors.append(" this._" + itemInfo.getJavascriptName() + ".push(value[i]);\n");
accessors.append(" } else {\n");
accessors.append(" if (value.raw) this._" + itemInfo.getJavascriptName() + " = value; "
+ "else this._" + itemInfo.getJavascriptName() + ".push(value);\n");
accessors.append(" }\n");
accessors.append(" }\n");
accessors.append(" return this;\n"); // for method chaining
accessors.append("}\n\n");
} else {
accessors.append(setFunctionProperty + " = function (" + valueType + ") { ");
accessors.append("this._" + itemInfo.getJavascriptName() + " = " + valueType + "; ");
accessors.append("return this; "); // for method chaining
accessors.append("}\n\n");
}

if (itemInfo.isOptional() || (itemInfo.isNillable() && !itemInfo.isArray())) {
utils.appendLine("this._" + itemInfo.getJavascriptName() + " = null;");
} else if (itemInfo.isArray()) {
Expand All @@ -272,7 +293,6 @@ private void constructOneItem(XmlSchemaComplexType type, final String elementPre
// application code is responsible for this.
utils.appendLine("this._" + itemInfo.getJavascriptName() + " = null;");
} else {

if (itemInfo.getDefaultValue() == null) {
itemInfo.setDefaultValue(utils.getDefaultValueForSimpleType(itemInfo.getType()));
}
Expand Down Expand Up @@ -331,10 +351,21 @@ public void complexTypeSerializerFunction(QName name, XmlSchemaComplexType type)
code.append(nameManager.getJavascriptName(name) + ".prototype.serialize = " + functionName + ";\n\n");
}

private void complexTypeSerializeAttributes(XmlSchemaComplexType type, String string) {
@SuppressWarnings("unused")
List<XmlSchemaAttributeOrGroupRef> attributes = type.getAttributes();
// work in progress.
private void complexTypeSerializeAttributes(XmlSchemaComplexType type, String attrPrefix) {
List<XmlSchemaAnnotated> attrs = XmlSchemaUtils.getContentAttributes(type, xmlSchemaCollection);

for (XmlSchemaAnnotated thing : attrs) {
AttributeInfo itemInfo = AttributeInfo.forLocalItem(thing, xmlSchema, xmlSchemaCollection,
prefixAccumulator, type.getQName());
String attrName = itemInfo.getJavascriptName();
utils.startIf(attrPrefix + attrName + " !== null");
utils.appendExpression("'" + attrName + "=\"' + " + attrPrefix + attrName + " + '\" '");
if (itemInfo.getDefaultValue() != null && !itemInfo.isOptional()) {
utils.appendElse();
utils.appendExpression("'" + attrName + "=\"" + itemInfo.getDefaultValue() + "\" '");
}
utils.endBlock();
}
}

/**
Expand Down Expand Up @@ -380,13 +411,31 @@ public void domDeserializerFunction(QName name, XmlSchemaComplexType type) {
utils = new JavascriptUtils(code);

List<XmlSchemaObject> contentElements = JavascriptUtils.getContentElements(type, xmlSchemaCollection);
List<XmlSchemaAnnotated> attributes = XmlSchemaUtils.getContentAttributes(type, xmlSchemaCollection);

String typeObjectName = nameManager.getJavascriptName(name);
code.append("function " + typeObjectName + "_deserialize (cxfjsutils, element) {\n");
// create the object we are deserializing into.
utils.appendLine("var newobject = new " + typeObjectName + "();");
utils.appendLine("cxfjsutils.trace('element: ' + cxfjsutils.traceElementName(element));");
utils.appendLine("var curElement = cxfjsutils.getFirstElementChild(element);");

// create attributes
utils.appendLine("var attributes = element.attributes;");
utils.startFor("var i = 0", "i < attributes.length", "i++");
for (XmlSchemaAnnotated thing : attributes) {
AttributeInfo itemInfo = AttributeInfo.forLocalItem(thing, xmlSchema, xmlSchemaCollection,
prefixAccumulator, type.getQName());

String attrName = itemInfo.getJavascriptName();
String setFunctionProperty = (itemInfo.isArray() ? "add" : "set") + StringUtils.capitalize(attrName);

utils.startIf("attributes[i] && attributes[i].nodeName === '" + attrName + "'");
utils.appendLine("newobject." + setFunctionProperty + "(attributes[i].nodeValue)");
utils.endBlock();
}
utils.endBlock();

utils.appendLine("var curElement = cxfjsutils.getFirstElementChild(element);");
utils.appendLine("var item;");

int nContentElements = contentElements.size();
Expand Down Expand Up @@ -449,7 +498,6 @@ private void deserializeAny(XmlSchemaComplexType type, ParticleInfo itemInfo, Pa
utils.appendLine("var anyObject = [];");
} else {
utils.appendLine("var anyObject = null;");

}

String anyNamespaceSpec = any.getNamespace();
Expand Down Expand Up @@ -540,7 +588,12 @@ private void deserializeAny(XmlSchemaComplexType type, ParticleInfo itemInfo, Pa
utils.endBlock(); // while

utils.appendLine("var anyHolder = new org_apache_cxf_any_holder(anyURI, anyLocalPart, anyValue);");
utils.appendLine("newobject.setAny(anyHolder);");

if (array) {
utils.appendLine("newobject.addAny(anyHolder);");
} else {
utils.appendLine("newobject.setAny(anyHolder);");
}
}

private void deserializeElement(XmlSchemaComplexType type, ParticleInfo itemInfo) {
Expand All @@ -555,7 +608,12 @@ private void deserializeElement(XmlSchemaComplexType type, ParticleInfo itemInfo
boolean simple = itemType instanceof XmlSchemaSimpleType
|| JavascriptUtils.notVeryComplexType(itemType);
boolean mtomCandidate = JavascriptUtils.mtomCandidateType(itemType);
String accessorName = "set" + StringUtils.capitalize(itemInfo.getJavascriptName());
String accessorName;
if (itemInfo.isArray()) {
accessorName = "add" + StringUtils.capitalize(itemInfo.getJavascriptName());
} else {
accessorName = "set" + StringUtils.capitalize(itemInfo.getJavascriptName());
}
utils.appendLine("cxfjsutils.trace('processing " + itemInfo.getJavascriptName() + "');");
XmlSchemaElement element = (XmlSchemaElement)itemInfo.getParticle();
QName elementQName = XmlSchemaUtils.getElementQualifiedName(element, xmlSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public static Scriptable testBean1ToJS(JavascriptTestUtilities testUtilities,
testUtilities.rhinoCallMethod(rv, "setLongItem", testUtilities.javaToJS(b1.longItem));
testUtilities.rhinoCallMethod(rv, "setBase64Item", testUtilities.javaToJS(b1.base64Item));
testUtilities.rhinoCallMethod(rv, "setOptionalIntItem", testUtilities.javaToJS(b1.optionalIntItem));
testUtilities.rhinoCallMethod(rv, "setOptionalIntArrayItem",
testUtilities.rhinoCallMethod(rv, "addOptionalIntArrayItem",
testUtilities.javaToJS(b1.optionalIntArrayItem));
testUtilities.rhinoCallMethod(rv, "setDoubleItem", testUtilities.javaToJS(b1.doubleItem));
testUtilities.rhinoCallMethod(rv, "setBeanTwoItem", testBean2ToJS(testUtilities,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public static Scriptable testBean1ToJS(JavascriptTestUtilities testUtilities,
testUtilities.rhinoCallMethod(rv, "setLongItem", testUtilities.javaToJS(b1.longItem));
testUtilities.rhinoCallMethod(rv, "setBase64Item", testUtilities.javaToJS(b1.base64Item));
testUtilities.rhinoCallMethod(rv, "setOptionalIntItem", testUtilities.javaToJS(b1.optionalIntItem));
testUtilities.rhinoCallMethod(rv, "setOptionalIntArrayItem",
testUtilities.rhinoCallMethod(rv, "addOptionalIntArrayItem",
testUtilities.javaToJS(b1.optionalIntArrayItem));
testUtilities.rhinoCallMethod(rv, "setDoubleItem", testUtilities.javaToJS(b1.doubleItem));
testUtilities.rhinoCallMethod(rv, "setBeanTwoItem", testBean2ToJS(testUtilities,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static Scriptable testBean1ToJS(JavascriptTestUtilities testUtilities,
testUtilities.rhinoCallMethod(rv, "setLongItem", testUtilities.javaToJS(b1.longItem));
testUtilities.rhinoCallMethod(rv, "setBase64Item", testUtilities.javaToJS(b1.base64Item));
testUtilities.rhinoCallMethod(rv, "setOptionalIntItem", testUtilities.javaToJS(b1.optionalIntItem));
testUtilities.rhinoCallMethod(rv, "setOptionalIntArrayItem",
testUtilities.rhinoCallMethod(rv, "addOptionalIntArrayItem",
testUtilities.javaToJS(b1.optionalIntArrayItem));
testUtilities.rhinoCallMethod(rv, "setDoubleItem", testUtilities.javaToJS(b1.doubleItem));
testUtilities.rhinoCallMethod(rv, "setBeanTwoItem", testBean2ToJS(testUtilities,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function testAnyNToServerRaw(url)
holderArray.push(new org_apache_cxf_raw_any_holder("<walrus xmlns='uri:iam'>tusks</walrus>"));
holderArray.push(new org_apache_cxf_raw_any_holder("<penguin xmlns='uri:linux'>emperor</penguin>"));
holderArray.push(new org_apache_cxf_raw_any_holder("<moon xmlns='uri:planets'>blue</moon>"));
arrayItem.setAnyType(holderArray);
arrayItem.addAnyType(holderArray);
service.acceptAny(success, error, "before items", arrayItem);
}

Expand All @@ -77,7 +77,7 @@ function testAnyNToServerRawTyped(url)
"This is the cereal &lt; shot from guns");

holderArray.push(holder);
arrayItem.setAnyType(holderArray);
arrayItem.addAnyType(holderArray);
service.acceptObjects(success, error, arrayItem);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function testAnyNToServerRaw(url)
var param = new cxf_apache_org_jstest_types_any_acceptAnyN();
param.setBefore("before chalk");
var holder = new org_apache_cxf_raw_any_holder("<walrus xmlns='uri:iam'>tusks</walrus><penguin xmlns='uri:linux'>emperor</penguin>");
param.setAny(holder);
param.addAny(holder);
param.setAfter("after chalk");
service.acceptAnyN(param);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function beanFunctionTest(url, beanArg, beansArg)
intf.url = url;
// provide the extra layer of object for the array part, save the Java code the trouble.
var beanArrayHolder = new org_apache_cxf_javascript_testns_testBean1Array();
beanArrayHolder.setItem(beansArg);
beanArrayHolder.addItem(beansArg);
// param order from the interface
intf.beanFunction(test1SuccessCallback, test1ErrorCallback, beanArg, beanArrayHolder);
// Return the notifier as a convenience to the Java code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function serializeTestBean1_1()
bean1.setBeanTwoItem(bean2);
var a = [];
a.push(543);
bean1.setOptionalIntArrayItem(a);
bean1.addOptionalIntArrayItem(a);
bean1.setEnum2('Mineral');
return bean1.serialize(jsutils, "testBean1");
}
Expand All @@ -56,7 +56,7 @@ function serializeTestBean1_1()
a.push(543);
a.push(null);
a.push(345);
bean1.setOptionalIntArrayItem(a);
bean1.addOptionalIntArrayItem(a);
return bean1.serialize(jsutils, "testBean1");
}

Expand All @@ -67,7 +67,7 @@ function serializeTestBean1_1()
bean1.setIntItem(64);
bean1.setBase64Item(''); // later
bean1.setOptionalIntItem(33);
bean1.setOptionalIntArrayItem(null);
bean1.addOptionalIntArrayItem(null);
bean1.setBeanTwoItem(bean2);
return bean1.serialize(jsutils, "testBean1");
}