1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.contract.writer;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.PrintStream;
23 import java.util.Set;
24 import java.util.TreeSet;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import org.kuali.student.contract.exception.DictionaryExecutionException;
29
30
31
32
33
34 public abstract class JavaClassWriter extends XmlWriter {
35
36 private String rootDirectory;
37 private String packageName;
38 private String className;
39 private String fileName;
40 private String directory;
41 private ByteArrayOutputStream body;
42 private Set<String> imports;
43
44 public JavaClassWriter(String rootDirectory, String packageName,
45 String className) {
46 super();
47 this.body = new ByteArrayOutputStream(1000);
48 this.setOut(new PrintStream(body));
49 this.setIndent(0);
50 this.rootDirectory = rootDirectory;
51
52 this.packageName = packageName;
53 this.className = className;
54 this.fileName =
55 new JavaClassFileNameBuilder(rootDirectory, packageName, className).build();
56 this.directory =
57 new JavaClassFileNameBuilder(rootDirectory, packageName, className).buildDirectory();
58 this.imports = new TreeSet();
59 }
60
61 public ByteArrayOutputStream getBody() {
62 return body;
63 }
64
65 public String getClassName() {
66 return className;
67 }
68
69 public String getDirectory() {
70 return directory;
71 }
72
73 public String getFileName() {
74 return fileName;
75 }
76
77 public String getPackageName() {
78 return packageName;
79 }
80
81 public String getRootDirectory() {
82 return rootDirectory;
83 }
84
85 public void importsAdd(String pack) {
86 this.imports.add(pack);
87 }
88
89 public void writeHeader() {
90 indentPrintln("/*");
91 indentPrintln(" * Copyright 2011 The Kuali Foundation");
92 indentPrintln(" *");
93 indentPrintln(" * Licensed under the Educational Community License, Version 2.0 (the \"License\");");
94 indentPrintln(" * you may not use this file except in compliance with the License.");
95 indentPrintln(" * You may obtain a copy of the License at");
96 indentPrintln(" *");
97 indentPrintln(" * http://www.osedu.org/licenses/ECL-2.0");
98 indentPrintln(" *");
99 indentPrintln(" * Unless required by applicable law or agreed to in writing, software");
100 indentPrintln(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
101 indentPrintln(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
102 indentPrintln(" * See the License for the specific language governing permissions and");
103 indentPrintln(" * limitations under the License.");
104 indentPrintln(" */");
105 indentPrintln("package " + packageName + ";");
106 indentPrintln("");
107 }
108
109 public void writeImports() {
110 if (imports.size() == 0) {
111 return;
112 }
113
114
115 for (String imprt : imports) {
116
117 if (imprt.startsWith(packageName)) {
118
119 if (!imprt.substring(packageName.length() + 1).contains(".")) {
120 continue;
121 }
122 }
123 indentPrintln("import " + imprt + ";");
124 }
125 indentPrintln("");
126 }
127
128 public void writeJavaClassAndImportsOutToFile() {
129
130 File dir = new File(this.directory);
131
132
133 if (!dir.exists()) {
134 if (!dir.mkdirs()) {
135 throw new DictionaryExecutionException("Could not create directory "
136 + this.directory);
137 }
138 }
139 try {
140 PrintStream out = new PrintStream(new FileOutputStream(fileName, false));
141 this.setOut(out);
142 } catch (FileNotFoundException ex) {
143 throw new DictionaryExecutionException(ex);
144 }
145 writeHeader();
146 indentPrintln("");
147 writeImports();
148 indentPrintln("");
149 indentPrintln(body.toString());
150 }
151
152 public void openBrace() {
153 indentPrintln("{");
154 incrementIndent();
155 }
156
157 public void closeBrace() {
158 decrementIndent();
159 indentPrintln("}");
160 }
161
162 public void indentPrintWrappedComment(String str) {
163 Pattern pattern = Pattern.compile(".{0,79}(?:\\S(?:-| |$)|$)");
164 Matcher m = pattern.matcher(str);
165 while (m.find()) {
166
167 if (m.group().equals("")) {
168 continue;
169 }
170 indentPrint("* ");
171 println(m.group());
172 }
173 }
174 }