001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/org/sakaiproject/tool/assessment/devtools/RenderMaker.java $
003: * $Id: RenderMaker.java 14180 2006-08-31 23:09:00Z ktsao@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 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.tool.assessment.devtools;
021:
022: import java.io.BufferedReader;
023: import java.io.FileReader;
024: import java.io.IOException;
025: import java.io.Reader;
026: import java.io.StringReader;
027: import java.util.StringTokenizer;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: public class RenderMaker {
033: private static Log log = LogFactory.getLog(RenderMaker.class);
034: // either run this application from the command line with the input file as an
035: // argument, or edit this name to suit your fancy.
036: // private static String fileName = "c:\\Navigo\\webapp\\html\\picker.html";
037: private static String fileName = "c:\\divstart.html";
038:
039: private static final String QUOTE = "\"";
040: private static final String ESCAPEDQUOTE = "\\" + "\"";
041:
042: // this provides for the notation to place a variable in the input
043: private static final String VARIABLEESCAPE = "%";
044:
045: private static final String PLUS = " + ";
046:
047: private static String WRITETEXT = " writer.write(\"";
048: private static String ENDWRITETEXT = "\");\n";
049: private static String JAVADOC = "/**\n"
050: + " * <p>Faces render output method .</p>\n"
051: + " * <p>Method Generator: "
052: + "org.sakaiproject.tool.assessment.devtoolsRenderMaker</p>\n"
053: + " *\n"
054: + " * @param context <code>FacesContext</code> for the current request\n"
055: + " * @param component <code>UIComponent</code> being rendered\n"
056: + " *\n"
057: + " * @throws IOException if an input/output error occurs\n"
058: + " *" + "/\n";
059: private static String METHODSIGNATURE = " public void ";
060: private static String METHODSIGNATUREEND = "(FacesContext context, UIComponent component)\n"
061: + " throws IOException {\n\n";
062: private static String GETAWRITER = " ResponseWriter writer = context.getResponseWriter();\n\n";
063:
064: private static String METHODEND = " }\n\n";
065:
066: private static String[] validMethods = { "encodeBegin",
067: "encodeChildren", "encodeEnd" };
068:
069: public RenderMaker() {
070: }
071:
072: public static void main(String[] args) {
073: if (args.length > 0) {
074: fileName = args[0];
075: }
076: try {
077: if (args.length > 2) {
078: log.debug(makeMethodFromFile(fileName, args[1]));
079: } else {
080: log.debug(makeMethodFromFile(fileName, "encodeBegin"));
081: }
082: } catch (IOException ex) {
083: log.error("ooops");
084: }
085: }
086:
087: public static void stringTest() {
088: StringReader sr = new StringReader(
089: "<table bgcolor=\"#000000\" width=\"100%\" border=\"0\" cellpadding=\"0\"");
090: StringReader sr2 = new StringReader(
091: "<a href=\"%xxx%\">100%%%</a>");
092: try {
093: log.debug(makeMethod(sr, "encodeBegin"));
094: log.debug(makeMethod(sr2, "encodeChildren"));
095: } catch (Exception ex) {
096: log
097: .error("\n\n***********\n\nError in makeMethod(): "
098: + ex);
099: }
100: }
101:
102: public static void fileTest() {
103: try {
104: log.debug(makeMethodFromFile(fileName, "encodeBegin"));
105: } catch (Exception ex) {
106: log
107: .error("\n\n***********\n\nError in makeMethodFromFile(): "
108: + ex);
109: }
110: }
111:
112: public static String makeMethodFromFile(String fileName,
113: String methodName) throws IOException {
114: return "\n/* *** GENERATOR FILE: "
115: + fileName
116: + "*** */\n"
117: + "\n/* *** IF SOURCE DOCUMENT CHANGES YOU NEED TO REGENERATE THIS METHOD"
118: + "*** */\n"
119: + makeMethod(new FileReader(fileName), methodName);
120: }
121:
122: public static String makeMethod(Reader reader, String methodName)
123: throws IOException {
124: // validate you are making a valid method source
125: boolean valid = false;
126: for (int i = 0; i < validMethods.length; i++) {
127: if (validMethods[i].equals(methodName)) {
128: valid = true;
129: break;
130: }
131: }
132: if (!valid)
133: throw new IllegalArgumentException(methodName);
134:
135: BufferedReader br = new BufferedReader(reader);
136: StringBuffer sb = new StringBuffer();
137:
138: sb.append(JAVADOC + METHODSIGNATURE + methodName
139: + METHODSIGNATUREEND);
140: sb.append(GETAWRITER);
141:
142: String buf;
143:
144: while ((buf = br.readLine()) != null) {
145: sb.append(makeLine(buf));
146: }
147:
148: sb.append(METHODEND);
149:
150: return sb.toString();
151: }
152:
153: private static String makeLine(String s) {
154: return makeWriterWriteText(normalize(s));
155: }
156:
157: private static String normalize(String abnormal) {
158: StringBuffer normal = new StringBuffer();
159: StringTokenizer st = new StringTokenizer(abnormal, QUOTE
160: + VARIABLEESCAPE, true);
161:
162: while (st.hasMoreTokens()) {
163: String token = st.nextToken();
164: if (QUOTE.equals(token)) {
165: normal.append(ESCAPEDQUOTE);
166: } else if (VARIABLEESCAPE.equals(token)) {
167: // normal.append(token);
168: normal.append(getEscapedVariableToken(st));
169: } else {
170: normal.append(token);
171: }
172: }
173:
174: return normal.toString();
175: }
176:
177: /**
178: * take variable expressed inside "%" escapes
179: *
180: *
181: * this provides for the notation to place a variable in the input
182: * you MUST add code to initialize the variables
183: * usage:
184: * <a href="%variableName%">%variableName%</a>' ==>
185: * 'writer.writeText("<a href=\""+variableName+"\">" + variableName + "</a>", null);'
186: *
187: * escaping out the escape:
188: * '%%%' => '%'
189: *
190: * @param st a StringTokeinzer tokenizing on the escapes and hitting one
191: * @return the concatentation of the variable into the writer
192: *
193: */
194: private static String getEscapedVariableToken(StringTokenizer st) {
195: String token = "";
196: String returnToken = "";
197:
198: if (st.hasMoreTokens()) {
199: token = st.nextToken();
200:
201: // if it is an escape it is an escaped escape so we return an escape
202: if (VARIABLEESCAPE.equals(token)) {
203: returnToken = token;
204: } else // well, it's not an escape, so we need to concatenate it in
205: {
206: returnToken = QUOTE + PLUS + token + PLUS + QUOTE;
207: }
208:
209: // now we throw away the end token
210: if (st.hasMoreTokens()) {
211: st.nextToken();
212: }
213: }
214: return returnToken;//QUOTE + PLUS + token + PLUS + QUOTE;
215: }
216:
217: private static String makeWriterWriteText(String s) {
218: return WRITETEXT + s + ENDWRITETEXT;
219: }
220:
221: }
|