001: /*
002: * Copyright (c) 2003, 2004 The Sakai Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.sakaiproject.jsf.devtool;
018:
019: import java.io.*;
020: import java.util.*;
021:
022: public class RenderMaker {
023: // either run this application from the command line with the input file as an
024: // argument, or edit this name to suit your fancy.
025: // private static String fileName = "c:\\Navigo\\webapp\\html\\picker.html";
026: private static String fileName = "c:\\JBuilderX\\bin\\popup.html";
027:
028: private static final String QUOTE = "\"";
029: private static final String ESCAPEDQUOTE = "\\" + "\"";
030:
031: // this provides for the notation to place a variable in the input
032: private static final String VARIABLEESCAPE = "%";
033:
034: private static final String PLUS = " + ";
035:
036: private static String WRITETEXT = " writer.write(\"";
037: private static String ENDWRITETEXT = "\");\n";
038: private static String JAVADOC = "/**\n"
039: + " * <p>Faces render output method .</p>\n"
040: + " * <p>Method Generator: "
041: + "org.sakaiproject.tool.assessment.devtoolsRenderMaker</p>\n"
042: + " *\n"
043: + " * @param context <code>FacesContext</code> for the current request\n"
044: + " * @param component <code>UIComponent</code> being rendered\n"
045: + " *\n"
046: + " * @throws IOException if an input/output error occurs\n"
047: + " *" + "/\n";
048: private static String METHODSIGNATURE = " public void ";
049: private static String METHODSIGNATUREEND = "(FacesContext context, UIComponent component)\n"
050: + " throws IOException {\n\n";
051: private static String GETAWRITER = " ResponseWriter writer = context.getResponseWriter();\n\n";
052:
053: private static String METHODEND = " }\n\n";
054:
055: private static String[] validMethods = { "encodeBegin",
056: "encodeChildren", "encodeEnd" };
057:
058: public RenderMaker() {
059: }
060:
061: public static void main(String[] args) {
062: if (args.length > 0) {
063: fileName = args[0];
064: }
065: try {
066: if (args.length > 2) {
067: System.out
068: .println(makeMethodFromFile(fileName, args[1]));
069: } else {
070: System.out.println(makeMethodFromFile(fileName,
071: "encodeBegin"));
072: }
073: } catch (IOException ex) {
074: System.out.println("ooops");
075: }
076: }
077:
078: public static void stringTest() {
079: StringReader sr = new StringReader(
080: "<table bgcolor=\"#000000\" width=\"100%\" border=\"0\" cellpadding=\"0\"");
081: StringReader sr2 = new StringReader(
082: "<a href=\"%xxx%\">100%%%</a>");
083: try {
084: System.out.println(makeMethod(sr, "encodeBegin"));
085: System.out.println(makeMethod(sr2, "encodeChildren"));
086: } catch (Exception ex) {
087: System.out
088: .println("\n\n***********\n\nError in makeMethod(): "
089: + ex);
090: }
091: }
092:
093: public static void fileTest() {
094: try {
095: System.out.println(makeMethodFromFile(fileName,
096: "encodeBegin"));
097: } catch (Exception ex) {
098: System.out
099: .println("\n\n***********\n\nError in makeMethodFromFile(): "
100: + ex);
101: }
102: }
103:
104: public static String makeMethodFromFile(String fileName,
105: String methodName) throws IOException {
106: return "\n/* *** GENERATOR FILE: "
107: + fileName
108: + "*** */\n"
109: + "\n/* *** IF SOURCE DOCUMENT CHANGES YOU NEED TO REGENERATE THIS METHOD"
110: + "*** */\n"
111: + makeMethod(new FileReader(fileName), methodName);
112: }
113:
114: public static String makeMethod(Reader reader, String methodName)
115: throws IOException {
116: // validate you are making a valid method source
117: boolean valid = false;
118: for (int i = 0; i < validMethods.length; i++) {
119: if (validMethods[i].equals(methodName)) {
120: valid = true;
121: break;
122: }
123: }
124: if (!valid)
125: throw new IllegalArgumentException(methodName);
126:
127: BufferedReader br = new BufferedReader(reader);
128: StringBuffer sb = new StringBuffer();
129:
130: sb.append(JAVADOC + METHODSIGNATURE + methodName
131: + METHODSIGNATUREEND);
132: sb.append(GETAWRITER);
133:
134: String buf;
135:
136: while ((buf = br.readLine()) != null) {
137: sb.append(makeLine(buf));
138: }
139:
140: sb.append(METHODEND);
141:
142: return sb.toString();
143: }
144:
145: private static String makeLine(String s) {
146: return makeWriterWriteText(normalize(s));
147: }
148:
149: private static String normalize(String abnormal) {
150: StringBuffer normal = new StringBuffer();
151: StringTokenizer st = new StringTokenizer(abnormal, QUOTE
152: + VARIABLEESCAPE, true);
153:
154: while (st.hasMoreTokens()) {
155: String token = st.nextToken();
156: if (QUOTE.equals(token)) {
157: normal.append(ESCAPEDQUOTE);
158: } else if (VARIABLEESCAPE.equals(token)) {
159: // normal.append(token);
160: normal.append(getEscapedVariableToken(st));
161: } else {
162: normal.append(token);
163: }
164: }
165:
166: return normal.toString();
167: }
168:
169: /**
170: * take variable expressed inside "%" escapes
171: *
172: *
173: * this provides for the notation to place a variable in the input
174: * you MUST add code to initialize the variables
175: * usage:
176: * <a href="%variableName%">%variableName%</a>' ==>
177: * 'writer.writeText("<a href=\""+variableName+"\">" + variableName + "</a>", null);'
178: *
179: * escaping out the escape:
180: * '%%%' => '%'
181: *
182: * @param st a StringTokeinzer tokenizing on the escapes and hitting one
183: * @return the concatentation of the variable into the writer
184: *
185: */
186: private static String getEscapedVariableToken(StringTokenizer st) {
187: String token = "";
188: String returnToken = "";
189:
190: if (st.hasMoreTokens()) {
191: token = st.nextToken();
192:
193: // if it is an escape it is an escaped escape so we return an escape
194: if (VARIABLEESCAPE.equals(token)) {
195: returnToken = token;
196: } else // well, it's not an escape, so we need to concatenate it in
197: {
198: returnToken = QUOTE + PLUS + token + PLUS + QUOTE;
199: }
200:
201: // now we throw away the end token
202: if (st.hasMoreTokens()) {
203: st.nextToken();
204: }
205: }
206: return returnToken;//QUOTE + PLUS + token + PLUS + QUOTE;
207: }
208:
209: private static String makeWriterWriteText(String s) {
210: return WRITETEXT + s + ENDWRITETEXT;
211: }
212:
213: }
|