Java Code Examples for org.w3c.dom.Node.ATTRIBUTE_NODE

The following are Jave code examples for showing how to use ATTRIBUTE_NODE of the org.w3c.dom.Node class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: openjdk-jdk10   File: RangeImpl.java   Source Code and License Vote up 6 votes
/**
 * Finds the root container for the given node and determines
 * if that root container is legal with respect to the
 * DOM 2 specification.  At present, that means the root
 * container must be either an attribute, a document,
 * or a document fragment.
 */
private boolean hasLegalRootContainer( Node node )
{
        if ( node==null )
                return false;

        Node rootContainer = getRootContainer( node );
        switch( rootContainer.getNodeType() )
        {
        case Node.ATTRIBUTE_NODE:
        case Node.DOCUMENT_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
                return true;
        }
        return false;
}
 
Example 2
Project: jdk8u-jdk   File: XMLUtils.java   Source Code and License Vote up 6 votes
/**
 * Method getStrFromNode
 *
 * @param xpathnode
 * @return the string for the node.
 */
public static String getStrFromNode(Node xpathnode) {
    if (xpathnode.getNodeType() == Node.TEXT_NODE) {
        // we iterate over all siblings of the context node because eventually,
        // the text is "polluted" with pi's or comments
        StringBuilder sb = new StringBuilder();

        for (Node currentSibling = xpathnode.getParentNode().getFirstChild();
            currentSibling != null;
            currentSibling = currentSibling.getNextSibling()) {
            if (currentSibling.getNodeType() == Node.TEXT_NODE) {
                sb.append(((Text) currentSibling).getData());
            }
        }

        return sb.toString();
    } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
        return ((Attr) xpathnode).getNodeValue();
    } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return ((ProcessingInstruction) xpathnode).getNodeValue();
    }

    return null;
}
 
Example 3
Project: convertigo-engine   File: ParameterUtils.java   Source Code and License Vote up 6 votes
private static String nodeToString(Node node) {
	int nodeType = node.getNodeType();
	switch (nodeType) {
		case Node.DOCUMENT_NODE:
			return XMLUtils.prettyPrintElement(((Document) node).getDocumentElement(), true, false);
		case Node.ELEMENT_NODE:
			return XMLUtils.prettyPrintElement((Element) node, true, false);
		case Node.CDATA_SECTION_NODE:
		case Node.TEXT_NODE:
			int len = node.getChildNodes().getLength();
			return ((len<2) ? node.getNodeValue():XMLUtils.getNormalizedText(node));
		case Node.ATTRIBUTE_NODE:
			return node.getNodeValue();
		default:
			return null;
	}
}
 
Example 4
Project: Equella   File: PropBagEx.java   Source Code and License Vote up 6 votes
/**
 * Counts the number of matching nodes.
 */
public int nodeCount(final String path)
{
	ensureRoot();

	int count = 0;

	final Node node = getNodeHelper(path, false, false);
	if( node != null )
	{
		if( node.getNodeType() == Node.ATTRIBUTE_NODE )
		{
			count = 1;
		}
		else
		{
			for( final Iterator<String> iter = iterateValues(path); iter.hasNext(); count++ )
			{
				iter.next();
			}
		}
	}
	return count;
}
 
Example 5
Project: jetfuel   File: XmlDocumentAdapter.java   Source Code and License Vote up 5 votes
public Map<String, Object> toMap(Node node) {

		Map<String, Object> map = new LinkedHashMap<String, Object>();
		for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling())
			if (child.getNodeType() == Node.ATTRIBUTE_NODE || child.getNodeType() == Node.ELEMENT_NODE)
				toMap(map, child);

		return map;
	}
 
Example 6
Project: incubator-netbeans   File: CompletionContextImpl.java   Source Code and License Vote up 5 votes
/**
 * Adds namespaces from the tag to this context, possibly overriding namespaces
 * from previously added tags. Tags should be added starting from the root down
 * to the context position.
 */
