KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > metadata > generators > TLDGenerator


1
2 /*
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * "The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
12  * License for the specific language governing rights and limitations under
13  * the License.
14  *
15  * The Original Code is ICEfaces 1.5 open source software code, released
16  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
17  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
18  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
19  *
20  * Contributor(s): _____________________.
21  *
22  * Alternatively, the contents of this file may be used under the terms of
23  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
24  * License), in which case the provisions of the LGPL License are
25  * applicable instead of those above. If you wish to allow use of your
26  * version of this file only under the terms of the LGPL License and not to
27  * allow others to use your version of this file under the MPL, indicate
28  * your decision by deleting the provisions above and replace them with
29  * the notice and other provisions required by the LGPL License. If you do
30  * not delete the provisions above, a recipient may use your version of
31  * this file under either the MPL or the LGPL License."
32  *
33  */

34
35 package com.icesoft.metadata.generators;
36
37 import com.icesoft.jsfmeta.util.AbstractGenerator;
38 import com.icesoft.jsfmeta.util.InternalConfig;
39 import java.io.BufferedWriter JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.FileWriter JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44 import java.util.HashSet JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.Set JavaDoc;
47 import java.util.TreeMap JavaDoc;
48
49 import com.sun.rave.jsfmeta.beans.AttributeBean;
50 import com.sun.rave.jsfmeta.beans.ComponentBean;
51 import com.sun.rave.jsfmeta.beans.DescriptionBean;
52 import com.sun.rave.jsfmeta.beans.PropertyBean;
53 import com.sun.rave.jsfmeta.beans.RendererBean;
54 import java.util.logging.Level JavaDoc;
55
56 /*
57  * DescriptorGenerator generate tld file
58  *
59  *
60  */

