1 16 package com.google.gwt.user.rebind; 17 18 import com.google.gwt.core.ext.GeneratorContext; 19 import com.google.gwt.core.ext.TreeLogger; 20 import com.google.gwt.user.rebind.ClassSourceFileComposerFactory.JavaSourceCategory; 21 22 import java.io.PrintWriter ; 23 24 class ClassSourceFileComposer implements SourceWriter { 25 26 29 private static final String STAR_COMMENT_LINE = " * "; 30 31 private boolean atStart; 32 33 37 private String commentIndicator; 38 39 private final GeneratorContext ctx; 40 41 44 private boolean inComment; 45 46 private int indent; 47 48 private final PrintWriter printWriter; 49 50 ClassSourceFileComposer(GeneratorContext ctx, PrintWriter printWriter, 51 String targetPackageName, String targetClassShortName, 52 String superClassName, String [] interfaceNames, String [] imports, 53 JavaSourceCategory category, String classJavaDocComment) { 54 this.ctx = ctx; 55 this.printWriter = printWriter; 56 if (targetPackageName == null) { 57 throw new IllegalArgumentException ("Cannot supply a null package name to" 58 + targetClassShortName); 59 } 60 println("package " + targetPackageName + ";"); 62 println(); 63 if (imports != null && imports.length > 0) { 64 for (int i = 0, n = imports.length; i < n; ++i) { 65 println("import " + imports[i] + ";"); 66 } 67 println(); 68 } 69 if (classJavaDocComment != null) { 70 beginJavaDocComment(); 71 print(classJavaDocComment); 72 endJavaDocComment(); 73 } 74 if (category == JavaSourceCategory.CLASS) { 75 emitClassDecl(targetClassShortName, superClassName, interfaceNames); 76 } else { 77 emitInterfaceDecl(targetClassShortName, superClassName, interfaceNames); 78 } 79 println(" {"); 80 indent(); 81 } 82 83 86 public void beginJavaDocComment() { 87 println("\n/**"); 88 inComment = true; 89 commentIndicator = STAR_COMMENT_LINE; 90 } 91 92 public void commit(TreeLogger logger) { 93 outdent(); 94 println("}"); 95 printWriter.close(); 96 if (ctx != null) { 98 ctx.commit(logger, printWriter); 99 } 100 } 101 102 105 public void endJavaDocComment() { 106 inComment = false; 107 println("\n */"); 108 } 109 110 public void indent() { 111 ++indent; 112 } 113 114 public void indentln(String s) { 115 indent(); 116 println(s); 117 outdent(); 118 } 119 120 public void outdent() { 121 --indent; 122 } 123 124 public void print(String s) { 125 if (atStart) { 128 for (int j = 0; j < indent; ++j) { 129 printWriter.print(" "); 130 } 131 if (inComment) { 132 printWriter.print(commentIndicator); 133 } 134 atStart = false; 135 } 136 String rest = null; 139 int i = s.indexOf("\n"); 140 if (i > -1 && i < s.length() - 1) { 141 rest = s.substring(i + 1); 142 s = s.substring(0, i + 1); 143 } 144 printWriter.print(s); 145 if (rest != null) { 148 atStart = true; 149 print(rest); 150 } 151 } 152 153 public void println() { 154 print("\n"); 155 atStart = true; 156 } 157 158 public void println(String s) { 159 print(s + "\n"); 160 atStart = true; 161 } 162 163 private void emitClassDecl(String targetClassShortName, 164 String superClassName, String [] interfaceNames) { 165 print("public class " + targetClassShortName); 166 if (superClassName != null) { 167 print(" extends " + superClassName); 168 } 169 if (interfaceNames != null && interfaceNames.length > 0) { 170 print(" implements "); 171 for (int i = 0, n = interfaceNames.length; i < n; ++i) { 172 if (i > 0) { 173 print(", "); 174 } 175 print(interfaceNames[i]); 176 } 177 } 178 } 179 180 private void emitInterfaceDecl(String targetClassShortName, 181 String superClassName, String [] interfaceNames) { 182 if (superClassName != null) { 183 throw new IllegalArgumentException ("Cannot set superclass name " 184 + superClassName + " on a interface."); 185 } 186 print("public interface " + targetClassShortName); 187 if (interfaceNames != null && interfaceNames.length > 0) { 188 print(" extends "); 189 for (int i = 0; i < interfaceNames.length; ++i) { 190 if (i > 0) { 191 print(", "); 192 } 193 print(interfaceNames[i]); 194 } 195 } 196 } 197 } 198 | Popular Tags |