Java Code Examples for org.w3c.dom.Node.getFirstChild()

The following are Jave code examples for showing how to use getFirstChild() 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: ccpa   File: TreeTransformer.java   Source Code and License Vote up 6 votes
private static int giveVariablesAddressesInNode(Node node, int index) {
	for (Node child = node.getFirstChild();child != null;child = child.getNextSibling())
	{
		if (child.getNodeName().equals("uses"))
		{
			Element element = (Element) child;
			element.setAttribute("address", "V" + index);
			try {
				index += TreeToAE2.getSizeOf(child.getFirstChild().getTextContent());
			}  catch (Exception e) {
				e.printStackTrace();
			}
		}
		else
		{
			index = giveVariablesAddressesInNode(child,index);
		}
	}
	return index;
}
 
Example 2
Project: Tarski   File: mxUtils.java   Source Code and License Vote up 6 votes
/**
 * Returns the first node where attr equals value. This implementation does not use XPath.
 */
public static Node findNode(Node node, String attr, String value) {
  String tmp = (node instanceof Element) ? ((Element) node).getAttribute(attr) : null;

  if (tmp != null && tmp.equals(value)) {
    return node;
  }

  node = node.getFirstChild();

  while (node != null) {
    Node result = findNode(node, attr, value);

    if (result != null) {
      return result;
    }

    node = node.getNextSibling();
  }

  return null;
}
 
Example 3
Project: s-store   File: ParsedSelectStmt.java   Source Code and License Vote up 6 votes
void parseDisplayColumns(Node columnsNode, Database db) {
    for (Node child = columnsNode.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() != Node.ELEMENT_NODE)
            continue;
        final String nodeName = child.getNodeName();
        ParsedColInfo col = new ParsedColInfo();
        col.expression = parseExpressionTree(child, db);
        ExpressionUtil.assignLiteralConstantTypesRecursively(col.expression);
        ExpressionUtil.assignOutputValueTypesRecursively(col.expression);
        assert(col.expression != null);
        col.alias = child.getAttributes().getNamedItem("alias").getNodeValue();

        if (nodeName.equals("columnref")) {
            col.columnName =
                child.getAttributes().getNamedItem("column").getNodeValue();
            col.tableName =
                child.getAttributes().getNamedItem("table").getNodeValue();
        }
        col.index = allColumns.size();
        displayColumns.add(col);
        allColumns.put(col.alias, col);
    }
}
 
Example 4
Project: jdk8u-jdk   File: JPEGMetadata.java   Source Code and License Vote up 6 votes
/**
 * Merge a JFIF subtree into the marker sequence, if the subtree
 * is non-empty.
 * If a JFIF marker exists, update it from the subtree.
 * If none exists, create one from the subtree and insert it at the
 * beginning of the marker sequence.
 */
private void mergeJFIFsubtree(Node JPEGvariety)
    throws IIOInvalidTreeException {
    if (JPEGvariety.getChildNodes().getLength() != 0) {
        Node jfifNode = JPEGvariety.getFirstChild();
        // is there already a jfif marker segment?
        JFIFMarkerSegment jfifSeg =
            (JFIFMarkerSegment) findMarkerSegment(JFIFMarkerSegment.class, true);
        if (jfifSeg != null) {
            jfifSeg.updateFromNativeNode(jfifNode, false);
        } else {
            // Add it as the first element in the list.
            markerSequence.add(0, new JFIFMarkerSegment(jfifNode));
        }
    }
}
 
Example 5
Project: javaide   File: SdkRepoSource.java   Source Code and License Vote up 6 votes
/**
 * Helper method used by {@link #findAlternateToolsXml(InputStream)} to find a given
 * element child in a root XML node.
 */
private Element findChild(Node rootNode, Node after, String prefix, String[] nodeNames) {
    for (int i = 0; i < nodeNames.length; i++) {
        if (nodeNames[i].indexOf(':') < 0) {
            nodeNames[i] = prefix + ":" + nodeNames[i];
        }
    }
    Node child = after == null ? rootNode.getFirstChild() : after.getNextSibling();
    for(; child != null; child = child.getNextSibling()) {
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        for (String nodeName : nodeNames) {
            if (nodeName.equals(child.getNodeName())) {
                return (Element) child;
            }
        }
    }
    return null;
}
 
Example 6
Project: javaide   File: AddonsListFetcher.java   Source Code and License Vote up 5 votes
/**
 * Returns the first child element with the given XML local name.
 * If xmlLocalName is null, returns the very first child element.
 */
