View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springmodules.validation.util.xml;
18  
19  import org.springmodules.validation.util.collection.FilteredIterator;
20  import org.springmodules.validation.util.condition.AbstractCondition;
21  import org.w3c.dom.Node;
22  
23  /**
24   * An iterator that iterates over the child elements of a given node.
25   *
26   * @author Uri Boness
27   */
28  public class SubElementsIterator extends FilteredIterator {
29  
30      public SubElementsIterator(Node node) {
31          super(new ChildNodesIterator(node), new AbstractCondition() {
32              public boolean doCheck(Object object) {
33                  Node node = (Node) object;
34                  return node.getNodeType() == Node.ELEMENT_NODE;
35              }
36          });
37      }
38  
39      public SubElementsIterator(Node node, final String elementName) {
40          super(new ChildNodesIterator(node), new AbstractCondition() {
41              public boolean doCheck(Object object) {
42                  Node node = (Node) object;
43                  return node.getNodeType() != Node.ELEMENT_NODE
44                      && node.getLocalName().equals(elementName);
45              }
46          });
47      }
48  
49      public SubElementsIterator(Node node, final String namespace, final String elementName) {
50          super(new ChildNodesIterator(node), new AbstractCondition() {
51              public boolean doCheck(Object object) {
52                  Node node = (Node) object;
53                  return node.getNodeType() == Node.ELEMENT_NODE
54                      && node.getLocalName().equals(elementName)
55                      && node.getNamespaceURI().equals(namespace);
56              }
57          });
58      }
59  }