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:org.apache.xml.security.utils.XMLUtils.java

/**
 * This method returns the first non-null owner document of the Nodes in this Set.
 * This method is necessary because it <I>always</I> returns a
 * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE>
 * if the {@link Node} is a {@link Document}.
 *
 * @param xpathNodeSet/*from w ww  .ja  v a  2 s. c om*/
 * @return the owner document 
 */
public static Document getOwnerDocument(Set<Node> xpathNodeSet) {
    NullPointerException npe = null;
    for (Node node : xpathNodeSet) {
        int nodeType = node.getNodeType();
        if (nodeType == Node.DOCUMENT_NODE) {
            return (Document) node;
        }
        try {
            if (nodeType == Node.ATTRIBUTE_NODE) {
                return ((Attr) node).getOwnerElement().getOwnerDocument();
            }
            return node.getOwnerDocument();
        } catch (NullPointerException e) {
            npe = e;
        }
    }

    throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0") + " Original message was \""
            + (npe == null ? "" : npe.getMessage()) + "\"");
}

From source file:org.chiba.xml.xforms.core.Instance.java

private ModelItem createModelItem(NodeImpl node) {
    String id = this.model.generateModelItemId();
    ModelItem modelItem = node.getNodeType() == Node.ELEMENT_NODE ? new XercesElementImpl(id)
            : new XercesNodeImpl(id);
    modelItem.setNode(node);//www  . ja v  a  2  s.c  om

    NodeImpl parentNode = (NodeImpl) (node.getNodeType() == Node.ATTRIBUTE_NODE
            ? ((Attr) node).getOwnerElement()
            : node.getParentNode());
    if (parentNode != null) {
        ModelItem parentItem = (ModelItem) parentNode.getUserData();
        if (parentItem == null) {
            parentItem = createModelItem(parentNode);
        }

        modelItem.setParent(parentItem);
    }

    node.setUserData(modelItem);
    return modelItem;
}

From source file:org.dita.dost.util.XMLUtils.java

/**
 * Add or set attribute. Convenience method for {@link #addOrSetAttribute(AttributesImpl, String, String, String, String, String)}.
 * //from   w ww  .ja v a2s .c om
 * @param atts attributes
 * @param att attribute node
 */
public static void addOrSetAttribute(final AttributesImpl atts, final Node att) {
    if (att.getNodeType() != Node.ATTRIBUTE_NODE) {
        throw new IllegalArgumentException();
    }
    final Attr a = (Attr) att;
    String localName = a.getLocalName();
    if (localName == null) {
        localName = a.getName();
        final int i = localName.indexOf(':');
        if (i != -1) {
            localName = localName.substring(i + 1);
        }
    }
    addOrSetAttribute(atts, a.getNamespaceURI() != null ? a.getNamespaceURI() : NULL_NS_URI, localName,
            a.getName() != null ? a.getName() : localName, a.isId() ? "ID" : "CDATA", a.getValue());
}

From source file:org.ecoinformatics.seek.ecogrid.EcogridWriter.java

/**
 * Set a new value for an attribute node.
 * /*  w w w.  j  a v  a  2 s . c  o m*/
 * @param list
 * @param newValue
 */
private void setNewValueForAttribute(NodeList list, String newValue) {
    if (list != null && list.getLength() > 0) {
        Node cn = list.item(0);
        if (cn != null && cn.getNodeType() == Node.ATTRIBUTE_NODE) {
            //System.out.println("Set new value "+newValue +" for attribute"+cn.getNodeName());
            cn.setNodeValue(newValue);
        }
    }
}

From source file:org.exist.dom.ElementImpl.java

private NodeList checkForAttributes(Txn transaction, NodeList nodes) throws DOMException {
    NodeListImpl attribs = null;//from  w w w  .j av a  2s .c om
    NodeListImpl rest = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        final Node next = nodes.item(i);
        if (next.getNodeType() == Node.ATTRIBUTE_NODE) {
            if (!next.getNodeName().startsWith("xmlns")) {
                if (attribs == null) {
                    attribs = new NodeListImpl();
                }
                attribs.add(next);
            }
        } else if (attribs != null) {
            if (rest == null) {
                rest = new NodeListImpl();
            }
            rest.add(next);
        }
    }
    if (attribs == null) {
        return nodes;
    }
    appendAttributes(transaction, attribs);
    return rest;
}

From source file:org.exist.dom.ElementImpl.java

