KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > util > DevTools


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.util;
16
17 import java.beans.BeanInfo JavaDoc;
18 import java.beans.Introspector JavaDoc;
19 import java.beans.PropertyDescriptor JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.text.DateFormat JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.SortedMap JavaDoc;
35 import java.util.TreeMap JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 import javax.el.Expression;
39 import javax.faces.component.UIComponent;
40 import javax.faces.component.UIViewRoot;
41 import javax.faces.context.ExternalContext;
42 import javax.faces.context.FacesContext;
43 import javax.faces.el.MethodBinding;
44 import javax.faces.el.ValueBinding;
45
46 public final class DevTools {
47     
48     private final static String JavaDoc TS = "<";
49     
50     private static final String JavaDoc ERROR_TEMPLATE = "META-INF/rsc/facelet-dev-error.xml";
51     
52     private static String JavaDoc[] ERROR_PARTS;
53     
54     private static final String JavaDoc DEBUG_TEMPLATE = "META-INF/rsc/facelet-dev-debug.xml";
55     
56     private static String JavaDoc[] DEBUG_PARTS;
57
58     public DevTools() {
59         super();
60     }
61     
62     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
63         DevTools.init();
64     }
65     
66     private static void init() throws IOException JavaDoc {
67         if (ERROR_PARTS == null) {
68             ERROR_PARTS = splitTemplate(ERROR_TEMPLATE);
69         }
70         
71         if (DEBUG_PARTS == null) {
72             DEBUG_PARTS = splitTemplate(DEBUG_TEMPLATE);
73         }
74     }
75     
76     private static String JavaDoc[] splitTemplate(String JavaDoc rsc) throws IOException JavaDoc {
77         InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(rsc);
78         if (is == null) {
79             throw new FileNotFoundException JavaDoc(rsc);
80         }
81         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
82         byte[] buff = new byte[512];
83         int read;
84         while ((read = is.read(buff)) != -1) {
85             baos.write(buff, 0, read);
86         }
87         String JavaDoc str = baos.toString();
88         return str.split("@@");
89     }
90     
91     public static void debugHtml(Writer JavaDoc writer, FacesContext faces, Exception JavaDoc e) throws IOException JavaDoc {
92         init();
93         Date JavaDoc now = new Date JavaDoc();
94         for (int i = 0; i < ERROR_PARTS.length; i++) {
95             if ("message".equals(ERROR_PARTS[i])) {
96                 String JavaDoc msg = e.getMessage();
97                 if (msg != null) {
98                     writer.write(msg.replaceAll("<", TS));
99                 } else {
100                     writer.write(e.getClass().getName());
101                 }
102             } else if ("trace".equals(ERROR_PARTS[i])) {
103                 writeException(writer, e);
104             } else if ("now".equals(ERROR_PARTS[i])) {
105                 writer.write(DateFormat.getDateTimeInstance().format(now));
106             } else if ("tree".equals(ERROR_PARTS[i])) {
107                 writeComponent(writer, faces.getViewRoot());
108             } else if ("vars".equals(ERROR_PARTS[i])) {
109                 writeVariables(writer, faces);
110             } else {
111                 writer.write(ERROR_PARTS[i]);
112             }
113         }
114     }
115     
116     private static void writeException(Writer JavaDoc writer, Exception JavaDoc e) throws IOException JavaDoc {
117         StringWriter JavaDoc str = new StringWriter JavaDoc(256);
118         PrintWriter JavaDoc pstr = new PrintWriter JavaDoc(str);
119         e.printStackTrace(pstr);
120         pstr.close();
121         writer.write(str.toString().replaceAll("<", TS));
122     }
123     
124     public static void debugHtml(Writer JavaDoc writer, FacesContext faces) throws IOException JavaDoc {
125         init();
126         Date JavaDoc now = new Date JavaDoc();
127         for (int i = 0; i < DEBUG_PARTS.length; i++) {
128             if ("message".equals(DEBUG_PARTS[i])) {
129                 writer.write(faces.getViewRoot().getViewId());
130             } else if ("now".equals(DEBUG_PARTS[i])) {
131                 writer.write(DateFormat.getDateTimeInstance().format(now));
132             } else if ("tree".equals(DEBUG_PARTS[i])) {
133                 writeComponent(writer, faces.getViewRoot());
134             } else if ("vars".equals(DEBUG_PARTS[i])) {
135                 writeVariables(writer, faces);
136             } else {
137                 writer.write(DEBUG_PARTS[i]);
138             }
139         }
140     }
141     
142     private static void writeVariables(Writer JavaDoc writer, FacesContext faces) throws IOException JavaDoc {
143         ExternalContext ctx = faces.getExternalContext();
144         writeVariables(writer, ctx.getRequestParameterMap(), "Request Parameters");
145         writeVariables(writer, ctx.getRequestMap(), "Request Attributes");
146         if (ctx.getSession(false) != null) {
147             writeVariables(writer, ctx.getSessionMap(), "Session Attributes");
148         }
149         writeVariables(writer, ctx.getApplicationMap(), "Application Attributes");
150     }
151     
152     private static void writeVariables(Writer JavaDoc writer, Map JavaDoc vars, String JavaDoc caption) throws IOException JavaDoc {
153         writer.write("<table><caption>");
154         writer.write(caption);
155         writer.write("</caption><thead><tr><th style=\"width: 10%; \">Name</th><th style=\"width: 90%; \">Value</th></tr></thead><tbody>");
156         boolean written = false;
157         if (!vars.isEmpty()) {
158             SortedMap JavaDoc map = new TreeMap JavaDoc(vars);
159             Map.Entry JavaDoc entry = null;
160             String JavaDoc key = null;
161             for (Iterator JavaDoc itr = map.entrySet().iterator(); itr.hasNext(); ) {
162                 entry = (Map.Entry JavaDoc) itr.next();
163                 key = entry.getKey().toString();
164                 if (key.indexOf('.') == -1) {
165                     writer.write("<tr><td>");
166                     writer.write(key.replaceAll("<", TS));
167                     writer.write("</td><td>");
168                     writer.write(entry.getValue().toString().replaceAll("<", TS));
169                     writer.write("</td></tr>");
170                     written = true;
171                 }
172             }
173         }
174         if (!written) {
175             writer.write("<tr><td colspan=\"2\"><em>None</em></td></tr>");
176         }
177         writer.write("</tbody></table>");
178     }
179     
180     private static void writeComponent(Writer JavaDoc writer, UIComponent c) throws IOException JavaDoc {
181         writer.write("<dl><dt");
182         if (isText(c)) {
183             writer.write(" class=\"uicText\"");
184         }
185         writer.write(">");
186         
187         boolean hasChildren = c.getChildCount() > 0 || c.getFacets().size() > 0;
188         
189         writeStart(writer, c, hasChildren);
190         writer.write("</dt>");
191         if (hasChildren) {
192             if (c.getFacets().size() > 0) {
193                 Map.Entry JavaDoc entry;
194                 for (Iterator JavaDoc itr = c.getFacets().entrySet().iterator(); itr.hasNext(); ) {
195                     entry = (Map.Entry JavaDoc) itr.next();
196                     writer.write("<dd class=\"uicFacet\">");
197                     writer.write("<span>");
198                     writer.write((String JavaDoc) entry.getKey());
199                     writer.write("</span>");
200                     writeComponent(writer, (UIComponent) entry.getValue());
201                     writer.write("</dd>");
202                 }
203             }
204             if (c.getChildCount() > 0) {
205                 for (Iterator JavaDoc itr = c.getChildren().iterator(); itr.hasNext(); ) {
206                     writer.write("<dd>");
207                     writeComponent(writer, (UIComponent) itr.next());
208                     writer.write("</dd>");
209                 }
210             }
211             writer.write("<dt>");
212             writeEnd(writer, c);
213             writer.write("</dt>");
214         }
215         writer.write("</dl>");
216     }
217     
218     private static void writeEnd(Writer JavaDoc writer, UIComponent c) throws IOException JavaDoc {
219         if (!isText(c)) {
220             writer.write(TS);
221             writer.write('/');
222             writer.write(getName(c));
223             writer.write('>');
224         }
225     }
226     
227     private final static String JavaDoc[] IGNORE = new String JavaDoc[] { "parent", "rendererType" };
228     
229     private static void writeAttributes(Writer JavaDoc writer, UIComponent c) {
230         try {
231             BeanInfo JavaDoc info = Introspector.getBeanInfo(c.getClass());
232             PropertyDescriptor JavaDoc[] pd = info.getPropertyDescriptors();
233             Method JavaDoc m = null;
234             Object JavaDoc v = null;
235             String JavaDoc str = null;
236             for (int i = 0; i < pd.length; i++) {
237                 if (pd[i].getWriteMethod() != null && Arrays.binarySearch(IGNORE, pd[i].getName()) < 0) {
238                     m = pd[i].getReadMethod();
239                     try {
240                         v = m.invoke(c, null);
241                         if (v != null) {
242                             if (v instanceof Collection JavaDoc || v instanceof Map JavaDoc || v instanceof Iterator JavaDoc) {
243                                 continue;
244                             }
245                             writer.write(" ");
246                             writer.write(pd[i].getName());
247                             writer.write("=\"");
248                             if (v instanceof Expression) {
249                                 str = ((Expression) v).getExpressionString();
250                             } else if (v instanceof ValueBinding) {
251                                 str = ((ValueBinding) v).getExpressionString();
252                             } else if (v instanceof MethodBinding) {
253                                 str = ((MethodBinding) v).getExpressionString();
254                             } else {
255                                 str = v.toString();
256                             }
257                             writer.write(str.replaceAll("<", TS));
258                             writer.write("\"");
259                         }
260                     } catch (Exception JavaDoc e) {
261                         // do nothing
262
}
263                 }
264             }
265
266             ValueBinding binding = c.getValueBinding("binding");
267             if (binding != null) {
268                 writer.write(" binding=\"");
269                 writer.write(binding.getExpressionString().replaceAll("<", TS));
270                 writer.write("\"");
271             }
272         } catch (Exception JavaDoc e) {
273             // do nothing
274
}
275     }
276     
277     private static void writeStart(Writer JavaDoc writer, UIComponent c, boolean children) throws IOException JavaDoc {
278         if (isText(c)) {
279             String JavaDoc str = c.toString().trim();
280             writer.write(str.replaceAll("<", TS));
281         } else {
282             writer.write(TS);
283             writer.write(getName(c));
284             writeAttributes(writer, c);
285             if (children) {
286                 writer.write('>');
287             } else {
288                 writer.write("/>");
289             }
290         }
291     }
292     
293     private static String JavaDoc getName(UIComponent c) {
294         String JavaDoc nm = c.getClass().getName();
295         return nm.substring(nm.lastIndexOf('.') + 1);
296     }
297     
298     private static boolean isText(UIComponent c) {
299         return (c.getClass().getName().startsWith("com.sun.facelets.compiler"));
300     }
301
302 }
303
Popular Tags