Example usage for org.w3c.dom Node ATTRIBUTE_NODE

List of usage examples for org.w3c.dom Node ATTRIBUTE_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node ATTRIBUTE_NODE.

Prototype

short ATTRIBUTE_NODE

To view the source code for org.w3c.dom Node ATTRIBUTE_NODE.

Click Source Link

Document

The node is an Attr.

Usage

From source file:com.centeractive.ws.builder.soap.XmlUtils.java

public static String getValueForMatch(Node domNode, boolean prettyPrintXml) {
    String stringValue;//from  w  ww  .  j a va 2 s .  com

    if (domNode.getNodeType() == Node.ATTRIBUTE_NODE || domNode.getNodeType() == Node.TEXT_NODE) {
        stringValue = domNode.getNodeValue();
    } else {
        if (domNode.getNodeType() == Node.ELEMENT_NODE) {
            Element elm = (Element) domNode;
            if (elm.getChildNodes().getLength() == 1 && !hasContentAttributes(elm)) {
                stringValue = getElementText(elm);
            } else {
                stringValue = XmlUtils.serialize(domNode, prettyPrintXml);
            }
        } else {
            stringValue = domNode.getNodeValue();
        }
    }

    return stringValue;
}

From source file:jef.tools.XMLUtils.java

/**
 * Element(??)/*ww w  .j a v a 2 s  . c o m*/
 * 
 * @param node
 *            
 * @param tagName
 *            ????nullElement
 * @return ??
 */
public static List<Element> childElements(Node node, String... tagName) {
    if (node == null)
        throw new NullPointerException("the input node can not be null!");
    List<Element> list = new ArrayList<Element>();
    NodeList nds = node.getChildNodes();
    if (tagName.length == 0 || tagName[0] == null) {// ?API
        tagName = null;
    }
    for (int i = 0; i < nds.getLength(); i++) {
        Node child = nds.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) child;
            if (tagName == null || ArrayUtils.contains(tagName, e.getNodeName())) {
                list.add(e);
            }
        } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
        } else if (child.getNodeType() == Node.COMMENT_NODE) {
        } else if (child.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {

        } else if (child.getNodeType() == Node.DOCUMENT_NODE) {

        } else if (child.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        } else if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
        } else if (child.getNodeType() == Node.TEXT_NODE) {
        }
    }
    return list;
}

From source file:de.betterform.xml.xforms.model.submission.Submission.java

private void updateInstanceAndModel(Model referedModel, Document responseInstance) throws XFormsException {
    if (this.targetExpr != null) {
        Node targetNode;//from  w  ww.  j av a2  s . c  o  m
        if (this.instance == null)
            targetNode = XPathUtil.getAsNode(XPathCache.getInstance().evaluate(evalInScopeContext(), 1,
                    this.targetExpr, this.prefixMapping, this.xpathFunctionContext), 1);
        else {
            targetNode = XPathUtil.getAsNode(XPathCache.getInstance().evaluate(
                    referedModel.getInstance(this.instance).getRootContext().getNodeset(), 1, this.targetExpr,
                    this.prefixMapping, this.xpathFunctionContext), 1);
        }
        if (targetNode != null && targetNode.getNodeType() == Node.ELEMENT_NODE) {
            targetNode.getParentNode().replaceChild(
                    targetNode.getOwnerDocument().importNode(responseInstance.getDocumentElement(), true),
                    targetNode);

        } else if (targetNode != null && targetNode.getNodeType() == Node.ATTRIBUTE_NODE) {
            if (LOGGER.isDebugEnabled()) {
                DOMUtil.prettyPrintDOM(responseInstance);
            }
            // targetNode.setContent(responseInstance.getTextContent());
            String attrValue = responseInstance.getDocumentElement().getTextContent();
            targetNode.setNodeValue(attrValue);
        } else {
            throw new XFormsSubmitError("Invalid target", this.getTarget(),
                    XFormsSubmitError.constructInfoObject(this.element, this.container, locationPath,
                            XFormsConstants.TARGET_ERROR, getResourceURI(), 200d, null, "", ""));
        }
    } else if (this.instance != null && referedModel.getInstance(this.instance) == null) {
        this.container.dispatch(referedModel.getId(), XFormsEventNames.BINDING_EXCEPTION);
        // throw new XFormsBindingException("invalid instance id at " + DOMUtil.getCanonicalPath(this.getElement()), this.target, this.instance);
    } else if (this.instance != null) {
        referedModel.getInstance(this.instance).setInstanceDocument(responseInstance);
    } else {
        referedModel.getInstance(getInstanceId()).setInstanceDocument(responseInstance);
    }

    // perform rebuild, recalculate, revalidate, and refresh
    referedModel.rebuild();
    referedModel.recalculate();
    referedModel.revalidate();
}