private Node appendChild(Txn transaction, NodeId newNodeId, NodeImplRef last, NodePath lastPath, Node child,
        StreamListener listener) throws DOMException {
    if (last == null || last.getNode() == null)
    //TODO : same test as above ? -pb
    {/*from  w w w. j  av a2 s .c om*/
        throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "invalid node");
    }
    final DocumentImpl owner = (DocumentImpl) getOwnerDocument();
    DBBroker broker = null;
    try {
        broker = ownerDocument.getBrokerPool().get(null);
        switch (child.getNodeType()) {
        case Node.DOCUMENT_FRAGMENT_NODE:
            appendChildren(transaction, newNodeId, null, last, lastPath, child.getChildNodes(), listener);
            return null; // TODO: implement document fragments so
        //we can return all newly appended children
        case Node.ELEMENT_NODE:
            // create new element
            final ElementImpl elem = new ElementImpl(
                    new QName(child.getLocalName() == null ? child.getNodeName() : child.getLocalName(),
                            child.getNamespaceURI(), child.getPrefix()),
                    broker.getBrokerPool().getSymbols());
            elem.setNodeId(newNodeId);
            elem.setOwnerDocument(owner);
            final NodeListImpl ch = new NodeListImpl();
            final NamedNodeMap attribs = child.getAttributes();
            int numActualAttribs = 0;
            for (int i = 0; i < attribs.getLength(); i++) {
                final Attr attr = (Attr) attribs.item(i);
                if (!attr.getNodeName().startsWith("xmlns")) {
                    ch.add(attr);
                    numActualAttribs++;
                } else {
                    final String xmlnsDecl = attr.getNodeName();
                    final String prefix = xmlnsDecl.length() == 5 ? "" : xmlnsDecl.substring(6);
                    elem.addNamespaceMapping(prefix, attr.getNodeValue());
                }
            }
            final NodeList cl = child.getChildNodes();
            for (int i = 0; i < cl.getLength(); i++) {
                final Node n = cl.item(i);
                if (n.getNodeType() != Node.ATTRIBUTE_NODE) {
                    ch.add(n);
                }
            }
            elem.setChildCount(ch.getLength());
            if (numActualAttribs != (short) numActualAttribs) {
                throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "Too many attributes");
            }
            elem.setAttributes((short) numActualAttribs);
            lastPath.addComponent(elem.getQName());
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), elem);
            broker.indexNode(transaction, elem, lastPath);
            broker.getIndexController().indexNode(transaction, elem, lastPath, listener);
            elem.setChildCount(0);
            last.setNode(elem);
            //process child nodes
            elem.appendChildren(transaction, newNodeId.newChild(), null, last, lastPath, ch, listener);
            broker.endElement(elem, lastPath, null);
            broker.getIndexController().endElement(transaction, elem, lastPath, listener);
            lastPath.removeLastComponent();
            return elem;
        case Node.TEXT_NODE:
            final TextImpl text = new TextImpl(newNodeId, ((Text) child).getData());
            text.setOwnerDocument(owner);
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), text);
            broker.indexNode(transaction, text, lastPath);
            broker.getIndexController().indexNode(transaction, text, lastPath, listener);
            last.setNode(text);
            return text;
        case Node.CDATA_SECTION_NODE:
            final CDATASectionImpl cdata = new CDATASectionImpl(newNodeId, ((CDATASection) child).getData());
            cdata.setOwnerDocument(owner);
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), cdata);
            broker.indexNode(transaction, cdata, lastPath);
            last.setNode(cdata);
            return cdata;
        case Node.ATTRIBUTE_NODE:
            final Attr attr = (Attr) child;
            final String ns = attr.getNamespaceURI();
            final String prefix = (Namespaces.XML_NS.equals(ns) ? "xml" : attr.getPrefix());
            String name = attr.getLocalName();
            if (name == null) {
                name = attr.getName();
            }
            final QName attrName = new QName(name, ns, prefix);
            final AttrImpl attrib = new AttrImpl(attrName, attr.getValue(),
                    broker.getBrokerPool().getSymbols());
            attrib.setNodeId(newNodeId);
            attrib.setOwnerDocument(owner);
            if (ns != null && attrName.compareTo(Namespaces.XML_ID_QNAME) == Constants.EQUAL) {
                // an xml:id attribute. Normalize the attribute and set its type to ID
                attrib.setValue(StringValue.trimWhitespace(StringValue.collapseWhitespace(attrib.getValue())));
                attrib.setType(AttrImpl.ID);
            } else {
                attrName.setNameType(ElementValue.ATTRIBUTE);
            }
            broker.insertNodeAfter(transaction, last.getNode(), attrib);
            broker.indexNode(transaction, attrib, lastPath);
            broker.getIndexController().indexNode(transaction, attrib, lastPath, listener);
            last.setNode(attrib);
            return attrib;
        case Node.COMMENT_NODE:
            final CommentImpl comment = new CommentImpl(((Comment) child).getData());
            comment.setNodeId(newNodeId);
            comment.setOwnerDocument(owner);
            // insert the node
            broker.insertNodeAfter(transaction, last.getNode(), comment);
            broker.indexNode(transaction, comment, lastPath);
            last.setNode(comment);
            return comment;
        case Node.PROCESSING_INSTRUCTION_NODE:
            final ProcessingInstructionImpl pi = new ProcessingInstructionImpl(newNodeId,
                    ((ProcessingInstruction) child).getTarget(), ((ProcessingInstruction) child).getData());
            pi.setOwnerDocument(owner);
            //insert the node
            broker.insertNodeAfter(transaction, last.getNode(), pi);
            broker.indexNode(transaction, pi, lastPath);
            last.setNode(pi);
            return pi;
        default:
            throw new DOMException(DOMException.INVALID_MODIFICATION_ERR,
                    "Unknown node type: " + child.getNodeType() + " " + child.getNodeName());
        }
    } catch (final EXistException e) {
        LOG.warn("Exception while appending node: " + e.getMessage(), e);
    } finally {
        if (broker != null)
            broker.release();
    }
    return null;
}