private Node getFirstChild(Node node, String nsUri, String xmlLocalName) {

    for(Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE &&
                nsUri.equals(child.getNamespaceURI())) {
            if (xmlLocalName == null || child.getLocalName().equals(xmlLocalName)) {
                return child;
            }
        }
    }

    return null;
}
 
Example 7
Project: OpenJSharp   File: TreeWalkerImpl.java   Source Code and License Vote up 5 votes
/** Internal function.
 *  Return the first child Node, from the input node
 *  after applying filter, whatToshow.
 *  The current node is not consulted or set.
 */
Node getFirstChild(Node node) {
    if (node == null) return null;

    if ( !fEntityReferenceExpansion
         && node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
        return null;
    Node newNode = node.getFirstChild();
    if (newNode == null)  return null;
    int accept = acceptNode(newNode);

    if (accept == NodeFilter.FILTER_ACCEPT)
        return newNode;
    else
    if (accept == NodeFilter.FILTER_SKIP
        && newNode.hasChildNodes())
    {
        Node fChild = getFirstChild(newNode);

        if (fChild == null) {
            return getNextSibling(newNode, node);
        }
        return fChild;
    }
    else
    //if (accept == NodeFilter.REJECT_NODE)
    {
        return getNextSibling(newNode, node);
    }


}
 
Example 8
Project: openjdk-jdk10   File: JPEGMetadata.java   Source Code and License Vote up 5 votes
private void setFromNativeTree(Node root) throws IIOInvalidTreeException {
    if (resetSequence == null) {
        resetSequence = markerSequence;
    }
    markerSequence = new ArrayList<>();

    // Build a whole new marker sequence from the tree

    String name = root.getNodeName();
    if (name != ((isStream) ? JPEG.nativeStreamMetadataFormatName
                            : JPEG.nativeImageMetadataFormatName)) {
        throw new IIOInvalidTreeException("Invalid root node name: " + name,
                                          root);
    }
    if (!isStream) {
        if (root.getChildNodes().getLength() != 2) { // JPEGvariety and markerSequence
            throw new IIOInvalidTreeException(
                "JPEGvariety and markerSequence nodes must be present", root);
        }

        Node JPEGvariety = root.getFirstChild();

        if (JPEGvariety.getChildNodes().getLength() != 0) {
            markerSequence.add(new JFIFMarkerSegment(JPEGvariety.getFirstChild()));
        }
    }

    Node markerSequenceNode = isStream ? root : root.getLastChild();
    setFromMarkerSequenceNode(markerSequenceNode);

}
 
Example 9
Project: ats-framework   File: DatabaseSnapshotUtils.java   Source Code and License Vote up 5 votes
public static List<Element> getChildrenByTagName(
                                                  Node parent,
                                                  String name ) {

    List<Element> nodeList = new ArrayList<Element>();
    for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE && name.equals(child.getNodeName())) {
            nodeList.add((Element) child);
        }
    }

    return nodeList;
}
 
Example 10
Project: L2J-Global   File: UIData.java   Source Code and License Vote up 5 votes
private void parseCategory(Node n)
{
	final int cat = parseInteger(n.getAttributes(), "id");
	for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
	{
		if ("commands".equalsIgnoreCase(d.getNodeName()))
		{
			parseCommands(cat, d);
		}
		else if ("keys".equalsIgnoreCase(d.getNodeName()))
		{
			parseKeys(cat, d);
		}
	}
}
 
Example 11
Project: openjdk-jdk10   File: NodeIteratorImpl.java   Source Code and License Vote up 5 votes
/** The method nextNode(Node, boolean) returns the next node
 *  from the actual DOM tree.
 *
 *  The boolean visitChildren determines whether to visit the children.
 *  The result is the nextNode.
 */
Node nextNode(Node node, boolean visitChildren) {

    if (node == null) return fRoot;

    Node result;
    // only check children if we visit children.
    if (visitChildren) {
        //if hasChildren, return 1st child.
        if (node.hasChildNodes()) {
            result = node.getFirstChild();
            return result;
        }
    }

    if (node == fRoot) { //if Root has no kids
        return null;
    }

    // if hasSibling, return sibling
    result = node.getNextSibling();
    if (result != null) return result;


    // return parent's 1st sibling.
    Node parent = node.getParentNode();
    while (parent != null && parent != fRoot) {
        result = parent.getNextSibling();
        if (result != null) {
            return result;
        } else {
            parent = parent.getParentNode();
        }

    } // while (parent != null && parent != fRoot) {

    // end of list, return null
    return null;
}
 
