/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.descriptor.spi.node.Query#execute(org.jboss.shrinkwrap.descriptor.spi.node.Node, * org.jboss.shrinkwrap.descriptor.spi.node.Pattern[]) */ @Override public Node execute(final Node node, final Pattern... patterns) { // Precondition checks QueryUtil.validateNodeAndPatterns(node, patterns); final List<Node> nodes = RelativeGetQuery.INSTANCE.execute(node, patterns); if (nodes == null || nodes.isEmpty()) { return null; } if (nodes.size() > 1) { throw new IllegalArgumentException("Multiple nodes matching expression found"); } return nodes.get(0); } }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.descriptor.spi.node.Query#execute(org.jboss.shrinkwrap.descriptor.spi.node.Node, * org.jboss.shrinkwrap.descriptor.spi.node.Pattern[]) */ @Override public List<Node> execute(final Node node, final Pattern... patterns) throws IllegalArgumentException { // Precondition checks QueryUtil.validateNodeAndPatterns(node, patterns); // Represent as a list List<Pattern> patternSequence = Arrays.asList(patterns); // Delegate to recursive handler, starting at the top return findMatch(node, patternSequence, patternSequence); }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.descriptor.spi.node.Query#execute(org.jboss.shrinkwrap.descriptor.spi.node.Node, * org.jboss.shrinkwrap.descriptor.spi.node.Pattern[]) */ @Override public List<Node> execute(final Node node, final Pattern... patterns) throws IllegalArgumentException { // Precondition checks QueryUtil.validateNodeAndPatterns(node, patterns); // Represent as a list List<Pattern> patternSequence = Arrays.asList(patterns); // Delegate to recursive handler, starting at the top return findMatch(node, patternSequence, patternSequence); }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.descriptor.spi.node.Query#execute(org.jboss.shrinkwrap.descriptor.spi.node.Node, * org.jboss.shrinkwrap.descriptor.spi.node.Pattern[]) */ @Override public Node execute(final Node node, final Pattern... patterns) { // Precondition checks QueryUtil.validateNodeAndPatterns(node, patterns); final List<Node> nodes = RelativeGetQuery.INSTANCE.execute(node, patterns); if (nodes == null || nodes.isEmpty()) { return null; } if (nodes.size() > 1) { throw new IllegalArgumentException("Multiple nodes matching expression found"); } return nodes.get(0); } }
private List<Node> findMatch(final Node start, final List<Pattern> patternSequence, final List<Pattern> entirePatternSequence) { // Hold the matched Nodes final List<Node> matchedNodes = new ArrayList<Node>(); // Get the next pattern in sequence final Pattern pattern = patternSequence.get(0); // See if we've got a match if (pattern.matches(start)) { // If no more patterns to check, we're at the end of the line; just add this Node if (patternSequence.size() == 1) { matchedNodes.add(start); } else { for (final Node child : start.getChildren()) { // Only use patterns that haven't already matched final List<Pattern> remainingPatterns = patternSequence.subList(1, patternSequence.size()); // Recursion point matchedNodes.addAll(findMatch(child, remainingPatterns, entirePatternSequence)); } } } // Apply whole pattern sequence starting from the subtrees // created by node's children for (final Node child : start.getChildren()) { matchedNodes.addAll(findMatch(child, entirePatternSequence, entirePatternSequence)); } return matchedNodes; }
private List<Node> findMatch(final Node start, final List<Pattern> patternSequence, final List<Pattern> entirePatternSequence) { // Hold the matched Nodes final List<Node> matchedNodes = new ArrayList<Node>(); // Get the next pattern in sequence final Pattern pattern = patternSequence.get(0); // See if we've got a match if (pattern.matches(start)) { // If no more patterns to check, we're at the end of the line; just add this Node if (patternSequence.size() == 1) { matchedNodes.add(start); } else { for (final Node child : start.getChildren()) { // Only use patterns that haven't already matched final List<Pattern> remainingPatterns = patternSequence.subList(1, patternSequence.size()); // Recursion point matchedNodes.addAll(findMatch(child, remainingPatterns, entirePatternSequence)); } } } // Apply whole pattern sequence starting from the subtrees // created by node's children for (final Node child : start.getChildren()) { matchedNodes.addAll(findMatch(child, entirePatternSequence, entirePatternSequence)); } return matchedNodes; }