Find first child node.

@param node start node
@param ruleName rule name
@return matched node
348639
public static Optional<ParserRuleContext> findFirstChildNode(final ParserRuleContext node, final RuleName ruleName) {
        Queue<ParserRuleContext> parserRuleContexts = new LinkedList<>();
        parserRuleContexts.add(node);
        ParserRuleContext parserRuleContext;
        while (null != (parserRuleContext = parserRuleContexts.poll())) {
            if (isMatchedNode(parserRuleContext, ruleName)) {
                return Optional.of(parserRuleContext);
            }
            for (int i = 0; i < parserRuleContext.getChildCount(); i++) {
                if (parserRuleContext.getChild(i) instanceof ParserRuleContext) {
                    parserRuleContexts.add((ParserRuleContext) parserRuleContext.getChild(i));
                }
            }
        }
        return Optional.absent();
    }
1187106
public static Optional<ParserRuleContext> findSingleNodeFromFirstDescendant(final ParserRuleContext node, final RuleName ruleName) {
        ParserRuleContext nextNode = node;
        do {
            if (isMatchedNode(nextNode, ruleName)) {
                return Optional.of(nextNode);
            }
            if (1 != nextNode.getChildCount() || !(nextNode.getChild(0) instanceof ParserRuleContext)) {
                return Optional.absent();
            }
            nextNode = (ParserRuleContext) nextNode.getChild(0);
        } while (null != nextNode);
        return Optional.absent();
    }
234534
def visit_rule(self, node, rule):
        """"""
        label, equals, expression = rule
        expression.name = label  # Assign a name to the expr.
        return expression
611236
function findChildContainingNode(root, node) {
  const parentNode = node.parentNode;
  return parentNode === root ?
    node :
    findChildContainingNode(root, parentNode);
}
1058615
@Nullable
  public SoyNode firstChildThatMatches(Predicate<SoyNode> condition) {
    int firstChildIndex = 0;
    while (firstChildIndex < numChildren() && !condition.test(getChild(firstChildIndex))) {
      firstChildIndex++;
    }
    if (firstChildIndex < numChildren()) {
      return getChild(firstChildIndex);
    }
    return null;
  }
767958
protected function seek(array $nodes, array $rule, array $options): array
    {
        // XPath index
        if (array_key_exists('tag', $rule) &&
            array_key_exists('key', $rule) &&
            is_numeric($rule['key'])
        ) {
            $count = 0;
            /** @var AbstractNode $node */
            foreach ($nodes as $node) {
                if ($rule['tag'] == '*' ||
                    $rule['tag'] == $node->getTag()->name()
                ) {
                    ++$count;
                    if ($count == $rule['key']) {
                        // found the node we wanted
                        return [$node];
                    }
                }
            }

            return [];
        }

        $options = $this->flattenOptions($options);

        $return = [];
        /** @var InnerNode $node */
        foreach ($nodes as $node) {
            // check if we are a leaf
            if ($node instanceof LeafNode ||
                ! $node->hasChildren()
            ) {
                continue;
            }

            $children = [];
            $child    = $node->firstChild();
            while ( ! is_null($child)) {
                // wild card, grab all
                if ($rule['tag'] == '*' && is_null($rule['key'])) {
                    $return[] = $child;
                    $child = $this->getNextChild($node, $child);
                    continue;
                }

                $pass = $this->checkTag($rule, $child);
                if ($pass && ! is_null($rule['key'])) {
                    $pass = $this->checkKey($rule, $child);
                }
                if ($pass && ! is_null($rule['key']) &&
                    ! is_null($rule['value']) && $rule['value'] != '*'
                ) {
                    $pass = $this->checkComparison($rule, $child);
                }

                if ($pass) {
                    // it passed all checks
                    $return[] = $child;
                } else {
                    // this child failed to be matched
                    if ($child instanceof InnerNode &&
                        $child->hasChildren()
                    ) {
                        // we still want to check its children
                        $children[] = $child;
                    }
                }

                $child = $this->getNextChild($node, $child);
            }

            if (( ! isset($options['checkGrandChildren']) ||
                    $options['checkGrandChildren'])
                && count($children) > 0
            ) {
                // we have children that failed but are not leaves.
                $matches = $this->seek($children, $rule, $options);
                foreach ($matches as $match) {
                    $return[] = $match;
                }
            }
        }

        return $return;
    }
871872
public Optional<Tree<T>> getChild(Tree<T> node) {
    return children.stream().filter(is(node)).findFirst();
  }
1062707
def on_rule(node, compiled_grammar)
      name = process(node.children[0], compiled_grammar)

      if compiled_grammar.has_terminal?(name)
        compiled_grammar.add_error(
          "the rule name #{name.inspect} is already used as a terminal name",
          node.source_line
        )
      end

      if compiled_grammar.has_rule_with_branches?(name)
        compiled_grammar.add_error(
          "the rule #{name.inspect} has already been defined",
          node.source_line
        )

        return
      end

      branches = node.children[1..-1].map do |child|
        process(child, compiled_grammar)
      end

      rule = compiled_grammar.lookup_rule(name)

      rule.branches.concat(branches)
    end