Example 12
Project: apache-tomcat-7.0.73-with-comment   File: DomUtil.java   Source Code and License Vote up 5 votes
/** Get the first direct child with a given type
 */
public static Node getChild( Node parent, int type ) {
    Node n=parent.getFirstChild();
    while( n!=null && type != n.getNodeType() ) {
        n=n.getNextSibling();
    }
    if( n==null ) return null;
    return n;
}
 
Example 13
Project: OpenJSharp   File: DOMNormalizer.java   Source Code and License Vote up 5 votes
protected final void expandEntityRef (Node parent, Node reference){
    Node kid, next;
    for (kid = reference.getFirstChild(); kid != null; kid = next) {
        next = kid.getNextSibling();
        parent.insertBefore(kid, reference);
    }
}
 
Example 14
Project: PeSanKita-android   File: SmilDocumentImpl.java   Source Code and License Vote up 5 votes
public SMILElement getHead() {
    Node rootElement = getDocumentElement();
    Node headElement = rootElement.getFirstChild();
    if (headElement == null || !(headElement instanceof SMILElement)) {
        // The head doesn't exist. Create a new one.
        headElement = createElement("head");
        rootElement.appendChild(headElement);
    }

    return (SMILElement) headElement;
}
 
Example 15
Project: openjdk-jdk10   File: TreeWalker.java   Source Code and License Vote up 4 votes
/**
 * Perform a pre-order traversal non-recursive style.
 *
 * Note that TreeWalker assumes that the subtree is intended to represent
 * a complete (though not necessarily well-formed) document and, during a
 * traversal, startDocument and endDocument will always be issued to the
 * SAX listener.
 *
 * @param pos Node in the tree where to start traversal
 *
 * @throws TransformerException
 */
public void traverse(Node pos) throws org.xml.sax.SAXException
{

  this.m_contentHandler.startDocument();

  Node top = pos;

  while (null != pos)
  {
    startNode(pos);

    Node nextNode = pos.getFirstChild();

    while (null == nextNode)
    {
      endNode(pos);

      if (top.equals(pos))
        break;

      nextNode = pos.getNextSibling();

      if (null == nextNode)
      {
        pos = pos.getParentNode();

        if ((null == pos) || (top.equals(pos)))
        {
          if (null != pos)
            endNode(pos);

          nextNode = null;

          break;
        }
      }
    }

    pos = nextNode;
  }
  this.m_contentHandler.endDocument();
}
 
Example 16
Project: openjdk-jdk10   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 17
Project: lams   File: XMLUtils.java   Source Code and License Vote up 4 votes
static String getTextValue( Node node ) throws SAXException {
    Node textNode = node.getFirstChild();
    if (textNode == null) return "";
    if (textNode.getNodeType() != Node.TEXT_NODE) throw new SAXException( "No text value found for <" + node.getNodeName() + "> node" );
    return textNode.getNodeValue();
}
 
Example 18
Project: TrabalhoFinalEDA2   File: mxChildChangeCodec.java   Source Code and License Vote up 4 votes
/**
 * Reads the cells into the graph model. All cells are children of the root
 * element in the node.
 */
public Node beforeDecode(mxCodec dec, Node node, Object into)
{
	if (into instanceof mxChildChange)
	{
		mxChildChange change = (mxChildChange) into;

		if (node.getFirstChild() != null
				&& node.getFirstChild().getNodeType() == Node.ELEMENT_NODE)
		{
			// Makes sure the original node isn't modified
			node = node.cloneNode(true);

			Node tmp = node.getFirstChild();
			change.setChild(dec.decodeCell(tmp, false));

			Node tmp2 = tmp.getNextSibling();
			tmp.getParentNode().removeChild(tmp);
			tmp = tmp2;

			while (tmp != null)
			{
				tmp2 = tmp.getNextSibling();

				if (tmp.getNodeType() == Node.ELEMENT_NODE)
				{
					// Ignores all existing cells because those do not need
					// to be re-inserted into the model. Since the encoded
					// version of these cells contains the new parent, this
					// would leave to an inconsistent state on the model
					// (ie. a parent change without a call to
					// parentForCellChanged).
					String id = ((Element) tmp).getAttribute("id");

					if (dec.lookup(id) == null)
					{
						dec.decodeCell(tmp, true);
					}
				}

				tmp.getParentNode().removeChild(tmp);
				tmp = tmp2;
			}
		}
		else
		{
			String childRef = ((Element) node).getAttribute("child");
			change.setChild((mxICell) dec.getObject(childRef));
		}
	}

	return node;
}
 
