KickJava   Java API By Example, From Geeks To Geeks.

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


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.OMAttribute;
19 import org.apache.axis2.om.OMElement;
20 import org.apache.axis2.om.OMNode;
21
22 import javax.xml.namespace.QName JavaDoc;
23
24 /**
25  * Class OMChildrenWithSpecificAttributeIterator
26  */

27 public class OMChildrenWithSpecificAttributeIterator
28         extends OMChildrenIterator {
29     /**
30      * Field attributeName
31      */

32     private QName JavaDoc attributeName;
33
34     /**
35      * Field attributeValue
36      */

37     private String JavaDoc attributeValue;
38
39     /**
40      * Field detach
41      */

42     private boolean detach;
43
44     /**
45      * Constructor OMChildrenWithSpecificAttributeIterator
46      *
47      * @param currentChild
48      * @param attributeName
49      * @param attributeValue
50      * @param detach
51      */

52     public OMChildrenWithSpecificAttributeIterator(OMNode currentChild,
53                                                    QName JavaDoc attributeName, String JavaDoc attributeValue, boolean detach) {
54         super(currentChild);
55         this.attributeName = attributeName;
56         this.attributeValue = attributeValue;
57         this.detach = detach;
58     }
59
60     /**
61      * Method hasNext
62      *
63      * @return
64      */

65     public boolean hasNext() {
66
67         // First check whether we have a child, using the super class
68
if (currentChild == null) {
69             return false;
70         }
71         boolean isMatchingNodeFound = false;
72         boolean needToMoveForward = true;
73
74         // now we have a child to check. If its an OMElement and matches the criteria, then we are done
75
while (needToMoveForward) {
76
77             // check the current node for the criteria
78
if (currentChild instanceof OMElement) {
79                 OMAttribute attr =
80                         ((OMElement) currentChild).getFirstAttribute(
81                         attributeName);
82                 if ((attr != null)
83                         && attr.getValue().equalsIgnoreCase(attributeValue)) {
84                     isMatchingNodeFound = true;
85                     needToMoveForward = false;
86                 } else {
87                     currentChild = currentChild.getNextSibling();
88                     needToMoveForward = !(currentChild == null);
89                 }
90             } else {
91
92                 // get the next named node
93
currentChild = currentChild.getNextSibling();
94                 needToMoveForward = !(currentChild == null);
95             }
96         }
97         return isMatchingNodeFound;
98     }
99
100     /**
101      * Method next
102      *
103      * @return
104      */

105     public Object JavaDoc next() {
106         Object JavaDoc o = super.next();
107         if ((o != null) && detach) {
108             ((OMElement) o).detach();
109         }
110         return o;
111     }
112 }
113
Popular Tags