private void addNamespacesFrom(SyntaxElement s) {
    Node e = s.getNode();
    NamedNodeMap attrs = e.getAttributes();
    String nodePrefix = getPrefix(e.getNodeName(), false);
    String version = null;
    String xsltAttrName = null;
    
    for (int i = attrs.getLength() - 1; i >= 0; i--) {
        Node n = attrs.item(i);
        if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
            Attr a = (Attr)n;
            String attrName = a.getName();
            String value = a.getValue();
            addNamespace(attrName, value, nodePrefix);

        
            if(value.trim().equals("http://www.w3.org/1999/XSL/Transform")) { //NOI18N
                xsltAttrName = attrName;
            }
            if(CompletionUtil.getLocalNameFromTag(attrName).
                    equals("version")) { //NOI18N
                version = value.trim();
            }
        } 
    }
    
    if (xsltAttrName != null && "2.0".equals(version)) {
        String prefix = getPrefix(xsltAttrName, false);
        if (prefix == null) {
            // override nonNS location because nonNS schema is XSLT 2.0
            noNamespaceSchemaLocation = "http://www.w3.org/2007/schema-for-xslt20.xsd";
        } else {
            addSchemaLocation(prefix + " http://www.w3.org/2007/schema-for-xslt20.xsd"); //NOI18N
        }
    }
}
 
Example 7
Project: incubator-netbeans   File: SyncUnit.java   Source Code and License Vote up 5 votes
public boolean hasWhitespaceChangeOnly() {
    for (ChangeInfo ci : getChanges()) {
        if (ci.isDomainElement()) {
            continue;
        }
        Node n = ci.getActualChangedNode();
        if (n.getNodeType() == Node.TEXT_NODE) {
            String text = ((Text)n).getNodeValue();
            if (text != null && text.trim().length() > 0) {
                return false;
            }
        } else if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
            String name =  ((Attr) n).getName();
            Attr removed = getRemovedAttributes().get(name);
            if (removed == null) {
                return false;
            }
            Attr added = getAddedAttributes().get(name);
            if (added == null) {
                return false;
            }
            if (removed.getValue() == null || 
                ! removed.getValue().equals(added.getValue())) {
                return false;
            }
        } else {
            // node type must be either element or comment or cdata...
            return false;
        }
    }
    return true;
}
 
Example 8
Project: openjdk-jdk10   File: DTMNodeProxy.java   Source Code and License Vote up 5 votes
/**
 *
 *
 * @see org.w3c.dom.Node
 */
@Override
public final Node getParentNode()
{

  if (getNodeType() == Node.ATTRIBUTE_NODE)
    return null;

  int newnode = dtm.getParent(node);

  return (newnode == DTM.NULL) ? null : dtm.getNode(newnode);
}
 
Example 9
Project: java-tools   File: XmlTreeOperations.java   Source Code and License Vote up 5 votes
private static String getTextContent(Node node)
{
    short nodeType = node.getNodeType();
    if( nodeType == Node.ATTRIBUTE_NODE ||
        nodeType == Node.PROCESSING_INSTRUCTION_NODE ||
        nodeType == Node.COMMENT_NODE ||
        nodeType == Node.TEXT_NODE ||
        nodeType == Node.CDATA_SECTION_NODE )
    {
        return com.altova.CoreTypes.castToString(node.getNodeValue());
    }
    else
    if( nodeType == Node.DOCUMENT_NODE ||
        nodeType == Node.DOCUMENT_FRAGMENT_NODE ||
        nodeType == Node.ELEMENT_NODE ||
        nodeType == Node.ENTITY_REFERENCE_NODE )
    {
        String result = "";
        Node child = node.getFirstChild();
        for( ; child != null ; child = child.getNextSibling() )
        {
            short childNodeType = child.getNodeType();
            if( childNodeType == Node.TEXT_NODE ||
                childNodeType == Node.CDATA_SECTION_NODE ||
                childNodeType == Node.ELEMENT_NODE ||
                childNodeType == Node.ENTITY_REFERENCE_NODE )
            {
                result += getTextContent( child );
            }
        }
        return result;
    }
    else
        return "";
}
 
Example 10
Project: openjdk-jdk10   File: DTMNodeProxy.java   Source Code and License Vote up 5 votes
/**
 *
 *
 * @see org.w3c.dom.Node
 */
