Search in sources :

Example 91 with DOMException

use of org.w3c.dom.DOMException in project oozie by apache.

the class XConfiguration method processNodes.

// Cannibalized from Hadoop <code>Configuration.loadResource()</code>.
private void processNodes(Element root) throws IOException {
    try {
        NodeList props = root.getChildNodes();
        for (int i = 0; i < props.getLength(); i++) {
            Node propNode = props.item(i);
            if (!(propNode instanceof Element)) {
                continue;
            }
            Element prop = (Element) propNode;
            if (prop.getLocalName().equals("configuration")) {
                processNodes(prop);
                continue;
            }
            if (!"property".equals(prop.getLocalName())) {
                throw new IOException("bad conf file: element not <property>");
            }
            NodeList fields = prop.getChildNodes();
            String attr = null;
            String value = null;
            for (int j = 0; j < fields.getLength(); j++) {
                Node fieldNode = fields.item(j);
                if (!(fieldNode instanceof Element)) {
                    continue;
                }
                Element field = (Element) fieldNode;
                if ("name".equals(field.getLocalName()) && field.hasChildNodes()) {
                    attr = ((Text) field.getFirstChild()).getData().trim();
                }
                if ("value".equals(field.getLocalName()) && field.hasChildNodes()) {
                    value = ((Text) field.getFirstChild()).getData();
                }
            }
            if (attr != null && value != null) {
                set(attr, value);
            }
        }
    } catch (DOMException e) {
        throw new IOException(e);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) IOException(java.io.IOException)

Example 92 with DOMException

use of org.w3c.dom.DOMException in project photon-model by vmware.

the class VcSessionHandler method handleMessage.

@Override
public boolean handleMessage(SOAPMessageContext smc) {
    if (isOutgoingMessage(smc)) {
        try {
            SOAPHeader header = getSOAPHeader(smc);
            SOAPElement vcsessionHeader = header.addChildElement(new javax.xml.namespace.QName("#", "vcSessionCookie"));
            vcsessionHeader.setValue(this.vcSessionCookie);
        } catch (DOMException e) {
            throw new RuntimeException(e);
        } catch (SOAPException e) {
            throw new RuntimeException(e);
        }
    }
    return true;
}
Also used : DOMException(org.w3c.dom.DOMException) SOAPException(javax.xml.soap.SOAPException) SOAPElement(javax.xml.soap.SOAPElement) QName(javax.xml.namespace.QName) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 93 with DOMException

use of org.w3c.dom.DOMException in project cerberus-source by cerberustesting.

the class XmlUtil method fromNode.

/**
 * Returns a {@link Document} from the given {@link Node}
 *
 * @param node to transform to {@link Document}
 * @return a {@link Document} from the given {@link Node}
 * @throws XmlUtilException if an error occurs
 */
public static Document fromNode(Node node) throws XmlUtilException {
    try {
        Document document = XmlUtil.newDocument();
        document.appendChild(document.adoptNode(node.cloneNode(true)));
        return document;
    } catch (DOMException e) {
        Log.warn("Unable to create document from node " + node, e);
        return null;
    }
}
Also used : DOMException(org.w3c.dom.DOMException) Document(org.w3c.dom.Document)

Example 94 with DOMException

use of org.w3c.dom.DOMException in project ant by apache.

the class DOMUtil method importNode.

/**
 * Simple tree walker that will clone recursively a node. This is to
 * avoid using parser-specific API such as Sun's <tt>changeNodeOwner</tt>
 * when we are dealing with DOM L1 implementations since <tt>cloneNode(boolean)</tt>
 * will not change the owner document.
 * <tt>changeNodeOwner</tt> is much faster and avoid the costly cloning process.
 * <tt>importNode</tt> is in the DOM L2 interface.
 * @param   parent  the node parent to which we should do the import to.
 * @param   child   the node to clone recursively. Its clone will be
 *              appended to <tt>parent</tt>.
 * @return  the cloned node that is appended to <tt>parent</tt>
 */
public static Node importNode(Node parent, Node child) {
    final Document doc = parent.getOwnerDocument();
    Node copy;
    switch(child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
            copy = doc.createCDATASection(((CDATASection) child).getData());
            break;
        case Node.COMMENT_NODE:
            copy = doc.createComment(((Comment) child).getData());
            break;
        case Node.DOCUMENT_FRAGMENT_NODE:
            copy = doc.createDocumentFragment();
            break;
        case Node.ELEMENT_NODE:
            final Element elem = doc.createElement(((Element) child).getTagName());
            copy = elem;
            final NamedNodeMap attributes = child.getAttributes();
            if (attributes != null) {
                final int size = attributes.getLength();
                for (int i = 0; i < size; i++) {
                    final Attr attr = (Attr) attributes.item(i);
                    elem.setAttribute(attr.getName(), attr.getValue());
                }
            }
            break;
        case Node.ENTITY_REFERENCE_NODE:
            copy = doc.createEntityReference(child.getNodeName());
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            final ProcessingInstruction pi = (ProcessingInstruction) child;
            copy = doc.createProcessingInstruction(pi.getTarget(), pi.getData());
            break;
        case Node.TEXT_NODE:
            copy = doc.createTextNode(((Text) child).getData());
            break;
        default:
            // this should never happen
            throw new IllegalStateException("Invalid node type: " + child.getNodeType());
    }
    // and we are iterating recursively over its children.
    try {
        final NodeList children = child.getChildNodes();
        if (children != null) {
            final int size = children.getLength();
            for (int i = 0; i < size; i++) {
                final Node newChild = children.item(i);
                if (newChild != null) {
                    importNode(copy, newChild);
                }
            }
        }
    } catch (DOMException ignored) {
    // Ignore
    }
    // bingo append it. (this should normally not be done here)
    parent.appendChild(copy);
    return copy;
}
Also used : Comment(org.w3c.dom.Comment) NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) DOMException(org.w3c.dom.DOMException) CDATASection(org.w3c.dom.CDATASection) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 95 with DOMException

use of org.w3c.dom.DOMException in project santuario-java by apache.

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.getFirstChild();
        if (xpathnode == null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Text must be in ds:Xpath");
        }
        String str = XMLUtils.getStrFromNode(xpathnode);
        input.setNeedsToBeExpanded(needsCircumvent(str));
        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(ex);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) XPathFactory(org.apache.xml.security.utils.XPathFactory) TransformationException(org.apache.xml.security.transforms.TransformationException) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) XPathAPI(org.apache.xml.security.utils.XPathAPI)

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