Search in sources :

Example 61 with DOMException

use of org.w3c.dom.DOMException in project JMRI by JMRI.

the class JmriConfiguration method removeConfigurationFragment.

@Override
public boolean removeConfigurationFragment(final String elementName, final String namespace, final boolean shared) throws IllegalArgumentException {
    synchronized (this) {
        File file = this.getConfigurationFile(shared);
        if (file.canWrite()) {
            try {
                Document doc;
                try (final InputStream is = new FileInputStream(file)) {
                    InputSource input = new InputSource(is);
                    input.setSystemId(file.toURI().toURL().toString());
                    doc = XMLUtil.parse(input, false, true, null, null);
                }
                Element root = doc.getDocumentElement();
                Element toRemove = XMLUtil.findElement(root, elementName, namespace);
                if (toRemove != null) {
                    root.removeChild(toRemove);
                    this.backup(shared);
                    if (root.getElementsByTagName("*").getLength() > 0) {
                        // NOI18N
                        try (final OutputStream os = new FileOutputStream(file)) {
                            // NOI18N
                            XMLUtil.write(doc, os, "UTF-8");
                        }
                    } else if (!file.delete()) {
                        log.debug("Unable to delete {}", file);
                    }
                    return true;
                }
            } catch (IOException | SAXException | DOMException ex) {
                log.error("Cannot remove {} from {}", elementName, file, ex);
            }
        }
        return false;
    }
}
Also used : DOMException(org.w3c.dom.DOMException) InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Document(org.w3c.dom.Document) File(java.io.File) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException)

Example 62 with DOMException

use of org.w3c.dom.DOMException in project JMRI by JMRI.

the class XMLUtil method normalize.

/**
     * Try to normalize a document by removing nonsignificant whitespace.
     *
     * @see "#62006"
     */
private static Document normalize(Document orig) throws IOException {
    DocumentBuilder builder = null;
    DocumentBuilderFactory factory = getFactory(false, false);
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        //NOI18N
        throw new IOException("Cannot create parser satisfying configuration parameters: " + e, e);
    }
    DocumentType doctype = null;
    NodeList nl = orig.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i) instanceof DocumentType) {
            // We cannot import DocumentType's, so we need to manually copy it.
            doctype = (DocumentType) nl.item(i);
        }
    }
    Document doc;
    if (doctype != null) {
        doc = builder.getDOMImplementation().createDocument(orig.getDocumentElement().getNamespaceURI(), orig.getDocumentElement().getTagName(), builder.getDOMImplementation().createDocumentType(orig.getDoctype().getName(), orig.getDoctype().getPublicId(), orig.getDoctype().getSystemId()));
        // XXX what about entity decls inside the DOCTYPE?
        doc.removeChild(doc.getDocumentElement());
    } else {
        doc = builder.newDocument();
    }
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (!(node instanceof DocumentType)) {
            try {
                doc.appendChild(doc.importNode(node, true));
            } catch (DOMException x) {
                // Thrown in NB-Core-Build #2896 & 2898 inside GeneratedFilesHelper.applyBuildExtensions
                throw new IOException("Could not import or append " + node + " of " + node.getClass(), x);
            }
        }
    }
    doc.normalize();
    // NOI18N
    nl = doc.getElementsByTagName("*");
    for (int i = 0; i < nl.getLength(); i++) {
        Element e = (Element) nl.item(i);
        removeXmlBase(e);
        NodeList nl2 = e.getChildNodes();
        for (int j = 0; j < nl2.getLength(); j++) {
            Node n = nl2.item(j);
            if (n instanceof Text && ((Text) n).getNodeValue().trim().length() == 0) {
                e.removeChild(n);
                // since list is dynamic
                j--;
            }
        }
    }
    return doc;
}
Also used : DOMException(org.w3c.dom.DOMException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) DocumentType(org.w3c.dom.DocumentType) Text(org.w3c.dom.Text) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document)

Example 63 with DOMException

use of org.w3c.dom.DOMException in project jdk8u_jdk by JetBrains.

the class TransformXPath method enginePerformTransform.