@Override
public final Node getNextSibling()
{

  // Attr's Next is defined at DTM level, but not at DOM level.
  if (dtm.getNodeType(node) == Node.ATTRIBUTE_NODE)
    return null;

  int newnode = dtm.getNextSibling(node);

  return (newnode == DTM.NULL) ? null : dtm.getNode(newnode);
}
 
Example 11
Project: convertigo-eclipse   File: XpathEvaluatorComposite.java   Source Code and License Vote up 5 votes
private Document getXpathData(Document document, String xPath) {
	if (document == null) {
		return null;
	}
	if (xPath == null) {
		return null;
	}

	try {
		Document doc = XMLUtils.getDefaultDocumentBuilder().newDocument();
		Element root =  (Element) doc.createElement("root"); 
		doc.appendChild(root);
		
		NodeList nl = getXpathApi().selectNodeList(document, xPath);
		if (nl != null)
			for (int i = 0; i< nl.getLength(); i++) {
				Node node = doc.importNode(nl.item(i), true);
				Element elt = doc.getDocumentElement();
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					elt.appendChild(node);
				}
				if (node.getNodeType() == Node.TEXT_NODE) {
					elt.appendChild(node);
				}
				if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
					elt.setAttribute(node.getNodeName() + "_" + i, node.getNodeValue());
				}
			}

		return doc;
	} catch (TransformerException e) {
		ConvertigoPlugin.logWarning("Error for xpath : '" + xPath + "'\r " + e.getMessage());
		return null;
	}
}
 
Example 12
Project: OpenJSharp   File: DOMHelper.java   Source Code and License Vote up 5 votes
/**
 * Retrieve the text content of a DOM subtree, appending it into a
 * user-supplied FastStringBuffer object. Note that attributes are
 * not considered part of the content of an element.
 * <p>
 * There are open questions regarding whitespace stripping.
 * Currently we make no special effort in that regard, since the standard
 * DOM doesn't yet provide DTD-based information to distinguish
 * whitespace-in-element-context from genuine #PCDATA. Note that we
 * should probably also consider xml:space if/when we address this.
 * DOM Level 3 may solve the problem for us.
 *
 * @param node Node whose subtree is to be walked, gathering the
 * contents of all Text or CDATASection nodes.
 * @param buf FastStringBuffer into which the contents of the text
 * nodes are to be concatenated.
 */
public static void getNodeData(Node node, FastStringBuffer buf)
{

  switch (node.getNodeType())
  {
  case Node.DOCUMENT_FRAGMENT_NODE :
  case Node.DOCUMENT_NODE :
  case Node.ELEMENT_NODE :
  {
    for (Node child = node.getFirstChild(); null != child;
            child = child.getNextSibling())
    {
      getNodeData(child, buf);
    }
  }
  break;
  case Node.TEXT_NODE :
  case Node.CDATA_SECTION_NODE :
    buf.append(node.getNodeValue());
    break;
  case Node.ATTRIBUTE_NODE :
    buf.append(node.getNodeValue());
    break;
  case Node.PROCESSING_INSTRUCTION_NODE :
    // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);
    break;
  default :
    // ignore
    break;
  }
}
 
Example 13
Project: opentest   File: ReadXml.java   Source Code and License Vote up 5 votes
private void processAttributes(Node xmlNode, Map<String, Object> nodeObject) {
    NamedNodeMap attributesMap = xmlNode.getAttributes();
    int attrCount = attributesMap.getLength();
    for (int attrIndex = 0; attrIndex < attrCount; attrIndex++) {
        Node attributeNode = attributesMap.item(attrIndex);
        if (attributeNode.getNodeType() == Node.ATTRIBUTE_NODE) {
            String prefix = attributeNode.getPrefix() != null ? attributeNode.getPrefix() : "";
            nodeObject.put("$" + prefix + attributeNode.getNodeName(), attributeNode.getNodeValue());
        }
    }
}
 
Example 14
Project: OpenJSharp   File: DocumentImpl.java   Source Code and License Vote up 5 votes
/**
 * NON-DOM INTERNAL: Pre-mutation context check, in
 * preparation for later generating DOMAttrModified events.
 * Determines whether this node is within an Attr
 * @param node node to get enclosing attribute for
 * @return either a description of that Attr, or null if none such.
 */