61 public class TLDGenerator extends AbstractGenerator {
62
63         
64     private TreeMap JavaDoc attributes;
65     
66     private PrintWriter JavaDoc writer;
67     
68     private boolean base;
69     
70     private static PropertyBean binding;
71     
72     private String JavaDoc descriptor;
73     
74     private String JavaDoc listeners[];
75     
76     private String JavaDoc prefix;
77     
78     private String JavaDoc tagClassPackage;
79     
80     private String JavaDoc uri;
81     
82     private String JavaDoc validators[];
83     
84     static {
85         binding = new PropertyBean();
86         binding.setPropertyName("binding");
87         String JavaDoc db_string = "The value binding expression linking this component to a property in a backing bean";
88         DescriptionBean dbBean = new DescriptionBean("", db_string);
89         binding.addDescription(dbBean);
90         binding.setRequired(false);
91     }
92     
93     public TLDGenerator(InternalConfig internalConfig) {
94         
95         super(internalConfig);
96         attributes = new TreeMap JavaDoc();
97         writer = null;
98         base = false;
99         descriptor = internalConfig.getProperty("project.tld.file");
100         listeners = null;
101         prefix = internalConfig.getProperty("project.taglib.prefix");
102         tagClassPackage = internalConfig.getProperty("project.taglib.package");
103         uri = internalConfig.getProperty("project.taglib.uri");
104         validators = null;
105     }
106     
107     public boolean getBase() {
108         return base;
109     }
110     
111     public void setBase(boolean base) {
112         this.base = base;
113     }
114     
115     public String JavaDoc getDescriptor() {
116         return descriptor;
117     }
118     
119     public void setDescriptor(String JavaDoc descriptor) {
120         this.descriptor = descriptor;
121     }
122     
123     public String JavaDoc[] getListeners() {
124         return listeners;
125     }
126     
127     public void setListeners(String JavaDoc listeners[]) {
128         this.listeners = listeners;
129     }
130     
131     public String JavaDoc getPrefix() {
132         return prefix;
133     }
134     
135     public void setPrefix(String JavaDoc prefix) {
136         this.prefix = prefix;
137     }
138     
139     public String JavaDoc getTagClassPackage() {
140         return tagClassPackage;
141     }
142     
143     public void setTagClassPackage(String JavaDoc tagClassPackage) {
144         this.tagClassPackage = tagClassPackage;
145     }
146     
147     public String JavaDoc getURI() {
148         return uri;
149     }
150     
151     public void setURI(String JavaDoc uri) {
152         this.uri = uri;
153     }
154     
155     public String JavaDoc[] getValidators() {
156         return validators;
157     }
158     
159     public void setValidators(String JavaDoc validators[]) {
160         this.validators = validators;
161     }
162     
163     public void generate() throws IOException JavaDoc {
164         
165         File JavaDoc outputFile = new File JavaDoc(getDest(), getDescriptor());
166         outputFile.mkdirs();
167         outputFile.delete();
168         writer = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(outputFile)));
169         logger.log(Level.FINEST, "Generate " + outputFile.getAbsoluteFile());
170         license();
171         header();
172         validators();
173         listeners();
174         components();
175         jspTagOnly();
176         
177         footer();
178         writer.flush();
179         writer.close();
180     }
181     
182     private void attribute(ComponentBean cb, RendererBean rb, PropertyBean pb)
183     throws IOException JavaDoc {
184         if ("com.icesoft.faces.component.menubar.MenuBar".equals(cb.getComponentClass())
185                 && (pb.getPropertyName().equals("action") ||
186                         pb.getPropertyName().equals("actionListener"))) {
187             return;
188         }
189         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
190         sb.append(" <attribute>\n");
191         sb.append(" <name>" + pb.getPropertyName() + "</name>\n");
192         sb.append(" <required>" + pb.isRequired() + "</required>\n");
193         sb.append(" <rtexprvalue>false</rtexprvalue>\n");
194         if (isVerbose()) {
195             AttributeBean ab = rb.getAttribute(pb.getPropertyName());
196             DescriptionBean db = null;
197             if (ab != null && !cb.getComponentType().startsWith("com.icesoft.faces"))
198                 db = ab.getDescription("");
199             else
200                 db = pb.getDescription("");
201             if (db != null) {
202                 sb.append(" <description><![CDATA[\n");
203                 sb.append(" " + db.getDescription() + "\n");
204                 sb.append(" ]]></description>\n");
205             }
206         }
207         sb.append(" </attribute>\n\n");
208         attributes.put(pb.getPropertyName(), sb.toString());
209     }
210     
211     private void attributes(ComponentBean cb, RendererBean rb)
212     throws IOException JavaDoc {
213         attribute(cb, rb, binding);
214         attributes(cb, rb, ((Set JavaDoc) (new HashSet JavaDoc())));
215     }
216     
217     private void attributes(ComponentBean cb, RendererBean rb, Set JavaDoc set)
218     throws IOException JavaDoc {
219         PropertyBean pbs[] = cb.getProperties();
220         if (pbs == null){
221             pbs = new PropertyBean[0];
222         }
223         for (int i = 0; i < pbs.length; i++) {
224             if (set.contains(pbs[i].getPropertyName()))
225                 continue;
226             set.add(pbs[i].getPropertyName());
227             PropertyBean pb = merge(pbs[i], rb.getAttribute(pbs[i]
228                     .getPropertyName()));
229             if (!pb.isSuppressed() && pb.isTagAttribute()){
230                 attribute(cb, rb, pb);
231             }
232         }
233         
234         String JavaDoc baseComponentType = cb.getBaseComponentType();
235         if (baseComponentType != null) {
236             ComponentBean bcb = getConfig().getComponent(baseComponentType);
237             if (bcb != null){
238                 attributes(bcb, rb, set);
239             }
240         }
241     }
242     
243     private void component(ComponentBean cb) throws IOException JavaDoc {
244         if (cb.isSuppressed())
245             return;
246         if(cb.getComponentClass().startsWith("javax.faces.component.")){
247             return;
248         }
249         RendererBean rb = renderer(cb);
250         
251         if(cb == null){
252             logger.log(Level.SEVERE, "component bean is null");
253         }else{
254             logger.log(Level.FINEST, "component bean class ="+cb.getClass().getName()+
255                     "RendererBean comp family="+cb.getComponentFamily()+
256                     "componentBean rendertype"+cb.getRendererType());
257         }
258         
259         if (rb == null){
260             rb = new RendererBean();
261         }
262         if (rb.getTagName() == null){
263             return;
264         }
265         writer.println(" <tag>");
266         writer.println();
267         writer.println(" <name>" + rb.getTagName() + "</name>");
268         writer.println(" <tag-class>" + tagClass(cb) + "</tag-class>");
269         writer.println(" <body-content>JSP</body-content>");
270         if (isVerbose()) {
271             DescriptionBean db = null;
272             if (rb != null)
273                 db = rb.getDescription("");
274             else
275                 db = cb.getDescription("");
276             if (db != null) {
277                 writer.println(" <description><![CDATA[");
278                 writer.println(" " + db.getDescription());
279                 writer.println(" ]]></description>");
280             }
281         }
282         writer.println();
283         attributes(cb, rb);
284         String JavaDoc name;
285         for (Iterator JavaDoc names = attributes.keySet().iterator(); names.hasNext(); writer
286                 .print((String JavaDoc) attributes.get(name)))
287             name = (String JavaDoc) names.next();
288         
289         attributes.clear();
290         writer.println(" </tag>");
291         writer.println();
292         writer.println();
293     }
294     
295     private void components() throws IOException JavaDoc {
296         ComponentBean cbs[] = getConfig().getComponents();
297         for (int i = 0; i < cbs.length; i++) {
298             logger.log(Level.FINEST, "component = "+ cbs[i].getRendererType());
299             if (generated(cbs[i].getComponentClass())) {
300                 
301                 if (cbs[i] == null) {
302                     logger.log(Level.SEVERE, "component is null");
303                 } else {
304                     logger.log(Level.FINEST, "component class="+cbs[i].getComponentClass());
305                     component(cbs[i]);
306                 }
307             }
308         }
309         
310     }
311     
312         /*
313          * TODO: move to metadata. jsp tag only
314          */

