Search in sources :

Example 1 with DOMImplementationRegistry

use of org.w3c.dom.bootstrap.DOMImplementationRegistry in project qi4j-sdk by Qi4j.

the class QuikitResolver method getLSInput.

private LSInput getLSInput() throws Exception {
    DOMImplementationLS impl;
    DOMImplementation docImpl = builder.getDOMImplementation();
    // defaulting to the sun implementation.
    if (docImpl != null && docImpl.hasFeature("LS", "3.0")) {
        impl = (DOMImplementationLS) docImpl.getFeature("LS", "3.0");
    } else {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        if (impl == null) {
            System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
            registry = DOMImplementationRegistry.newInstance();
            impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        }
    }
    return impl.createLSInput();
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) DOMImplementation(org.w3c.dom.DOMImplementation)

Example 2 with DOMImplementationRegistry

use of org.w3c.dom.bootstrap.DOMImplementationRegistry in project camel by apache.

the class XmlSignatureHelper method transformNonTextNodeToOutputStream.

/**
     * Serializes a node using a certain character encoding.
     * 
     * @param node
     *            DOM node to serialize
     * @param os
     *            output stream, to which the node is serialized
     * @param omitXmlDeclaration
     *            indicator whether to omit the XML declaration or not
     * @param encoding
     *            character encoding, can be <code>null</code>, if
     *            <code>null</code> then "UTF-8" is used
     * @throws Exception
     */
public static void transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration, String encoding) throws Exception {
    // therefore we switched to DOMImplementationLS
    if (encoding == null) {
        encoding = "UTF-8";
    }
    DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry.getDOMImplementation("LS");
    LSOutput lsOutput = domImplementationLS.createLSOutput();
    lsOutput.setEncoding(encoding);
    lsOutput.setByteStream(os);
    LSSerializer lss = domImplementationLS.createLSSerializer();
    lss.getDomConfig().setParameter("xml-declaration", !omitXmlDeclaration);
    lss.write(node, lsOutput);
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSSerializer(org.w3c.dom.ls.LSSerializer) LSOutput(org.w3c.dom.ls.LSOutput)

Example 3 with DOMImplementationRegistry

use of org.w3c.dom.bootstrap.DOMImplementationRegistry in project languagetool by languagetool-org.

the class PatternRuleXmlCreator method getDocument.

private Document getDocument(InputStream is) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
    // we need to ignore whitespace here so the nodeToString() method will be able to indent it properly:
    parser.setFilter(new IgnoreWhitespaceFilter());
    LSInput domInput = impl.createLSInput();
    domInput.setByteStream(is);
    return parser.parse(domInput);
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSInput(org.w3c.dom.ls.LSInput) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSParser(org.w3c.dom.ls.LSParser)

Example 4 with DOMImplementationRegistry

use of org.w3c.dom.bootstrap.DOMImplementationRegistry in project jdk8u_jdk by JetBrains.

the class ItxtUtf8Test method runTest.

public static void runTest(boolean dump, boolean truncate) throws Exception {
    String format = "javax_imageio_png_1.0";
    BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
    DOMImplementationRegistry registry;
    registry = DOMImplementationRegistry.newInstance();
    DOMImplementation impl = registry.getDOMImplementation("XML 3.0");
    Document doc = impl.createDocument(null, format, null);
    Element root, itxt, entry;
    root = doc.getDocumentElement();
    root.appendChild(itxt = doc.createElement("iTXt"));
    itxt.appendChild(entry = doc.createElement("iTXtEntry"));
    entry.setAttribute("keyword", "verbatim");
    entry.setAttribute("compressionFlag", "false");
    entry.setAttribute("compressionMethod", "0");
    entry.setAttribute("languageTag", "x-circled");
    entry.setAttribute("translatedKeyword", VERBATIM);
    entry.setAttribute("text", TEXT);
    itxt.appendChild(entry = doc.createElement("iTXtEntry"));
    entry.setAttribute("keyword", "compressed");
    entry.setAttribute("compressionFlag", "true");
    entry.setAttribute("compressionMethod", "0");
    entry.setAttribute("languageTag", "x-circled");
    entry.setAttribute("translatedKeyword", COMPRESSED);
    entry.setAttribute("text", TEXT);
    meta.mergeTree(format, root);
    iw.write(new IIOImage(img, null, meta));
    iw.dispose();
    byte[] bytes = os.toByteArray();
    if (dump)
        System.out.write(bytes);
    if (findBytes(VBYTES, bytes) < 0)
        throw new AssertionError("verbatim block not found");
    if (findBytes(CBYTES, bytes) < 0)
        throw new AssertionError("compressed block not found");
    int length = bytes.length;
    if (truncate)
        length = findBytes(VBYTES, bytes) + 32;
    ImageReader ir = ImageIO.getImageReader(iw);
    ByteArrayInputStream is = new ByteArrayInputStream(bytes, 0, length);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    meta = ir.getImageMetadata(0);
    Node node = meta.getAsTree(format);
    for (node = node.getFirstChild(); !"iTXt".equals(node.getNodeName()); node = node.getNextSibling()) ;
    boolean verbatimSeen = false, compressedSeen = false;
    for (node = node.getFirstChild(); node != null; node = node.getNextSibling()) {
        entry = (Element) node;
        String keyword = entry.getAttribute("keyword");
        String translatedKeyword = entry.getAttribute("translatedKeyword");
        String text = entry.getAttribute("text");
        if ("verbatim".equals(keyword)) {
            if (verbatimSeen)
                throw new AssertionError("Duplicate");
            verbatimSeen = true;
            if (!VERBATIM.equals(translatedKeyword))
                throw new AssertionError("Wrong translated keyword");
            if (!TEXT.equals(text))
                throw new AssertionError("Wrong text");
        } else if ("compressed".equals(keyword)) {
            if (compressedSeen)
                throw new AssertionError("Duplicate");
            compressedSeen = true;
            if (!COMPRESSED.equals(translatedKeyword))
                throw new AssertionError("Wrong translated keyword");
            if (!TEXT.equals(text))
                throw new AssertionError("Wrong text");
        } else {
            throw new AssertionError("Unexpected keyword");
        }
    }
    if (!(verbatimSeen && compressedSeen))
        throw new AssertionError("Missing chunk");
}
Also used : Element(org.w3c.dom.Element) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) Node(org.w3c.dom.Node) ImageWriter(javax.imageio.ImageWriter) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) DOMImplementation(org.w3c.dom.DOMImplementation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream)

