001: /*
002: * Copyright 2006 Ethan Nicholas. All rights reserved.
003: * Use is subject to license terms.
004: */
005: package jaxx.compiler;
006:
007: import java.lang.reflect.*;
008: import java.util.*;
009:
010: /** A Java source file being generated for output. Once the class is completely initialized, use the
011: * {@link #toString} method to generate source code for it.
012: */
013: public class JavaFile {
014: private int modifiers;
015: private String className;
016: private List/*<String>*/imports = new ArrayList/*<String>*/();
017: private List/*<JavaField>*/fields = new ArrayList/*<JavaField>*/();
018: private List/*<JavaMethod>*/methods = new ArrayList/*<JavaMethod>*/();
019: private List/*<JavaFile>*/innerClasses = new ArrayList/*<JavaFile>*/();
020: private String super Class;
021: private String[] interfaces;
022: private StringBuffer rawBodyCode = new StringBuffer();
023:
024: public JavaFile() {
025: }
026:
027: public JavaFile(int modifiers, String className, String super Class) {
028: this (modifiers, className, super Class, null);
029: }
030:
031: public JavaFile(int modifiers, String className, String super Class,
032: String[] interfaces) {
033: this .modifiers = modifiers;
034: this .className = className;
035: this .super Class = super Class;
036: this .interfaces = interfaces;
037: }
038:
039: public void addImport(String importString) {
040: imports.add(importString);
041: }
042:
043: public String[] getImports() {
044: return (String[]) imports.toArray(new String[imports.size()]);
045: }
046:
047: public int getModifiers() {
048: return modifiers;
049: }
050:
051: public void setModifiers(int modifiers) {
052: this .modifiers = modifiers;
053: }
054:
055: public String getClassName() {
056: return className;
057: }
058:
059: public void setClassName(String className) {
060: this .className = className;
061: }
062:
063: public String getSuperClass() {
064: return super Class;
065: }
066:
067: public void setSuperClass(String super Class) {
068: this .super Class = super Class;
069: }
070:
071: public String[] getInterfaces() {
072: return interfaces;
073: }
074:
075: public void setInterfaces(String[] interfaces) {
076: this .interfaces = interfaces;
077: }
078:
079: public void addMethod(JavaMethod method) {
080: methods.add(method);
081: }
082:
083: public JavaMethod[] getMethods() {
084: return (JavaMethod[]) methods.toArray(new JavaMethod[methods
085: .size()]);
086: }
087:
088: public void addField(JavaField field) {
089: fields.add(field);
090: }
091:
092: public JavaField[] getFields() {
093: return (JavaField[]) fields
094: .toArray(new JavaField[fields.size()]);
095: }
096:
097: public static String addIndentation(String source, int indentation) {
098: return indent(source, indentation, false);
099: }
100:
101: public static String setIndentation(String source, int indentation) {
102: return indent(source, indentation, true);
103: }
104:
105: public static String indent(String source, int indentation,
106: boolean trim) {
107: if (trim)
108: source = source.trim();
109: char[] spaces = new char[indentation];
110: Arrays.fill(spaces, ' ');
111: StringBuffer result = new StringBuffer();
112: String[] lines = source.split(System
113: .getProperty("line.separator")
114: + "|\n");
115: for (int i = 0; i < lines.length; i++) {
116: if (i > 0)
117: result.append(JAXXCompiler.getLineSeparator());
118: result.append(spaces);
119: result.append(trim ? lines[i].trim() : lines[i]);
120: }
121: return result.toString();
122: }
123:
124: public void addBodyCode(String bodyCode) {
125: rawBodyCode.append(bodyCode);
126: }
127:
128: public String getClassBody() {
129: StringBuffer result = new StringBuffer();
130: if (fields.size() > 0) {
131: Iterator/*<JavaField>*/fieldIterator = fields.iterator();
132: while (fieldIterator.hasNext()) {
133: result.append(addIndentation(fieldIterator.next()
134: .toString(), 4));
135: result.append(JAXXCompiler.getLineSeparator());
136: }
137:
138: result.append(JAXXCompiler.getLineSeparator());
139: }
140:
141: if (rawBodyCode.length() > 0) {
142: result.append(" /* begin raw body code */");
143: result.append(rawBodyCode);
144: result.append(" /* end raw body code */");
145: }
146:
147: Iterator/*<JavaFile>*/innerClassIterator = innerClasses
148: .iterator();
149: while (innerClassIterator.hasNext()) {
150: result.append(addIndentation(innerClassIterator.next()
151: .toString(), 4));
152: result.append(JAXXCompiler.getLineSeparator());
153: result.append(JAXXCompiler.getLineSeparator());
154: }
155:
156: Iterator/*<JavaMethod>*/methodIterator = methods.iterator();
157: while (methodIterator.hasNext()) {
158: result.append(addIndentation(methodIterator.next()
159: .toString(), 4));
160: result.append(JAXXCompiler.getLineSeparator());
161: result.append(JAXXCompiler.getLineSeparator());
162: }
163:
164: return result.toString();
165: }
166:
167: public String getClassDefinition() {
168: StringBuffer result = new StringBuffer();
169: result.append(getModifiersText(modifiers));
170: result.append("class ");
171: result.append(className
172: .substring(className.lastIndexOf(".") + 1));
173: result.append(" extends ");
174: result.append(super Class);
175: if (interfaces != null && interfaces.length > 0) {
176: result.append(" implements ");
177: for (int i = 0; i < interfaces.length; i++) {
178: if (i > 0)
179: result.append(", ");
180: result.append(interfaces[i]);
181: }
182: }
183: result.append(" {");
184: result.append(JAXXCompiler.getLineSeparator());
185: result.append(getClassBody());
186: result.append("}");
187: return result.toString();
188: }
189:
190: public static String getModifiersText(int modifiers) {
191: if (modifiers == 0)
192: return "";
193: else
194: return Modifier.toString(modifiers) + ' ';
195: }
196:
197: /** Returns the Java source code for this class.
198: *
199: *@return a complete Java file for this class
200: */
201: public String toString() {
202: StringBuffer result = new StringBuffer();
203: if (className.indexOf(".") != -1) {
204: result.append("package "
205: + className
206: .substring(0, className.lastIndexOf("."))
207: + ";");
208: result.append(JAXXCompiler.getLineSeparator());
209: result.append(JAXXCompiler.getLineSeparator());
210: }
211:
212: if (imports.size() > 0) {
213: Iterator/*<String>*/importIterator = imports.iterator();
214: while (importIterator.hasNext()) {
215: result.append("import ");
216: result.append(importIterator.next());
217: result.append(';');
218: result.append(JAXXCompiler.getLineSeparator());
219: }
220: result.append(JAXXCompiler.getLineSeparator());
221: }
222:
223: result.append(getClassDefinition());
224: return result.toString();
225: }
226: }
|