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