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 com.icesoft.jsfmeta.util.JavaSourceWriter;
039: import java.io.BufferedWriter;
040: import java.io.File;
041: import java.io.FileWriter;
042: import java.io.IOException;
043:
044: import com.sun.rave.jsfmeta.beans.AttributeBean;
045: import com.sun.rave.jsfmeta.beans.ComponentBean;
046: import com.sun.rave.jsfmeta.beans.DescriptionBean;
047: import com.sun.rave.jsfmeta.beans.PropertyBean;
048: import com.sun.rave.jsfmeta.beans.RendererBean;
049:
050: public class BaseComponentGenerator extends AbstractGenerator {
051:
052: private boolean base;
053:
054: private boolean override;
055:
056: public BaseComponentGenerator(InternalConfig internalConfig) {
057:
058: super (internalConfig);
059: base = true;
060: override = false;
061: }
062:
063: public boolean getBase() {
064: return base;
065: }
066:
067: public void setBase(boolean base) {
068: this .base = base;
069: }
070:
071: public boolean isOverride() {
072: return override;
073: }
074:
075: public void setOverride(boolean override) {
076: this .override = override;
077: }
078:
079: public void generate() throws IOException {
080:
081: ComponentBean cbs[] = getConfig().getComponents();
082: for (int i = 0; i < cbs.length; i++) {
083: if (generated(cbs[i].getComponentClass()))
084: generate(cbs[i]);
085: }
086: }
087:
088: private boolean isAliasFor(PropertyBean[] pbs) {
089:
090: for (int i = 0; i < pbs.length; i++) {
091:
092: if (pbs[i].getAliasFor() != null) {
093: return true;
094: }
095: }
096:
097: return false;
098: }
099:
100: private void bindings(ComponentBean cb) throws IOException {
101:
102: PropertyBean pbs[] = cb.getProperties();
103:
104: if (!isAliasFor(pbs)) {
105: return;
106: }
107: JavaSourceWriter writer = getWriter();
108: String propertyName = null;
109: String aliasName = null;
110: writer.startJavaDoc();
111: writer
112: .emitJavaDoc("<p>Return the <code>ValueBinding</code> stored for the");
113: writer
114: .emitJavaDoc("specified name (if any), respecting any property aliases.</p>");
115: writer.emitJavaDoc();
116: writer
117: .emitJavaDoc("@param name Name of value binding to retrieve");
118: writer.endJavaDoc();
119: writer.startMethod("getValueBinding", "ValueBinding",
120: new String[] { "String" }, new String[] { "name" });
121: for (int k = 0; k < pbs.length; k++) {
122: aliasName = pbs[k].getAliasFor();
123: if (aliasName != null) {
124: propertyName = pbs[k].getPropertyName();
125: writer.emitExpression("if (name.equals("
126: + writer.toJavaString(propertyName) + ")) {",
127: true);
128: writer.indent();
129: writer.emitExpression("return super.getValueBinding("
130: + writer.toJavaString(aliasName) + ");", true);
131: writer.outdent();
132: writer.emitExpression("}", true);
133: }
134: }
135:
136: writer.emitExpression("return super.getValueBinding(name);",
137: true);
138: writer.endMethod();
139: writer.emitNewline();
140: writer.startJavaDoc();
141: writer
142: .emitJavaDoc("<p>Set the <code>ValueBinding</code> stored for the");
143: writer
144: .emitJavaDoc("specified name (if any), respecting any property");
145: writer.emitJavaDoc("aliases.</p>");
146: writer.emitJavaDoc();
147: writer
148: .emitJavaDoc("@param name Name of value binding to set");
149: writer
150: .emitJavaDoc("@param binding ValueBinding to set, or null to remove");
151: writer.endJavaDoc();
152: writer.startMethod("setValueBinding", "void", new String[] {
153: "String", "ValueBinding" }, new String[] { "name",
154: "binding" });
155: for (int j = 0; j < pbs.length; j++) {
156: aliasName = pbs[j].getAliasFor();
157: if (aliasName != null) {
158: propertyName = pbs[j].getPropertyName();
159: writer.emitExpression("if (name.equals("
160: + writer.toJavaString(propertyName) + ")) {",
161: true);
162: writer.indent();
163: writer.emitExpression("super.setValueBinding("
164: + writer.toJavaString(aliasName)
165: + ", binding);", true);
166: writer.emitExpression("return;", true);
167: writer.outdent();
168: writer.emitExpression("}", true);
169: }
170: }
171:
172: writer.emitExpression("super.setValueBinding(name, binding);",
173: true);
174: writer.endMethod();
175: writer.emitNewline();
176: }
177:
178: private void constructor(ComponentBean cb) throws IOException {
179: JavaSourceWriter writer = getWriter();
180: String simple = simpleClassName(cb.getComponentClass());
181: if (getBase())
182: simple = simple + "Base";
183: String rendererType = rendererType(cb);
184: writer.startJavaDoc();
185: writer.emitJavaDoc("<p>Construct a new <code>" + simple
186: + "</code>.</p>");
187: writer.endJavaDoc();
188: writer.startMethod(simple, null, null, null);
189: writer.emitExpression("super();", true);
190: if (rendererType != null) {
191: writer.emitExpression("setRendererType(\"" + rendererType
192: + "\");", true);
193: writer.endMethod();
194: }
195: writer.emitNewline();
196: }
197:
198: private void family(ComponentBean cb) throws IOException {
199: JavaSourceWriter writer = getWriter();
200: writer.startJavaDoc();
201: writer
202: .emitJavaDoc("<p>Return the family for this component.</p>");
203: writer.endJavaDoc();
204: writer.startMethod("getFamily", "String", null, null);
205: writer.emitExpression(
206: "return \"" + componentFamily(cb) + "\";", true);
207: writer.endMethod();
208: writer.emitNewline();
209: }
210:
211: private void footer(ComponentBean cb) throws IOException {
212: JavaSourceWriter writer = getWriter();
213: writer.endClass();
214: }
215:
216: private void generate(ComponentBean cb) throws IOException {
217: if (cb.getComponentClass().startsWith("javax.faces.component."))
218: return;
219: if (cb.isSuppressed()) {
220: return;
221: } else {
222: File outputFile = outputFile(cb.getComponentClass()
223: + (getBase() ? "Base" : ""));
224: outputFile.mkdirs();
225: outputFile.delete();
226: getWriter().setOutputWriter(
227: new BufferedWriter(new FileWriter(outputFile)));
228: license(cb);
229: header(cb);
230: constructor(cb);
231: family(cb);
232: bindings(cb);
233: properties(cb);
234: restore(cb);
235: save(cb);
236: footer(cb);
237: getWriter().flush();
238: getWriter().close();
239: return;
240: }
241: }
242:
243: private void header(ComponentBean cb) throws IOException {
244:
245: ComponentBean bcb = null;
246: String baseComponentType = cb.getBaseComponentType();
247: if (baseComponentType != null)
248: bcb = getConfig().getComponent(baseComponentType);
249: RendererBean rb = renderer(cb);
250: JavaSourceWriter writer = getWriter();
251: String simple = simpleClassName(cb.getComponentClass());
252: if (getBase())
253: simple = simple + "Base";
254: String componentClass = cb.getComponentClass();
255: int last = componentClass.lastIndexOf('.');
256: if (last >= 0) {
257: writer.emitPackage(componentClass.substring(0, last));
258: writer.emitNewline();
259: }
260: writer.emitImport("java.io.IOException");
261: writer.emitImport("javax.faces.component.UIComponent");
262: writer.emitImport("javax.faces.context.FacesContext");
263: writer.emitImport("javax.faces.el.MethodBinding");
264: writer.emitImport("javax.faces.el.ValueBinding");
265: writer.emitNewline();
266: writer.startJavaDoc();
267: DescriptionBean db = null;
268: if (isOverride() && rb != null) {
269: db = rb.getDescription("");
270: }
271: if (db == null) {
272: db = cb.getDescription("");
273: }
274: if (db != null) {
275: String description = db.getDescription();
276: if (description != null && description.length() > 0)
277: writer.emitJavaDocMultiLine(description.trim());
278: }
279: writer.emitJavaDoc("<p>Auto-generated component class.");
280: writer
281: .emitJavaDoc("Do <strong>NOT</strong> modify; all changes");
282: writer.emitJavaDoc("<strong>will</strong> be lost!</p>");
283: writer.endJavaDoc();
284: writer.emitNewline();
285: if (bcb != null) {
286: writer.startClass(simple, bcb.getComponentClass(), null,
287: !getBase(), getBase());
288: } else {
289: writer.startClass(simple,
290: "javax.faces.component.UIComponentBase", null,
291: true, getBase());
292: }
293: writer.emitNewline();
294: }
295:
296: private void license(ComponentBean componentbean)
297: throws IOException {
298:
299: JavaSourceWriter writer = getWriter();
300: writer.emitLicense();
301: }
302:
303: private void properties(ComponentBean cb) throws IOException {
304: PropertyBean pbs[] = cb.getProperties();
305: if (pbs == null) {
306: return;
307: }
308: for (int i = 0; i < pbs.length; i++) {
309: if (!pbs[i].isSuppressed()) {
310: property(cb, pbs[i]);
311: }
312: }
313:
314: }
315:
316: private void property(ComponentBean cb, PropertyBean pb)
317: throws IOException {
318: String name = pb.getPropertyName();
319: String type = pb.getPropertyClass();
320: if (type == null) {
321: type = "String";
322: } else if (type.startsWith("java.lang.")) {
323: type = type.substring(10);
324: }
325: RendererBean rb = renderer(cb);
326: AttributeBean ab = null;
327: if (rb != null) {
328: ab = rb.getAttribute(pb.getPropertyName());
329: }
330: String var = mangle(name);
331: JavaSourceWriter writer = getWriter();
332: String aliasFor = pb.getAliasFor();
333: if (aliasFor != null) {
334: PropertyBean pb1 = cb.getProperty(aliasFor);
335: if (pb1 == null) {
336: for (ComponentBean cb1 = cb; pb1 == null && cb1 != null; pb1 = cb1
337: .getProperty(aliasFor)) {
338: String bct = cb1.getBaseComponentType();
339: if (bct == null)
340: throw new IllegalArgumentException(aliasFor);
341: cb1 = getConfig().getComponent(bct);
342: if (cb1 == null)
343: throw new IllegalArgumentException(bct);
344: }
345:
346: if (pb1 == null)
347: throw new IllegalArgumentException(aliasFor);
348: }
349: String aliasType = pb1.getPropertyClass();
350:
351: if (aliasType == null) {
352: aliasType = "String";
353: } else if (aliasType.startsWith("java.lang.")) {
354: aliasType = aliasType.substring(10);
355: }
356: writer.emitExpression("// " + name, true);
357: String readMethod = null;
358: if (!pb.isWriteOnly()) {
359: DescriptionBean db = null;
360: if (isOverride() && ab != null) {
361: db = ab.getDescription("");
362: }
363: if (db == null) {
364: db = pb.getDescription("");
365: }
366: if (db != null) {
367: String description = db.getDescription();
368: if (description != null && description.length() > 0) {
369: writer.startJavaDoc();
370: description = description.trim();
371: if (description.startsWith("<")) {
372: writer.emitJavaDocMultiLine(description);
373: } else {
374: writer.emitJavaDocMultiLine("<p>"
375: + description + "</p>");
376: }
377: writer.endJavaDoc();
378: }
379: }
380: String method = pb.getReadMethod();
381: if (method == null)
382: if ("boolean".equals(type))
383: method = "is" + capitalize(name);
384: else
385: method = "get" + capitalize(name);
386: readMethod = method;
387: writer.startMethod(method, type, null, null);
388: StringBuffer sb = new StringBuffer("return ");
389: if (!type.equals(aliasType)) {
390: sb.append("(");
391: sb.append(type);
392: sb.append(") ");
393: }
394: String aliasMethod = pb1.getReadMethod();
395: if (aliasMethod == null) {
396: if ("boolean".equals(aliasType)) {
397: aliasMethod = "is" + capitalize(aliasFor);
398: } else {
399: aliasMethod = "get" + capitalize(aliasFor);
400: }
401: }
402: sb.append(aliasMethod);
403: sb.append("();");
404: writer.emitExpression(sb.toString(), true);
405: writer.endMethod();
406: writer.emitNewline();
407: }
408: if (!pb.isReadOnly()) {
409: DescriptionBean db = null;
410: if (isOverride() && ab != null)
411: db = ab.getDescription("");
412: if (db == null)
413: db = pb.getDescription("");
414: if (db != null) {
415: String description = db.getDescription();
416: if (description != null && description.length() > 0) {
417: writer.startJavaDoc();
418: description = description.trim();
419: if (description.startsWith("<"))
420: writer.emitJavaDocMultiLine(description);
421: else
422: writer.emitJavaDocMultiLine("<p>"
423: + description + "</p>");
424: if (readMethod != null)
425: writer.emitJavaDoc("@see #" + readMethod
426: + "()");
427: writer.endJavaDoc();
428: }
429: }
430: String method = pb.getWriteMethod();
431: if (method == null)
432: method = "set" + capitalize(name);
433: writer.startMethod(method, "void",
434: new String[] { type }, new String[] { var });
435: String aliasMethod = pb1.getWriteMethod();
436: if (aliasMethod == null)
437: aliasMethod = "set" + capitalize(aliasFor);
438: StringBuffer sb = new StringBuffer(aliasMethod);
439: sb.append("(");
440: if (!type.equals(aliasType)) {
441: sb.append("(");
442: sb.append(aliasType);
443: sb.append(") ");
444: }
445: sb.append(var);
446: sb.append(");");
447: writer.emitExpression(sb.toString(), true);
448: writer.endMethod();
449: writer.emitNewline();
450: }
451: return;
452: }
453: writer.emitExpression("// " + name, true);
454: StringBuffer sb = new StringBuffer("private ");
455: if ("java.lang.String".equals(type))
456: sb.append("String");
457: else
458: sb.append(type);
459: sb.append(" ");
460: sb.append(var);
461: sb.append(" = ");
462: if (primitive(type))
463: sb.append((String) defaults.get(type));
464: else
465: sb.append("null");
466: sb.append(";");
467: writer.emitExpression(sb.toString(), true);
468: if (primitive(type) && pb.isBindable()) {
469: sb = new StringBuffer("private boolean ");
470: sb.append(var);
471: sb.append("_set = false;");
472: writer.emitExpression(sb.toString(), true);
473: }
474: writer.emitNewline();
475: String readMethod = null;
476: if (!pb.isWriteOnly()) {
477: DescriptionBean db = null;
478: if (isOverride() && ab != null)
479: db = ab.getDescription("");
480: if (db == null)
481: db = pb.getDescription("");
482: if (db != null) {
483: String description = db.getDescription();
484: if (description != null && description.length() > 0) {
485: writer.startJavaDoc();
486: description = description.trim();
487: if (description.startsWith("<"))
488: writer.emitJavaDocMultiLine(description);
489: else
490: writer.emitJavaDocMultiLine("<p>" + description
491: + "</p>");
492: writer.endJavaDoc();
493: }
494: }
495: String method = pb.getReadMethod();
496: if (method == null)
497: if ("boolean".equals(type))
498: method = "is" + capitalize(name);
499: else
500: method = "get" + capitalize(name);
501: readMethod = method;
502: writer.startMethod(method, type, null, null);
503: if (!pb.isBindable()) {
504: writer.emitExpression("return this." + var + ";", true);
505: } else {
506: if (primitive(type))
507: writer.emitExpression(
508: "if (this." + var + "_set) {", true);
509: else
510: writer.emitExpression("if (this." + var
511: + " != null) {", true);
512: writer.indent();
513: writer.emitExpression("return this." + var + ";", true);
514: writer.outdent();
515: writer.emitExpression("}", true);
516: writer.emitExpression(
517: "ValueBinding _vb = getValueBinding(\"" + name
518: + "\");", true);
519: writer.emitExpression("if (_vb != null) {", true);
520: writer.indent();
521: if (primitive(type)) {
522: writer
523: .emitExpression(
524: "Object _result = _vb.getValue(getFacesContext());",
525: true);
526: writer.emitExpression("if (_result == null) {",
527: true);
528: writer.indent();
529: writer.emitExpression("return "
530: + defaults.get(type) + ";", true);
531: writer.outdent();
532: writer.emitExpression("} else {", true);
533: writer.indent();
534: writer.emitExpression("return (("
535: + wrappers.get(type) + ") _result)."
536: + unwrappers.get(type) + "();", true);
537: writer.outdent();
538: writer.emitExpression("}", true);
539: } else {
540: writer.emitExpression("return (" + type + ") "
541: + "_vb.getValue(getFacesContext());", true);
542: }
543: writer.outdent();
544: writer.emitExpression("}", true);
545: sb = new StringBuffer("return ");
546: if (pb.getDefaultValue() != null)
547: sb.append(pb.getDefaultValue());
548: else if (primitive(type))
549: sb.append((String) defaults.get(type));
550: else
551: sb.append("null");
552: sb.append(";");
553: writer.emitExpression(sb.toString(), true);
554: }
555: writer.endMethod();
556: writer.emitNewline();
557: }
558: if (!pb.isReadOnly()) {
559: DescriptionBean db = null;
560: if (isOverride() && ab != null)
561: db = ab.getDescription("");
562: if (db == null)
563: db = pb.getDescription("");
564: if (db != null) {
565: String description = db.getDescription();
566: if (description != null && description.length() > 0) {
567: writer.startJavaDoc();
568: description = description.trim();
569: if (description.startsWith("<"))
570: writer.emitJavaDocMultiLine(description);
571: else
572: writer.emitJavaDocMultiLine("<p>" + description
573: + "</p>");
574: if (readMethod != null)
575: writer
576: .emitJavaDoc("@see #" + readMethod
577: + "()");
578: writer.endJavaDoc();
579: }
580: }
581: String method = pb.getWriteMethod();
582: if (method == null)
583: method = "set" + capitalize(name);
584: writer.startMethod(method, "void", new String[] { type },
585: new String[] { var });
586: writer.emitExpression("this." + var + " = " + var + ";",
587: true);
588: if (primitive(type) && pb.isBindable())
589: writer.emitExpression("this." + var + "_set = true;",
590: true);
591: writer.endMethod();
592: }
593: writer.emitNewline();
594: }
595:
596: private void restore(ComponentBean cb) throws IOException {
597: PropertyBean pbs[] = cb.getProperties();
598: if (pbs == null)
599: pbs = new PropertyBean[0];
600: JavaSourceWriter writer = getWriter();
601: writer.startJavaDoc();
602: writer
603: .emitJavaDoc("<p>Restore the state of this component.</p>");
604: writer.endJavaDoc();
605: writer.startMethod("restoreState", "void", new String[] {
606: "FacesContext", "Object" }, new String[] { "_context",
607: "_state" });
608: writer.emitExpression("Object _values[] = (Object[]) _state;",
609: true);
610: writer.emitExpression(
611: "super.restoreState(_context, _values[0]);", true);
612: int index = 1;
613: for (int i = 0; i < pbs.length; i++) {
614: if (pbs[i].getAliasFor() != null)
615: continue;
616: String name = pbs[i].getPropertyName();
617: String type = pbs[i].getPropertyClass();
618: if (type == null)
619: type = "String";
620: else if (type.startsWith("java.lang."))
621: type = type.substring(10);
622: String var = mangle(name);
623: if (primitive(type)) {
624: writer.emitExpression("this." + var + " = (("
625: + wrappers.get(type) + ") _values[" + index++
626: + "])." + unwrappers.get(type) + "();", true);
627: if (pbs[i].isBindable())
628: writer.emitExpression("this." + var
629: + "_set = ((Boolean) _values[" + index++
630: + "]).booleanValue();", true);
631: continue;
632: }
633: if (type.equals("javax.faces.el.MethodBinding"))
634: writer.emitExpression("this." + var + " = (" + type
635: + ") "
636: + "restoreAttachedState(_context, _values["
637: + index++ + "]);", true);
638: else
639: writer.emitExpression("this." + var + " = (" + type
640: + ") _values[" + index++ + "];", true);
641: }
642:
643: writer.endMethod();
644: writer.emitNewline();
645: }
646:
647: private void save(ComponentBean cb) throws IOException {
648: PropertyBean pbs[] = cb.getProperties();
649: if (pbs == null)
650: pbs = new PropertyBean[0];
651: JavaSourceWriter writer = getWriter();
652: writer.startJavaDoc();
653: writer.emitJavaDoc("<p>Save the state of this component.</p>");
654: writer.endJavaDoc();
655: writer.startMethod("saveState", "Object",
656: new String[] { "FacesContext" },
657: new String[] { "_context" });
658: int n = pbs.length + 1;
659: for (int i = 0; i < pbs.length; i++) {
660: if (pbs[i].getAliasFor() != null) {
661: n--;
662: continue;
663: }
664: if (primitive(pbs[i].getPropertyClass())
665: && pbs[i].isBindable())
666: n++;
667: }
668:
669: writer.emitExpression("Object _values[] = new Object[" + n
670: + "];", true);
671: writer.emitExpression(
672: "_values[0] = super.saveState(_context);", true);
673: int index = 1;
674: for (int i = 0; i < pbs.length; i++) {
675: if (pbs[i].getAliasFor() != null)
676: continue;
677: String name = pbs[i].getPropertyName();
678: String type = pbs[i].getPropertyClass();
679: if (type == null)
680: type = "String";
681: else if (type.startsWith("java.lang."))
682: type = type.substring(10);
683: String var = mangle(name);
684: if (primitive(type)) {
685: if (type.equals("boolean"))
686: writer.emitExpression("_values[" + index++ + "] = "
687: + "this." + var
688: + " ? Boolean.TRUE : Boolean.FALSE;", true);
689: else
690: writer.emitExpression("_values[" + index++
691: + "] = new " + wrappers.get(type)
692: + "(this." + var + ");", true);
693: if (pbs[i].isBindable())
694: writer.emitExpression("_values[" + index++ + "] = "
695: + "this." + var
696: + "_set ? Boolean.TRUE : Boolean.FALSE;",
697: true);
698: continue;
699: }
700: if (type.equals("javax.faces.el.MethodBinding"))
701: writer.emitExpression("_values[" + index++
702: + "] = saveAttachedState(_context, " + var
703: + ");", true);
704: else
705: writer.emitExpression("_values[" + index++
706: + "] = this." + var + ";", true);
707: }
708:
709: writer.emitExpression("return _values;", true);
710: writer.endMethod();
711: writer.emitNewline();
712: }
713:
714: }
|