Example 5 with DOMImplementationRegistry

use of org.w3c.dom.bootstrap.DOMImplementationRegistry in project zxing by zxing.

the class HtmlAssetTranslator method translateOneFile.

private static void translateOneFile(String language, Path targetHtmlDir, Path sourceFile, String translationTextTranslated) throws IOException {
    Path destFile = targetHtmlDir.resolve(sourceFile.getFileName());
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document document;
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(sourceFile.toFile());
    } catch (ParserConfigurationException pce) {
        throw new IllegalStateException(pce);
    } catch (SAXException sae) {
        throw new IOException(sae);
    }
    Element rootElement = document.getDocumentElement();
    rootElement.normalize();
    Queue<Node> nodes = new LinkedList<>();
    nodes.add(rootElement);
    while (!nodes.isEmpty()) {
        Node node = nodes.poll();
        if (shouldTranslate(node)) {
            NodeList children = node.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                nodes.add(children.item(i));
            }
        }
        if (node.getNodeType() == Node.TEXT_NODE) {
            String text = node.getTextContent();
            if (!text.trim().isEmpty()) {
                text = StringsResourceTranslator.translateString(text, language);
                node.setTextContent(' ' + text + ' ');
            }
        }
    }
    Node translateText = document.createTextNode(translationTextTranslated);
    Node paragraph = document.createElement("p");
    paragraph.appendChild(translateText);
    Node body = rootElement.getElementsByTagName("body").item(0);
    body.appendChild(paragraph);
    DOMImplementationRegistry registry;
    try {
        registry = DOMImplementationRegistry.newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSSerializer writer = impl.createLSSerializer();
    String fileAsString = writer.writeToString(document);
    // Replace default XML header with HTML DOCTYPE
    fileAsString = fileAsString.replaceAll("<\\?xml[^>]+>", "<!DOCTYPE HTML>");
    Files.write(destFile, Collections.singleton(fileAsString), StandardCharsets.UTF_8);
}
Also used : Path(java.nio.file.Path) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) IOException(java.io.IOException) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

DOMImplementationRegistry (org.w3c.dom.bootstrap.DOMImplementationRegistry)11 DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)6 DOMImplementation (org.w3c.dom.DOMImplementation)4 Document (org.w3c.dom.Document)4 LSSerializer (org.w3c.dom.ls.LSSerializer)4 Element (org.w3c.dom.Element)3 LSOutput (org.w3c.dom.ls.LSOutput)3 BufferedImage (java.awt.image.BufferedImage)2 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)2 ImageWriter (javax.imageio.ImageWriter)2 IIOMetadata (javax.imageio.metadata.IIOMetadata)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 DOMError (org.w3c.dom.DOMError)2 DOMErrorHandler (org.w3c.dom.DOMErrorHandler)2 Node (org.w3c.dom.Node)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1