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.dinochiesa.edgecallouts.EditXmlNode.java

private void append(NodeList nodes, Node newNode, short newNodeType) {
    Node currentNode = nodes.item(0);
    switch (newNodeType) {
    case Node.ATTRIBUTE_NODE:
        Element parent = ((Attr) currentNode).getOwnerElement();
        parent.setAttributeNode((Attr) newNode);
        break;//w w  w. jav a  2 s  . c o m
    case Node.ELEMENT_NODE:
        currentNode.appendChild(newNode);
        break;
    case Node.TEXT_NODE:
        if (currentNode.getNodeType() != Node.TEXT_NODE) {
            throw new IllegalStateException("wrong source node type.");
        }
        String v = currentNode.getNodeValue();
        currentNode.setNodeValue(v + newNode.getNodeValue());
        break;
    }
}

From source file:com.espertech.esper.event.xml.XPathPropertyGetter.java

private Object castToArray(Object result) {
    if (!(result instanceof NodeList)) {
        return null;
    }/* ww  w  . ja v  a  2  s .c o m*/

    NodeList nodeList = (NodeList) result;
    Object array = Array.newInstance(optionalCastToType, nodeList.getLength());

    for (int i = 0; i < nodeList.getLength(); i++) {
        Object arrayItem = null;
        try {
            Node item = nodeList.item(i);
            String textContent;
            if ((item.getNodeType() == Node.ATTRIBUTE_NODE) || (item.getNodeType() == Node.ELEMENT_NODE)) {
                textContent = nodeList.item(i).getTextContent();
            } else {
                continue;
            }

            arrayItem = simpleTypeParser.parse(textContent);
        } catch (Exception ex) {
            if (log.isInfoEnabled()) {
                log.info("Parse error for text content " + nodeList.item(i).getTextContent()
                        + " for expression " + expression);
            }
        }
        Array.set(array, i, arrayItem);
    }

    return array;
}

From source file:com.jaspersoft.studio.data.xml.XMLDataAdapterDescriptor.java

private void addNewField(String nodeName, LinkedHashMap<String, JRDesignField> fieldsMap, Node item,
        String prefix) {//from w  w w.  jav  a 2s  .c  o  m
    JRDesignField f = new JRDesignField();
    String description = "";
    f.setName(ModelUtils.getNameForField(new ArrayList<JRDesignField>(fieldsMap.values()), nodeName));
    f.setValueClass(String.class);
    if (item.getNodeType() == Node.ATTRIBUTE_NODE) {
        description = prefix + "@" + item.getNodeName();
        f.setDescription(description); //$NON-NLS-1$
    } else {
        description = prefix + item.getNodeName();
        f.setDescription(description);
    }
    // Let's consider the description indicating the XPath query
    // as unique and therefore as map key.
    fieldsMap.put(description, f);
}

From source file:com.dinochiesa.edgecallouts.EditXmlNode.java

private void replace(NodeList nodes, Node newNode, short newNodeType) {
    Node currentNode = nodes.item(0);
    switch (newNodeType) {
    case Node.ATTRIBUTE_NODE:
        Element parent = ((Attr) currentNode).getOwnerElement();
        parent.removeAttributeNode((Attr) currentNode);
        parent.setAttributeNode((Attr) newNode);
        break;//from  www. j  ava 2s  . c  om
    case Node.ELEMENT_NODE:
        currentNode.getParentNode().replaceChild(newNode, currentNode);
        break;
    case Node.TEXT_NODE:
        currentNode.setNodeValue(newNode.getNodeValue());
        break;
    }
}

From source file:org.dozer.eclipse.plugin.sourcepage.hyperlink.DozerClassHyperlinkDetector.java

