use of org.apache.ws.commons.schema.XmlSchemaExternal in project cxf by apache.
the class XmlSchemaUtils method addImportIfNeeded.
/**
* Assist in managing the required <import namespace='uri'> for imports of peer schemas.
* @param schema
* @param namespaceUri
*/
public static void addImportIfNeeded(XmlSchema schema, String namespaceUri) {
// no need to import nothing or the XSD schema, or the schema we are fixing.
if ("".equals(namespaceUri) || Constants.URI_2001_SCHEMA_XSD.equals(namespaceUri) || schema.getTargetNamespace().equals(namespaceUri)) {
return;
}
List<XmlSchemaExternal> externals = schema.getExternals();
for (XmlSchemaExternal what : externals) {
if (what instanceof XmlSchemaImport) {
XmlSchemaImport imp = (XmlSchemaImport) what;
// already there.
if (namespaceUri.equals(imp.getNamespace())) {
return;
}
}
}
XmlSchemaImport imp = new XmlSchemaImport(schema);
imp.setNamespace(namespaceUri);
}
use of org.apache.ws.commons.schema.XmlSchemaExternal in project cxf by apache.
the class Stax2ValidationUtils method getValidator.
/**
* Create woodstox validator for a schema set.
*
* @throws XMLStreamException
*/
private XMLValidationSchema getValidator(Endpoint endpoint, ServiceInfo serviceInfo) throws XMLStreamException {
synchronized (endpoint) {
XMLValidationSchema ret = (XMLValidationSchema) endpoint.get(KEY);
if (ret == null) {
if (endpoint.containsKey(KEY)) {
return null;
}
Map<String, EmbeddedSchema> sources = new TreeMap<>();
for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
XmlSchema sch = schemaInfo.getSchema();
String uri = sch.getTargetNamespace();
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri)) {
continue;
}
if (sch.getTargetNamespace() == null && sch.getExternals().size() > 0) {
for (XmlSchemaExternal xmlSchemaExternal : sch.getExternals()) {
addSchema(sources, xmlSchemaExternal.getSchema(), getElement(xmlSchemaExternal.getSchema().getSourceURI()));
}
continue;
} else if (sch.getTargetNamespace() == null) {
throw new IllegalStateException("An Schema without imports must have a targetNamespace");
}
addSchema(sources, sch, schemaInfo.getElement());
}
W3CMultiSchemaFactory factory = new W3CMultiSchemaFactory();
// I don't think that we need the baseURI.
try {
ret = factory.loadSchemas(null, sources);
endpoint.put(KEY, ret);
} catch (XMLStreamException ex) {
LOG.log(Level.INFO, "Problem loading schemas. Falling back to slower method.", ret);
endpoint.put(KEY, null);
}
}
return ret;
}
}
use of org.apache.ws.commons.schema.XmlSchemaExternal 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.XmlSchemaExternal in project cxf by apache.
the class WSDLToCorbaHelper method findTypeInSchema.
private XmlSchemaType findTypeInSchema(XmlSchema xmlSchema, QName typeName) {
XmlSchemaType schemaType = null;
if (xmlSchema.getElementByName(typeName) != null) {
XmlSchemaElement schemaElement = xmlSchema.getElementByName(typeName);
schemaType = schemaElement.getSchemaType();
} else if (xmlSchema.getTypeByName(typeName) != null) {
schemaType = xmlSchema.getTypeByName(typeName);
}
if (schemaType != null) {
return schemaType;
}
for (XmlSchemaExternal extSchema : xmlSchema.getExternals()) {
if (!(extSchema instanceof XmlSchemaImport)) {
schemaType = findTypeInSchema(extSchema.getSchema(), typeName);
if (schemaType != null) {
return schemaType;
}
}
}
return null;
}
use of org.apache.ws.commons.schema.XmlSchemaExternal in project cxf by apache.
the class WSDLSchemaManager method addXmlSchemaImport.
public void addXmlSchemaImport(XmlSchema rootSchema, XmlSchema schema, File file) {
// Make sure we haven't already imported the schema.
String importNamespace = schema.getTargetNamespace();
boolean included = false;
for (XmlSchemaExternal ext : rootSchema.getExternals()) {
if (ext instanceof XmlSchemaImport) {
XmlSchemaImport imp = (XmlSchemaImport) ext;
if (imp.getNamespace().equals(importNamespace)) {
included = true;
break;
}
}
}
if (!included) {
XmlSchemaImport importSchema = new XmlSchemaImport(rootSchema);
if (!ignoreImports) {
importSchema.setSchemaLocation(file.toURI().toString());
}
importSchema.setNamespace(schema.getTargetNamespace());
}
if (!importedSchemas.containsKey(file)) {
importedSchemas.put(file, schema);
}
}
Aggregations