315     private void jspTagOnly() {
316         
317         writer.println(" <tag>");
318         writer.println(" <name>tabChangeListener</name>");
319         writer
320                 .println(" <tag-class>com.icesoft.faces.component.paneltabset.TabChangeListenerTag</tag-class>");
321         writer.println(" <body-content>empty</body-content>");
322         writer.println(" <attribute>");
323         writer.println(" <name>type</name>");
324         writer.println(" <required>true</required>");
325         writer.println(" <rtexprvalue>false</rtexprvalue>");
326         writer
327                 .println(" <description>the name of the class that will be added to the HtmlPanelTabbedPane component as a TabChangeListener</description>");
328         writer.println(" </attribute>");
329         writer.println(" </tag>");
330         writer.println("");
331     }
332     
333     private void footer() throws IOException JavaDoc {
334         writer.println("</taglib>");
335     }
336     
337     private void header() throws IOException JavaDoc {
338         writer.println("<?xml version=\"1.0\"?>");
339         writer.println("<!DOCTYPE taglib PUBLIC");
340         writer
341                 .println(" \"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN\"");
342         writer
343                 .println(" \"http://java.sun.com/dtd//web-jsptaglibrary_1_2.dtd\">");
344         writer.println();
345         writer.println("<taglib>");
346         writer.println();
347         writer.println();
348         
349         // TODO: pick up version
350
writer.println(" <tlib-version>" + "1.6" + "</tlib-version>");
351         writer.println(" <jsp-version>1.2</jsp-version>");
352         writer.println(" <short-name>" + getPrefix() + "</short-name>");
353         writer.println(" <uri>" + getURI() + "</uri>");
354         writer.println(" <display-name>" + "ICEfaces Component Suite" + "</display-name>");
355         writer.println();
356     }
357     
358     private void license() throws IOException JavaDoc {
359     }
360     
361     private void listeners() throws IOException JavaDoc {
362         if (listeners == null)
363             return;
364         for (int i = 0; i < listeners.length; i++) {
365             writer.println(" <listener>");
366             writer.println(" <listener-class>" + listeners[i]
367                     + "</listener-class>");
368             writer.println(" </listener>");
369             writer.println();
370         }
371         
372     }
373     
374     private String JavaDoc stripHtmlName(String JavaDoc s, String JavaDoc word){
375         
376         String JavaDoc tmp = "";
377         if(!s.startsWith(word)){
378             tmp = s;
379         }else{
380             tmp = s.substring(word.length());
381         }
382         return tmp;
383     }
384     
385     private String JavaDoc tagClass(ComponentBean cb) {
386         
387         String JavaDoc componentClass = cb.getComponentClass();
388         if (tagClassPackage != null && componentClass.indexOf(".ext.") >0){
389             
390             return tagClassPackage + "."
391                     + stripHtmlName(simpleClassName(cb.getComponentClass()), "Html") + "Tag";
392             
393         }else{
394             return componentClass + "Tag";
395         }
396     }
397     
398     private void validators() throws IOException JavaDoc {
399         if (validators == null)
400             return;
401         for (int i = 0; i < validators.length; i++) {
402             writer.println(" <validator>");
403             writer.println(" <validator-class>" + validators[i]
404                     + "</validator-class>");
405             writer.println(" </validator>");
406             writer.println();
407         }
408         
409     }
410
411 }
412
Popular Tags