KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > traversal > TreeNodeFilter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax.traversal;
20
21 import java.io.*;
22 import java.util.Arrays JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Vector JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.netbeans.tax.*;
29
30 /**
31  *
32  * @author Libor Kramolis
33  * @version 0.1
34  */

35 public final class TreeNodeFilter {
36
37     // Constants returned by acceptNode
38
/** */
39     public static final short FILTER_ACCEPT = 0;
40     /** */
41     public static final short FILTER_REJECT = 1;
42     // /** */
43
// public static final short FILTER_SKIP = 2;
44

45
46     // Constants of acceptPolicy property
47
/** */
48     public static final short ACCEPT_TYPES = 10;
49     /** */
50     public static final short REJECT_TYPES = 11;
51     
52     
53     /** */
54     private Class JavaDoc[] nodeTypes;
55     /** */
56     private short acceptPolicy;
57     
58     
59     /** */
60     public static final TreeNodeFilter SHOW_ALL_FILTER = new TreeNodeFilter ();
61     /** */
62     public static final TreeNodeFilter SHOW_DATA_ONLY_FILTER =
63     new TreeNodeFilter (new Class JavaDoc[] { TreeComment.class, TreeProcessingInstruction.class }, REJECT_TYPES);
64     
65     
66     //
67
// init
68
//
69

70     /** */
71     public TreeNodeFilter (Class JavaDoc[] nodeTypes, short acceptPolicy) throws IllegalArgumentException JavaDoc {
72         for (int i = 0; i < nodeTypes.length; i++) {
73             if ( isValidNodeType (nodeTypes[i]) == false ) {
74                 throw new IllegalArgumentException JavaDoc ();
75             }
76         }
77         
78         this.nodeTypes = nodeTypes;
79         this.acceptPolicy = acceptPolicy;
80     }
81     
82     /** */
83     public TreeNodeFilter (Class JavaDoc[] nodeTypes) {
84         this (nodeTypes, ACCEPT_TYPES);
85     }
86     
87     /** */
88     public TreeNodeFilter () {
89         this (new Class JavaDoc[] { TreeNode.class });
90     }
91     
92     
93     //
94
// itself
95
//
96

97     /**
98      */

99     public short acceptNode (TreeNode node) {
100         short isInstanceReturn;
101         short isNotInstanceReturn;
102         
103         if ( acceptPolicy == ACCEPT_TYPES ) {
104             isInstanceReturn = FILTER_ACCEPT;
105             isNotInstanceReturn = FILTER_REJECT;
106         } else {
107             isInstanceReturn = FILTER_REJECT;
108             isNotInstanceReturn = FILTER_ACCEPT;
109         }
110         
111         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ TreeNodeFilter::acceptNode: this = " + this); // NOI18N
112
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ ::acceptNode: node = " + node); // NOI18N
113
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ ::acceptNode: acceptPolicy = " + acceptPolicy); // NOI18N
114

115         for (int i = 0; i < nodeTypes.length; i++) {
116             if ( nodeTypes[i].isInstance (node) ) {
117                 
118                 if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ ::acceptNode: RETURN " + isInstanceReturn); // NOI18N
119

120                 return isInstanceReturn;
121             }
122         }
123         
124         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ ::acceptNode: RETURN " + isNotInstanceReturn); // NOI18N
125

126         return isNotInstanceReturn;
127     }
128     
129     
130     /**
131      */

132     public boolean equals (Object JavaDoc object) {
133         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("-=#| TreeNodeFilter.equals");
134         
135         if ( (object instanceof TreeNodeFilter) == false ) {
136             return false;
137         }
138         
139         TreeNodeFilter peer = (TreeNodeFilter)object;
140         
141         Set JavaDoc thisSet = new HashSet JavaDoc (Arrays.asList (this.nodeTypes));
142         Set JavaDoc peerSet = new HashSet JavaDoc (Arrays.asList (peer.nodeTypes));
143         
144         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("-=#| thisSet = " + thisSet);
145         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("-=#| peerSet = " + peerSet);
146         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("-=#| acceptPolicy? " + (this.acceptPolicy == peer.acceptPolicy));
147         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("-=#| nodeTypes ? " + (thisSet.equals (peerSet)));
148         
149         if ( this.acceptPolicy != peer.acceptPolicy ) {
150             return false;
151         }
152         
153         return thisSet.equals (peerSet);
154     }
155     
156     
157     /**
158      */

159     public Class JavaDoc[] getNodeTypes () {
160         return nodeTypes;
161     }
162     
163     /**
164      */

165     public short getAcceptPolicy () {
166         return acceptPolicy;
167     }
168     
169     /**
170      */

171     public static boolean isValidNodeType (Class JavaDoc type) {
172         if ( TreeNode.class.isAssignableFrom (type) ) {
173             return true;
174         }
175         if ( TreeCharacterData.class.isAssignableFrom (type) ) {
176             return true;
177         }
178         if ( TreeReference.class.isAssignableFrom (type) ) {
179             return true;
180         }
181         if ( TreeEntityReference.class.isAssignableFrom (type) ) {
182             return true;
183         }
184         if ( TreeNodeDecl.class.isAssignableFrom (type) ) {
185             return true;
186         }
187         
188         return false;
189     }
190     
191     
192     /**
193      */

194     public String JavaDoc toString () {
195         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
196         
197         sb.append (super.toString ()).append (" [ ");
198         sb.append ("acceptPolicy= [").append (acceptPolicy).append ("] ");
199         if ( acceptPolicy == ACCEPT_TYPES ) {
200             sb.append ("ACCEPT");
201         } else {
202             sb.append ("REJECT");
203         }
204         sb.append (" | nodeTypes= [");
205         for (int i = 0; i < nodeTypes.length; i++) {
206             if ( i != 0 ) {
207                 sb.append (" | ");
208             }
209             sb.append (nodeTypes[i].getName ());
210         }
211         sb.append ("] ]");
212         
213         return sb.toString ();
214     }
215     
216 }
217
Popular Tags