use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class JAXBDataBinding method addBindingFiles.
private void addBindingFiles(Options opts, List<InputSource> jaxbBindings, SchemaCollection schemas) {
for (InputSource binding : jaxbBindings) {
XMLStreamReader r = StaxUtils.createXMLStreamReader(binding);
try {
StaxUtils.toNextTag(r);
String s = r.getAttributeValue(null, "schemaLocation");
if (StringUtils.isEmpty(s)) {
Document d = StaxUtils.read(r);
XPath p = XPathFactory.newInstance().newXPath();
p.setNamespaceContext(new W3CNamespaceContext(d.getDocumentElement()));
XPathExpression xpe = p.compile(d.getDocumentElement().getAttribute("node"));
for (XmlSchema schema : schemas.getXmlSchemas()) {
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
continue;
}
Object src = getSchemaNode(schema, schemas);
NodeList nodes = (NodeList) xpe.evaluate(src, XPathConstants.NODESET);
if (nodes.getLength() > 0) {
String key = schema.getSourceURI();
binding = convertToTmpInputSource(d.getDocumentElement(), key);
opts.addBindFile(binding);
binding = null;
}
}
}
} catch (Exception ex) {
// ignore, just pass to jaxb
} finally {
try {
r.close();
} catch (Exception ex) {
// ignore
}
}
if (binding != null) {
opts.addBindFile(binding);
}
}
}
use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class SourceGenerator method createSchemaInfo.
private SchemaInfo createSchemaInfo(Element schemaEl, String systemId) {
SchemaInfo info = new SchemaInfo(schemaEl.getAttribute("targetNamespace"));
info.setElement(schemaEl);
info.setSystemId(systemId);
// eviction of the DOM element from the memory
try {
XmlSchema xmlSchema = schemaCollection.read(schemaEl, systemId);
info.setSchema(xmlSchema);
} catch (Exception ex) {
// may be due to unsupported resolvers for protocols like
// classpath: or not the valid schema definition, may not be critical
// for the purpose of the schema compilation.
}
return info;
}
use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class JAXBDataBinding method addSchemas.
private void addSchemas(Options opts, SchemaCompiler schemaCompiler, SchemaCollection schemaCollection) {
Set<String> ids = new HashSet<>();
@SuppressWarnings("unchecked") List<ServiceInfo> serviceList = (List<ServiceInfo>) context.get(ToolConstants.SERVICE_LIST);
for (ServiceInfo si : serviceList) {
for (SchemaInfo sci : si.getSchemas()) {
String key = sci.getSystemId();
if (ids.contains(key)) {
continue;
}
ids.add(key);
}
}
Bus bus = context.get(Bus.class);
OASISCatalogManager catalog = bus.getExtension(OASISCatalogManager.class);
for (XmlSchema schema : schemaCollection.getXmlSchemas()) {
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
continue;
}
String key = schema.getSourceURI();
if (ids.contains(key)) {
continue;
}
if (!key.startsWith("file:") && !key.startsWith("jar:")) {
XmlSchemaSerializer xser = new XmlSchemaSerializer();
xser.setExtReg(schemaCollection.getExtReg());
Document[] docs;
try {
docs = xser.serializeSchema(schema, false);
} catch (XmlSchemaSerializerException e) {
throw new RuntimeException(e);
}
Element ele = docs[0].getDocumentElement();
if (context.fullValidateWSDL()) {
String uri = null;
try {
uri = docs[0].getDocumentURI();
} catch (Throwable ex) {
// ignore - DOM level 3
}
validateSchema(ele, uri, catalog, schemaCollection);
}
ele = removeImportElement(ele, key, catalog);
try {
docs[0].setDocumentURI(key);
} catch (Throwable t) {
// ignore - DOM level 3
}
InputSource is = new InputSource((InputStream) null);
// key = key.replaceFirst("#types[0-9]+$", "");
is.setSystemId(key);
is.setPublicId(key);
opts.addGrammar(is);
try {
schemaCompiler.parseSchema(key, createNoCDATAReader(StaxUtils.createXMLStreamReader(ele, key)));
} catch (XMLStreamException e) {
throw new ToolException(e);
}
}
}
for (XmlSchema schema : schemaCollection.getXmlSchemas()) {
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
continue;
}
String key = schema.getSourceURI();
String tns = schema.getTargetNamespace();
String ltns = schema.getLogicalTargetNamespace();
// accepting also a null tns (e.g., reported by apache.ws.xmlschema for no-namespace)
if (ids.contains(key) || (tns == null && !StringUtils.isEmpty(ltns))) {
continue;
}
if (key.startsWith("file:") || key.startsWith("jar:")) {
InputStream in = null;
try {
if (key.startsWith("file:")) {
in = Files.newInputStream(new File(new URI(key)).toPath());
} else {
in = new URL(key).openStream();
}
XMLStreamReader reader = StaxUtils.createXMLStreamReader(key, in);
reader = createNoCDATAReader(new LocationFilterReader(reader, catalog));
InputSource is = new InputSource(key);
opts.addGrammar(is);
schemaCompiler.parseSchema(key, reader);
reader.close();
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
}
}
}
addSchemasForServiceInfos(catalog, serviceList, opts, schemaCompiler, schemaCollection);
}
use of org.apache.ws.commons.schema.XmlSchema 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.XmlSchema in project cxf by apache.
the class WSDLToCorbaHelper method getSchemaType.
public XmlSchemaType getSchemaType(QName name) throws Exception {
XmlSchemaType type = null;
for (XmlSchema xmlSchema : xmlSchemaList.getXmlSchemas()) {
String nspace = name.getNamespaceURI();
if (nspace == null) {
nspace = xmlSchema.getTargetNamespace();
}
// QName tname = createQName(nspace, name.getLocalPart(), "xsd");
QName tname = createQName(nspace, name.getLocalPart(), "");
type = findSchemaType(tname);
if (type != null) {
break;
}
}
return type;
}
Aggregations