From source file:de.betterform.xml.xforms.model.submission.Submission.java

/**
 * Performs replace processing according to section 11.1, para 5.
 *///  w w w  . j  av  a2 s  .co  m
protected void submitReplaceText(Map response) throws XFormsException {

    if (getLogger().isDebugEnabled()) {
        getLogger().debug(this + " submit: replacing text");
    }
    Node targetNode;
    if (this.targetExpr != null) {
        targetNode = XPathUtil
                .getAsNode(
                        XPathCache.getInstance()
                                .evaluate(
                                        this.instance == null ? evalInScopeContext()
                                                : this.model.getInstance(this.instance).getRootContext()
                                                        .getNodeset(),
                                        1, this.targetExpr, this.prefixMapping, this.xpathFunctionContext),
                        1);
    } else if (this.instance == null) {
        targetNode = this.model.getInstance(getInstanceId()).getInstanceDocument().getDocumentElement();
    } else {
        targetNode = this.model.getInstance(this.instance).getInstanceDocument().getDocumentElement();
    }
    final InputStream responseStream = (InputStream) response.get(XFormsProcessor.SUBMISSION_RESPONSE_STREAM);

    StringBuilder text = new StringBuilder(512);
    try {
        String contentType = (String) response.get("Content-Type");
        String encoding = "UTF-8";

        if (contentType != null) {
            final String[] contTypeEntries = contentType.split(", ?");

            for (int i = 0; i < contTypeEntries.length; i++) {
                if (contTypeEntries[i].startsWith("charset=")) {
                    encoding = contTypeEntries[i].substring(8);
                }
            }
        }
        byte[] buffer = new byte[512];
        int bytesRead;
        while ((bytesRead = responseStream.read(buffer)) > 0) {
            text.append(new String(buffer, 0, bytesRead, encoding));
        }

        responseStream.close();
    } catch (Exception e) {
        // todo: check for response media type (needs submission response
        // refactoring) in order to dispatch xforms-link-exception
        throw new XFormsSubmitError("instance parsing failed", e, this.getTarget(),
                XFormsSubmitError.constructInfoObject(this.element, this.container, locationPath,
                        XFormsConstants.PARSE_ERROR, getResourceURI(), 200d, null, "", ""));
    }

    if (targetNode == null) {
        throw new XFormsSubmitError("Invalid target", this.getTarget(),
                XFormsSubmitError.constructInfoObject(this.element, this.container, locationPath,
                        XFormsConstants.TARGET_ERROR, getResourceURI(), 200d, null, "", ""));
    }

    else if (targetNode.getNodeType() == Node.ELEMENT_NODE) {
        while (targetNode.getFirstChild() != null) {
            targetNode.removeChild(targetNode.getFirstChild());
        }

        targetNode.appendChild(targetNode.getOwnerDocument().createTextNode(text.toString()));
    } else if (targetNode.getNodeType() == Node.ATTRIBUTE_NODE) {
        targetNode.setNodeValue(text.toString());
    } else {
        LOGGER.warn("Don't know how to handle targetNode '" + targetNode.getLocalName()
                + "', node is neither an element nor an attribute Node");
    }

    // perform rebuild, recalculate, revalidate, and refresh
    this.model.rebuild();
    this.model.recalculate();
    this.model.revalidate();
    this.container.refresh();

    // deferred update behaviour
    UpdateHandler updateHandler = this.model.getUpdateHandler();
    if (updateHandler != null) {
        updateHandler.doRebuild(false);
        updateHandler.doRecalculate(false);
        updateHandler.doRevalidate(false);
        updateHandler.doRefresh(false);
    }

    // dispatch xforms-submit-done
    this.container.dispatch(this.target, XFormsEventNames.SUBMIT_DONE, constructEventInfo(response));
}

From source file:com.twinsoft.convertigo.beans.core.Sequence.java

private static void append(Element parent, Node node) {
    if (parent != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            parent.appendChild(node);// w  w  w.ja  va  2  s .co  m
        } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            parent.setAttributeNode((Attr) node);
        }
    }
}

From source file:jef.tools.XMLUtils.java

/**
 * DOM?/* w w w  .  j a va2s .  com*/
 * 
 * @param node
 *            DOM
 * @param charset
 *            ??xmlstring?unicode string
 *            ???.
 * @param xmlHeader
 *            ???XML<?xml ....>
 * @return ??XML
 */
public static String toString(Node node, String charset, Boolean xmlHeader) {
    if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        return node.getNodeValue();
    }
    StringWriter sw = new StringWriter(4096);
    StreamResult sr = new StreamResult(sw);
    try {
        output(node, sr, charset, 4, xmlHeader);
    } catch (IOException e) {
        LogUtil.exception(e);
    }
    return sw.toString();
}

From source file:de.escidoc.core.test.EscidocTestBase.java