@SuppressWarnings("restriction")
protected final IRegion getHyperlinkRegion(Node node) {
    if (node != null) {
        switch (node.getNodeType()) {
        case Node.DOCUMENT_TYPE_NODE:
        case Node.TEXT_NODE:
            IDOMNode docNode = (IDOMNode) node;
            return new Region(docNode.getStartOffset(), docNode.getEndOffset() - docNode.getStartOffset());

        case Node.ELEMENT_NODE:
            IDOMElement element = (IDOMElement) node;
            int endOffset;
            if (element.hasEndTag() && element.isClosed()) {
                endOffset = element.getStartEndOffset();
            } else {
                endOffset = element.getEndOffset();
            }//from   www .  jav a2 s.co  m
            return new Region(element.getStartOffset(), endOffset - element.getStartOffset());

        case Node.ATTRIBUTE_NODE:
            IDOMAttr att = (IDOMAttr) node;
            // do not include quotes in attribute value region
            int regOffset = att.getValueRegionStartOffset();
            int regLength = att.getValueRegionText().length();
            String attValue = att.getValueRegionText();
            if (StringUtils.isQuoted(attValue)) {
                regOffset += 1;
                regLength = regLength - 2;
            }
            return new Region(regOffset, regLength);
        }
    }
    return null;
}

From source file:com.dinochiesa.edgecallouts.EditXmlNode.java

private void execute0(Document document, MessageContext msgCtxt) throws Exception {
    String xpath = getXpath(msgCtxt);
    XPathEvaluator xpe = getXpe(msgCtxt);
    NodeList nodes = (NodeList) xpe.evaluate(xpath, document, XPathConstants.NODESET);
    validate(nodes);/*from  w  ww.j a  va 2 s  .com*/
    EditAction action = getAction(msgCtxt);
    if (action == EditAction.Remove) {
        remove(nodes);
        return;
    }

    short newNodeType = getNewNodeType(msgCtxt);
    String text = getNewNodeText(msgCtxt);
    Node newNode = null;
    switch (newNodeType) {
    case Node.ELEMENT_NODE:
        // Create a duplicate node and transfer ownership of the
        // new node into the destination document.
        Document temp = XmlUtils.parseXml(text);
        newNode = document.importNode(temp.getDocumentElement(), true);
        break;
    case Node.ATTRIBUTE_NODE:
        if (text.indexOf("=") < 1) {
            throw new IllegalStateException("attribute spec must be name=value");
        }
        String[] parts = text.split("=", 2);
        if (parts.length != 2)
            throw new IllegalStateException("attribute spec must be name=value");
        Attr attr = document.createAttribute(parts[0]);
        attr.setValue(parts[1]);
        newNode = attr;
        break;
    case Node.TEXT_NODE:
        newNode = document.createTextNode(text);
        break;
    }
    switch (action) {
    case InsertBefore:
        insertBefore(nodes, newNode, newNodeType);
        break;
    case Append:
        append(nodes, newNode, newNodeType);
        break;
    case Replace:
        replace(nodes, newNode, newNodeType);
        break;
    }
}

From source file:de.betterform.xml.xforms.model.Instance.java

/**
 * Inserts the specified node./*from w w w. j  a  v a2 s.  com*/
 *
 * @param parentNode the path pointing to the origin node.
 * @param beforeNode the path pointing to the node before which a clone of the
 *               origin node will be inserted.
 * @return 
 */