Example 19
Project: sstore-soft   File: ParsedSelectStmt.java   Source Code and License Vote up 4 votes
void parseOrderColumn(Node orderByNode, Database db) {
    // make sure everything is kosher
    assert(orderByNode.getNodeType() == Node.ELEMENT_NODE);
    assert(orderByNode.getNodeName().equalsIgnoreCase("operation"));
    NamedNodeMap attrs = orderByNode.getAttributes();
    Node operationTypeNode = attrs.getNamedItem("type");
    assert(operationTypeNode != null);
    assert(operationTypeNode.getNodeValue().equalsIgnoreCase("orderby"));

    // get desc/asc
    Node descNode = attrs.getNamedItem("desc");
    boolean descending = descNode.getNodeValue().equalsIgnoreCase("true");

    // get the columnref expression inside the orderby node
    Node child = orderByNode.getFirstChild();
    while (child.getNodeType() != Node.ELEMENT_NODE)
        child = child.getNextSibling();
    assert(child != null);
    if (child.getNodeName().equals("columnref"))
    {
        NamedNodeMap childAttrs = child.getAttributes();
        String alias = childAttrs.getNamedItem("alias").getNodeValue();

        // create the orderby column
        ParsedColInfo col = allColumns.get(alias);
        if (col == null) {
            col = new ParsedColInfo();
            col.alias = alias;
            col.expression = parseExpressionTree(child, db);
            ExpressionUtil.assignLiteralConstantTypesRecursively(col.expression);
            ExpressionUtil.assignOutputValueTypesRecursively(col.expression);
            col.index = allColumns.size();
            col.columnName =
                childAttrs.getNamedItem("column").getNodeValue();
            col.tableName =
                childAttrs.getNamedItem("table").getNodeValue();
            allColumns.put(alias, col);
        }
        col.orderBy = true;
        col.ascending = !descending;
        orderColumns.add(col);
    }
    else
    {
        throw new RuntimeException("ORDER BY with complex expressions not yet supported");
    }
}
 
Example 20
Project: s-store   File: ParsedSelectStmt.java   Source Code and License Vote up 4 votes
void parseOrderColumn(Node orderByNode, Database db) {
    // make sure everything is kosher
    assert(orderByNode.getNodeType() == Node.ELEMENT_NODE);
    assert(orderByNode.getNodeName().equalsIgnoreCase("operation"));
    NamedNodeMap attrs = orderByNode.getAttributes();
    Node operationTypeNode = attrs.getNamedItem("type");
    assert(operationTypeNode != null);
    assert(operationTypeNode.getNodeValue().equalsIgnoreCase("orderby"));

    // get desc/asc
    Node descNode = attrs.getNamedItem("desc");
    boolean descending = descNode.getNodeValue().equalsIgnoreCase("true");

    // get the columnref expression inside the orderby node
    Node child = orderByNode.getFirstChild();
    while (child.getNodeType() != Node.ELEMENT_NODE)
        child = child.getNextSibling();
    assert(child != null);
    if (child.getNodeName().equals("columnref"))
    {
        NamedNodeMap childAttrs = child.getAttributes();
        String alias = childAttrs.getNamedItem("alias").getNodeValue();

        // create the orderby column
        ParsedColInfo col = allColumns.get(alias);
        if (col == null) {
            col = new ParsedColInfo();
            col.alias = alias;
            col.expression = parseExpressionTree(child, db);
            ExpressionUtil.assignLiteralConstantTypesRecursively(col.expression);
            ExpressionUtil.assignOutputValueTypesRecursively(col.expression);
            col.index = allColumns.size();
            col.columnName =
                childAttrs.getNamedItem("column").getNodeValue();
            col.tableName =
                childAttrs.getNamedItem("table").getNodeValue();
            allColumns.put(alias, col);
        }
        col.orderBy = true;
        col.ascending = !descending;
        orderColumns.add(col);
    }
    else
    {
        throw new RuntimeException("ORDER BY with complex expressions not yet supported");
    }
}