use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.
the class ObjectReferenceVisitor method addWSAddressingImport.
private void addWSAddressingImport(XmlSchema s) {
boolean alreadyImported = false;
for (XmlSchemaExternal ext : s.getExternals()) {
if (ext instanceof XmlSchemaImport) {
XmlSchemaImport schemaImport = (XmlSchemaImport) ext;
if (schemaImport.getNamespace().equals(ReferenceConstants.WSADDRESSING_NAMESPACE)) {
alreadyImported = true;
break;
}
}
}
if (!alreadyImported) {
// We need to add an import statement to include the WS addressing types
XmlSchemaImport wsaImport = new XmlSchemaImport(s);
wsaImport.setNamespace(ReferenceConstants.WSADDRESSING_NAMESPACE);
wsaImport.setSchemaLocation(ReferenceConstants.WSADDRESSING_LOCATION);
}
// Add the addressing namespace to the WSDLs list of namespaces.
definition.addNamespace(ReferenceConstants.WSADDRESSING_PREFIX, ReferenceConstants.WSADDRESSING_NAMESPACE);
try {
// This is used to get the correct prefix in the schema section of
// the wsdl. If we don't have this, then this namespace gets an
// arbitrary prefix (e.g. ns5 instead of wsa).
NamespaceMap nsMap = (NamespaceMap) s.getNamespaceContext();
if (nsMap == null) {
nsMap = new NamespaceMap();
nsMap.add(ReferenceConstants.WSADDRESSING_PREFIX, ReferenceConstants.WSADDRESSING_NAMESPACE);
s.setNamespaceContext(nsMap);
} else {
nsMap.add(ReferenceConstants.WSADDRESSING_PREFIX, ReferenceConstants.WSADDRESSING_NAMESPACE);
}
} catch (ClassCastException ex) {
// Consume the exception. It is still OK with the default prefix,
// just not as clear.
}
}
use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.
the class ImportRepairTest method newSchema.
private XmlSchema newSchema(String uri) {
XmlSchema schema = collection.newXmlSchemaInCollection(uri);
NamespaceMap map = new NamespaceMap();
map.add("", XMLConstants.W3C_XML_SCHEMA_NS_URI);
schema.setNamespaceContext(map);
return schema;
}
use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.
the class ReflectionServiceFactoryBean method getOrCreateSchema.
private SchemaInfo getOrCreateSchema(ServiceInfo serviceInfo, String namespaceURI, boolean qualified) {
for (SchemaInfo s : serviceInfo.getSchemas()) {
if (s.getNamespaceURI().equals(namespaceURI)) {
return s;
}
}
SchemaInfo schemaInfo = new SchemaInfo(namespaceURI);
SchemaCollection col = serviceInfo.getXmlSchemaCollection();
XmlSchema schema = col.getSchemaByTargetNamespace(namespaceURI);
if (schema != null) {
schemaInfo.setSchema(schema);
serviceInfo.addSchema(schemaInfo);
return schemaInfo;
}
schema = col.newXmlSchemaInCollection(namespaceURI);
if (qualified) {
schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
}
schemaInfo.setSchema(schema);
Map<String, String> explicitNamespaceMappings = this.getDataBinding().getDeclaredNamespaceMappings();
if (explicitNamespaceMappings == null) {
explicitNamespaceMappings = Collections.emptyMap();
}
NamespaceMap nsMap = new NamespaceMap();
for (Map.Entry<String, String> mapping : explicitNamespaceMappings.entrySet()) {
nsMap.add(mapping.getValue(), mapping.getKey());
}
if (!explicitNamespaceMappings.containsKey(WSDLConstants.NS_SCHEMA_XSD)) {
nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
}
if (!explicitNamespaceMappings.containsKey(serviceInfo.getTargetNamespace())) {
nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, serviceInfo.getTargetNamespace());
}
schema.setNamespaceContext(nsMap);
serviceInfo.addSchema(schemaInfo);
return schemaInfo;
}
use of org.apache.ws.commons.schema.utils.NamespaceMap in project cxf by apache.
the class AegisDatabinding method createSchemas.
private void createSchemas(Service s, Set<AegisType> deps) {
Map<String, Set<AegisType>> tns2Type = new HashMap<>();
for (AegisType t : deps) {
String ns = t.getSchemaType().getNamespaceURI();
Set<AegisType> types = tns2Type.get(ns);
if (types == null) {
types = new HashSet<>();
tns2Type.put(ns, types);
}
types.add(t);
}
for (ServiceInfo si : s.getServiceInfos()) {
SchemaCollection col = si.getXmlSchemaCollection();
if (col.getXmlSchemas().length > 1) {
// someone has already filled in the types
continue;
}
}
Map<String, String> namespaceMap = getDeclaredNamespaceMappings();
for (ServiceInfo si : s.getServiceInfos()) {
// these two must be recalculated per-service-info!
boolean needXmimeSchema = false;
boolean needTypesSchema = false;
for (Map.Entry<String, Set<AegisType>> entry : tns2Type.entrySet()) {
String schemaNamespaceUri = entry.getKey();
if (Constants.URI_2001_SCHEMA_XSD.equals(schemaNamespaceUri)) {
continue;
}
if (AegisContext.UTILITY_TYPES_SCHEMA_NS.equals(schemaNamespaceUri)) {
// we handle this separately.
continue;
}
if (AbstractXOPType.XML_MIME_NS.equals(schemaNamespaceUri)) {
// similiarly.
continue;
}
SchemaInfo schemaInfo = si.addNewSchema(entry.getKey());
XmlSchema schema = schemaInfo.getSchema();
NamespaceMap xmlsNamespaceMap = new NamespaceMap();
// user-requested prefix mappings.
if (namespaceMap != null) {
for (Map.Entry<String, String> e : namespaceMap.entrySet()) {
xmlsNamespaceMap.add(e.getValue(), e.getKey());
}
}
// tns: is conventional, and besides we have unit tests that are hardcoded to it.
if (!xmlsNamespaceMap.containsKey(WSDLConstants.CONVENTIONAL_TNS_PREFIX) && // if some wants something other than TNS, they get it.
!xmlsNamespaceMap.containsValue(entry.getKey())) {
xmlsNamespaceMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, entry.getKey());
}
// ditto for xsd: instead of just namespace= for the schema schema.
if (!xmlsNamespaceMap.containsKey("xsd") && !xmlsNamespaceMap.containsValue(Constants.URI_2001_SCHEMA_XSD)) {
xmlsNamespaceMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
}
schema.setNamespaceContext(xmlsNamespaceMap);
schema.setTargetNamespace(entry.getKey());
schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
schema.setAttributeFormDefault(XmlSchemaForm.QUALIFIED);
for (AegisType t : entry.getValue()) {
try {
t.writeSchema(schema);
} catch (XmlSchemaException ex) {
QName name = t.getSchemaType();
String expected = " Schema for namespace '" + name.getNamespaceURI() + "' already contains type '" + name.getLocalPart() + "'";
String message = ex.getMessage();
if (expected.equals(message)) {
continue;
}
throw ex;
}
}
if (schemaImportsXmime(schema)) {
needXmimeSchema = true;
}
if (AegisContext.schemaImportsUtilityTypes(schema)) {
needTypesSchema = true;
}
}
if (needXmimeSchema) {
XmlSchema schema = aegisContext.addXmimeSchemaDocument(si.getXmlSchemaCollection().getXmlSchemaCollection());
SchemaInfo schemaInfo = new SchemaInfo(schema.getTargetNamespace());
schemaInfo.setSchema(schema);
si.addSchema(schemaInfo);
}
if (needTypesSchema) {
XmlSchema schema = aegisContext.addTypesSchemaDocument(si.getXmlSchemaCollection().getXmlSchemaCollection());
SchemaInfo schemaInfo = new SchemaInfo(schema.getTargetNamespace());
schemaInfo.setSchema(schema);
si.addSchema(schemaInfo);
}
// it's quite likely that the code in Aegis missed at least one ...
si.getXmlSchemaCollection().addCrossImports();
}
}
Aggregations