1 33 34 package net.percederberg.grammatica.code.java; 35 36 import java.io.File ; 37 import java.io.FileWriter ; 38 import java.io.IOException ; 39 import java.io.PrintWriter ; 40 41 import net.percederberg.grammatica.code.CodeElement; 42 import net.percederberg.grammatica.code.CodeElementContainer; 43 import net.percederberg.grammatica.code.CodeStyle; 44 45 51 public class JavaFile extends CodeElementContainer { 52 53 56 private File dir; 57 58 61 private JavaPackage filePackage; 62 63 66 private CodeElement first = null; 67 68 73 public JavaFile(File basedir) { 74 this.dir = basedir; 75 this.filePackage = null; 76 } 77 78 86 public JavaFile(File basedir, JavaPackage filePackage) { 87 this.dir = filePackage.toFile(basedir); 88 this.filePackage = filePackage; 89 addElement(filePackage); 90 } 91 92 98 public String toString() { 99 if (first == null) { 100 return "UnknownFileName.java"; 101 } else { 102 return first.toString() + ".java"; 103 } 104 } 105 106 111 public void addComment(JavaComment comment) { 112 addElement(comment); 113 } 114 115 120 public void addImport(JavaImport imp) { 121 addElement(imp); 122 } 123 124 129 public void addClass(JavaClass cls) { 130 if (first == null) { 131 first = cls; 132 } 133 addElement(cls); 134 } 135 136 141 public void addInterface(JavaInterface ifc) { 142 if (first == null) { 143 first = ifc; 144 } 145 addElement(ifc); 146 } 147 148 156 public int category() { 157 return 0; 158 } 159 160 168 public void writeCode(CodeStyle style) throws IOException { 169 File file = new File (dir, toString()); 170 PrintWriter out; 171 172 createFile(file); 173 out = new PrintWriter (new FileWriter (file)); 174 print(out, style, 0); 175 out.close(); 176 } 177 178 185 public void print(PrintWriter out, CodeStyle style, int indent) { 186 printContents(out, style, indent); 187 } 188 } 189 | Popular Tags |