/**
 * Deletes the node selected by the given XPath from the provided node.
 * /*  w w  w  . j a  v a 2  s .c  o  m*/
 * @param node
 *            The Node to delete the selected nodes from.
 * @param xPath
 *            The XPath selecting the sub nodes in the provided node.
 * @return returns the provided <code>Node</code> object. This <code>Node</code> object may be changed.
 * @throws Exception
 *             Thrown if anything fails.
 */
public static Node deleteNodes(final Node node, final String xPath) throws Exception {

    NodeList nodes = selectNodeList(node, xPath);
    if (nodes == null || nodes.getLength() == 0) {
        return node;
    }

    for (int i = 0; i < nodes.getLength(); ++i) {
        Node delete = nodes.item(i);
        if (delete.getNodeType() == Node.ATTRIBUTE_NODE) {
            final int index = xPath.lastIndexOf('/');
            String attribute = delete.getNodeName();
            attribute = attribute.substring(attribute.lastIndexOf(':') + 1);
            String elementXpath = xPath.substring(0, index);
            elementXpath += "[@" + attribute + "=\"" + delete.getTextContent().trim() + "\"]";
            Node parent = selectSingleNode(node, elementXpath);
            if (parent.hasAttributes()) {
                parent.getAttributes().removeNamedItem(delete.getNodeName());
            }
        } else {
            delete.getParentNode().removeChild(delete);
        }
    }

    return node;
}

From source file:nl.nn.adapterframework.util.XmlUtils.java

public static Collection<String> evaluateXPathNodeSet(String input, String xpathExpr)
        throws DomBuilderException, XPathExpressionException {
    String msg = XmlUtils.removeNamespaces(input);

    Collection<String> c = new LinkedList<String>();
    Document doc = buildDomDocument(msg, true, true);
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression xPathExpression = xPath.compile(xpathExpr);
    Object result = xPathExpression.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        if (nodes.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
            c.add(nodes.item(i).getNodeValue());
        } else {/*  w  ww .  j a v a  2 s. c o  m*/
            //c.add(nodes.item(i).getTextContent());
            c.add(nodes.item(i).getFirstChild().getNodeValue());
        }
    }
    if (c != null && c.size() > 0) {
        return c;
    }
    return null;
}

From source file:org.adl.parsers.dom.DOMTreeUtility.java

/**
 * This method determins if a node in the DOM Tree <code>(iNode)</code> is
 * the node we are looking for.  This is done by comparing the node's
 * local name and namespace with a given node name <code>(iNodeName)</code>
 * and namespace <code>(iNamespace)</code>.
 *
 * @param iNode The Node we are trying to determine if it is the correct
 *              node/*from  w w w . jav  a  2  s  .  c  om*/
 * @param iNodeName The name of the node we are looking for.
 * @param iNamespace The namespace of the node we are looking for.
 *
 * @return A boolean value indicating whether or not this is the
 *         correct node we are looking for
 */
public static boolean isAppropriateElement(Node iNode, String iNodeName, String iNamespace) {
    log.debug("DOMTreeUtility isAppropriateElement()");
    log.debug("Input Parent Node: " + iNode.getLocalName());
    log.debug("Input Node being searched for: " + iNodeName);
    log.debug("Input Namespace of node being searched for: " + iNamespace);

    boolean result = false;

    if (iNode.getNodeType() == Node.ATTRIBUTE_NODE) {
        if (iNode.getNamespaceURI() == null) {
            // Attribute has been passed in and its namepsace is null, get the
            // attributes parent's namespace
            String parentsNamespace = ((Attr) iNode).getOwnerElement().getNamespaceURI();
            if ((iNode.getLocalName().equals(iNodeName)) && (parentsNamespace.equals(iNamespace))) {
                result = true;
            }
        } else {
            if ((iNode.getLocalName().equals(iNodeName)) && (iNode.getNamespaceURI().equals(iNamespace))) {
                result = true;
            }
        }
    } else if ((iNode.getLocalName().equals(iNodeName)) && (iNode.getNamespaceURI().equals(iNamespace))) {
        result = true;
    }

    return result;
}

From source file:org.ala.documentmapper.XMLDocumentMapper.java

/**
 * Retrieve a Node value and append to the supplied StringBuffer.
 * //from  w  ww.  ja v a 2 s . c  o m
 * @param sb
 * @param node
 */
private void getNodeValue(StringBuilder sb, Node node) {
    String value = node.getNodeValue();
    if (value != null && value.length() > 0) {
        sb.append(value);
    }

    // It seems Attribute Nodes - do this implicitly.
    if (node.getNodeType() != Node.ATTRIBUTE_NODE) {
        NodeList nodes = node.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            sb.append(" ");
            getNodeValue(sb, nodes.item(i));
        }
    }
}