use of javax.wsdl.Types in project cxf by apache.
the class IDLToWSDLProcessor method addTypeMapSchemaImports.
public void addTypeMapSchemaImports(Definition def, WSDLASTVisitor visitor) {
List<CorbaType> types = visitor.getTypeMap().getStructOrExceptionOrUnion();
ModuleToNSMapper mapper = visitor.getModuleToNSMapper();
WSDLSchemaManager manager = visitor.getManager();
Collection<String> namespaces = CastUtils.cast(def.getNamespaces().values());
Set<Map.Entry<String, String>> userModuleMappings = mapper.getUserMapping().entrySet();
if (types != null) {
for (int i = 0; i < types.size(); i++) {
CorbaType type = types.get(i);
QName schemaType = type.getType();
if (schemaType != null) {
String typeNamespace = schemaType.getNamespaceURI();
try {
// a remote location.
if (!namespaces.contains(typeNamespace) && typeNamespace.equals(ReferenceConstants.WSADDRESSING_NAMESPACE)) {
// build up the ws-addressing schema import
Schema wsdlSchema = (Schema) def.getExtensionRegistry().createExtension(Types.class, new QName(Constants.URI_2001_SCHEMA_XSD, "schema"));
SchemaImport schemaimport = wsdlSchema.createImport();
schemaimport.setNamespaceURI(ReferenceConstants.WSADDRESSING_NAMESPACE);
schemaimport.setSchemaLocationURI(ReferenceConstants.WSADDRESSING_LOCATION);
wsdlSchema.addImport(schemaimport);
// add the import and the prefix to the definition
def.getTypes().addExtensibilityElement(wsdlSchema);
CastUtils.cast(def.getNamespaces(), String.class, String.class).put(ReferenceConstants.WSADDRESSING_PREFIX, typeNamespace);
} else if (!namespaces.contains(typeNamespace)) {
String prefix = getModulePrefixForNamespace(userModuleMappings, mapper, typeNamespace);
// prefix = mapper.mapNSToPrefix(typeNamespace);
XmlSchema schema = manager.getXmlSchema(typeNamespace);
// TODO: REVISIT - Is this the only way we can create the file name for the
// imported schema?
String importFile = visitor.getOutputDir() + System.getProperty("file.separator") + prefix + ".xsd";
manager.addWSDLSchemaImport(def, typeNamespace, importFile);
manager.getImportedXmlSchemas().put(new File(importFile), schema);
CastUtils.cast(def.getNamespaces(), String.class, String.class).put(prefix, typeNamespace);
}
} catch (Exception ex) {
throw new ToolException("Unable to add schema import for namespace" + typeNamespace);
}
}
}
}
}
use of javax.wsdl.Types in project cxf by apache.
the class ServiceWSDLBuilderTest method testXsdImportMultipleSchemas.
@Test
public void testXsdImportMultipleSchemas() throws Exception {
setupWSDL(WSDL_XSD_IMPORT_PATH, true);
Types types = newDef.getTypes();
assertNotNull(types);
Collection<ExtensibilityElement> schemas = CastUtils.cast(types.getExtensibilityElements(), ExtensibilityElement.class);
assertEquals(1, schemas.size());
Schema schema = (Schema) schemas.iterator().next();
assertEquals(1, schema.getImports().values().size());
SchemaImport serviceTypesSchemaImport = getImport(schema.getImports(), "http://apache.org/hello_world_soap_http/servicetypes");
Schema serviceTypesSchema = serviceTypesSchemaImport.getReferencedSchema();
assertEquals(1, serviceTypesSchema.getImports().values().size());
SchemaImport typesSchemaImport = getImport(serviceTypesSchema.getImports(), "http://apache.org/hello_world_soap_http/types");
Schema typesSchema = typesSchemaImport.getReferencedSchema();
Document doc = typesSchema.getElement().getOwnerDocument();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(outputStream, "utf-8");
StaxUtils.writeNode(doc, writer, true);
writer.close();
// this is a test to make sure any embedded namespaces are properly included
String savedSchema = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
assertTrue(savedSchema.contains("http://www.w3.org/2005/05/xmlmime"));
SchemaImport types2SchemaImport = getImport(typesSchema.getImports(), "http://apache.org/hello_world_soap_http/types2");
Schema types2Schema = types2SchemaImport.getReferencedSchema();
assertNotNull(types2Schema);
}
use of javax.wsdl.Types in project cxf by apache.
the class ServiceWSDLBuilderTest method testSchemas.
@Test
public void testSchemas() throws Exception {
setupWSDL(WSDL_PATH);
Types types = newDef.getTypes();
assertNotNull(types);
Collection<ExtensibilityElement> schemas = CastUtils.cast(types.getExtensibilityElements(), ExtensibilityElement.class);
assertEquals(1, schemas.size());
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
Element schemaElem = ((Schema) schemas.iterator().next()).getElement();
XmlSchema newSchema = schemaCollection.read(schemaElem);
assertEquals("http://apache.org/hello_world_soap_http/types", newSchema.getTargetNamespace());
}
use of javax.wsdl.Types in project cxf by apache.
the class SchemaUtil method extractSchema.
private void extractSchema(Definition def, SchemaCollection schemaCol, List<SchemaInfo> schemaInfos) {
Types typesElement = def.getTypes();
if (typesElement != null) {
int schemaCount = 1;
for (Object obj : typesElement.getExtensibilityElements()) {
org.w3c.dom.Element schemaElem = null;
if (obj instanceof Schema) {
Schema schema = (Schema) obj;
schemaElem = schema.getElement();
} else if (obj instanceof UnknownExtensibilityElement) {
org.w3c.dom.Element elem = ((UnknownExtensibilityElement) obj).getElement();
synchronized (elem.getOwnerDocument()) {
if ("schema".equals(elem.getLocalName())) {
schemaElem = elem;
}
}
}
if (schemaElem != null) {
synchronized (schemaElem.getOwnerDocument()) {
for (Object prefix : def.getNamespaces().keySet()) {
String ns = (String) def.getNamespaces().get(prefix);
if ("".equals(prefix)) {
if (!schemaElem.hasAttribute("xmlns")) {
Attr attr = schemaElem.getOwnerDocument().createAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns");
attr.setValue(ns);
schemaElem.setAttributeNodeNS(attr);
}
} else if (!schemaElem.hasAttribute("xmlns:" + prefix)) {
String namespace = javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
Attr attr = schemaElem.getOwnerDocument().createAttributeNS(namespace, "xmlns:" + prefix);
attr.setValue(ns);
schemaElem.setAttributeNodeNS(attr);
}
}
String systemId = def.getDocumentBaseURI() + "#types" + schemaCount;
String suffix = ".xsd";
String baseURI = def.getDocumentBaseURI();
if (baseURI != null && baseURI.regionMatches(true, baseURI.length() - suffix.length(), suffix, 0, suffix.length()) && def.getTargetNamespace() == null && obj instanceof Schema && baseURI.equals(((Schema) obj).getDocumentBaseURI())) {
systemId = baseURI;
}
schemaCol.setBaseUri(def.getDocumentBaseURI());
CatalogXmlSchemaURIResolver schemaResolver = new CatalogXmlSchemaURIResolver(bus);
schemaCol.setSchemaResolver(schemaResolver);
XmlSchema xmlSchema = schemaCol.read(schemaElem, systemId);
catalogResolved.putAll(schemaResolver.getResolvedMap());
SchemaInfo schemaInfo = new SchemaInfo(xmlSchema.getTargetNamespace());
schemaInfo.setSchema(xmlSchema);
schemaInfo.setSystemId(systemId);
schemaInfo.setElement(schemaElem);
schemaInfos.add(schemaInfo);
schemaCount++;
}
}
}
}
}
use of javax.wsdl.Types in project cxf by apache.
the class ServiceWSDLBuilder method buildTypes.
protected void buildTypes(final Collection<SchemaInfo> schemas, final Map<String, SchemaInfo> imports, final Definition def) {
Types types = def.createTypes();
for (SchemaInfo schemaInfo : schemas) {
Schema schemaImpl = getSchemaImplementation(def);
schemaImpl.setRequired(true);
schemaImpl.setElementType(WSDLConstants.QNAME_SCHEMA);
schemaImpl.setElement(schemaInfo.getElement());
for (XmlSchemaExternal ext : schemaInfo.getSchema().getExternals()) {
if (ext.getSchema() == null) {
continue;
}
if (ext instanceof XmlSchemaImport) {
SchemaImport imp = schemaImpl.createImport();
imp.setNamespaceURI(((XmlSchemaImport) ext).getNamespace());
imp.setSchemaLocationURI(((XmlSchemaImport) ext).getSchemaLocation());
Schema schemaImpl2 = getSchemaImplementation(def);
schemaImpl2.setRequired(true);
schemaImpl2.setElementType(WSDLConstants.QNAME_SCHEMA);
schemaImpl2.setDocumentBaseURI(ext.getSchema().getSourceURI());
try {
schemaImpl2.setElement(ext.getSchema().getSchemaDocument().getDocumentElement());
} catch (XmlSchemaSerializerException e) {
// ignore
}
imp.setReferencedSchema(schemaImpl2);
schemaImpl.addImport(imp);
} else if (ext instanceof XmlSchemaInclude) {
SchemaReference imp = schemaImpl.createInclude();
imp.setSchemaLocationURI(((XmlSchemaInclude) ext).getSchemaLocation());
Schema schemaImpl2 = getSchemaImplementation(def);
schemaImpl2.setRequired(true);
schemaImpl2.setElementType(WSDLConstants.QNAME_SCHEMA);
schemaImpl2.setDocumentBaseURI(ext.getSchema().getSourceURI());
try {
schemaImpl2.setElement(ext.getSchema().getSchemaDocument().getDocumentElement());
} catch (XmlSchemaSerializerException e) {
// ignore
}
imp.setReferencedSchema(schemaImpl2);
schemaImpl.addInclude(imp);
} else if (ext instanceof XmlSchemaRedefine) {
SchemaReference imp = schemaImpl.createRedefine();
imp.setSchemaLocationURI(((XmlSchemaRedefine) ext).getSchemaLocation());
Schema schemaImpl2 = getSchemaImplementation(def);
schemaImpl2.setRequired(true);
schemaImpl2.setElementType(WSDLConstants.QNAME_SCHEMA);
schemaImpl2.setDocumentBaseURI(ext.getSchema().getSourceURI());
try {
schemaImpl2.setElement(ext.getSchema().getSchemaDocument().getDocumentElement());
} catch (XmlSchemaSerializerException e) {
// ignore
}
imp.setReferencedSchema(schemaImpl2);
schemaImpl.addRedefine(imp);
}
}
types.addExtensibilityElement(schemaImpl);
}
def.setTypes(types);
}
Aggregations