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