1 19 package org.netbeans.tax.traversal; 20 21 import java.io.*; 22 import java.util.Arrays ; 23 import java.util.Set ; 24 import java.util.HashSet ; 25 import java.util.Vector ; 26 import java.util.Iterator ; 27 28 import org.netbeans.tax.*; 29 30 35 public final class TreeNodeFilter { 36 37 39 public static final short FILTER_ACCEPT = 0; 40 41 public static final short FILTER_REJECT = 1; 42 45 46 48 public static final short ACCEPT_TYPES = 10; 49 50 public static final short REJECT_TYPES = 11; 51 52 53 54 private Class [] 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 [] { TreeComment.class, TreeProcessingInstruction.class }, REJECT_TYPES); 64 65 66 70 71 public TreeNodeFilter (Class [] nodeTypes, short acceptPolicy) throws IllegalArgumentException { 72 for (int i = 0; i < nodeTypes.length; i++) { 73 if ( isValidNodeType (nodeTypes[i]) == false ) { 74 throw new IllegalArgumentException (); 75 } 76 } 77 78 this.nodeTypes = nodeTypes; 79 this.acceptPolicy = acceptPolicy; 80 } 81 82 83 public TreeNodeFilter (Class [] nodeTypes) { 84 this (nodeTypes, ACCEPT_TYPES); 85 } 86 87 88 public TreeNodeFilter () { 89 this (new Class [] { TreeNode.class }); 90 } 91 92 93 97 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() ) Util.THIS.debug ("+ TreeNodeFilter::acceptNode: this = " + this); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("+ ::acceptNode: node = " + node); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("+ ::acceptNode: acceptPolicy = " + acceptPolicy); 115 for (int i = 0; i < nodeTypes.length; i++) { 116 if ( nodeTypes[i].isInstance (node) ) { 117 118 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("+ ::acceptNode: RETURN " + isInstanceReturn); 120 return isInstanceReturn; 121 } 122 } 123 124 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("+ ::acceptNode: RETURN " + isNotInstanceReturn); 126 return isNotInstanceReturn; 127 } 128 129 130 132 public boolean equals (Object object) { 133 if ( Util.THIS.isLoggable() ) 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 thisSet = new HashSet (Arrays.asList (this.nodeTypes)); 142 Set peerSet = new HashSet (Arrays.asList (peer.nodeTypes)); 143 144 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("-=#| thisSet = " + thisSet); 145 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("-=#| peerSet = " + peerSet); 146 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("-=#| acceptPolicy? " + (this.acceptPolicy == peer.acceptPolicy)); 147 if ( Util.THIS.isLoggable() ) 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 159 public Class [] getNodeTypes () { 160 return nodeTypes; 161 } 162 163 165 public short getAcceptPolicy () { 166 return acceptPolicy; 167 } 168 169 171 public static boolean isValidNodeType (Class 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 194 public String toString () { 195 StringBuffer sb = new StringBuffer (); 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 |