Search in sources :

Example 21 with DOMImplementation

use of org.w3c.dom.DOMImplementation in project j2objc by google.

the class DOMHelper method getParentOfNode.

/**
   * Obtain the XPath-model parent of a DOM node -- ownerElement for Attrs,
   * parent for other nodes. 
   * <p>
   * Background: The DOM believes that you must be your Parent's
   * Child, and thus Attrs don't have parents. XPath said that Attrs
   * do have their owning Element as their parent. This function
   * bridges the difference, either by using the DOM Level 2 ownerElement
   * function or by using a "silly and expensive function" in Level 1
   * DOMs.
   * <p>
   * (There's some discussion of future DOMs generalizing ownerElement 
   * into ownerNode and making it work on all types of nodes. This
   * still wouldn't help the users of Level 1 or Level 2 DOMs)
   * <p>
   *
   * @param node Node whose XPath parent we want to obtain
   *
   * @return the parent of the node, or the ownerElement if it's an
   * Attr node, or null if the node is an orphan.
   *
   * @throws RuntimeException if the Document has no root element.
   * This can't arise if the Document was created
   * via the DOM Level 2 factory methods, but is possible if other
   * mechanisms were used to obtain it
   */
public static Node getParentOfNode(Node node) throws RuntimeException {
    Node parent;
    short nodeType = node.getNodeType();
    if (Node.ATTRIBUTE_NODE == nodeType) {
        Document doc = node.getOwnerDocument();
        /*
      TBD:
      if(null == doc)
      {
        throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_CHILD_HAS_NO_OWNER_DOCUMENT, null));//"Attribute child does not have an owner document!");
      }
      */
        // Given how expensive the tree walk may be, we should first ask 
        // whether this DOM can answer the question for us. The additional
        // test does slow down Level 1 DOMs slightly. DOMHelper2, which
        // is currently specialized for Xerces, assumes it can use the
        // Level 2 solution. We might want to have an intermediate stage,
        // which would assume DOM Level 2 but not assume Xerces.
        //
        // (Shouldn't have to check whether impl is null in a compliant DOM,
        // but let's be paranoid for a moment...)
        DOMImplementation impl = doc.getImplementation();
        if (impl != null && impl.hasFeature("Core", "2.0")) {
            parent = ((Attr) node).getOwnerElement();
            return parent;
        }
        // DOM Level 1 solution, as fallback. Hugely expensive. 
        Element rootElem = doc.getDocumentElement();
        if (null == rootElem) {
            throw new RuntimeException(XMLMessages.createXMLMessage(XMLErrorResources.ER_CHILD_HAS_NO_OWNER_DOCUMENT_ELEMENT, //"Attribute child does not have an owner document element!");
            null));
        }
        parent = locateAttrParent(rootElem, node);
    } else {
        parent = node.getParentNode();
    // if((Node.DOCUMENT_NODE != nodeType) && (null == parent))
    // {
    //   throw new RuntimeException("Child does not have parent!");
    // }
    }
    return parent;
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) DOMImplementation(org.w3c.dom.DOMImplementation) Document(org.w3c.dom.Document)

Example 22 with DOMImplementation

use of org.w3c.dom.DOMImplementation in project j2objc by google.

the class DocumentBuilderTest method testGetImplementation.

public void testGetImplementation() {
    DOMImplementation d;
    try {
        d = dbf.newDocumentBuilder().getDOMImplementation();
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    assertNotNull(d);
}
Also used : DOMImplementation(org.w3c.dom.DOMImplementation) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 23 with DOMImplementation

use of org.w3c.dom.DOMImplementation in project j2objc by google.

the class XPathImpl method getDummyDocument.

private static Document getDummyDocument() {
    // enter this code at the same time, we just waste a little time
    if (d == null) {
        DOMImplementation dim = getParser().getDOMImplementation();
        d = dim.createDocument("http://java.sun.com/jaxp/xpath", "dummyroot", null);
    }
    return d;
}
Also used : DOMImplementation(org.w3c.dom.DOMImplementation)

Example 24 with DOMImplementation

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

use of org.w3c.dom.DOMImplementation in project CloudStack-archive by CloudStack-extras.

the class VsmCommand method getPolicyMap.

public static String getPolicyMap(String name) {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        DOMImplementation domImpl = docBuilder.getDOMImplementation();
        Document doc = createDocument(domImpl);
        Element get = doc.createElement("nf:get");
        doc.getDocumentElement().appendChild(get);
        Element filter = doc.createElement("nf:filter");
        filter.setAttribute("type", "subtree");
        get.appendChild(filter);
        // Create the show port-profile name <profile-name> command.
        Element show = doc.createElement("show");
        filter.appendChild(show);
        Element policyMap = doc.createElement("policy-map");
        show.appendChild(policyMap);
        Element nameNode = doc.createElement("name");
        nameNode.setTextContent(name);
        policyMap.appendChild(nameNode);
        return serialize(domImpl, doc);
    } catch (ParserConfigurationException e) {
        s_logger.error("Error while creating the message to get policy map details : " + e.getMessage());
        return null;
    } catch (DOMException e) {
        s_logger.error("Error while creating the message to get policy map details : " + e.getMessage());
        return null;
    }
}
Also used : DOMException(org.w3c.dom.DOMException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) DOMImplementation(org.w3c.dom.DOMImplementation) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document)

Aggregations

DOMImplementation (org.w3c.dom.DOMImplementation)73 Document (org.w3c.dom.Document)61 DOMException (org.w3c.dom.DOMException)35 Element (org.w3c.dom.Element)32 DocumentType (org.w3c.dom.DocumentType)28 DocumentBuilder (javax.xml.parsers.DocumentBuilder)23 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)22 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)20 ArrayList (java.util.ArrayList)7 TransformerException (javax.xml.transform.TransformerException)6 Transformer (javax.xml.transform.Transformer)4 DOMSource (javax.xml.transform.dom.DOMSource)4 StreamResult (javax.xml.transform.stream.StreamResult)4 NodeList (org.w3c.dom.NodeList)4 DOMImplementationRegistry (org.w3c.dom.bootstrap.DOMImplementationRegistry)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 Node (org.w3c.dom.Node)3 DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)3 BufferedImage (java.awt.image.BufferedImage)2