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 this.imports.add(pack);
97 }
98
99 public void writeHeader() {
100 indentPrintln("/*");
101 indentPrintln(" * Copyright " + Calendar.getInstance().get(Calendar.YEAR) + " The Kuali Foundation");
102 indentPrintln(" *");
103 indentPrintln(" * Licensed under the Educational Community License, Version 2.0 (the \"License\");");
104 indentPrintln(" * you may not use this file except in compliance with the License.");
105 indentPrintln(" * You may obtain a copy of the License at");
106 indentPrintln(" *");
107 indentPrintln(" * http://www.osedu.org/licenses/ECL-2.0");
108 indentPrintln(" *");
109 indentPrintln(" * Unless required by applicable law or agreed to in writing, software");
110 indentPrintln(" * distributed under the Lic" +
111 "+ense is distributed on an \"AS IS\" BASIS,");
112 indentPrintln(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
113 indentPrintln(" * See the License for the specific language governing permissions and");
114 indentPrintln(" * limitations under the License.");
115 indentPrintln(" */");
116 indentPrintln("package " + packageName + ";");
117 indentPrintln("");
118 }
119
120 public void writeImports() {
121 if (imports.size() == 0) {
122 return;
123 }
124
125
126 for (String imprt : imports) {
127
128 if (imprt.startsWith(packageName)) {
129
130 if (!imprt.substring(packageName.length() + 1).contains(".")) {
131 continue;
132 }
133 }
134 indentPrintln("import " + imprt + ";");
135 }
136 indentPrintln("");
137 }
138
139 public void writeJavaClassAndImportsOutToFile() {
140
141 File dir = new File(this.directory);
142 log.debug ("Writing java class: " + fileName + " to " + dir.getAbsolutePath ());
143
144 if (!dir.exists()) {
145 if (!dir.mkdirs()) {
146 throw new DictionaryExecutionException("Could not create directory "
147 + this.directory);
148 }
149 }
150 try {
151 PrintStream out = new PrintStream(new FileOutputStream(fileName, false));
152 this.setOut(out);
153 } catch (FileNotFoundException ex) {
154 throw new DictionaryExecutionException(ex);
155 }
156 writeHeader();
157 indentPrintln("");
158 writeImports();
159 indentPrintln("");
160 indentPrintln(body.toString());
161 }
162
163 public void openBrace() {
164 indentPrintln("{");
165 incrementIndent();
166 }
167
168 public void closeBrace() {
169 decrementIndent();
170 indentPrintln("}");
171 }
172
173 public void indentPrintWrappedComment(String str) {
174 Pattern pattern = Pattern.compile(".{0,79}(?:\\S(?:-| |$)|$)");
175 Matcher m = pattern.matcher(str);
176 while (m.find()) {
177
178 if (m.group().equals("")) {
179 continue;
180 }
181 indentPrint("* ");
182 println(m.group());
183 }
184 }
185 }