KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > debug > variablesfilterring > JSPVariablesFilter


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
20 package org.netbeans.modules.web.debug.variablesfilterring;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
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 /**
38  *
39  * @author Libor Kotouc
40  */

41 public class JSPVariablesFilter implements TreeModelFilter {
42     
43     private static final boolean verbose = true;
44     
45     
46     /** Creates a new instance of JSPVariablesFilter */
47     public JSPVariablesFilter() {
48     }
49
50     /**
51      *
52      * Returns filtered root of hierarchy.
53      *
54      * @param original the original tree model
55      * @return filtered root of hierarchy
56      */

57     public Object JavaDoc getRoot(TreeModel original) {
58         return original.getRoot ();
59     }
60
61     
62     
63     public Object JavaDoc[] getChildren(TreeModel original, Object JavaDoc parent, int from, int to)
64         throws UnknownTypeException
65     {
66         Object JavaDoc[] visibleChildren = null;
67         if (parent.equals (original.getRoot())) {
68             //retrieve all children
69
int parentChildrenCount = original.getChildrenCount(parent);
70             Object JavaDoc[] children = original.getChildren(parent, 0, parentChildrenCount);
71             parentChildrenCount = children.length;
72             if (parentChildrenCount == 1 && children[0] instanceof java.lang.String JavaDoc)
73                 return children;
74
75             List JavaDoc visibleChildrenList = new ArrayList JavaDoc();
76             ImplicitLocals implicitLocals = new ImplicitLocals();
77             Object JavaDoc 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 JavaDoc 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 JavaDoc 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 /*
118         else if (parent instanceof LocalVariable || parent instanceof Field) {
119             if (parent instanceof LocalVariable &&
120                 ((LocalVariable)parent).getDeclaredType().equals("javax.servlet.http.HttpSession")
121                 ||
122                 parent instanceof Field &&
123                 ((Field)parent).getDeclaredType().equals("javax.servlet.http.HttpSession"))
124             {
125                 int parentChildrenCount = original.getChildrenCount(parent);
126                 Object[] sessionChildren = original.getChildren(parent, 0, parentChildrenCount);
127                 //TODO find child with name "session"
128                 Object session = sessionChildren[0];
129                 visibleChildren = original.getChildren(session, from, to);
130             }
131             else if (parent instanceof LocalVariable &&
132                     ((LocalVariable)parent).getDeclaredType().equals("javax.servlet.ServletConfig")
133                     ||
134                     parent instanceof Field &&
135                     ((Field)parent).getDeclaredType().equals("javax.servlet.ServletConfig"))
136             {
137                 int parentChildrenCount = original.getChildrenCount(parent);
138                 Object[] configChildren = original.getChildren(parent, 0, parentChildrenCount);
139                 //TODO find child with name "session"
140                 Object config = configChildren[0];
141                 visibleChildren = original.getChildren(config, from, to);
142             }
143             else
144                 visibleChildren = original.getChildren(parent, from, to);
145         }
146 */

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 // Object[] attributes = ((AttributeMap)parent).entrySet().toArray();
152
// visibleChildren = Arrays.asList(attributes).subList(from, to).toArray();
153
}
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 JavaDoc node)
163         throws UnknownTypeException
164     {
165         
166         int countVisible = 0;
167
168         //in case of ROOT
169
if (node.equals (original.getRoot())) {
170             countVisible = original.getChildrenCount(node);
171             Object JavaDoc[] children = original.getChildren (node, 0, countVisible);
172             //original.getChildrenCount(...) needn't be equal to original.getChildren (...).length()
173
countVisible = children.length;
174             if (countVisible == 1 && children[0] instanceof java.lang.String JavaDoc)
175                 return countVisible;
176             for (int i = 0; i < children.length; i++) {
177                 Object JavaDoc var = children[i];
178                 //show the locals except of hidden locals and implicit locals
179
if (var instanceof LocalVariable) {
180                     if (isHiddenLocal(((LocalVariable)var).getName()) ||
181                         ImplicitLocals.isImplicitLocal(((LocalVariable)var).getName()))
182                         countVisible--;
183                 }
184                 //do not show anything but this
185
else if (!(var instanceof This))
186                     countVisible--;
187             }
188             //fold implicit locals and request/session/application attributes in the special nodes
189
countVisible += 4;
190         }
191 /*
192         else if (node instanceof LocalVariable || node instanceof Field) {
193             if (node instanceof LocalVariable &&
194                 ((LocalVariable)node).getDeclaredType().equals("javax.servlet.http.HttpSession")
195                 ||
196                 node instanceof Field &&
197                 ((Field)node).getDeclaredType().equals("javax.servlet.http.HttpSession"))
198             {
199                 //TODO retrieve children only _once_ for all cases (maybe not)
200                 countVisible = original.getChildrenCount(node);
201                 Object[] children = original.getChildren (node, 0, countVisible);
202                 //TODO find child with name "session"
203                 Object session = children[0];
204                 countVisible = original.getChildrenCount(session);
205             }
206             else if (node instanceof LocalVariable &&
207                     ((LocalVariable)node).getDeclaredType().equals("javax.servlet.ServletConfig")
208                     ||
209                     node instanceof Field &&
210                     ((Field)node).getDeclaredType().equals("javax.servlet.ServletConfig"))
211             {
212                 //TODO retrieve children only _once_ for all cases (maybe not)
213                 countVisible = original.getChildrenCount(node);
214                 Object[] children = original.getChildren (node, 0, countVisible);
215                 //TODO find child with name "config"
216                 Object config = children[0];
217                 countVisible = original.getChildrenCount(config);
218             }
219         }
220  */

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     /**
234      * Returns true if node is leaf. You should not throw UnknownTypeException
235      * directly from this method!
236      *
237      * @param original the original tree model
238      * @throws UnknownTypeException this exception can be thrown from
239      * <code>original.isLeaf (...)</code> method call only!
240      * @return true if node is leaf
241      */

242     public boolean isLeaf(TreeModel original, Object JavaDoc 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     /**
264      *
265      * Unregisters given listener.
266      *
267      * @param l the listener to remove
268      */

269     public void removeModelListener(ModelListener l) {
270     }
271
272     /**
273      *
274      * Registers given listener.
275      *
276      * @param l the listener to add
277      */

278     public void addModelListener(ModelListener l) {
279     }
280
281
282     private static HashSet JavaDoc hiddenLocals = null;
283     private static boolean isHiddenLocal(String JavaDoc aLocalName) {
284
285         if (hiddenLocals == null) {
286             hiddenLocals = new HashSet JavaDoc();
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 JavaDoc leafType = null;
298     private static boolean isLeafType (String JavaDoc type) {
299         if (leafType == null) {
300             leafType = new HashSet JavaDoc ();
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 //---------------------------------------------------------------------------------------
315
// inner classes
316
//---------------------------------------------------------------------------------------
317

318     public static class ImplicitLocals {
319         private List JavaDoc locals = new ArrayList JavaDoc ();
320         private static HashSet JavaDoc localsNames = null;
321
322         public static boolean isImplicitLocal(String JavaDoc aLocalName) {
323
324             if (localsNames == null) {
325                 localsNames = new HashSet JavaDoc();
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 JavaDoc getLocals () {
344             return locals;
345         }
346         
347         public boolean equals (Object JavaDoc 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 {// extends java.util.HashMap {
358
private ArrayList JavaDoc attributes = new ArrayList JavaDoc();
359         private ObjectVariable owner = null;
360         private String JavaDoc ownerName = null;
361
362         public class UnknownOwnerNameException extends RuntimeException JavaDoc {
363             public UnknownOwnerNameException(String JavaDoc name) {
364                 super("Unknown owner name: " + name);
365             }
366         };
367         
368         public AttributeMap(String JavaDoc aOwnerName) {
369             setOwnerName(aOwnerName);
370         }
371         
372         public AttributeMap(ObjectVariable aVar) {
373             owner = aVar;
374             setOwnerName(((LocalVariable)owner).getName());
375             Iterator JavaDoc 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 JavaDoc 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 JavaDoc getAttributes() { return attributes; }
390         public String JavaDoc getOwnerName() { return ownerName; }
391         
392         public class Attribute {
393             private String JavaDoc name;
394             private Variable value;
395             public Attribute(String JavaDoc aName, Variable aValue) {
396                 name = aName;
397                 value = aValue;
398             }
399             public String JavaDoc getName() { return name; }
400             public Variable getValue() { return value; }
401         }
402         
403         private class AttributeIterator implements Iterator JavaDoc {
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 JavaDoc 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 JavaDoc e) {
437                 }
438
439                 return ret;
440             }
441
442             public Object JavaDoc next() {
443                 
444                 Object JavaDoc 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 JavaDoc e) {
463                 }
464                 
465                 return nextElement;
466             }
467
468             public void remove() {
469                 throw new UnsupportedOperationException JavaDoc();
470             }
471
472         }
473     }
474 }
475
Popular Tags