777939
protected function seek(array $nodes, array $rule, array $options)
    {
        // XPath index
        if (count($rule['tag']) > 0 &&
            count($rule['key']) > 0 &&
            is_numeric($rule['key'])
        ) {
            $count = 0;
            /** @var AbstractNode $node */
            foreach ($nodes as $node) {
                if ($rule['tag'] == '*' ||
                    $rule['tag'] == $node->getTag()->name()
                ) {
                    ++$count;
                    if ($count == $rule['key']) {
                        // found the node we wanted
                        return [$node];
                    }
                }
            }

            return [];
        }

        $options = $this->flattenOptions($options);

        $return = [];
        /** @var InnerNode $node */
        foreach ($nodes as $node) {
            // check if we are a leaf
            if ($node instanceof LeafNode ||
                ! $node->hasChildren()
            ) {
                continue;
            }

            $children = [];
            $child    = $node->firstChild();
            while ( ! is_null($child)) {
                // wild card, grab all
                if ($rule['tag'] == '*' && is_null($rule['key'])) {
                    $return[] = $child;
                    try {
                        $child = $node->nextChild($child->id());
                    } catch (ChildNotFoundException $e) {
                        // no more children
                        $child = null;
                    }
                    continue;
                }

                $pass = true;
                // check tag
                if ( ! empty($rule['tag']) && $rule['tag'] != $child->getTag()->name() &&
                    $rule['tag'] != '*'
                ) {
                    // child failed tag check
                    $pass = false;
                }

                // check key
                if ($pass && ! is_null($rule['key'])) {
                    if ($rule['noKey']) {
                        if ( ! is_null($child->getAttribute($rule['key']))) {
                            $pass = false;
                        }
                    } else {
                        if ($rule['key'] != 'plaintext' &&
                            is_null($child->getAttribute($rule['key']))
                        ) {
                            $pass = false;
                        }
                    }
                }

                // compare values
                if ($pass && ! is_null($rule['key']) &&
                    ! is_null($rule['value']) && $rule['value'] != '*'
                ) {
                    if ($rule['key'] == 'plaintext') {
                        // plaintext search
                        $nodeValue = $child->text();
                    } else {
                        // normal search
                        $nodeValue = $child->getAttribute($rule['key']);
                    }

                    $check = $this->match($rule['operator'], $rule['value'], $nodeValue);

                    // handle multiple classes
                    if ( ! $check && $rule['key'] == 'class') {
                        $childClasses = explode(' ', $child->getAttribute('class'));
                        foreach ($childClasses as $class) {
                            if ( ! empty($class)) {
                                $check = $this->match($rule['operator'], $rule['value'], $class);
                            }
                            if ($check) {
                                break;
                            }
                        }
                    }

                    if ( ! $check) {
                        $pass = false;
                    }
                }

                if ($pass) {
                    // it passed all checks
                    $return[] = $child;
                } else {
                    // this child failed to be matched
                    if ($child instanceof InnerNode &&
                        $child->hasChildren()
                    ) {
                        // we still want to check its children
                        $children[] = $child;
                    }
                }

                try {
                    // get next child
                    $child = $node->nextChild($child->id());
                } catch (ChildNotFoundException $e) {
                    // no more children
                    $child = null;
                }
            }

            if (( ! isset($options['checkGrandChildren']) ||
                    $options['checkGrandChildren'])
                && count($children) > 0
            ) {
                // we have children that failed but are not leaves.
                $matches = $this->seek($children, $rule, $options);
                foreach ($matches as $match) {
                    $return[] = $match;
                }
            }
        }

        return $return;
    }
13340
private Node getParentNode(Node node) {

		if (node == null || node == root)
			return null;

		Node newNode = node.getParentNode();
		if (newNode == null)
			return null;

		int accept = acceptNode(newNode);

		if (accept == NodeFilter.FILTER_ACCEPT)
			return newNode;
		else
			// if (accept == NodeFilter.SKIP_NODE)
			// and REJECT too.
			return getParentNode(newNode);

	}
168312
def get_child_by_name(self, name):
        
        for c in self.child_nodes:
            if c.name == name:
                return c
        return None
683936
public static void validateNodeAndPatterns(final Node node, final Pattern... patterns)
            throws IllegalArgumentException
   {
      // Precondition checks
      if (node == null)
      {
         throw new IllegalArgumentException("node must be specified");
      }
      if (patterns == null || patterns.length == 0)
      {
         throw new IllegalArgumentException("At least one pattern must be specified");
      }

      // OK
   }
1341546
private Node getFirstChild(Node node) {

		if (node == null)
			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())
			return getFirstChild(newNode);

		// if (accept == NodeFilter.REJECT_NODE)
		return getNextSibling(newNode);
	}
1182084
def find(self, node, list):
        
        if self.matcher.match(node):
            list.append(node)
            self.limit -= 1
            if self.limit == 0:
                return
        for c in node.rawchildren:
            self.find(c, list)
        return self
382666
public static List<Node> getChildren(Node node, String childNodeName)
    {
        List<Node> children = new ArrayList<Node>();
        NodeList childNodes = node.getChildNodes();
        for (int i=0; i<childNodes.getLength(); i++)
        {
            Node child = childNodes.item(i);
            String childName = child.getNodeName();
            if (childName.equalsIgnoreCase(childNodeName))
            {
                children.add(child);
            }
        }
        return children;
    }
370336
private Element findFirstElementByTagName(String tag, Node node) {
        if (node.nodeName().equals(tag))
            return (Element) node;
        else {
            int size = node.childNodeSize();
            for (int i = 0; i < size; i++) {
                Element found = findFirstElementByTagName(tag, node.childNode(i));
                if (found != null)
                    return found;
            }
        }
        return null;
    }
214601
@Nullable
  public static Element getFirstChildElement (@Nullable final Node aStartNode)
  {
    if (aStartNode == null)
      return null;
    return NodeListIterator.createChildNodeIterator (aStartNode)
                           .findFirstMapped (filterNodeIsElement (), x -> (Element) x);
  }