KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > impl > llom > traverse > OMChildrenQNameIterator


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
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 package org.apache.axis2.om.impl.llom.traverse;
17
18 import org.apache.axis2.om.OMNode;
19 import org.apache.axis2.om.impl.llom.OMElementImpl;
20
21 import javax.xml.namespace.QName JavaDoc;
22
23 /**
24  * Class OMChildrenQNameIterator
25  */

26 public class OMChildrenQNameIterator extends OMChildrenIterator {
27     /**
28      * Field givenQName
29      */

30     private QName JavaDoc givenQName;
31
32     /**
33      * Field needToMoveForward
34      */

35     private boolean needToMoveForward = true;
36
37     /**
38      * Field isMatchingNodeFound
39      */

40     private boolean isMatchingNodeFound = false;
41
42     /**
43      * Constructor OMChildrenQNameIterator
44      *
45      * @param currentChild
46      * @param givenQName
47      */

48     public OMChildrenQNameIterator(OMNode currentChild, QName JavaDoc givenQName) {
49         super(currentChild);
50         this.givenQName = givenQName;
51     }
52
53     /**
54      * Returns <tt>true</tt> if the iteration has more elements. (In other
55      * words, returns <tt>true</tt> if <tt>next</tt> would return an element
56      * rather than throwing an exception.)
57      *
58      * @return <tt>true</tt> if the iterator has more elements.
59      */

60     public boolean hasNext() {
61         while (needToMoveForward) {
62             if (currentChild != null) {
63
64                 // check the current node for the criteria
65
if ((currentChild instanceof OMElementImpl)
66                         && (isQNamesMatch(
67                                    ((OMElementImpl) currentChild).getQName(),
68                                    this.givenQName))) {
69                     isMatchingNodeFound = true;
70                     needToMoveForward = false;
71                 } else {
72
73                     // get the next named node
74
currentChild = currentChild.getNextSibling();
75                     isMatchingNodeFound = needToMoveForward = !(currentChild
76                                                                            == null);
77                 }
78             } else {
79                 needToMoveForward = false;
80             }
81         }
82         return isMatchingNodeFound;
83     }
84
85     /**
86      * Returns the next element in the iteration.
87      *
88      * @return the next element in the iteration.
89      * @throws java.util.NoSuchElementException
90      * iteration has no more elements.
91      */

92     public Object JavaDoc next() {
93
94         // reset the flags
95
needToMoveForward = true;
96         isMatchingNodeFound = false;
97         nextCalled = true;
98         removeCalled = false;
99         lastChild = currentChild;
100         currentChild = currentChild.getNextSibling();
101         return lastChild;
102     }
103
104     /**
105      * Here I can not use the overriden equals method of QName, as one might want to get
106      * some element just by giving the localname, even though a matching element has a namespace uri as well.
107      * This will not be supported in the equals method of the QName
108      *
109      * @param elementQName
110      * @param qNameToBeMatched
111      * @return
112      */

113     private boolean isQNamesMatch(QName JavaDoc elementQName, QName JavaDoc qNameToBeMatched) {
114
115         // if no QName was given, that means one needs all
116
if (qNameToBeMatched == null) {
117             return true;
118         }
119
120         // if the given localname is null, whatever value this.qname has, its a match
121
boolean localNameMatch =
122         (qNameToBeMatched.getLocalPart() == null)
123                 || (qNameToBeMatched.getLocalPart() == "")
124                 || ((elementQName != null)
125                                && elementQName.getLocalPart().equalsIgnoreCase(
126                                        qNameToBeMatched.getLocalPart()));
127         boolean namespaceURIMatch =
128         (qNameToBeMatched.getNamespaceURI() == null)
129                 || (qNameToBeMatched.getNamespaceURI() == "")
130                 || ((elementQName != null)
131                                && elementQName.getNamespaceURI().equalsIgnoreCase(
132                                        qNameToBeMatched.getNamespaceURI()));
133         return localNameMatch && namespaceURIMatch;
134     }
135 }
136
Popular Tags