public Node insertNode(Node parentNode, Node originNode, Node beforeNode) throws XFormsException {
    // insert a deep clone of 'origin' node before 'before' node. if
    // 'before' node is null, the clone will be appended to 'parent' node.
    ModelItem miOrigin = getModelItem(originNode);
    Node insertedNode;
    if (originNode.getNodeType() == Node.ATTRIBUTE_NODE) {
        insertedNode = this.instanceDocument.importNode(originNode, true);
        ((Element) parentNode).setAttributeNode((Attr) insertedNode);
    } else {
        insertedNode = parentNode.insertBefore(this.instanceDocument.importNode(originNode, true), beforeNode);
    }
    String canonPath = DOMUtil.getCanonicalPath(insertedNode);

    ModelItem insertedModelItem = getModelItem(insertedNode);
    insertedModelItem.getDeclarationView().setDatatype(miOrigin.getDeclarationView().getDatatype());
    insertedModelItem.getDeclarationView().setConstraints(miOrigin.getDeclarationView().getConstraints());

    // get canonical path for inserted node
    String canonicalPath;
    if (beforeNode != null || originNode.getNodeType() == Node.ATTRIBUTE_NODE) {
        canonicalPath = BindingResolver.getCanonicalPath(insertedNode);
    } else {
        Node lastChild = ((DocumentTraversal) instanceDocument)
                .createTreeWalker(parentNode, NodeFilter.SHOW_ALL, null, false).lastChild();
        canonicalPath = BindingResolver.getCanonicalPath(lastChild);
    }

    String[] canonicalParts = XPathUtil.getNodesetAndPredicates(canonicalPath);

    if (originNode.hasChildNodes()) {
        setDatatypeOnChilds(originNode, insertedNode);
    }

    // dispatch internal betterform event (for instant repeat updating)
    HashMap map = new HashMap();
    map.put("nodeset", canonicalParts[0]);
    map.put("position", canonicalParts.length > 1 ? canonicalParts[1] : "1");
    map.put("canonPath", canonPath);
    this.container.dispatch(this.target, BetterFormEventNames.NODE_INSERTED, map);

    if (getLogger().isDebugEnabled()) {
        getLogger().debug(
                this + " insert node: instance data after manipulation" + toString(this.instanceDocument));
    }

    return insertedNode;
}

From source file:cc.siara.csv_ml_demo.MultiLevelCSVSwingDemo.java

/**
 * Evaluates given XPath from Input box against Document generated by
 * parsing csv_ml in input box and sets value or node list to output box.
 *///from  ww w. ja  v a 2 s  . c o  m
private void processXPath() {
    XPath xpath = XPathFactory.newInstance().newXPath();
    Document doc = parseInputToDOM();
    if (doc == null)
        return;
    StringBuffer out_str = new StringBuffer();
    try {
        XPathExpression expr = xpath.compile(tfXPath.getText());
        try {
            Document outDoc = Util.parseXMLToDOM("<output></output>");
            Element rootElement = outDoc.getDocumentElement();
            NodeList ret = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i < ret.getLength(); i++) {
                Object o = ret.item(i);
                if (o instanceof String) {
                    out_str.append(o);
                } else if (o instanceof Node) {
                    Node n = (Node) o;
                    short nt = n.getNodeType();
                    switch (nt) {
                    case Node.TEXT_NODE:
                    case Node.ATTRIBUTE_NODE:
                    case Node.CDATA_SECTION_NODE: // Only one value gets
                                                  // evaluated?
                        if (out_str.length() > 0)
                            out_str.append(',');
                        if (nt == Node.ATTRIBUTE_NODE)
                            out_str.append(n.getNodeValue());
                        else
                            out_str.append(n.getTextContent());
                        break;
                    case Node.ELEMENT_NODE:
                        rootElement.appendChild(outDoc.importNode(n, true));
                        break;
                    }
                }
            }
            if (out_str.length() > 0) {
                rootElement.setTextContent(out_str.toString());
                out_str.setLength(0);
            }
            out_str.append(Util.docToString(outDoc, true));
        } catch (Exception e) {
            // Thrown most likely because the given XPath evaluates to a
            // string
            out_str.append(expr.evaluate(doc));
        }
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    taOutput.setText(out_str.toString());
    tfOutputSize.setText(String.valueOf(out_str.length()));
}

From source file:de.betterform.xml.xforms.model.Instance.java

/**
 * returns true if current Node's modelitem is readonly or any of it's ancestors is readonly
 * @param n/*  w  w w . j  a va 2s. c  om*/
 * @return true if any of the ancestors or the node itself is readonly false otherwise
 */
private boolean isReadonly(Node n) {
    if (n.getNodeType() == Node.DOCUMENT_NODE)
        return false;
    if (getModelItem(n).isReadonly()) {
        return true;
    } else if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
        return isReadonly(((Attr) n).getOwnerElement());
    } else {
        return isReadonly(n.getParentNode());
    }
}