Search in sources :

Example 36 with DOMImplementation

use of org.w3c.dom.DOMImplementation 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 37 with DOMImplementation

use of org.w3c.dom.DOMImplementation in project cxf by apache.

the class HWStreamSourcePayloadProvider method getSOAPBodyStream.

private InputStream getSOAPBodyStream(Document doc) throws Exception {
    // Try to get the DOMImplementation from the doc before
    // defaulting to the sun implementation class (which uses
    // sun internal xerces classes not found in the ibm jdk).
    DOMImplementationLS impl = null;
    DOMImplementation docImpl = doc.getImplementation();
    if (docImpl != null && docImpl.hasFeature("LS", "3.0")) {
        impl = (DOMImplementationLS) docImpl.getFeature("LS", "3.0");
    } else {
        System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    }
    LSOutput output = impl.createLSOutput();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    output.setByteStream(byteArrayOutputStream);
    LSSerializer writer = impl.createLSSerializer();
    writer.write(doc, output);
    byte[] buf = byteArrayOutputStream.toByteArray();
    return new ByteArrayInputStream(buf);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) DOMImplementation(org.w3c.dom.DOMImplementation) LSSerializer(org.w3c.dom.ls.LSSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LSOutput(org.w3c.dom.ls.LSOutput)

Example 38 with DOMImplementation

use of org.w3c.dom.DOMImplementation in project Payara by payara.

the class DeveloperContentHandler method writeXML.

private synchronized void writeXML(final Node node, final Writer writer) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    if (lsSerializer == null) {
        final DOMImplementation domImpl = DOMImplementationRegistry.newInstance().getDOMImplementation("");
        final DOMImplementationLS domLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
        lsOutput = domLS.createLSOutput();
        lsOutput.setEncoding("UTF-8");
        lsSerializer = domLS.createLSSerializer();
    }
    lsOutput.setCharacterStream(writer);
    lsSerializer.write(node, lsOutput);
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementation(org.w3c.dom.DOMImplementation)

Example 39 with DOMImplementation

use of org.w3c.dom.DOMImplementation in project Payara by payara.

the class J2EEDocumentBuilder method newDocument.

/**
 * Creates and Return a new DOM document based on the current
 * configuration
 *
 * @return the new DOM Document object
 */
public static Document newDocument() {
    try {
        // always use system default, see IT 8229
        ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(J2EEDocumentBuilder.class.getClassLoader());
        DocumentBuilderFactory factory = null;
        try {
            factory = DocumentBuilderFactory.newInstance();
        } finally {
            Thread.currentThread().setContextClassLoader(currentLoader);
        }
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation domImplementation = builder.getDOMImplementation();
        Document document = builder.newDocument();
        return document;
    } catch (Exception e) {
        DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError", new Object[] { "JAXP configuration error" });
        DOLUtils.getDefaultLogger().log(Level.WARNING, "Error occurred", e);
    }
    return null;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DOMImplementation(org.w3c.dom.DOMImplementation) Document(org.w3c.dom.Document) IOException(java.io.IOException)

Example 40 with DOMImplementation

use of org.w3c.dom.DOMImplementation in project yamcs-studio by yamcs.

the class SVGHandler method createWrapper.

private Document createWrapper(final SVGDocument doc) {
    // creation of the SVG document
    String svgNamespace = SVGDOMImplementation.SVG_NAMESPACE_URI;
    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    final Document newDocument = impl.createDocument(svgNamespace, "svg", null);
    // get the root element
    svgRootNode = newDocument.getDocumentElement();
    mainGraphicNode = newDocument.createElementNS(svgNamespace, "g");
    // attach the root of original doc to transform to the root
    Node copiedRoot = newDocument.importNode(doc.getDocumentElement(), true);
    mainGraphicNode.appendChild(copiedRoot);
    svgRootNode.appendChild(mainGraphicNode);
    updateMatrix();
    return newDocument;
}
Also used : CanvasGraphicsNode(org.apache.batik.gvt.CanvasGraphicsNode) CompositeGraphicsNode(org.apache.batik.gvt.CompositeGraphicsNode) GraphicsNode(org.apache.batik.gvt.GraphicsNode) Node(org.w3c.dom.Node) CSSStyleSheetNode(org.apache.batik.css.engine.CSSStyleSheetNode) DOMImplementation(org.w3c.dom.DOMImplementation) SVGDOMImplementation(org.apache.batik.anim.dom.SVGDOMImplementation) Document(org.w3c.dom.Document) SVGOMDocument(org.apache.batik.anim.dom.SVGOMDocument) SVGDocument(org.w3c.dom.svg.SVGDocument)

Aggregations

DOMImplementation (org.w3c.dom.DOMImplementation)82 Document (org.w3c.dom.Document)67 DOMException (org.w3c.dom.DOMException)35 Element (org.w3c.dom.Element)34 DocumentType (org.w3c.dom.DocumentType)28 DocumentBuilder (javax.xml.parsers.DocumentBuilder)25 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)22 ArrayList (java.util.ArrayList)8 TransformerException (javax.xml.transform.TransformerException)6 DOMImplementationRegistry (org.w3c.dom.bootstrap.DOMImplementationRegistry)6 DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)6 IOException (java.io.IOException)5 Transformer (javax.xml.transform.Transformer)5 DOMSource (javax.xml.transform.dom.DOMSource)5 StreamResult (javax.xml.transform.stream.StreamResult)5 Node (org.w3c.dom.Node)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 NodeList (org.w3c.dom.NodeList)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3