use of org.apache.cxf.service.model.SchemaInfo 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.cxf.service.model.SchemaInfo in project cxf by apache.
the class WSDL11Generator method generate.
public Definition generate(final File dir) {
File file = getOutputBase();
if (file == null && dir != null) {
if (dir.isDirectory()) {
file = new File(dir, getServiceModel().getName().getLocalPart() + ".wsdl");
} else {
file = dir;
}
} else if (dir == null) {
file = new File(getServiceModel().getName().getLocalPart() + ".wsdl");
}
File outputdir = createOutputDir(file);
Definition def = null;
try {
Writer os = new FileWriterUtil(file.getParent(), getOutputStreamCreator()).getWriter(file, StandardCharsets.UTF_8.name());
WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
ServiceWSDLBuilder builder = new ServiceWSDLBuilder(getBus(), getServiceModel());
builder.setUseSchemaImports(this.allowImports());
String name = file.getName();
if (name.endsWith(".wsdl")) {
name = name.substring(0, name.lastIndexOf(".wsdl"));
}
builder.setBaseFileName(name);
Map<String, SchemaInfo> imports = new HashMap<>();
def = builder.build(imports);
wsdlWriter.writeWSDL(def, os);
os.close();
if (def.getImports().size() > 0) {
for (Import wsdlImport : WSDLDefinitionBuilder.getImports(def)) {
Definition wsdlDef = wsdlImport.getDefinition();
File wsdlFile = null;
if (!StringUtils.isEmpty(wsdlImport.getLocationURI())) {
wsdlFile = new File(outputdir, wsdlImport.getLocationURI());
} else {
wsdlFile = new File(outputdir, wsdlDef.getQName().getLocalPart() + ".wsdl");
}
try (OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()))) {
wsdlWriter.writeWSDL(wsdlDef, wsdlOs);
}
}
}
for (Map.Entry<String, SchemaInfo> imp : imports.entrySet()) {
File impfile = new File(file.getParentFile(), imp.getKey());
Element el = imp.getValue().getElement();
updateImports(el, imports);
FileWriterUtil fileWriterUtil = new FileWriterUtil(impfile.getParent(), getToolContext().get(OutputStreamCreator.class));
os = fileWriterUtil.getWriter(impfile, StandardCharsets.UTF_8.name());
StaxUtils.writeTo(el, os, 2);
os.close();
}
customizing(outputdir, name, imports.keySet());
} catch (WSDLException wex) {
wex.printStackTrace();
} catch (FileNotFoundException fnfe) {
throw new ToolException("Output file " + file + " not found", fnfe);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
return def;
}
use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.
the class SourceGenerator method getGrammarInfo.
private GrammarInfo getGrammarInfo(Application app, List<SchemaInfo> schemaElements) {
if (schemaElements == null || schemaElements.isEmpty()) {
return new GrammarInfo();
}
Map<String, String> nsMap = new HashMap<>();
NamedNodeMap attrMap = app.getAppElement().getAttributes();
for (int i = 0; i < attrMap.getLength(); i++) {
Node node = attrMap.item(i);
String nodeName = node.getNodeName();
if (nodeName.startsWith("xmlns:")) {
String nsValue = node.getNodeValue();
nsMap.put(nodeName.substring(6), nsValue);
}
}
Map<String, String> elementTypeMap = new HashMap<>();
for (SchemaInfo schemaEl : schemaElements) {
populateElementTypeMap(app, schemaEl.getElement(), schemaEl.getSystemId(), elementTypeMap);
}
boolean noTargetNamespace = schemaElements.size() == 1 && schemaElements.get(0).getNamespaceURI().isEmpty();
return new GrammarInfo(nsMap, elementTypeMap, noTargetNamespace);
}
use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.
the class SourceGenerator method getSchemaElements.
private List<SchemaInfo> getSchemaElements(Application app) {
List<Element> grammarEls = getWadlElements(app.getAppElement(), "grammars");
if (grammarEls.size() != 1) {
return null;
}
List<SchemaInfo> schemas = new ArrayList<>();
List<Element> schemasEls = DOMUtils.getChildrenWithName(grammarEls.get(0), Constants.URI_2001_SCHEMA_XSD, "schema");
for (int i = 0; i < schemasEls.size(); i++) {
String systemId = app.getWadlPath();
if (schemasEls.size() > 1) {
systemId += "#grammar" + (i + 1);
}
schemas.add(createSchemaInfo(schemasEls.get(i), systemId));
}
List<Element> includeEls = getWadlElements(grammarEls.get(0), "include");
for (Element includeEl : includeEls) {
String href = includeEl.getAttribute("href");
String schemaURI = resolveLocationWithCatalog(href);
if (schemaURI == null) {
if (!URI.create(href).isAbsolute() && app.getWadlPath() != null) {
String baseWadlPath = getBaseWadlPath(app.getWadlPath());
if (!href.startsWith("/") && !href.contains("..")) {
schemaURI = baseWadlPath + href;
} else {
try {
schemaURI = new URL(new URL(baseWadlPath), href).toString();
} catch (Exception ex) {
schemaURI = URI.create(baseWadlPath).resolve(href).toString();
}
}
} else {
schemaURI = href;
}
}
schemas.add(createSchemaInfo(readIncludedDocument(schemaURI), schemaURI));
}
return schemas;
}
use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.
the class EndpointReferenceUtils method createSchema.
private static synchronized Schema createSchema(ServiceInfo serviceInfo, Bus b) {
if (b == null) {
b = BusFactory.getThreadDefaultBus(false);
}
Schema schema = serviceInfo.getProperty(Schema.class.getName(), Schema.class);
if (schema == null) {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Map<String, byte[]> schemaSourcesMap = new LinkedHashMap<String, byte[]>();
Map<String, Source> schemaSourcesMap2 = new LinkedHashMap<String, Source>();
XMLStreamWriter writer = null;
try {
for (SchemaInfo si : serviceInfo.getSchemas()) {
Element el = si.getElement();
unsetReadonly(el);
String baseURI = null;
try {
baseURI = el.getBaseURI();
} catch (Exception ex) {
// ignore - not DOM level 3
}
if (baseURI == null) {
baseURI = si.getSystemId();
}
DOMSource ds = new DOMSource(el, baseURI);
schemaSourcesMap2.put(si.getSystemId() + ":" + si.getNamespaceURI(), ds);
LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
writer = StaxUtils.createXMLStreamWriter(out);
StaxUtils.copy(el, writer);
writer.flush();
schemaSourcesMap.put(si.getSystemId() + ":" + si.getNamespaceURI(), out.toByteArray());
}
for (XmlSchema sch : serviceInfo.getXmlSchemaCollection().getXmlSchemas()) {
if (sch.getSourceURI() != null && !schemaSourcesMap.containsKey(sch.getSourceURI() + ":" + sch.getTargetNamespace())) {
InputStream ins = null;
try {
URL url = new URL(sch.getSourceURI());
ins = url.openStream();
} catch (Exception e) {
// ignore, we'll just use what we have. (though
// bugs in XmlSchema could make this less useful)
}
LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
if (ins == null) {
sch.write(out);
} else {
IOUtils.copyAndCloseInput(ins, out);
}
schemaSourcesMap.put(sch.getSourceURI() + ":" + sch.getTargetNamespace(), out.toByteArray());
Source source = new StreamSource(out.createInputStream(), sch.getSourceURI());
schemaSourcesMap2.put(sch.getSourceURI() + ":" + sch.getTargetNamespace(), source);
}
}
factory.setResourceResolver(new SchemaLSResourceResolver(schemaSourcesMap, b));
schema = factory.newSchema(schemaSourcesMap2.values().toArray(new Source[schemaSourcesMap2.size()]));
} catch (Exception ex) {
// Something not right with the schema from the wsdl.
LOG.log(Level.WARNING, "SAXException for newSchema()", ex);
for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
String s = StaxUtils.toString(schemaInfo.getElement(), 4);
LOG.log(Level.INFO, "Schema for: " + schemaInfo.getNamespaceURI() + "\n" + s);
}
} finally {
for (Source src : schemaSourcesMap2.values()) {
if (src instanceof DOMSource) {
Node nd = ((DOMSource) src).getNode();
unsetReadonly(nd);
}
}
StaxUtils.close(writer);
}
serviceInfo.setProperty(Schema.class.getName(), schema);
}
return schema;
}
Aggregations