use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.
the class WSDLSchemaManager method attachSchemaToWSDL.
public void attachSchemaToWSDL(Definition definition, XmlSchema schema, boolean isSchemaGenerated) throws Exception {
Types types = definition.getTypes();
if (types == null) {
types = definition.createTypes();
definition.setTypes(types);
}
Schema wsdlSchema = (Schema) definition.getExtensionRegistry().createExtension(Types.class, new QName(Constants.URI_2001_SCHEMA_XSD, "schema"));
// See if a NamespaceMap has already been added to the schema (this can be the case with object
// references. If so, simply add the XSD URI to the map. Otherwise, create a new one.
NamespaceMap nsMap = null;
try {
nsMap = (NamespaceMap) schema.getNamespaceContext();
} catch (ClassCastException ex) {
// Consume. This will mean that the context has not been set.
}
if (nsMap == null) {
nsMap = new NamespaceMap();
nsMap.add("xs", Constants.URI_2001_SCHEMA_XSD);
schema.setNamespaceContext(nsMap);
} else {
nsMap.add("xs", Constants.URI_2001_SCHEMA_XSD);
}
if (isSchemaGenerated) {
nsMap.add("tns", schema.getTargetNamespace());
}
org.w3c.dom.Element el = schema.getAllSchemas()[0].getDocumentElement();
wsdlSchema.setElement(el);
for (XmlSchemaExternal ext : schema.getExternals()) {
if (ext instanceof XmlSchemaImport) {
XmlSchemaImport xmlSchemaImport = (XmlSchemaImport) ext;
SchemaImport schemaimport = wsdlSchema.createImport();
schemaimport.setNamespaceURI(xmlSchemaImport.getNamespace());
if (xmlSchemaImport.getSchemaLocation() != null && !ignoreImports) {
schemaimport.setSchemaLocationURI(xmlSchemaImport.getSchemaLocation());
}
wsdlSchema.addImport(schemaimport);
}
}
types.addExtensibilityElement(wsdlSchema);
}
use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.
the class SchemaInfo method getElement.
/**
* Build and return a DOM tree for this schema.
* @return a DOM Element representation of the schema
*/
public synchronized Element getElement() {
// if someone recently used this DOM tree, take advantage.
Element element = cachedElement == null ? null : cachedElement.get();
if (element != null) {
return element;
}
if (getSchema() == null) {
throw new RuntimeException("No XmlSchema in SchemaInfo");
}
XmlSchema sch = getSchema();
synchronized (sch) {
XmlSchema schAgain = getSchema();
// Some unit tests really want to see 'tns:'.
if (schAgain.getNamespaceContext() == null) {
NamespaceMap nsMap = new NamespaceMap();
nsMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
nsMap.add("tns", schAgain.getTargetNamespace());
schAgain.setNamespaceContext(nsMap);
}
Document serializedSchema;
try {
serializedSchema = schAgain.getSchemaDocument();
} catch (XmlSchemaSerializerException e) {
throw new RuntimeException("Error serializing Xml Schema", e);
}
element = serializedSchema.getDocumentElement();
cachedElement = new SoftReference<Element>(element);
}
// The aegis databinding tests demonstrate this particularly.
if (element.getPrefix() == null && !Constants.URI_2001_SCHEMA_XSD.equals(element.getAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE))) {
Attr attr = element.getOwnerDocument().createAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE);
attr.setValue(Constants.URI_2001_SCHEMA_XSD);
element.setAttributeNodeNS(attr);
}
return element;
}
use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.
the class JAXBSchemaInitializer method createBridgeXsElement.
private void createBridgeXsElement(MessagePartInfo part, QName qn, QName typeName) {
XmlSchemaElement el = null;
SchemaInfo schemaInfo = serviceInfo.getSchema(qn.getNamespaceURI());
if (schemaInfo != null) {
el = schemaInfo.getElementByQName(qn);
if (el == null) {
createXsElement(schemaInfo.getSchema(), part, typeName, schemaInfo);
} else if (!typeName.equals(el.getSchemaTypeName())) {
throw new Fault(new Message("CANNOT_CREATE_ELEMENT", LOG, qn, typeName, el.getSchemaTypeName()));
}
return;
}
XmlSchema schema = schemas.newXmlSchemaInCollection(qn.getNamespaceURI());
if (qualifiedSchemas) {
schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
}
schemaInfo = new SchemaInfo(qn.getNamespaceURI(), qualifiedSchemas, false);
schemaInfo.setSchema(schema);
el = createXsElement(schema, part, typeName, schemaInfo);
NamespaceMap nsMap = new NamespaceMap();
nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, schema.getTargetNamespace());
nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
schema.setNamespaceContext(nsMap);
serviceInfo.addSchema(schemaInfo);
}
use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.
the class JAXBSchemaInitializer method buildExceptionType.
private void buildExceptionType(MessagePartInfo part, Class<?> cls) {
SchemaInfo schemaInfo = null;
for (SchemaInfo s : serviceInfo.getSchemas()) {
if (s.getNamespaceURI().equals(part.getElementQName().getNamespaceURI())) {
schemaInfo = s;
break;
}
}
XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
XmlType xmlTypeAnno = cls.getAnnotation(XmlType.class);
String[] propertyOrder = null;
boolean respectXmlTypeNS = false;
XmlSchema faultBeanSchema = null;
if (xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.namespace()) && !xmlTypeAnno.namespace().equals(part.getElementQName().getNamespaceURI())) {
respectXmlTypeNS = true;
NamespaceMap nsMap = new NamespaceMap();
nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, xmlTypeAnno.namespace());
nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
SchemaInfo faultBeanSchemaInfo = createSchemaIfNeeded(xmlTypeAnno.namespace(), nsMap);
faultBeanSchema = faultBeanSchemaInfo.getSchema();
}
if (xmlTypeAnno != null && xmlTypeAnno.propOrder().length > 0) {
propertyOrder = xmlTypeAnno.propOrder();
// TODO: handle @XmlAccessOrder
}
XmlSchema schema = null;
if (schemaInfo == null) {
NamespaceMap nsMap = new NamespaceMap();
nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, part.getElementQName().getNamespaceURI());
nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
schemaInfo = createSchemaIfNeeded(part.getElementQName().getNamespaceURI(), nsMap);
}
schema = schemaInfo.getSchema();
// Before updating everything, make sure we haven't added this
// type yet. Multiple methods that throw the same exception
// types will cause duplicates.
String faultTypeName = xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.name()) ? xmlTypeAnno.name() : part.getElementQName().getLocalPart();
XmlSchemaType existingType = schema.getTypeByName(faultTypeName);
if (existingType != null) {
return;
}
XmlSchemaElement el = new XmlSchemaElement(schema, true);
el.setName(part.getElementQName().getLocalPart());
part.setXmlSchema(el);
schemaInfo.setElement(null);
if (respectXmlTypeNS) {
// create complexType in the new created schema for xmlType
schema = faultBeanSchema;
}
XmlSchemaComplexType ct = new XmlSchemaComplexType(schema, true);
ct.setName(faultTypeName);
el.setSchemaTypeName(ct.getQName());
XmlSchemaSequence seq = new XmlSchemaSequence();
ct.setParticle(seq);
String namespace = part.getElementQName().getNamespaceURI();
XmlAccessType accessType = Utils.getXmlAccessType(cls);
//
for (Field f : Utils.getFields(cls, accessType)) {
// map field
Type type = Utils.getFieldType(f);
// generic return type.
if ((type == null) && (f.getGenericType() instanceof ParameterizedType)) {
type = f.getGenericType();
}
if (generateGenericType(type)) {
buildGenericElements(schema, seq, f);
} else {
JAXBBeanInfo beanInfo = getBeanInfo(type);
if (beanInfo != null) {
XmlElement xmlElementAnno = f.getAnnotation(XmlElement.class);
addElement(schema, seq, beanInfo, new QName(namespace, f.getName()), isArray(type), xmlElementAnno);
}
}
}
for (Method m : Utils.getGetters(cls, accessType)) {
// map method
Type type = Utils.getMethodReturnType(m);
// generic return type.
if ((type == null) && (m.getGenericReturnType() instanceof ParameterizedType)) {
type = m.getGenericReturnType();
}
if (generateGenericType(type)) {
buildGenericElements(schema, seq, m, type);
} else {
JAXBBeanInfo beanInfo = getBeanInfo(type);
if (beanInfo != null) {
int idx = m.getName().startsWith("get") ? 3 : 2;
String name = m.getName().substring(idx);
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
XmlElement xmlElementAnno = m.getAnnotation(XmlElement.class);
addElement(schema, seq, beanInfo, new QName(namespace, name), isArray(type), xmlElementAnno);
}
}
}
// Create element in xsd:sequence for Exception.class
if (Exception.class.isAssignableFrom(cls)) {
addExceptionMessage(cls, schema, seq);
}
if (propertyOrder != null) {
if (propertyOrder.length == seq.getItems().size()) {
sortItems(seq, propertyOrder);
} else if (propertyOrder.length > 1 || (propertyOrder.length == 1 && !propertyOrder[0].isEmpty())) {
LOG.log(Level.WARNING, "propOrder in @XmlType doesn't define all schema elements :" + Arrays.toString(propertyOrder));
}
}
if (xmlAccessorOrder != null && xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL) && propertyOrder == null) {
sort(seq);
}
schemas.addCrossImports();
part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
}
use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.
the class AbstractAegisTest method newXmlSchema.
protected XmlSchema newXmlSchema(String targetNamespace) {
XmlSchema s = new XmlSchema();
s.setTargetNamespace(targetNamespace);
NamespaceMap xmlsNamespaceMap = new NamespaceMap();
s.setNamespaceContext(xmlsNamespaceMap);
// tns: is conventional, and besides we have unit tests that are hardcoded to it.
xmlsNamespaceMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, targetNamespace);
// ditto for xsd: instead of just namespace= for the schema schema.
xmlsNamespaceMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
return s;
}
Aggregations