use of org.apache.ws.commons.schema.XmlSchemaElement in project cxf by apache.
the class SchemaJavascriptBuilder method deserializeAny.
private void deserializeAny(XmlSchemaComplexType type, ParticleInfo itemInfo, ParticleInfo nextItem) {
XmlSchemaAny any = (XmlSchemaAny) itemInfo.getParticle();
boolean array = XmlSchemaUtils.isParticleArray(any);
boolean optional = XmlSchemaUtils.isParticleOptional(any);
if (array) {
utils.appendLine("var anyObject = [];");
} else {
utils.appendLine("var anyObject = null;");
}
String anyNamespaceSpec = any.getNamespace();
// we aren't dealing with any-after-any.
XmlSchemaElement nextElement = null;
if (nextItem != null) {
nextElement = (XmlSchemaElement) nextItem.getParticle();
}
String matchType;
String namespaceList = "[]";
if (anyNamespaceSpec == null || "##any".equals(anyNamespaceSpec) || "".equals(anyNamespaceSpec)) {
matchType = "org_apache_cxf_any_ns_matcher.ANY";
} else if ("##other".equals(anyNamespaceSpec)) {
matchType = "org_apache_cxf_any_ns_matcher.OTHER";
} else if ("##local".equals(anyNamespaceSpec)) {
matchType = "org_apache_cxf_any_ns_matcher.LOCAL";
} else {
matchType = "org_apache_cxf_any_ns_matcher.LISTED";
namespaceList = buildNamespaceList(anyNamespaceSpec);
}
String nextLocalPartConstant = "null";
if (nextElement != null) {
nextLocalPartConstant = "'" + nextElement.getQName().getLocalPart() + "'";
}
utils.appendLine("var matcher = new org_apache_cxf_any_ns_matcher(" + matchType + ", '" + xmlSchema.getTargetNamespace() + "'" + ", " + namespaceList + ", " + nextLocalPartConstant + ");");
if (array) {
utils.appendLine("var anyNeeded = " + any.getMinOccurs() + ";");
utils.appendLine("var anyAllowed = " + any.getMaxOccurs() + ";");
} else if (optional) {
utils.appendLine("var anyNeeded = 0;");
utils.appendLine("var anyAllowed = 1;");
} else {
utils.appendLine("var anyNeeded = 1;");
utils.appendLine("var anyAllowed = 1;");
}
utils.startWhile("anyNeeded > 0 || anyAllowed > 0");
utils.appendLine("var anyURI;");
utils.appendLine("var anyLocalPart;");
utils.appendLine("var anyMatched = false;");
utils.startIf("curElement");
utils.appendLine("anyURI = cxfjsutils.getElementNamespaceURI(curElement);");
utils.appendLine("anyLocalPart = cxfjsutils.getNodeLocalName(curElement);");
utils.appendLine("var anyQName = '{' + anyURI + '}' + anyLocalPart;");
utils.appendLine("cxfjsutils.trace('any match: ' + anyQName);");
utils.appendLine("anyMatched = matcher.match(anyURI, anyLocalPart)");
utils.appendLine("cxfjsutils.trace(' --> ' + anyMatched);");
// curElement != null
utils.endBlock();
// if match
utils.startIf("anyMatched");
utils.appendLine("anyDeserializer = " + "cxfjsutils.interfaceObject.globalElementDeserializers[anyQName];");
utils.appendLine("cxfjsutils.trace(' deserializer: ' + anyDeserializer);");
// if complex/serializer function
utils.startIf("anyDeserializer");
utils.appendLine("var anyValue = anyDeserializer(cxfjsutils, curElement);");
// else complex/serializer function
utils.appendElse();
// TODO: for simple types we really need a dictionary of the simple type qnames.
utils.appendLine("var anyValue = curElement.nodeValue;");
// complex/serializer function
utils.endBlock();
if (array) {
utils.appendLine("anyObject.push(anyValue);");
} else {
utils.appendLine("anyObject = anyValue;");
}
utils.appendLine("anyNeeded--;");
utils.appendLine("anyAllowed--;");
// if we consumed the element, we advance.
utils.appendLine("curElement = cxfjsutils.getNextElementSibling(curElement);");
// match
utils.appendElse();
// non-matching case
utils.startIf("anyNeeded > 0");
utils.appendLine("throw 'not enough ws:any elements';");
// else non-match
utils.appendElse();
utils.appendLine("break;");
// non-match+required
utils.endBlock();
// match/non-match.
utils.endBlock();
// while
utils.endBlock();
utils.appendLine("var anyHolder = new org_apache_cxf_any_holder(anyURI, anyLocalPart, anyValue);");
utils.appendLine("newobject.setAny(anyHolder);");
}
use of org.apache.ws.commons.schema.XmlSchemaElement in project cxf by apache.
the class SchemaJavascriptBuilder method deserializeElement.
private void deserializeElement(XmlSchemaComplexType type, ParticleInfo itemInfo) {
if (itemInfo.isGroup()) {
for (ParticleInfo childElement : itemInfo.getChildren()) {
deserializeElement(type, childElement);
}
return;
}
XmlSchemaType itemType = itemInfo.getType();
boolean simple = itemType instanceof XmlSchemaSimpleType || JavascriptUtils.notVeryComplexType(itemType);
boolean mtomCandidate = JavascriptUtils.mtomCandidateType(itemType);
String accessorName = "set" + StringUtils.capitalize(itemInfo.getJavascriptName());
utils.appendLine("cxfjsutils.trace('processing " + itemInfo.getJavascriptName() + "');");
XmlSchemaElement element = (XmlSchemaElement) itemInfo.getParticle();
QName elementQName = XmlSchemaUtils.getElementQualifiedName(element, xmlSchema);
String elementNamespaceURI = elementQName.getNamespaceURI();
boolean elementNoNamespace = "".equals(elementNamespaceURI);
XmlSchema elementSchema = null;
if (!elementNoNamespace) {
elementSchema = xmlSchemaCollection.getSchemaByTargetNamespace(elementNamespaceURI);
}
boolean qualified = !elementNoNamespace && XmlSchemaUtils.isElementQualified(element, itemInfo.isGlobal(), xmlSchema, elementSchema);
if (!qualified) {
elementNamespaceURI = "";
}
String localName = elementQName.getLocalPart();
String valueTarget = "item";
if (itemInfo.isOptional() || itemInfo.isArray()) {
utils.startIf("curElement != null && cxfjsutils.isNodeNamedNS(curElement, '" + elementNamespaceURI + "', '" + localName + "')");
if (itemInfo.isArray()) {
utils.appendLine("item = [];");
utils.startDo();
valueTarget = "arrayItem";
utils.appendLine("var arrayItem = null;");
}
}
utils.appendLine("var value = null;");
utils.startIf("!cxfjsutils.isElementNil(curElement)");
if (itemInfo.isAnyType()) {
// use our utility
utils.appendLine(valueTarget + " = org_apache_cxf_deserialize_anyType(cxfjsutils, curElement);");
} else if (simple) {
if (mtomCandidate) {
utils.appendLine(valueTarget + " = cxfjsutils.deserializeBase64orMom(curElement);");
} else {
utils.appendLine("value = cxfjsutils.getNodeText(curElement);");
utils.appendLine(valueTarget + " = " + utils.javascriptParseExpression(itemType, "value") + ";");
}
} else {
XmlSchemaComplexType complexType = (XmlSchemaComplexType) itemType;
QName baseQName = complexType.getQName();
if (baseQName == null) {
baseQName = element.getQName();
}
String elTypeJsName = nameManager.getJavascriptName(baseQName);
utils.appendLine(valueTarget + " = " + elTypeJsName + "_deserialize(cxfjsutils, curElement);");
}
// the if for the nil.
utils.endBlock();
if (itemInfo.isArray()) {
utils.appendLine("item.push(arrayItem);");
utils.appendLine("curElement = cxfjsutils.getNextElementSibling(curElement);");
utils.endBlock();
utils.appendLine(" while(curElement != null && cxfjsutils.isNodeNamedNS(curElement, '" + elementNamespaceURI + "', '" + localName + "'));");
}
utils.appendLine("newobject." + accessorName + "(item);");
utils.appendLine("var item = null;");
if (!itemInfo.isArray()) {
utils.startIf("curElement != null");
utils.appendLine("curElement = cxfjsutils.getNextElementSibling(curElement);");
utils.endBlock();
}
if (itemInfo.isOptional() || itemInfo.isArray()) {
utils.endBlock();
}
}
use of org.apache.ws.commons.schema.XmlSchemaElement in project cxf by apache.
the class SchemaJavascriptBuilder method generateCodeForSchema.
public String generateCodeForSchema(XmlSchema schema) {
xmlSchema = schema;
code = new StringBuilder();
code.append("//\n");
code.append("// Definitions for schema: ").append(schema.getTargetNamespace());
if (schema.getSourceURI() != null) {
code.append("\n// ").append(schema.getSourceURI());
}
code.append("\n//\n");
Map<QName, XmlSchemaType> schemaTypes = schema.getSchemaTypes();
for (Map.Entry<QName, XmlSchemaType> e : schemaTypes.entrySet()) {
XmlSchemaType type = e.getValue();
if (type instanceof XmlSchemaComplexType) {
try {
XmlSchemaComplexType complexType = (XmlSchemaComplexType) type;
if (!JavascriptUtils.notVeryComplexType(complexType) && complexType.getName() != null) {
complexTypeConstructorAndAccessors(complexType.getQName(), complexType);
complexTypeSerializerFunction(complexType.getQName(), complexType);
domDeserializerFunction(complexType.getQName(), complexType);
}
} catch (UnsupportedConstruct usc) {
LOG.warning(usc.toString());
// it could be empty, but the style checker
continue;
// would complain.
}
} else if (type instanceof XmlSchemaSimpleType) {
XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) type;
if (XmlSchemaUtils.isEumeration(simpleType)) {
List<String> values = XmlSchemaUtils.enumeratorValues(simpleType);
code.append("//\n");
code.append("// Simple type (enumeration) ").append(simpleType.getQName()).append('\n');
code.append("//\n");
for (String value : values) {
code.append("// - ").append(value).append('\n');
}
}
}
}
for (Map.Entry<QName, XmlSchemaElement> e : schema.getElements().entrySet()) {
XmlSchemaElement element = e.getValue();
try {
if (element.getSchemaTypeName() == null && element.getSchemaType() == null) {
Message message = new Message("ELEMENT_MISSING_TYPE", LOG, element.getQName(), element.getSchemaTypeName(), schema.getTargetNamespace());
LOG.warning(message.toString());
continue;
}
XmlSchemaType type;
if (element.getSchemaType() != null) {
type = element.getSchemaType();
} else {
type = schema.getTypeByName(element.getSchemaTypeName());
}
if (!(type instanceof XmlSchemaComplexType)) {
// we never make classes for simple type.
continue;
}
XmlSchemaComplexType complexType = (XmlSchemaComplexType) type;
// element.
if (!JavascriptUtils.notVeryComplexType(complexType) && complexType.getName() == null) {
complexTypeConstructorAndAccessors(element.getQName(), complexType);
complexTypeSerializerFunction(element.getQName(), complexType);
domDeserializerFunction(element.getQName(), complexType);
}
} catch (UnsupportedConstruct usc) {
LOG.warning(usc.getMessage());
// it could be empty, but the style checker
continue;
// would complain.
}
}
String returnValue = code.toString();
LOG.finer(returnValue);
return returnValue;
}
use of org.apache.ws.commons.schema.XmlSchemaElement in project cxf by apache.
the class ServiceJavascriptBuilder method getElementType.
/**
* Follow a chain of references from element to element until we can obtain
* a type.
*
* @param element
*/
public static XmlSchemaType getElementType(SchemaCollection xmlSchemaCollection, String referencingURI, XmlSchemaElement element, XmlSchemaType containingType) {
assert element != null;
if (element.getSchemaTypeName() != null) {
XmlSchemaType type = xmlSchemaCollection.getTypeByQName(element.getSchemaTypeName());
if (type == null) {
Message message = new Message("ELEMENT_TYPE_MISSING", LOG, element.getQName(), element.getSchemaTypeName().toString());
throw new UnsupportedConstruct(message);
}
return type;
}
// The referencing URI only helps if there is a schema that points to
// it.
// It might be the URI for the wsdl TNS, which might have no schema.
// if (xmlSchemaCollection.getSchemaByTargetNamespace(referencingURI) == null) {
// referencingURI = null;
// }
//
// if (referencingURI == null && containingType != null) {
// referencingURI = containingType.getQName().getNamespaceURI();
// }
XmlSchemaElement originalElement = element;
while (element.getSchemaType() == null && element.isRef()) {
/*
* This code assumes that all schemas are in the collection.
*/
XmlSchemaElement nextElement = element.getRef().getTarget();
assert nextElement != null;
element = nextElement;
}
if (element.getSchemaType() == null) {
unsupportedConstruct("ELEMENT_HAS_NO_TYPE", originalElement.getName(), containingType.getQName(), containingType);
}
return element.getSchemaType();
}
use of org.apache.ws.commons.schema.XmlSchemaElement in project cxf by apache.
the class ParticleInfo method factoryCommon.
private static void factoryCommon(XmlSchemaParticle particle, XmlSchema currentSchema, SchemaCollection schemaCollection, NamespacePrefixAccumulator prefixAccumulator, ParticleInfo elementInfo) {
if (particle instanceof XmlSchemaElement) {
XmlSchemaElement element = (XmlSchemaElement) particle;
QName elementQName = XmlSchemaUtils.getElementQualifiedName(element, currentSchema);
String elementNamespaceURI = elementQName.getNamespaceURI();
boolean elementNoNamespace = "".equals(elementNamespaceURI);
XmlSchema elementSchema = null;
if (!elementNoNamespace) {
elementSchema = schemaCollection.getSchemaByTargetNamespace(elementNamespaceURI);
if (elementSchema == null) {
throw new RuntimeException("Missing schema " + elementNamespaceURI);
}
}
boolean qualified = !elementNoNamespace && XmlSchemaUtils.isElementQualified(element, true, currentSchema, elementSchema);
elementInfo.xmlName = prefixAccumulator.xmlElementString(elementQName, qualified);
// we are assuming here that we are not dealing, in close proximity,
// with elements with identical local names and different
// namespaces.
elementInfo.javascriptName = elementQName.getLocalPart();
String schemaDefaultValue = element.getDefaultValue();
/*
* Schema default values are carried as strings.
* In javascript, for actual strings, we need quotes, but not for
* numbers. The following is a trick.
*/
schemaDefaultValue = protectDefaultValue(schemaDefaultValue);
elementInfo.defaultValue = schemaDefaultValue;
factorySetupType(element, schemaCollection, elementInfo);
elementInfo.isGroup = false;
} else if (particle instanceof XmlSchemaChoice) {
elementInfo.isGroup = true;
} else if (particle instanceof XmlSchemaSequence) {
elementInfo.isGroup = true;
} else {
// any
elementInfo.any = true;
// unknown until runtime.
elementInfo.xmlName = null;
// TODO: multiple 'any'
elementInfo.javascriptName = "any";
// runtime for any.
elementInfo.type = null;
elementInfo.isGroup = false;
}
}
Aggregations