Search in sources :

Example 1 with XmlSchemaSerializer

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();
    }
}
Also used : DOMErrorHandler(org.w3c.dom.DOMErrorHandler) HashMap(java.util.HashMap) DOMError(org.w3c.dom.DOMError) ArrayList(java.util.ArrayList) DOMImplementation(org.w3c.dom.DOMImplementation) DOMConfiguration(org.w3c.dom.DOMConfiguration) XmlSchemaSerializer(org.apache.ws.commons.schema.XmlSchemaSerializer) Method(java.lang.reflect.Method) Document(org.w3c.dom.Document) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) LSResourceResolver(org.w3c.dom.ls.LSResourceResolver) LSInput(org.w3c.dom.ls.LSInput) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry)

Example 2 with XmlSchemaSerializer

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);
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaSerializer(org.apache.ws.commons.schema.XmlSchemaSerializer) Document(org.w3c.dom.Document) Test(org.junit.Test) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest)

Example 3 with XmlSchemaSerializer

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);
}
Also used : InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) URI(java.net.URI) URL(java.net.URL) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) OASISCatalogManager(org.apache.cxf.catalog.OASISCatalogManager) ArrayList(java.util.ArrayList) List(java.util.List) NodeList(org.w3c.dom.NodeList) ToolException(org.apache.cxf.tools.common.ToolException) HashSet(java.util.HashSet) SchemaInfo(org.apache.cxf.service.model.SchemaInfo) Bus(org.apache.cxf.Bus) InputStream(java.io.InputStream) XmlSchemaSerializer(org.apache.ws.commons.schema.XmlSchemaSerializer) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) SAXException(org.xml.sax.SAXException) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) DOMException(org.w3c.dom.DOMException) BadCommandLineException(com.sun.tools.xjc.BadCommandLineException) SAXParseException(org.xml.sax.SAXParseException) XMLStreamException(javax.xml.stream.XMLStreamException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) File(java.io.File)

Example 4 with XmlSchemaSerializer

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();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DOMConfiguration(org.w3c.dom.DOMConfiguration) XmlSchemaSerializer(org.apache.ws.commons.schema.XmlSchemaSerializer) Method(java.lang.reflect.Method) Document(org.w3c.dom.Document) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) LSResourceResolver(org.w3c.dom.ls.LSResourceResolver) LSInput(org.w3c.dom.ls.LSInput)

Example 5 with XmlSchemaSerializer

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]);
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) AegisContext(org.apache.cxf.aegis.AegisContext) XmlSchemaSerializer(org.apache.ws.commons.schema.XmlSchemaSerializer) Document(org.w3c.dom.Document) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) Test(org.junit.Test)

Aggregations

XmlSchemaSerializer (org.apache.ws.commons.schema.XmlSchemaSerializer)8 Document (org.w3c.dom.Document)8 XmlSchema (org.apache.ws.commons.schema.XmlSchema)7 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 XmlSchemaSerializerException (org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException)3 Test (org.junit.Test)3 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 URI (java.net.URI)2 URL (java.net.URL)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)2 OASISCatalogManager (org.apache.cxf.catalog.OASISCatalogManager)2 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)2 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)2