KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dom > traversal > NameNodeFilter


1 /*
2  * Copyright 1999,2000,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 dom.traversal;
17
18
19 import org.w3c.dom.Node JavaDoc;
20 import org.w3c.dom.traversal.NodeFilter;
21
22  /** An example filter which enables the client to set a <b>name</b> value
23   * accept those node names which <b>match</b> (or explicitly <b>not match</b>)
24   * the set name value.
25   */

26  public class NameNodeFilter implements NodeFilter {
27     
28     String JavaDoc fName;
29     boolean fMatch = true;
30             
31         /** The name to compare with the node name. If null, all node names
32          * are successfully matched.
33          */

34         public void setName(String JavaDoc name) {
35             this.fName = name;
36         }
37         
38         /** Return the name to compare with node name. If null, all node names
39          * are successfully matched. */

40         public String JavaDoc getName() {
41             return this.fName;
42         }
43         
44         /**
45          * Controls whether the node name is accepted when it <b>does</b> match
46          * the setName value, or when it <b>does not</b> match the setName value.
47          * If the setName value is null this match value does not matter, and
48          * all names will match.
49          * If match is true, the node name is accepted when it matches.
50          * If match is false, the node name is accepted when does not match.
51          */

52         public void setMatch(boolean match) {
53             this.fMatch = match;
54         }
55         
56         /** Return match value. */
57         public boolean getMatch() {
58             return this.fMatch;
59         }
60         
61         /** acceptNode determines if this filter accepts a node name or not. */
62         public short acceptNode(Node JavaDoc n) {
63
64             if (fName == null || fMatch && n.getNodeName().equals(fName)
65             || !fMatch && !n.getNodeName().equals(fName))
66                 return FILTER_ACCEPT;
67             else
68                 return FILTER_REJECT;
69         }
70     }
71
Popular Tags