/** * Get general sibling elements. * * @see <a href="http://www.w3.org/TR/css3-selectors/#general-sibling-combinators">General sibling * combinator</a> */ private void getGeneralSiblingElements() { for (Branch node : nodes) { Branch n = DOMHelper.getNextSiblingElement(node); while (n != null) { if (selector.getTagName().equals(n.getName()) || selector.getTagName().equals(Selector.UNIVERSAL_TAG)) { result.add(n); } n = DOMHelper.getNextSiblingElement(n); } } }
/** * Get adjacent sibling elements. * * @see <a href="http://www.w3.org/TR/css3-selectors/#adjacent-sibling-combinators">Adjacent * sibling combinator</a> */ private void getAdjacentSiblingElements() { String tag = selector.getTagName(); for (Branch node : nodes) { Branch n = DOMHelper.getNextSiblingElement(node); if (n != null) { if (tag.equals(n.getName()) || tag.equals(Selector.UNIVERSAL_TAG)) { result.add(n); } } } }
private void getDescentantElements(Branch node) { String tagName = selector.getTagName(); Iterator<Node> nodeIterator = node.nodeIterator(); while (nodeIterator.hasNext()) { Node child = nodeIterator.next(); if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { if (tagName.equals("*") || child.getName().equals(tagName)) { result.add((Branch) child); } getDescentantElements((Branch) child); } } }
/** * Get child elements. * * @see <a href="http://www.w3.org/TR/css3-selectors/#child-combinators">Child combinators</a> */ private void getChildElements() { String tag = selector.getTagName(); for (Branch node : nodes) { Iterator<Node> nodeIterator = node.nodeIterator(); while (nodeIterator.hasNext()) { Node child = nodeIterator.next(); if (child.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) { continue; } if (tag.equals(child.getName()) || tag.equals(Selector.UNIVERSAL_TAG)) { result.add((Branch) child); } } } }
@Override public Set<Branch> check(Set<Branch> nodes) throws NodeSelectorException { Assert.notNull(nodes, "nodes is null!"); this.nodes = nodes; result = new LinkedHashSet<Branch>(); switch (selector.getCombinator()) { case DESCENDANT: getDescentantElements(); break; case CHILD: getChildElements(); break; case ADJACENT_SIBLING: getAdjacentSiblingElements(); break; case GENERAL_SIBLING: getGeneralSiblingElements(); break; } return result; }