/**
     * Method enginePerformTransform
     * @inheritDoc
     * @param input
     *
     * @throws TransformationException
     */
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws TransformationException {
    try {
        /**
             * If the actual input is an octet stream, then the application MUST
             * convert the octet stream to an XPath node-set suitable for use by
             * Canonical XML with Comments. (A subsequent application of the
             * REQUIRED Canonical XML algorithm would strip away these comments.)
             *
             * ...
             *
             * The evaluation of this expression includes all of the document's nodes
             * (including comments) in the node-set representing the octet stream.
             */
        Element xpathElement = XMLUtils.selectDsNode(transformObject.getElement().getFirstChild(), Constants._TAG_XPATH, 0);
        if (xpathElement == null) {
            Object[] exArgs = { "ds:XPath", "Transform" };
            throw new TransformationException("xml.WrongContent", exArgs);
        }
        Node xpathnode = xpathElement.getChildNodes().item(0);
        String str = XMLUtils.getStrFromNode(xpathnode);
        input.setNeedsToBeExpanded(needsCircumvent(str));
        if (xpathnode == null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Text must be in ds:Xpath");
        }
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPathAPI xpathAPIInstance = xpathFactory.newXPathAPI();
        input.addNodeFilter(new XPathNodeFilter(xpathElement, xpathnode, str, xpathAPIInstance));
        input.setNodeSet(true);
        return input;
    } catch (DOMException ex) {
        throw new TransformationException("empty", ex);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) XPathFactory(com.sun.org.apache.xml.internal.security.utils.XPathFactory) TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) XPathAPI(com.sun.org.apache.xml.internal.security.utils.XPathAPI)

Example 64 with DOMException

use of org.w3c.dom.DOMException in project jdk8u_jdk by JetBrains.

the class IIOMetadataNode method setAttributeNode.

public Attr setAttributeNode(Attr newAttr) throws DOMException {
    Element owner = newAttr.getOwnerElement();
    if (owner != null) {
        if (owner == this) {
            return null;
        } else {
            throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR, "Attribute is already in use");
        }
    }
    IIOAttr attr;
    if (newAttr instanceof IIOAttr) {
        attr = (IIOAttr) newAttr;
        attr.setOwnerElement(this);
    } else {
        attr = new IIOAttr(this, newAttr.getName(), newAttr.getValue());
    }
    Attr oldAttr = getAttributeNode(attr.getName());
    if (oldAttr != null) {
        removeAttributeNode(oldAttr);
    }
    attributes.add(attr);
    return oldAttr;
}
Also used : DOMException(org.w3c.dom.DOMException) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Example 65 with DOMException

use of org.w3c.dom.DOMException in project webservices-axiom by apache.

the class TestAllowedChildren method runTest.

protected void runTest() throws Throwable {
    Document doc = dbf.newDocumentBuilder().newDocument();
    doc.appendChild(doc.createComment("some comment"));
    doc.appendChild(doc.createProcessingInstruction("pi", "data"));
    // says that text nodes are not allowed as children of a document.
    try {
        doc.appendChild(doc.createTextNode("    "));
        fail("Expected DOMException");
    } catch (DOMException ex) {
        assertEquals(DOMException.HIERARCHY_REQUEST_ERR, ex.code);
    }
    doc.appendChild(doc.createElement("root1"));
    // Multiple document elements are not allowed
    try {
        doc.appendChild(doc.createElement("root2"));
        fail("Expected DOMException");
    } catch (DOMException ex) {
        assertEquals(DOMException.HIERARCHY_REQUEST_ERR, ex.code);
    }
    // PIs and comments after the document element are allowed
    doc.appendChild(doc.createProcessingInstruction("pi", "data"));
    doc.appendChild(doc.createComment("some comment"));
    // Again, text nodes are not allowed
    try {
        doc.appendChild(doc.createTextNode("    "));
        fail("Expected DOMException");
    } catch (DOMException ex) {
        assertEquals(DOMException.HIERARCHY_REQUEST_ERR, ex.code);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) Document(org.w3c.dom.Document)

Aggregations

DOMException (org.w3c.dom.DOMException)323 Document (org.w3c.dom.Document)165 Element (org.w3c.dom.Element)131 Node (org.w3c.dom.Node)73 NodeList (org.w3c.dom.NodeList)57 DocumentBuilder (javax.xml.parsers.DocumentBuilder)42 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)42 Attr (org.w3c.dom.Attr)40 DOMImplementation (org.w3c.dom.DOMImplementation)37 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)28 FrameworkException (org.structr.common.error.FrameworkException)27 IOException (java.io.IOException)26 NamedNodeMap (org.w3c.dom.NamedNodeMap)25 DocumentType (org.w3c.dom.DocumentType)19 ArrayList (java.util.ArrayList)17 Text (org.w3c.dom.Text)17 DOMNode (org.structr.web.entity.dom.DOMNode)16 XPathExpressionException (javax.xml.xpath.XPathExpressionException)15 SAXException (org.xml.sax.SAXException)13 TransformerException (javax.xml.transform.TransformerException)12