001: package com.mockrunner.gen.proc;
002:
003: import java.util.List;
004:
005: public class JavaLineAssembler {
006: private StringBuffer buffer;
007: private String newLine;
008: private int indentLevel;
009: private String defaultMethodModifier;
010:
011: public JavaLineAssembler() {
012: reset();
013: newLine = System.getProperty("line.separator");
014: }
015:
016: public void reset() {
017: buffer = new StringBuffer();
018: indentLevel = 0;
019: defaultMethodModifier = "";
020: }
021:
022: public String getResult() {
023: return buffer.toString();
024: }
025:
026: public StringBuffer getResultAsBuffer() {
027: return new StringBuffer(getResult());
028: }
029:
030: public void setIndentLevel(int indentLevel) {
031: this .indentLevel = indentLevel;
032: }
033:
034: public void setDefaultMethodModifier(String defaultMethodModifier) {
035: this .defaultMethodModifier = defaultMethodModifier;
036: }
037:
038: public void appendBlank() {
039: buffer.append(" ");
040: }
041:
042: public void appendBlank(int number) {
043: for (int ii = 0; ii < number; ii++) {
044: appendBlank();
045: }
046: }
047:
048: public void appendNewLine() {
049: buffer.append(newLine);
050: }
051:
052: public void appendNewLine(int number) {
053: for (int ii = 0; ii < number; ii++) {
054: appendNewLine();
055: }
056: }
057:
058: public void appendLeftBrace() {
059: buffer.append("{");
060: }
061:
062: public void appendRightBrace() {
063: buffer.append("}");
064: }
065:
066: public void appendLeftParenthesis() {
067: buffer.append("(");
068: }
069:
070: public void appendRightParenthesis() {
071: buffer.append(")");
072: }
073:
074: public void appendComma() {
075: buffer.append(",");
076: }
077:
078: public void appendIndent() {
079: appendBlank(indentLevel * 4);
080: }
081:
082: public void appendLine(String line) {
083: if (null != line && line.length() > 0) {
084: appendIndent();
085: buffer.append(line);
086: appendNewLine();
087: }
088: }
089:
090: public void appendCodeLines(String[] lines) {
091: if (null == lines || lines.length <= 0) {
092: appendNewLine();
093: } else {
094: for (int ii = 0; ii < lines.length; ii++) {
095: appendLine(lines[ii]);
096: }
097: }
098: }
099:
100: public void appendPackageInfo(String packageName) {
101: if (null != packageName && packageName.length() > 0) {
102: appendLine("package " + packageName + ";");
103: appendNewLine();
104: }
105: }
106:
107: public void appendImport(String importLine) {
108: appendLine("import " + importLine + ";");
109: }
110:
111: public void appendImports(List imports) {
112: if (null == imports)
113: return;
114: for (int ii = 0; ii < imports.size(); ii++) {
115: appendImport((String) imports.get(ii));
116: }
117: }
118:
119: public void appendClassDefintion(String className) {
120: appendClassDefintion(className, "");
121: }
122:
123: public void appendClassDefintion(String className, String super Class) {
124: appendClassDefintion(className, "", super Class, null);
125: }
126:
127: public void appendClassDefintion(String className,
128: String[] interfaceDef) {
129: appendClassDefintion(className, "", "", interfaceDef);
130: }
131:
132: public void appendClassDefintion(String className, String modifier,
133: String super Class, String[] interfaceDef) {
134: if (null == className || className.length() <= 0)
135: return;
136: if (null == super Class || super Class.length() <= 0) {
137: super Class = "";
138: } else {
139: super Class = " extends " + super Class;
140: }
141: String interfaceDefList = "";
142: if (null != interfaceDef && interfaceDef.length > 0) {
143: interfaceDefList = " implements "
144: + prepareCommaSeparatedList(interfaceDef, null);
145: }
146: if (null == modifier) {
147: modifier = "";
148: }
149: if (modifier.length() > 0) {
150: modifier += " ";
151: }
152: appendLine("public " + modifier + "class " + className
153: + super Class + interfaceDefList);
154: }
155:
156: public void appendMemberDeclaration(String type, String name) {
157: if (null == type || type.length() <= 0)
158: return;
159: if (null == name || name.length() <= 0)
160: return;
161: appendLine("private " + type + " " + name + ";");
162: }
163:
164: public void appendConstructorDeclaration(String name) {
165: appendConstructorDeclaration(name, null, null);
166: }
167:
168: public void appendConstructorDeclaration(String name,
169: String[] parameterTypes, String[] parameterNames) {
170: appendConstructorDeclaration(name, parameterTypes,
171: parameterNames, null);
172: }
173:
174: public void appendConstructorDeclaration(String name,
175: String[] parameterTypes, String[] parameterNames,
176: String[] exceptions) {
177: if (null == name || name.length() <= 0)
178: return;
179: StringBuffer buffer = new StringBuffer(30);
180: buffer.append("public ");
181: appendSignature(name, parameterTypes, parameterNames,
182: exceptions, buffer);
183: appendLine(buffer.toString());
184: }
185:
186: public void appendMethodDeclaration(String name) {
187: appendMethodDeclaration("void", name);
188: }
189:
190: public void appendMethodDeclaration(String returnType, String name) {
191: appendMethodDeclaration(returnType, name, null, null);
192: }
193:
194: public void appendMethodDeclaration(String returnType, String name,
195: String[] parameterTypes, String[] parameterNames) {
196: appendMethodDeclaration(null, returnType, name, parameterTypes,
197: parameterNames);
198: }
199:
200: public void appendMethodDeclaration(String[] modifiers,
201: String returnType, String name, String[] parameterTypes,
202: String[] parameterNames) {
203: appendMethodDeclaration(modifiers, returnType, name,
204: parameterTypes, parameterNames, null);
205: }
206:
207: public void appendMethodDeclaration(String[] modifiers,
208: String returnType, String name, String[] parameterTypes,
209: String[] parameterNames, String[] exceptions) {
210: if (null == name || name.length() <= 0)
211: return;
212: if (null == returnType || returnType.length() <= 0) {
213: returnType = "void";
214: }
215: StringBuffer buffer = new StringBuffer(30);
216: if (null != defaultMethodModifier
217: && defaultMethodModifier.length() > 0) {
218: buffer.append(defaultMethodModifier + " ");
219: }
220: buffer.append(prepareModifierList(modifiers));
221: buffer.append(returnType + " ");
222: appendSignature(name, parameterTypes, parameterNames,
223: exceptions, buffer);
224: appendLine(buffer.toString());
225: }
226:
227: private void appendSignature(String name, String[] parameterTypes,
228: String[] parameterNames, String[] exceptions,
229: StringBuffer buffer) {
230: buffer.append(name);
231: buffer.append("(");
232: buffer.append(prepareCommaSeparatedList(parameterTypes,
233: getParameterNameList(parameterTypes, parameterNames)));
234: buffer.append(")");
235: appendThrowsClause(exceptions, buffer);
236: }
237:
238: private void appendThrowsClause(String[] exceptions,
239: StringBuffer buffer) {
240: if (null == exceptions || exceptions.length <= 0)
241: return;
242: String throwsClause = prepareCommaSeparatedList(exceptions,
243: null);
244: buffer.append(" throws " + throwsClause);
245: }
246:
247: public void appendComment(String oneLineComment) {
248: if (null == oneLineComment || oneLineComment.length() <= 0)
249: return;
250: appendLine("//" + oneLineComment);
251: }
252:
253: public void appendBlockComment(String[] commentLines) {
254: appendBlockComment(commentLines, "/*");
255: }
256:
257: public void appendJavaDocComment(String[] commentLines) {
258: appendBlockComment(commentLines, "/**");
259: }
260:
261: private void appendBlockComment(String[] commentLines,
262: String commentStart) {
263: if (null == commentLines || commentLines.length <= 0)
264: return;
265: appendLine(commentStart);
266: for (int ii = 0; ii < commentLines.length; ii++) {
267: appendLine(" * " + commentLines[ii]);
268: }
269: appendLine(" */");
270: }
271:
272: private String prepareModifierList(String[] modifiers) {
273: if (null == modifiers)
274: modifiers = new String[0];
275: StringBuffer listBuffer = new StringBuffer(50);
276: for (int ii = 0; ii < modifiers.length; ii++) {
277: listBuffer.append(modifiers[ii] + " ");
278: }
279: return listBuffer.toString();
280: }
281:
282: private String[] getParameterNameList(String[] types, String[] names) {
283: if (null == types)
284: types = new String[0];
285: if (null == names)
286: names = new String[0];
287: if (names.length >= types.length)
288: return names;
289: String[] newNames = new String[types.length];
290: System.arraycopy(names, 0, newNames, 0, names.length);
291: for (int ii = 0; ii < types.length - names.length; ii++) {
292: newNames[names.length + ii] = "param" + (names.length + ii);
293: }
294: return newNames;
295: }
296:
297: private String prepareCommaSeparatedList(String[] types,
298: String[] names) {
299: if (null == types)
300: types = new String[0];
301: if (null == names)
302: names = new String[0];
303: StringBuffer listBuffer = new StringBuffer(50);
304: for (int ii = 0; ii < types.length; ii++) {
305: listBuffer.append(types[ii]);
306: if (ii < names.length) {
307: listBuffer.append(" " + names[ii]);
308: }
309: if (ii < types.length - 1) {
310: listBuffer.append(", ");
311: }
312: }
313: return listBuffer.toString();
314: }
315: }
|