protected void saveEnclosingAttr(NodeImpl node) {
    savedEnclosingAttr = null;
    // MUTATION PREPROCESSING AND PRE-EVENTS:
    // If we're within the scope of an Attr and DOMAttrModified
    // was requested, we need to preserve its previous value for
    // that event.
    LCount lc = LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED);
    if (lc.total > 0) {
        NodeImpl eventAncestor = node;
        while (true) {
            if (eventAncestor == null)
                return;
            int type = eventAncestor.getNodeType();
            if (type == Node.ATTRIBUTE_NODE) {
                EnclosingAttr retval = new EnclosingAttr();
                retval.node = (AttrImpl) eventAncestor;
                retval.oldvalue = retval.node.getNodeValue();
                savedEnclosingAttr = retval;
                return;
            }
            else if (type == Node.ENTITY_REFERENCE_NODE)
                eventAncestor = eventAncestor.parentNode();
            else if (type == Node.TEXT_NODE)
                eventAncestor = eventAncestor.parentNode();
            else
                return;
            // Any other parent means we're not in an Attr
        }
    }
}
 
Example 15
Project: convertigo-engine   File: XpathEvaluatorCompositeWrap.java   Source Code and License Vote up 5 votes
public static Document getXpathData(Document document, String xPath) {
    if (document == null) {
        return null;
    }
    if (xPath == null) {
        return null;
    }

    try {
        Document doc = XMLUtils.getDefaultDocumentBuilder().newDocument();
        Element root =  (Element) doc.createElement("root"); 
        doc.appendChild(root);

        NodeList nl = new TwsCachedXPathAPI().selectNodeList(document, xPath);
        if (nl != null)
            for (int i = 0; i < nl.getLength(); i++) {
                Node node = doc.importNode(nl.item(i), true);
                Element elt = doc.getDocumentElement();
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    elt.appendChild(node);
                }
                if (node.getNodeType() == Node.TEXT_NODE) {
                    elt.appendChild(node);
                }
                if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                    elt.setAttribute(node.getNodeName() + "_" + i, node.getNodeValue());
                }
            }

        return doc;
    }
    catch (TransformerException e) {
        //ConvertigoPlugin.logWarning("Error for xpath : '" + xPath + "'\r " + e.getMessage());
        return null;
    }
}
 
Example 16
Project: openjdk-jdk10   File: DTMNodeProxy.java   Source Code and License Vote up 4 votes
/**
     *
     * DOM Level 3
     * Look up the prefix associated to the given namespace URI, starting from this node.
     *
     * @param namespaceURI
     * @return the prefix for the namespace
     */
    @Override
    public String lookupPrefix(String namespaceURI){

        // REVISIT: When Namespaces 1.1 comes out this may not be true
        // Prefix can't be bound to null namespace
        if (namespaceURI == null) {
            return null;
        }

        short type = this.getNodeType();

        switch (type) {
/*
        case Node.ELEMENT_NODE: {

                String namespace = this.getNamespaceURI(); // to flip out children
                return lookupNamespacePrefix(namespaceURI, (ElementImpl)this);
            }

        case Node.DOCUMENT_NODE:{
                return((NodeImpl)((Document)this).getDocumentElement()).lookupPrefix(namespaceURI);
            }
*/
        case Node.ENTITY_NODE :
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            // type is unknown
            return null;
        case Node.ATTRIBUTE_NODE:{
                if (this.getOwnerElement().getNodeType() == Node.ELEMENT_NODE) {
                    return getOwnerElement().lookupPrefix(namespaceURI);

                }
                return null;
            }
        default:{
/*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupPrefix(namespaceURI);
                }
*/
                return null;
            }
         }
    }
 
Example 17
Project: incubator-netbeans   File: AntGrammar.java   Source Code and License Vote up 4 votes
public @Override short getNodeType() {
    return Node.ATTRIBUTE_NODE;
}
 
