001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.metadata.generators;
035:
036: import com.icesoft.jsfmeta.util.AbstractGenerator;
037: import com.icesoft.jsfmeta.util.InternalConfig;
038: import java.io.BufferedWriter;
039: import java.io.File;
040: import java.io.FileWriter;
041: import java.io.IOException;
042: import java.io.PrintWriter;
043: import java.util.HashSet;
044: import java.util.Iterator;
045: import java.util.Set;
046: import java.util.TreeMap;
047:
048: import com.sun.rave.jsfmeta.beans.AttributeBean;
049: import com.sun.rave.jsfmeta.beans.ComponentBean;
050: import com.sun.rave.jsfmeta.beans.DescriptionBean;
051: import com.sun.rave.jsfmeta.beans.PropertyBean;
052: import com.sun.rave.jsfmeta.beans.RendererBean;
053: import java.util.logging.Level;
054:
055: /*
056: * DescriptorGenerator generate tld file
057: *
058: *
059: */
060: public class TLDGenerator extends AbstractGenerator {
061:
062: private TreeMap attributes;
063:
064: private PrintWriter writer;
065:
066: private boolean base;
067:
068: private static PropertyBean binding;
069:
070: private String descriptor;
071:
072: private String listeners[];
073:
074: private String prefix;
075:
076: private String tagClassPackage;
077:
078: private String uri;
079:
080: private String validators[];
081:
082: static {
083: binding = new PropertyBean();
084: binding.setPropertyName("binding");
085: String db_string = "The value binding expression linking this component to a property in a backing bean";
086: DescriptionBean dbBean = new DescriptionBean("", db_string);
087: binding.addDescription(dbBean);
088: binding.setRequired(false);
089: }
090:
091: public TLDGenerator(InternalConfig internalConfig) {
092:
093: super (internalConfig);
094: attributes = new TreeMap();
095: writer = null;
096: base = false;
097: descriptor = internalConfig.getProperty("project.tld.file");
098: listeners = null;
099: prefix = internalConfig.getProperty("project.taglib.prefix");
100: tagClassPackage = internalConfig
101: .getProperty("project.taglib.package");
102: uri = internalConfig.getProperty("project.taglib.uri");
103: validators = null;
104: }
105:
106: public boolean getBase() {
107: return base;
108: }
109:
110: public void setBase(boolean base) {
111: this .base = base;
112: }
113:
114: public String getDescriptor() {
115: return descriptor;
116: }
117:
118: public void setDescriptor(String descriptor) {
119: this .descriptor = descriptor;
120: }
121:
122: public String[] getListeners() {
123: return listeners;
124: }
125:
126: public void setListeners(String listeners[]) {
127: this .listeners = listeners;
128: }
129:
130: public String getPrefix() {
131: return prefix;
132: }
133:
134: public void setPrefix(String prefix) {
135: this .prefix = prefix;
136: }
137:
138: public String getTagClassPackage() {
139: return tagClassPackage;
140: }
141:
142: public void setTagClassPackage(String tagClassPackage) {
143: this .tagClassPackage = tagClassPackage;
144: }
145:
146: public String getURI() {
147: return uri;
148: }
149:
150: public void setURI(String uri) {
151: this .uri = uri;
152: }
153:
154: public String[] getValidators() {
155: return validators;
156: }
157:
158: public void setValidators(String validators[]) {
159: this .validators = validators;
160: }
161:
162: public void generate() throws IOException {
163:
164: File outputFile = new File(getDest(), getDescriptor());
165: outputFile.mkdirs();
166: outputFile.delete();
167: writer = new PrintWriter(new BufferedWriter(new FileWriter(
168: outputFile)));
169: logger.log(Level.FINEST, "Generate "
170: + outputFile.getAbsoluteFile());
171: license();
172: header();
173: validators();
174: listeners();
175: components();
176: jspTagOnly();
177:
178: footer();
179: writer.flush();
180: writer.close();
181: }
182:
183: private void attribute(ComponentBean cb, RendererBean rb,
184: PropertyBean pb) throws IOException {
185: if ("com.icesoft.faces.component.menubar.MenuBar".equals(cb
186: .getComponentClass())
187: && (pb.getPropertyName().equals("action") || pb
188: .getPropertyName().equals("actionListener"))) {
189: return;
190: }
191: StringBuffer sb = new StringBuffer();
192: sb.append(" <attribute>\n");
193: sb.append(" <name>" + pb.getPropertyName() + "</name>\n");
194: sb.append(" <required>" + pb.isRequired()
195: + "</required>\n");
196: sb.append(" <rtexprvalue>false</rtexprvalue>\n");
197: if (isVerbose()) {
198: AttributeBean ab = rb.getAttribute(pb.getPropertyName());
199: DescriptionBean db = null;
200: if (ab != null
201: && !cb.getComponentType().startsWith(
202: "com.icesoft.faces"))
203: db = ab.getDescription("");
204: else
205: db = pb.getDescription("");
206: if (db != null) {
207: sb.append(" <description><![CDATA[\n");
208: sb.append(" " + db.getDescription() + "\n");
209: sb.append(" ]]></description>\n");
210: }
211: }
212: sb.append(" </attribute>\n\n");
213: attributes.put(pb.getPropertyName(), sb.toString());
214: }
215:
216: private void attributes(ComponentBean cb, RendererBean rb)
217: throws IOException {
218: attribute(cb, rb, binding);
219: attributes(cb, rb, ((Set) (new HashSet())));
220: }
221:
222: private void attributes(ComponentBean cb, RendererBean rb, Set set)
223: throws IOException {
224: PropertyBean pbs[] = cb.getProperties();
225: if (pbs == null) {
226: pbs = new PropertyBean[0];
227: }
228: for (int i = 0; i < pbs.length; i++) {
229: if (set.contains(pbs[i].getPropertyName()))
230: continue;
231: set.add(pbs[i].getPropertyName());
232: PropertyBean pb = merge(pbs[i], rb.getAttribute(pbs[i]
233: .getPropertyName()));
234: if (!pb.isSuppressed() && pb.isTagAttribute()) {
235: attribute(cb, rb, pb);
236: }
237: }
238:
239: String baseComponentType = cb.getBaseComponentType();
240: if (baseComponentType != null) {
241: ComponentBean bcb = getConfig().getComponent(
242: baseComponentType);
243: if (bcb != null) {
244: attributes(bcb, rb, set);
245: }
246: }
247: }
248:
249: private void component(ComponentBean cb) throws IOException {
250: if (cb.isSuppressed())
251: return;
252: if (cb.getComponentClass().startsWith("javax.faces.component.")) {
253: return;
254: }
255: RendererBean rb = renderer(cb);
256:
257: if (cb == null) {
258: logger.log(Level.SEVERE, "component bean is null");
259: } else {
260: logger
261: .log(Level.FINEST, "component bean class ="
262: + cb.getClass().getName()
263: + "RendererBean comp family="
264: + cb.getComponentFamily()
265: + "componentBean rendertype"
266: + cb.getRendererType());
267: }
268:
269: if (rb == null) {
270: rb = new RendererBean();
271: }
272: if (rb.getTagName() == null) {
273: return;
274: }
275: writer.println(" <tag>");
276: writer.println();
277: writer.println(" <name>" + rb.getTagName() + "</name>");
278: writer.println(" <tag-class>" + tagClass(cb)
279: + "</tag-class>");
280: writer.println(" <body-content>JSP</body-content>");
281: if (isVerbose()) {
282: DescriptionBean db = null;
283: if (rb != null)
284: db = rb.getDescription("");
285: else
286: db = cb.getDescription("");
287: if (db != null) {
288: writer.println(" <description><![CDATA[");
289: writer.println(" " + db.getDescription());
290: writer.println(" ]]></description>");
291: }
292: }
293: writer.println();
294: attributes(cb, rb);
295: String name;
296: for (Iterator names = attributes.keySet().iterator(); names
297: .hasNext(); writer.print((String) attributes.get(name)))
298: name = (String) names.next();
299:
300: attributes.clear();
301: writer.println(" </tag>");
302: writer.println();
303: writer.println();
304: }
305:
306: private void components() throws IOException {
307: ComponentBean cbs[] = getConfig().getComponents();
308: for (int i = 0; i < cbs.length; i++) {
309: logger.log(Level.FINEST, "component = "
310: + cbs[i].getRendererType());
311: if (generated(cbs[i].getComponentClass())) {
312:
313: if (cbs[i] == null) {
314: logger.log(Level.SEVERE, "component is null");
315: } else {
316: logger.log(Level.FINEST, "component class="
317: + cbs[i].getComponentClass());
318: component(cbs[i]);
319: }
320: }
321: }
322:
323: }
324:
325: /*
326: * TODO: move to metadata. jsp tag only
327: */
328: private void jspTagOnly() {
329:
330: writer.println(" <tag>");
331: writer.println(" <name>tabChangeListener</name>");
332: writer
333: .println(" <tag-class>com.icesoft.faces.component.paneltabset.TabChangeListenerTag</tag-class>");
334: writer.println(" <body-content>empty</body-content>");
335: writer.println(" <attribute>");
336: writer.println(" <name>type</name>");
337: writer.println(" <required>true</required>");
338: writer.println(" <rtexprvalue>false</rtexprvalue>");
339: writer
340: .println(" <description>the name of the class that will be added to the HtmlPanelTabbedPane component as a TabChangeListener</description>");
341: writer.println(" </attribute>");
342: writer.println(" </tag>");
343: writer.println("");
344: }
345:
346: private void footer() throws IOException {
347: writer.println("</taglib>");
348: }
349:
350: private void header() throws IOException {
351: writer.println("<?xml version=\"1.0\"?>");
352: writer.println("<!DOCTYPE taglib PUBLIC");
353: writer
354: .println(" \"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN\"");
355: writer
356: .println(" \"http://java.sun.com/dtd//web-jsptaglibrary_1_2.dtd\">");
357: writer.println();
358: writer.println("<taglib>");
359: writer.println();
360: writer.println();
361:
362: // TODO: pick up version
363: writer.println(" <tlib-version>" + "1.6" + "</tlib-version>");
364: writer.println(" <jsp-version>1.2</jsp-version>");
365: writer
366: .println(" <short-name>" + getPrefix()
367: + "</short-name>");
368: writer.println(" <uri>" + getURI() + "</uri>");
369: writer.println(" <display-name>" + "ICEfaces Component Suite"
370: + "</display-name>");
371: writer.println();
372: }
373:
374: private void license() throws IOException {
375: }
376:
377: private void listeners() throws IOException {
378: if (listeners == null)
379: return;
380: for (int i = 0; i < listeners.length; i++) {
381: writer.println(" <listener>");
382: writer.println(" <listener-class>" + listeners[i]
383: + "</listener-class>");
384: writer.println(" </listener>");
385: writer.println();
386: }
387:
388: }
389:
390: private String stripHtmlName(String s, String word) {
391:
392: String tmp = "";
393: if (!s.startsWith(word)) {
394: tmp = s;
395: } else {
396: tmp = s.substring(word.length());
397: }
398: return tmp;
399: }
400:
401: private String tagClass(ComponentBean cb) {
402:
403: String componentClass = cb.getComponentClass();
404: if (tagClassPackage != null
405: && componentClass.indexOf(".ext.") > 0) {
406:
407: return tagClassPackage
408: + "."
409: + stripHtmlName(simpleClassName(cb
410: .getComponentClass()), "Html") + "Tag";
411:
412: } else {
413: return componentClass + "Tag";
414: }
415: }
416:
417: private void validators() throws IOException {
418: if (validators == null)
419: return;
420: for (int i = 0; i < validators.length; i++) {
421: writer.println(" <validator>");
422: writer.println(" <validator-class>" + validators[i]
423: + "</validator-class>");
424: writer.println(" </validator>");
425: writer.println();
426: }
427:
428: }
429:
430: }
|