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.jsfmeta.util;
035:
036: import java.io.File;
037: import java.util.ArrayList;
038: import java.util.HashMap;
039: import java.util.HashSet;
040: import java.util.Iterator;
041: import java.util.List;
042: import java.util.Map;
043: import java.util.Set;
044:
045: import javax.faces.render.RenderKitFactory;
046:
047: import com.sun.rave.jsfmeta.beans.AttributeBean;
048: import com.sun.rave.jsfmeta.beans.ComponentBean;
049: import com.sun.rave.jsfmeta.beans.DescriptionBean;
050: import com.sun.rave.jsfmeta.beans.DisplayNameBean;
051: import com.sun.rave.jsfmeta.beans.FacesConfigBean;
052: import com.sun.rave.jsfmeta.beans.FacetBean;
053: import com.sun.rave.jsfmeta.beans.IconBean;
054: import com.sun.rave.jsfmeta.beans.NamedValueBean;
055: import com.sun.rave.jsfmeta.beans.PropertyBean;
056: import com.sun.rave.jsfmeta.beans.RenderKitBean;
057: import com.sun.rave.jsfmeta.beans.RendererBean;
058: import java.util.Arrays;
059: import java.util.logging.Level;
060: import java.util.logging.Logger;
061:
062: /*
063: * Base Generator
064: *
065: */
066:
067: public abstract class AbstractGenerator {
068:
069: protected static Map defaults;
070:
071: protected static Set keywords;
072:
073: protected static Map unwrappers;
074:
075: protected static Map wrappers;
076:
077: private String defaultRenderKitId;
078:
079: private String excludes[];
080:
081: private String includes[];
082:
083: private FacesConfigBean config;
084:
085: private File dest;
086:
087: private boolean verbose;
088:
089: private JavaSourceWriter writer;
090:
091: protected static final Logger logger = Logger
092: .getLogger("com.icesoft.metadata.generators");
093:
094: static {
095:
096: defaults = new HashMap();
097: defaults.put("boolean", "false");
098: defaults.put("byte", "Byte.MIN_VALUE");
099: defaults.put("char", "Character.MIN_VALUE");
100: defaults.put("double", "Double.MIN_VALUE");
101: defaults.put("float", "Float.MIN_VALUE");
102: defaults.put("int", "Integer.MIN_VALUE");
103: defaults.put("long", "Long.MIN_VALUE");
104: defaults.put("short", "Short.MIN_VALUE");
105: keywords = new HashSet();
106: keywords.addAll(Arrays
107: .asList(new String[] { "abstract", "boolean", "break",
108: "byte", "case", "cast", "catch", "char",
109: "class", "const", "continue", "default", "do",
110: "double", "else", "extends", "final",
111: "finally", "float", "for", "future", "generic",
112: "goto", "if", "implements", "import", "inner",
113: "instanceof", "int", "interface", "long",
114: "native", "new", "null", "operator", "outer",
115: "package", "private", "protected", "public",
116: "rest", "return", "short", "static", "super",
117: "switch", "synchronized", "this", "throw",
118: "throws", "transient", "try", "var", "void",
119: "volatile", "while" }));
120:
121: unwrappers = new HashMap();
122: unwrappers.put("boolean", "booleanValue");
123: unwrappers.put("byte", "byteValue");
124: unwrappers.put("char", "charValue");
125: unwrappers.put("double", "doubleValue");
126: unwrappers.put("float", "floatValue");
127: unwrappers.put("int", "intValue");
128: unwrappers.put("long", "longValue");
129: unwrappers.put("short", "shortValue");
130: wrappers = new HashMap();
131: wrappers.put("boolean", "Boolean");
132: wrappers.put("byte", "Byte");
133: wrappers.put("char", "Character");
134: wrappers.put("double", "Double");
135: wrappers.put("float", "Float");
136: wrappers.put("int", "Integer");
137: wrappers.put("long", "Long");
138: wrappers.put("short", "Short");
139: }
140:
141: public AbstractGenerator(InternalConfig internalConfig) {
142:
143: defaultRenderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
144: excludes = new String[0];
145: includes = new String[0];
146: config = null;
147: dest = new File(".");
148: verbose = Boolean.getBoolean(internalConfig
149: .getProperty("project.verbose"));
150: writer = new JavaSourceWriter();
151: }
152:
153: public String getDefaultRenderKitId() {
154: return defaultRenderKitId;
155: }
156:
157: public void setDefaultRenderKitId(String defaultRenderKitId) {
158: this .defaultRenderKitId = defaultRenderKitId;
159: }
160:
161: public String[] getExcludes() {
162: return excludes;
163: }
164:
165: public void setExcludes(String excludes[]) {
166: this .excludes = excludes;
167: }
168:
169: public String[] getIncludes() {
170: return includes;
171: }
172:
173: public void setIncludes(String includes[]) {
174: this .includes = includes;
175: }
176:
177: public FacesConfigBean getConfig() {
178: return config;
179: }
180:
181: public void setConfig(FacesConfigBean config) {
182: this .config = config;
183: }
184:
185: public File getDest() {
186: return dest;
187: }
188:
189: public void setDest(File dest) {
190: this .dest = dest;
191: }
192:
193: public boolean isVerbose() {
194: return verbose;
195: }
196:
197: public void setVerbose(boolean verbose) {
198: this .verbose = verbose;
199: }
200:
201: public JavaSourceWriter getWriter() {
202: return writer;
203: }
204:
205: public void setWriter(JavaSourceWriter writer) {
206: this .writer = writer;
207: }
208:
209: protected ComponentBean baseComponent(ComponentBean cb) {
210: String baseComponentType = cb.getBaseComponentType();
211: if (baseComponentType == null) {
212: return null;
213: }
214: ComponentBean bcb = getConfig().getComponent(baseComponentType);
215: if (bcb == null) {
216:
217: logger.log(Level.SEVERE, " invalid base component");
218: throw new IllegalArgumentException(
219: " invalid base component");
220: } else {
221: return bcb;
222: }
223: }
224:
225: protected String capitalize(String name) {
226: return Character.toUpperCase(name.charAt(0))
227: + name.substring(1);
228: }
229:
230: public String componentFamily(ComponentBean cb) {
231:
232: String componentFamily = cb.getComponentFamily();
233: if (componentFamily == null) {
234: ComponentBean bcb = baseComponent(cb);
235: do {
236: if ((componentFamily != null) || (bcb == null)) {
237: break;
238: }
239: componentFamily = bcb.getComponentFamily();
240: if (componentFamily == null) {
241: bcb = baseComponent(bcb);
242: }
243: } while (true);
244: }
245: return componentFamily;
246: }
247:
248: protected FacetBean[] facets(ComponentBean cb, RendererBean rb) {
249:
250: List components = new ArrayList();
251: components.add(cb);
252: ComponentBean current = cb;
253: do {
254: ComponentBean bcb = baseComponent(current);
255: if (bcb == null) {
256: break;
257: }
258: components.add(bcb);
259: current = bcb;
260: } while (true);
261:
262: List facets = new ArrayList();
263: FacetBean f[] = null;
264: for (int i = components.size() - 1; i >= 0; i--) {
265: current = (ComponentBean) components.get(i);
266: f = current.getFacets();
267: for (int j = 0; j < f.length; j++) {
268: FacetBean facet = lookupFacet(facets, f[j]
269: .getFacetName());
270: if (facet == null) {
271: facets.add(f[j].clone());
272: } else {
273: mergeFacet(facet, f[j]);
274: }
275: }
276:
277: }
278:
279: f = rb.getFacets();
280: for (int j = 0; j < f.length; j++) {
281: FacetBean facet = lookupFacet(facets, f[j].getFacetName());
282: if (facet == null) {
283: facets.add(f[j].clone());
284: } else {
285: mergeFacet(facet, f[j]);
286: }
287: }
288:
289: return (FacetBean[]) facets
290: .toArray(new FacetBean[facets.size()]);
291: }
292:
293: protected boolean generated(String fqcn) {
294:
295: for (int i = 0; i < excludes.length; i++) {
296: if (fqcn.equals(excludes[i])) {
297: return false;
298: }
299: }
300:
301: for (int i = 0; i < includes.length; i++) {
302: if (fqcn.equals(includes[i])) {
303: return true;
304: }
305: }
306:
307: return includes.length == 0;
308: }
309:
310: protected String mangle(String name) {
311: if (keywords.contains(name)) {
312: return "_" + name;
313: } else {
314: return name;
315: }
316: }
317:
318: protected PropertyBean merge(PropertyBean pb, AttributeBean ab) {
319: if (ab == null) {
320: return pb;
321: }
322: PropertyBean newPb = new PropertyBean();
323: pb.copy(newPb);
324: if (!ab.isBindable()) {
325: newPb.setBindable(false);
326: }
327: if (ab.isExpert()) {
328: newPb.setExpert(true);
329: }
330: if (ab.isHidden()) {
331: newPb.setHidden(true);
332: }
333: if (ab.isRequired()) {
334: newPb.setRequired(true);
335: }
336: if (ab.isSuppressed()) {
337: newPb.setSuppressed(true);
338: }
339: if (!ab.isTagAttribute()) {
340: newPb.setTagAttribute(false);
341: }
342: return newPb;
343: }
344:
345: protected File outputFile(String fqcn) {
346: return new File(getDest(), fqcn
347: .replace('.', File.separatorChar)
348: + ".java");
349: }
350:
351: protected boolean primitive(String type) {
352: return defaults.containsKey(type);
353: }
354:
355: protected RendererBean renderer(ComponentBean cb) {
356:
357: String rendererType = rendererType(cb);
358: if (rendererType == null) {
359: return null;
360: }
361: String componentFamily = componentFamily(cb);
362: if (componentFamily == null) {
363:
364: logger.log(Level.SEVERE, "NoComponentFamily");
365: throw new IllegalArgumentException("NoComponentFamily");
366: }
367:
368: RenderKitBean renderKitBean = getConfig().getRenderKit(
369: getDefaultRenderKitId());
370: logger.log(Level.FINEST, "renderKit="
371: + renderKitBean.getRenderKitId());
372:
373: RendererBean rb = renderKitBean.getRenderer(componentFamily,
374: rendererType);
375:
376: if (rb == null) {
377: logger
378: .log(Level.SEVERE, "RenderBean componentFamily="
379: + componentFamily + " rendererType="
380: + rendererType);
381: }
382:
383: if (rb == null) {
384: logger.log(Level.SEVERE, "InvalidRendererType");
385: throw new IllegalArgumentException("InvalidRendererType");
386: } else {
387: return rb;
388: }
389: }
390:
391: public String rendererType(ComponentBean cb) {
392:
393: String rendererType = cb.getRendererType();
394: if (rendererType == null) {
395: ComponentBean bcb = baseComponent(cb);
396: do {
397: if ((rendererType != null) || (bcb == null)) {
398: break;
399: }
400: rendererType = bcb.getRendererType();
401: if (rendererType == null) {
402: bcb = baseComponent(bcb);
403: }
404: } while (true);
405: }
406: return rendererType;
407: }
408:
409: protected String simpleClassName(String fqcn) {
410:
411: int last = fqcn.lastIndexOf('.');
412: if (last >= 0) {
413: return fqcn.substring(last + 1);
414: } else {
415: return fqcn;
416: }
417: }
418:
419: private FacetBean lookupFacet(List list, String name) {
420:
421: for (Iterator facets = list.iterator(); facets.hasNext();) {
422: FacetBean facet = (FacetBean) facets.next();
423: if (name.equals(facet.getFacetName())) {
424: return facet;
425: }
426: }
427: return null;
428: }
429:
430: private void mergeFacet(FacetBean orig, FacetBean upd) {
431: DescriptionBean dbs[] = upd.getDescriptions();
432: for (int i = 0; i < dbs.length; i++) {
433: orig.addDescription(new DescriptionBean(dbs[i]
434: .getDescription(), dbs[i].getLang()));
435: }
436:
437: DisplayNameBean dns[] = upd.getDisplayNames();
438: for (int i = 0; i < dns.length; i++) {
439: orig.addDisplayName(new DisplayNameBean(dns[i]
440: .getDisplayName(), dns[i].getLang()));
441: }
442:
443: IconBean ibs[] = upd.getIcons();
444: for (int i = 0; i < ibs.length; i++) {
445: orig.addIcon(new IconBean(ibs[i].getLang(), ibs[i]
446: .getLargeIcon(), ibs[i].getSmallIcon()));
447: }
448:
449: Map map = upd.getNamedValues();
450: NamedValueBean nvb;
451: for (Iterator keys = map.keySet().iterator(); keys.hasNext(); orig
452: .addNamedValue(new NamedValueBean(nvb.getName(), nvb
453: .getExpression(), nvb.getValue()))) {
454: String key = (String) keys.next();
455: nvb = (NamedValueBean) map.get(key);
456: }
457:
458: }
459:
460: }
|