Example 18
Project: OpenJSharp   File: DOM2DTM.java   Source Code and License Vote up 4 votes
/**
   * Retrieve the text content of a DOM subtree, appending it into a
   * user-supplied FastStringBuffer object. Note that attributes are
   * not considered part of the content of an element.
   * <p>
   * There are open questions regarding whitespace stripping.
   * Currently we make no special effort in that regard, since the standard
   * DOM doesn't yet provide DTD-based information to distinguish
   * whitespace-in-element-context from genuine #PCDATA. Note that we
   * should probably also consider xml:space if/when we address this.
   * DOM Level 3 may solve the problem for us.
   * <p>
   * %REVIEW% Note that as a DOM-level operation, it can be argued that this
   * routine _shouldn't_ perform any processing beyond what the DOM already
   * does, and that whitespace stripping and so on belong at the DTM level.
   * If you want a stripped DOM view, wrap DTM2DOM around DOM2DTM.
   *
   * @param node Node whose subtree is to be walked, gathering the
   * contents of all Text or CDATASection nodes.
   */
  protected static void dispatchNodeData(Node node,
                                         org.xml.sax.ContentHandler ch,
                                         int depth)
            throws org.xml.sax.SAXException
  {

    switch (node.getNodeType())
    {
    case Node.DOCUMENT_FRAGMENT_NODE :
    case Node.DOCUMENT_NODE :
    case Node.ELEMENT_NODE :
    {
      for (Node child = node.getFirstChild(); null != child;
              child = child.getNextSibling())
      {
        dispatchNodeData(child, ch, depth+1);
      }
    }
    break;
    case Node.PROCESSING_INSTRUCTION_NODE : // %REVIEW%
    case Node.COMMENT_NODE :
      if(0 != depth)
        break;
        // NOTE: Because this operation works in the DOM space, it does _not_ attempt
        // to perform Text Coalition. That should only be done in DTM space.
    case Node.TEXT_NODE :
    case Node.CDATA_SECTION_NODE :
    case Node.ATTRIBUTE_NODE :
      String str = node.getNodeValue();
      if(ch instanceof CharacterNodeHandler)
      {
        ((CharacterNodeHandler)ch).characters(node);
      }
      else
      {
        ch.characters(str.toCharArray(), 0, str.length());
      }
      break;
//    /* case Node.PROCESSING_INSTRUCTION_NODE :
//      // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);
//      break; */
    default :
      // ignore
      break;
    }
  }
 
Example 19
Project: openjdk-jdk10   File: FuncHere.java   Source Code and License Vote up 4 votes
@Override
public XObject execute(XPathContext xctxt) throws TransformerException {
    Node xpathOwnerNode = (Node)xctxt.getOwnerObject();
    if (xpathOwnerNode == null) {
        return null;
    }

    int xpathOwnerNodeDTM = xctxt.getDTMHandleFromNode(xpathOwnerNode);
    int currentNode = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(currentNode);
    int docContext = dtm.getDocument();

    if (docContext == DTM.NULL) {
        error(xctxt, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC, null);
    }

    // check whether currentNode and the node containing the XPath
    // expression are in the same document
    Document currentDoc = getOwnerDocument(dtm.getNode(currentNode));
    Document xpathOwnerDoc = getOwnerDocument(xpathOwnerNode);

    if (currentDoc != xpathOwnerDoc) {
        throw new TransformerException("Owner documents differ");
    }

    XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
    NodeSetDTM nodeSet = nodes.mutableNodeset();

    int hereNode = DTM.NULL;

    switch (dtm.getNodeType(xpathOwnerNodeDTM)) {

        case Node.ATTRIBUTE_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE: {
            // returns a node-set containing the attribute /  processing
            // instruction node
            hereNode = xpathOwnerNodeDTM;
            nodeSet.addNode(hereNode);
            break;
        }
        case Node.TEXT_NODE : {
            // returns a node-set containing the parent element of the
            // text node that directly bears the XPath expression
            hereNode = dtm.getParent(xpathOwnerNodeDTM);
            nodeSet.addNode(hereNode);
            break;
        }
        default :
            break;
    }

    /** $todo$ Do I have to do this detach() call? */
    nodeSet.detach();

    return nodes;
}
 
Example 20
Project: openjdk-jdk10   File: DOM2DTMdefaultNamespaceDeclarationNode.java   Source Code and License Vote up 0 votes
public short getNodeType() {return Node.ATTRIBUTE_NODE;}