001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/devtool/src/java/org/sakaiproject/jsf/devtool/JSFGenerator.java $
003: * $Id: JSFGenerator.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.jsf.devtool;
021:
022: import java.io.BufferedReader;
023: import java.io.BufferedWriter;
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileWriter;
027: import java.io.InputStreamReader;
028: import java.io.StringReader;
029: import java.util.ArrayList;
030: import java.util.Arrays;
031: import java.util.Enumeration;
032: import java.util.HashSet;
033: import java.util.Properties;
034: import java.util.Set;
035:
036: import org.apache.velocity.Template;
037: import org.apache.velocity.VelocityContext;
038: import org.apache.velocity.app.Velocity;
039:
040: public class JSFGenerator {
041:
042: private static final String OUTPUT_DIR = "/w/jsf/widgets/src/java/org/sakaiproject/jsf";
043: private static final String PIECES_DIR = "./pieces";
044: private static final String TAG_DIR = "./tags";
045:
046: private static final Set standardAttributes = new HashSet(Arrays
047: .asList(new String[] { "id", "binding", "rendered" }));
048:
049: public static void main(String[] args) throws Exception {
050:
051: JSFGenerator g = new JSFGenerator();
052:
053: // initialize Velocity
054: Velocity.init("velocity.properties");
055:
056: // load attribute descriptions
057: Properties attributeDescriptions = new Properties();
058: BufferedReader in = new BufferedReader(new InputStreamReader(
059: new FileInputStream(PIECES_DIR
060: + "/attribute_descriptions.txt")));
061:
062: String line = null;
063: String curTag = null;
064: String curDesc = null;
065:
066: while ((line = in.readLine()) != null) {
067: line = line.trim();
068: if (line.startsWith("#"))
069: continue;
070:
071: if (line.length() == 0) {
072: if (curTag != null && curDesc != null) {
073: attributeDescriptions.setProperty(curTag, curDesc);
074: }
075: curTag = null;
076: curDesc = null;
077: } else if (curTag == null) {
078: curTag = line;
079: } else {
080: if (curDesc == null) {
081: curDesc = line;
082: } else {
083: curDesc = curDesc + "\n" + line;
084: }
085: }
086: }
087:
088: // find all tag definitions, and generate JSF tags for each one
089: File dir = new File(TAG_DIR);
090: File[] files = dir.listFiles();
091: for (int i = 0; i < files.length; i++) {
092: File f = files[i];
093: String filename = f.getName();
094: if (filename.endsWith(".tag")) {
095: g.generate(f, OUTPUT_DIR, PIECES_DIR,
096: attributeDescriptions, standardAttributes);
097: System.out.println("Generated tag: " + filename);
098: }
099: }
100:
101: }
102:
103: public void generate(File file, String outputBaseDir,
104: String piecesDir, Properties attributeDescriptions,
105: Set standardAttributes) throws Exception {
106: String tag = file.getName().substring(0,
107: file.getName().length() - 4);
108:
109: String[] templates = { "Tag", "Component", "Renderer",
110: "faces-config_component", "faces-config_renderer",
111: "tld" };
112: String[] outputpostfixes = { "Tag.java", "Component.java",
113: "Renderer.java", "faces-config_component.xml",
114: "faces-config_renderer.xml", "sakai_jsf.tld" };
115: String[] outputprefixes = { "tag/", "component/", "renderer/",
116: "", "", "" };
117: boolean[] outputconcat = { false, false, false, true, true,
118: true };
119:
120: Properties tagProps = new Properties();
121: tagProps.load(new FileInputStream(file));
122:
123: // set props from file
124: VelocityContext context = new VelocityContext();
125: Enumeration e = tagProps.keys();
126: while (e.hasMoreElements()) {
127: String key = (String) e.nextElement();
128: context.put(key, tagProps.get(key));
129: }
130:
131: // get rendered output from file
132: BufferedReader in = new BufferedReader(new StringReader(
133: tagProps.getProperty("sampleOutput")));
134: String l;
135: ArrayList lines = new ArrayList();
136: while ((l = in.readLine()) != null) {
137: lines.add(l.replaceAll("\\\"", "\\\\\""));
138: }
139: in.close();
140:
141: // set calculated props
142: context.put("rendered", lines);
143: context.put("caps", this );
144:
145: String attrsStr = tagProps.getProperty("attrs");
146: String[] attrs = attrsStr.split(",");
147: Set attrsSet = new HashSet(Arrays.asList(attrs));
148: Set tagAttrsSet = new HashSet(attrsSet);
149: tagAttrsSet.removeAll(standardAttributes);
150:
151: context.put("attrdescriptions", attributeDescriptions);
152: context.put("tag", tag);
153: context.put("attrs", attrs);
154: context.put("tagattrs", tagAttrsSet);
155: context.put("componentType", "org.sakaiproject." + caps(tag));
156: context.put("rendererType", "org.sakaiproject." + caps(tag));
157: context.put("tagClassName", caps(tag) + "Tag");
158: context.put("componentClassName", caps(tag) + "Component");
159: context.put("rendererClassName", caps(tag) + "Renderer");
160: context.put("tagClass", "org.sakaiproject.jsf.tag." + caps(tag)
161: + "Tag");
162: context.put("componentClass", "org.sakaiproject.jsf.component."
163: + caps(tag) + "Component");
164: context.put("rendererClass", "org.sakaiproject.jsf.renderer."
165: + caps(tag) + "Renderer");
166:
167: for (int i = 0; i < templates.length; i++) {
168: BufferedWriter out;
169:
170: String inputvmname = piecesDir + "/" + templates[i] + ".vm";
171: String outputfilename;
172: if (!outputconcat[i]) {
173: outputfilename = outputBaseDir + "/"
174: + outputprefixes[i] + caps(tag)
175: + outputpostfixes[i];
176: } else {
177: outputfilename = outputBaseDir + "/"
178: + outputprefixes[i] + outputpostfixes[i];
179: }
180:
181: Template template = Velocity.getTemplate(inputvmname);
182:
183: out = new BufferedWriter(new FileWriter(outputfilename,
184: outputconcat[i]));
185:
186: template.merge(context, out);
187:
188: out.flush();
189: out.close();
190: //System.out.println("Output "+outputfilename);
191: }
192: }
193:
194: /** Capitalize first letter */
195: public String caps(String arg) {
196: return arg.substring(0, 1).toUpperCase()
197: + arg.substring(1, arg.length());
198: }
199:
200: }
|