use of org.apache.ws.commons.schema.XmlSchemaSerializer in project cxf by apache.
the class ImportRepairTest method tryToParseSchemas.
private void tryToParseSchemas() throws Exception {
// Get DOM Implementation using DOM Registry
final List<DOMLSInput> inputs = new ArrayList<>();
final Map<String, LSInput> resolverMap = new HashMap<>();
for (XmlSchema schema : collection.getXmlSchemas()) {
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
continue;
}
Document document = new XmlSchemaSerializer().serializeSchema(schema, false)[0];
DOMLSInput input = new DOMLSInput(document, schema.getTargetNamespace());
dumpSchema(document);
resolverMap.put(schema.getTargetNamespace(), input);
inputs.add(input);
}
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementation impl = registry.getDOMImplementation("XS-Loader");
try {
Object schemaLoader = findMethod(impl, "createXSLoader").invoke(impl, new Object[1]);
DOMConfiguration config = (DOMConfiguration) findMethod(schemaLoader, "getConfig").invoke(schemaLoader);
config.setParameter("validate", Boolean.TRUE);
try {
// bug in the JDK doesn't set this, but accesses it
config.setParameter("http://www.oracle.com/xml/jaxp/properties/xmlSecurityPropertyManager", Class.forName("com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager").newInstance());
config.setParameter("http://apache.org/xml/properties/security-manager", Class.forName("com.sun.org.apache.xerces.internal.utils.XMLSecurityManager").newInstance());
} catch (Throwable t) {
// ignore
}
config.setParameter("error-handler", new DOMErrorHandler() {
public boolean handleError(DOMError error) {
LOG.info("Schema parsing error: " + error.getMessage() + " " + error.getType() + " " + error.getLocation().getUri() + " " + error.getLocation().getLineNumber() + ":" + error.getLocation().getColumnNumber());
throw new DOMErrorException(error);
}
});
config.setParameter("resource-resolver", new LSResourceResolver() {
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
return resolverMap.get(namespaceURI);
}
});
Method m = findMethod(schemaLoader, "loadInputList");
String name = m.getParameterTypes()[0].getName() + "Impl";
name = name.replace("xs.LS", "impl.xs.util.LS");
Class<?> c = Class.forName(name);
Object inputList = c.getConstructor(LSInput[].class, Integer.TYPE).newInstance(inputs.toArray(new LSInput[inputs.size()]), inputs.size());
findMethod(schemaLoader, "loadInputList").invoke(schemaLoader, inputList);
} catch (InvocationTargetException ite) {
throw (Exception) ite.getTargetException();
}
}
use of org.apache.ws.commons.schema.XmlSchemaSerializer in project cxf by apache.
the class EnumTypeTest method testWsdlFromJaxbAnnotations.
/**
* {@link https://issues.apache.org/jira/browse/CXF-7188}
*/
@Test
public void testWsdlFromJaxbAnnotations() throws Exception {
AegisType type = tm.getTypeCreator().createType(JaxbTestEnum.class);
XmlSchema schema = newXmlSchema("urn:test");
type.writeSchema(schema);
XmlSchemaSerializer ser = new XmlSchemaSerializer();
Document doc = ser.serializeSchema(schema, false)[0];
addNamespace("xsd", Constants.URI_2001_SCHEMA_XSD);
assertValid("//xsd:simpleType[@name='bar']/xsd:restriction[@base='xsd:string']", doc);
assertValid("//xsd:restriction[@base='xsd:string']/xsd:enumeration[@value='Value1']", doc);
assertValid("//xsd:restriction[@base='xsd:string']/xsd:enumeration[@value='VALUE2']", doc);
}
use of org.apache.ws.commons.schema.XmlSchemaSerializer 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.XmlSchemaSerializer in project cxf by apache.
the class XercesSchemaValidationUtils method tryToParseSchemas.
void tryToParseSchemas(XmlSchemaCollection collection, DOMErrorHandler handler) throws Exception {
final List<DOMLSInput> inputs = new ArrayList<>();
final Map<String, LSInput> resolverMap = new HashMap<>();
for (XmlSchema schema : collection.getXmlSchemas()) {
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
continue;
}
Document document = new XmlSchemaSerializer().serializeSchema(schema, false)[0];
DOMLSInput input = new DOMLSInput(document, schema.getTargetNamespace());
resolverMap.put(schema.getTargetNamespace(), input);
inputs.add(input);
}
try {
Object schemaLoader = findMethod(impl, "createXSLoader").invoke(impl, new Object[1]);
DOMConfiguration config = (DOMConfiguration) findMethod(schemaLoader, "getConfig").invoke(schemaLoader);
config.setParameter("validate", Boolean.TRUE);
config.setParameter("error-handler", handler);
config.setParameter("resource-resolver", new LSResourceResolver() {
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
return resolverMap.get(namespaceURI);
}
});
Method m = findMethod(schemaLoader, "loadInputList");
String name = m.getParameterTypes()[0].getName() + "Impl";
name = name.replace("xs.LS", "impl.xs.util.LS");
Class<?> c = Class.forName(name);
Object inputList = c.getConstructor(LSInput[].class, Integer.TYPE).newInstance(inputs.toArray(new LSInput[inputs.size()]), inputs.size());
m.invoke(schemaLoader, inputList);
} catch (InvocationTargetException e) {
throw (Exception) e.getTargetException();
}
}
use of org.apache.ws.commons.schema.XmlSchemaSerializer in project cxf by apache.
the class SchemaAddinsTest method testAegisTypeSchema.
@Test
public void testAegisTypeSchema() throws Exception {
AegisContext context = new AegisContext();
context.initialize();
XmlSchemaCollection collection = new XmlSchemaCollection();
context.addTypesSchemaDocument(collection);
XmlSchema[] schemas = collection.getXmlSchemas();
XmlSchema typeSchema = null;
for (XmlSchema schema : schemas) {
if (AegisContext.UTILITY_TYPES_SCHEMA_NS.equals(schema.getTargetNamespace())) {
typeSchema = schema;
break;
}
}
assertNotNull(typeSchema);
assertNotSame(0, typeSchema.getItems().size());
XmlSchemaSerializer serializer = new XmlSchemaSerializer();
Document[] docs = serializer.serializeSchema(typeSchema, false);
testUtilities.assertValid("/xsd:schema/xsd:simpleType[@name='char']", docs[0]);
}
Aggregations