1 19 20 package org.netbeans.modules.web.debug.variablesfilterring; 21 22 import java.util.ArrayList ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import org.netbeans.api.debugger.jpda.InvalidExpressionException; 27 import org.netbeans.api.debugger.jpda.LocalVariable; 28 import org.netbeans.api.debugger.jpda.ObjectVariable; 29 import org.netbeans.api.debugger.jpda.This; 30 import org.netbeans.api.debugger.jpda.Variable; 31 import org.netbeans.spi.viewmodel.TreeModel; 32 import org.netbeans.spi.viewmodel.TreeModelFilter; 33 import org.netbeans.spi.viewmodel.ModelListener; 34 import org.netbeans.spi.viewmodel.UnknownTypeException; 35 36 37 41 public class JSPVariablesFilter implements TreeModelFilter { 42 43 private static final boolean verbose = true; 44 45 46 47 public JSPVariablesFilter() { 48 } 49 50 57 public Object getRoot(TreeModel original) { 58 return original.getRoot (); 59 } 60 61 62 63 public Object [] getChildren(TreeModel original, Object parent, int from, int to) 64 throws UnknownTypeException 65 { 66 Object [] visibleChildren = null; 67 if (parent.equals (original.getRoot())) { 68 int parentChildrenCount = original.getChildrenCount(parent); 70 Object [] children = original.getChildren(parent, 0, parentChildrenCount); 71 parentChildrenCount = children.length; 72 if (parentChildrenCount == 1 && children[0] instanceof java.lang.String ) 73 return children; 74 75 List visibleChildrenList = new ArrayList (); 76 ImplicitLocals implicitLocals = new ImplicitLocals(); 77 Object refThis = null; 78 AttributeMap requestAttributes = new AttributeMap("request"); 79 AttributeMap sessionAttributes = new AttributeMap("session"); 80 AttributeMap applicationAttributes = new AttributeMap("application"); 81 for (int i = 0; i < parentChildrenCount; i++) { 82 83 Object var = children[i]; 84 85 if (var instanceof LocalVariable) { 86 LocalVariable lvar = (LocalVariable)var; 87 if (ImplicitLocals.isImplicitLocal(lvar.getName())) { 88 implicitLocals.addLocal(lvar); 89 90 if (lvar instanceof ObjectVariable) { 91 String varName = lvar.getName(); 92 if (varName.equals("request")) 93 requestAttributes = new AttributeMap((ObjectVariable)lvar); 94 else if (varName.equals("session")) 95 sessionAttributes = new AttributeMap((ObjectVariable)lvar); 96 else if (varName.equals("application")) 97 applicationAttributes = new AttributeMap((ObjectVariable)lvar); 98 } 99 } 100 else if (!isHiddenLocal(lvar.getName())) { 101 visibleChildrenList.add(var); 102 } 103 } 104 else if (var instanceof This) 105 refThis = var; 106 } 107 108 visibleChildrenList.add(0, applicationAttributes); 109 visibleChildrenList.add(0, sessionAttributes); 110 visibleChildrenList.add(0, requestAttributes); 111 if (refThis != null) 112 visibleChildrenList.add(0, refThis); 113 visibleChildrenList.add(implicitLocals); 114 115 visibleChildren = visibleChildrenList.subList(from, to).toArray(); 116 } 117 147 else if (parent instanceof ImplicitLocals) 148 visibleChildren = ((ImplicitLocals)parent).getLocals().subList(from, to).toArray (); 149 else if (parent instanceof AttributeMap) { 150 visibleChildren = ((AttributeMap)parent).getAttributes().subList(from, to).toArray(); 151 } 154 else if (parent instanceof AttributeMap.Attribute) 155 visibleChildren = original.getChildren(((AttributeMap.Attribute)parent).getValue(), from, to); 156 else 157 visibleChildren = original.getChildren(parent, from, to); 158 159 return visibleChildren; 160 } 161 162 public int getChildrenCount(TreeModel original, Object node) 163 throws UnknownTypeException 164 { 165 166 int countVisible = 0; 167 168 if (node.equals (original.getRoot())) { 170 countVisible = original.getChildrenCount(node); 171 Object [] children = original.getChildren (node, 0, countVisible); 172 countVisible = children.length; 174 if (countVisible == 1 && children[0] instanceof java.lang.String ) 175 return countVisible; 176 for (int i = 0; i < children.length; i++) { 177 Object var = children[i]; 178 if (var instanceof LocalVariable) { 180 if (isHiddenLocal(((LocalVariable)var).getName()) || 181 ImplicitLocals.isImplicitLocal(((LocalVariable)var).getName())) 182 countVisible--; 183 } 184 else if (!(var instanceof This)) 186 countVisible--; 187 } 188 countVisible += 4; 190 } 191 221 else if (node instanceof ImplicitLocals) 222 countVisible = ((ImplicitLocals)node).getLocals().size(); 223 else if (node instanceof AttributeMap) 224 countVisible = ((AttributeMap)node).getAttributes().size(); 225 else if (node instanceof AttributeMap.Attribute) 226 countVisible = original.getChildrenCount(((AttributeMap.Attribute)node).getValue()); 227 else 228 countVisible = original.getChildrenCount(node); 229 230 return countVisible; 231 } 232 233 242 public boolean isLeaf(TreeModel original, Object node) 243 throws UnknownTypeException 244 { 245 boolean il; 246 if (node instanceof ImplicitLocals) 247 il = false; 248 else if (node instanceof AttributeMap) 249 il = false; 250 else if (node instanceof AttributeMap.Attribute) { 251 Variable attributeValue = ((AttributeMap.Attribute)node).getValue(); 252 if (isLeafType(attributeValue.getType())) 253 il = true; 254 else 255 il = original.isLeaf(attributeValue); 256 } 257 else 258 il = original.isLeaf(node); 259 260 return il; 261 } 262 263 269 public void removeModelListener(ModelListener l) { 270 } 271 272 278 public void addModelListener(ModelListener l) { 279 } 280 281 282 private static HashSet hiddenLocals = null; 283 private static boolean isHiddenLocal(String aLocalName) { 284 285 if (hiddenLocals == null) { 286 hiddenLocals = new HashSet (); 287 288 hiddenLocals.add("_jspxFactory"); 289 hiddenLocals.add("_jspx_out"); 290 hiddenLocals.add("_jspx_page_context"); 291 292 } 293 294 return hiddenLocals.contains(aLocalName); 295 } 296 297 private static HashSet leafType = null; 298 private static boolean isLeafType (String type) { 299 if (leafType == null) { 300 leafType = new HashSet (); 301 leafType.add ("java.lang.String"); 302 leafType.add ("java.lang.Character"); 303 leafType.add ("java.lang.Integer"); 304 leafType.add ("java.lang.Float"); 305 leafType.add ("java.lang.Byte"); 306 leafType.add ("java.lang.Boolean"); 307 leafType.add ("java.lang.Double"); 308 leafType.add ("java.lang.Long"); 309 leafType.add ("java.lang.Short"); 310 } 311 return leafType.contains (type); 312 } 313 314 318 public static class ImplicitLocals { 319 private List locals = new ArrayList (); 320 private static HashSet localsNames = null; 321 322 public static boolean isImplicitLocal(String aLocalName) { 323 324 if (localsNames == null) { 325 localsNames = new HashSet (); 326 localsNames.add("application"); 327 localsNames.add("config"); 328 localsNames.add("out"); 329 localsNames.add("page"); 330 localsNames.add("pageContext"); 331 localsNames.add("request"); 332 localsNames.add("response"); 333 localsNames.add("session"); 334 } 335 336 return localsNames.contains(aLocalName); 337 } 338 339 void addLocal (LocalVariable local) { 340 locals.add (local); 341 } 342 343 List getLocals () { 344 return locals; 345 } 346 347 public boolean equals (Object o) { 348 return o instanceof ImplicitLocals; 349 } 350 351 public int hashCode () { 352 if (locals.size () == 0) return super.hashCode (); 353 return locals.get (0).hashCode (); 354 } 355 } 356 357 public static class AttributeMap { private ArrayList attributes = new ArrayList (); 359 private ObjectVariable owner = null; 360 private String ownerName = null; 361 362 public class UnknownOwnerNameException extends RuntimeException { 363 public UnknownOwnerNameException(String name) { 364 super("Unknown owner name: " + name); 365 } 366 }; 367 368 public AttributeMap(String aOwnerName) { 369 setOwnerName(aOwnerName); 370 } 371 372 public AttributeMap(ObjectVariable aVar) { 373 owner = aVar; 374 setOwnerName(((LocalVariable)owner).getName()); 375 Iterator it = new AttributeIterator(); 376 while (it.hasNext()) { 377 Attribute attribute = (Attribute)it.next(); 378 attributes.add(attribute); 379 } 380 } 381 382 private void setOwnerName(String aOwnerName) { 383 if (aOwnerName.equals("request") || aOwnerName.equals("session") || aOwnerName.equals("application")) 384 ownerName = aOwnerName; 385 else 386 throw new UnknownOwnerNameException(aOwnerName); 387 } 388 389 public ArrayList getAttributes() { return attributes; } 390 public String getOwnerName() { return ownerName; } 391 392 public class Attribute { 393 private String name; 394 private Variable value; 395 public Attribute(String aName, Variable aValue) { 396 name = aName; 397 value = aValue; 398 } 399 public String getName() { return name; } 400 public Variable getValue() { return value; } 401 } 402 403 private class AttributeIterator implements Iterator { 404 ObjectVariable reqAttributes = null; 405 406 public AttributeIterator() { 407 try { 408 reqAttributes = (ObjectVariable)owner.invokeMethod( 409 "getAttributeNames", 410 "()Ljava/util/Enumeration;", 411 new Variable[0] 412 ); 413 } 414 catch (InvalidExpressionException e) { 415 } 416 catch (NoSuchMethodException e) { 417 } 418 } 419 420 421 public boolean hasNext() { 422 423 if (reqAttributes == null) return false; 424 425 boolean ret = false; 426 try { 427 Variable hasMoreElements = reqAttributes.invokeMethod( 428 "hasMoreElements", 429 "()Z", 430 new Variable[0] 431 ); 432 ret = (hasMoreElements != null && "true".equals(hasMoreElements.getValue())); 433 } 434 catch (InvalidExpressionException e) { 435 } 436 catch (NoSuchMethodException e) { 437 } 438 439 return ret; 440 } 441 442 public Object next() { 443 444 Object nextElement = null; 445 try { 446 Variable attributeName = reqAttributes.invokeMethod( 447 "nextElement", 448 "()Ljava/lang/Object;", 449 new Variable[0] 450 ); 451 Variable attributeValue = owner.invokeMethod( 452 "getAttribute", 453 "(Ljava/lang/String;)Ljava/lang/Object;", 454 new Variable[] { attributeName } 455 ); 456 nextElement = new AttributeMap.Attribute( 457 (attributeName.getValue() == null ? "" : attributeName.getValue()), 458 attributeValue); 459 } 460 catch (InvalidExpressionException e) { 461 } 462 catch (NoSuchMethodException e) { 463 } 464 465 return nextElement; 466 } 467 468 public void remove() { 469 throw new UnsupportedOperationException (); 470 } 471 472 } 473 } 474 } 475 | Popular Tags |