From source file:org.exist.dom.ElementImpl.java

/**
 * @see org.w3c.dom.Node#getAttributes()
 *//*from   w ww .ja v  a  2 s .c  o m*/
@Override
public NamedNodeMap getAttributes() {
    final NamedNodeMapImpl map = new NamedNodeMapImpl();
    if (getAttributesCount() > 0) {
        DBBroker broker = null;
        try {
            broker = ownerDocument.getBrokerPool().get(null);
            final Iterator<StoredNode> iterator = broker.getNodeIterator(this);
            iterator.next();
            final int ccount = getChildCount();
            for (int i = 0; i < ccount; i++) {
                final StoredNode next = iterator.next();
                if (next.getNodeType() != Node.ATTRIBUTE_NODE) {
                    break;
                }
                map.setNamedItem(next);
            }
        } catch (final EXistException e) {
            LOG.warn("Exception while retrieving attributes: " + e.getMessage());
        } finally {
            if (broker != null)
                broker.release();
        }
    }
    if (declaresNamespacePrefixes()) {
        for (final Iterator<Map.Entry<String, String>> i = namespaceMappings.entrySet().iterator(); i
                .hasNext();) {
            final Map.Entry<String, String> entry = i.next();
            final String prefix = entry.getKey().toString();
            final String ns = entry.getValue().toString();
            final QName attrName = new QName(prefix, Namespaces.XMLNS_NS, "xmlns");
            final AttrImpl attr = new AttrImpl(attrName, ns, null);
            attr.setOwnerDocument(ownerDocument);
            map.setNamedItem(attr);
        }
    }
    return map;
}

From source file:org.exist.dom.ElementImpl.java

private AttrImpl findAttribute(String qname, Iterator<StoredNode> iterator, StoredNode current) {
    final int ccount = current.getChildCount();
    StoredNode next;//from  w ww.  j  a  v a2s.  c  om
    for (int i = 0; i < ccount; i++) {
        next = iterator.next();
        if (next.getNodeType() != Node.ATTRIBUTE_NODE) {
            break;
        }
        if (next.getNodeName().equals(qname)) {
            return (AttrImpl) next;
        }
    }
    return null;
}

From source file:org.exist.dom.ElementImpl.java

private AttrImpl findAttribute(QName qname, Iterator<StoredNode> iterator, StoredNode current) {
    final int ccount = current.getChildCount();
    for (int i = 0; i < ccount; i++) {
        final StoredNode next = iterator.next();
        if (next.getNodeType() != Node.ATTRIBUTE_NODE) {
            break;
        }// w ww .ja v  a2s  . co  m
        if (next.getQName().equalsSimple(qname)) {
            return (AttrImpl) next;
        }
    }
    return null;
}

From source file:org.exist.dom.ElementImpl.java

/**
 * @see org.w3c.dom.Node#getFirstChild()
 *///from   w w  w .  j  a va 2 s  .c  om
@Override
public Node getFirstChild() {
    if (!hasChildNodes() || getChildCount() == getAttributesCount()) {
        return null;
    }
    DBBroker broker = null;
    try {
        broker = ownerDocument.getBrokerPool().get(null);
        final Iterator<StoredNode> iterator = broker.getNodeIterator(this);
        iterator.next();
        StoredNode next;
        for (int i = 0; i < getChildCount(); i++) {
            next = iterator.next();
            if (next.getNodeType() != Node.ATTRIBUTE_NODE) {
                return next;
            }
        }
    } catch (final EXistException e) {
        LOG.warn("Exception while retrieving child node: " + e.getMessage(), e);
    } finally {
        if (broker != null)
            broker.release();
    }
    return null;
}