Search in sources :

Example 1 with JavascriptUtils

use of org.apache.cxf.javascript.JavascriptUtils in project cxf by apache.

the class SchemaJavascriptBuilder method complexTypeSerializerFunction.

/**
 * Produce a serializer function for a type. These functions emit the surrounding element XML if the
 * caller supplies an XML element name. It's not quite as simple as that, though. The element name may
 * need namespace qualification, and this function will add more namespace prefixes as needed.
 *
 * @param type
 * @return
 */
public void complexTypeSerializerFunction(QName name, XmlSchemaComplexType type) {
    StringBuilder bodyCode = new StringBuilder();
    JavascriptUtils bodyUtils = new JavascriptUtils(bodyCode);
    bodyUtils.setXmlStringAccumulator("xml");
    complexTypeSerializerBody(type, "this._", bodyUtils);
    utils = new JavascriptUtils(code);
    String functionName = nameManager.getJavascriptName(name) + "_" + "serialize";
    code.append("//\n");
    code.append("// Serialize " + name + "\n");
    code.append("//\n");
    code.append("function " + functionName + "(cxfjsutils, elementName, extraNamespaces) {\n");
    utils.startXmlStringAccumulator("xml");
    utils.startIf("elementName !== null");
    utils.appendString("<");
    utils.appendExpression("elementName");
    // now add any accumulated namespaces.
    String moreNamespaces = prefixAccumulator.getAttributes();
    if (moreNamespaces.length() > 0) {
        utils.appendString(" ");
        utils.appendString(moreNamespaces);
    }
    utils.startIf("extraNamespaces");
    utils.appendExpression("' ' + extraNamespaces");
    utils.endBlock();
    // attributes
    complexTypeSerializeAttributes(type, "this._");
    utils.appendString(">");
    utils.endBlock();
    code.append(bodyCode);
    utils.startIf("elementName !== null");
    utils.appendString("</");
    utils.appendExpression("elementName");
    utils.appendString(">");
    utils.endBlock();
    utils.appendLine("return xml;");
    code.append("}\n\n");
    code.append(nameManager.getJavascriptName(name) + ".prototype.serialize = " + functionName + ";\n\n");
}
Also used : JavascriptUtils(org.apache.cxf.javascript.JavascriptUtils)

Example 2 with JavascriptUtils

use of org.apache.cxf.javascript.JavascriptUtils in project cxf by apache.

the class SchemaJavascriptBuilder method domDeserializerFunction.

/**
 * 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 void domDeserializerFunction(QName name, XmlSchemaComplexType type) {
    utils = new JavascriptUtils(code);
    List<XmlSchemaObject> contentElements = JavascriptUtils.getContentElements(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);");
    utils.appendLine("var item;");
    int nContentElements = contentElements.size();
    for (int i = 0; i < contentElements.size(); i++) {
        XmlSchemaObject contentElement = contentElements.get(i);
        utils.appendLine("cxfjsutils.trace('curElement: ' + cxfjsutils.traceElementName(curElement));");
        ParticleInfo itemInfo = ParticleInfo.forLocalItem(contentElement, xmlSchema, xmlSchemaCollection, prefixAccumulator, type.getQName());
        if (itemInfo.isAny()) {
            ParticleInfo nextItem = null;
            if (i != nContentElements - 1) {
                XmlSchemaObject nextThing = contentElements.get(i + 1);
                nextItem = ParticleInfo.forLocalItem(nextThing, xmlSchema, xmlSchemaCollection, prefixAccumulator, type.getQName());
                // namespaces.
                if (nextItem.isAny()) {
                    unsupportedConstruct("MULTIPLE_ANY", type.getQName());
                }
            }
            deserializeAny(type, itemInfo, nextItem);
        } else {
            deserializeElement(type, itemInfo);
        }
    }
    utils.appendLine("return newobject;");
    code.append("}\n\n");
}
Also used : XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) JavascriptUtils(org.apache.cxf.javascript.JavascriptUtils) ParticleInfo(org.apache.cxf.javascript.ParticleInfo)

Example 3 with JavascriptUtils

use of org.apache.cxf.javascript.JavascriptUtils in project cxf by apache.

the class SchemaJavascriptBuilder method complexTypeConstructorAndAccessors.

// In general, I (bimargulies) hate fields that are temporary communications
// between members of a class. However, given the style restrictions on the
// number
// of parameters, it's the least of the evils.
public void complexTypeConstructorAndAccessors(QName name, XmlSchemaComplexType type) {
    accessors = new StringBuilder();
    utils = new JavascriptUtils(code);
    List<XmlSchemaObject> items = JavascriptUtils.getContentElements(type, xmlSchemaCollection);
    List<XmlSchemaAnnotated> attrs = XmlSchemaUtils.getContentAttributes(type, xmlSchemaCollection);
    final String elementPrefix = "this._";
    String typeObjectName = nameManager.getJavascriptName(name);
    code.append("//\n");
    code.append("// Constructor for XML Schema item " + name.toString() + "\n");
    code.append("//\n");
    code.append("function " + typeObjectName + " () {\n");
    // to assist in debugging we put a type property into every object.
    utils.appendLine("this.typeMarker = '" + typeObjectName + "';");
    for (XmlSchemaObject thing : items) {
        ParticleInfo itemInfo = ParticleInfo.forLocalItem(thing, xmlSchema, xmlSchemaCollection, prefixAccumulator, type.getQName());
        constructItem(type, elementPrefix, typeObjectName, itemInfo);
    }
    for (XmlSchemaAnnotated thing : attrs) {
        AttributeInfo itemInfo = AttributeInfo.forLocalItem(thing, xmlSchema, xmlSchemaCollection, prefixAccumulator, type.getQName());
        constructOneItem(type, elementPrefix, typeObjectName, itemInfo);
    }
    code.append("}\n\n");
    code.append(accessors.toString());
}
Also used : AttributeInfo(org.apache.cxf.javascript.AttributeInfo) XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) JavascriptUtils(org.apache.cxf.javascript.JavascriptUtils) ParticleInfo(org.apache.cxf.javascript.ParticleInfo) XmlSchemaAnnotated(org.apache.ws.commons.schema.XmlSchemaAnnotated)

Aggregations

JavascriptUtils (org.apache.cxf.javascript.JavascriptUtils)3 ParticleInfo (org.apache.cxf.javascript.ParticleInfo)2 XmlSchemaObject (org.apache.ws.commons.schema.XmlSchemaObject)2 AttributeInfo (org.apache.cxf.javascript.AttributeInfo)1 XmlSchemaAnnotated (org.apache.ws.commons.schema.XmlSchemaAnnotated)1