001 /*
002 * Copyright 2009 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.osedu.org/licenses/ECL-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.student.contract.writer;
017
018 import java.io.ByteArrayOutputStream;
019 import java.io.File;
020 import java.io.FileNotFoundException;
021 import java.io.FileOutputStream;
022 import java.io.PrintStream;
023 import java.util.Set;
024 import java.util.TreeSet;
025 import java.util.regex.Matcher;
026 import java.util.regex.Pattern;
027
028 import org.kuali.student.contract.exception.DictionaryExecutionException;
029
030 /**
031 *
032 * @author nwright
033 */
034 public abstract class JavaClassWriter extends XmlWriter {
035
036 private String rootDirectory;
037 private String packageName;
038 private String className;
039 private String fileName;
040 private String directory;
041 private ByteArrayOutputStream body;
042 private Set<String> imports;
043
044 public JavaClassWriter(String rootDirectory, String packageName,
045 String className) {
046 super();
047 this.body = new ByteArrayOutputStream(1000);
048 this.setOut(new PrintStream(body));
049 this.setIndent(0);
050 this.rootDirectory = rootDirectory;
051
052 this.packageName = packageName;
053 this.className = className;
054 this.fileName =
055 new JavaClassFileNameBuilder(rootDirectory, packageName, className).build();
056 this.directory =
057 new JavaClassFileNameBuilder(rootDirectory, packageName, className).buildDirectory();
058 this.imports = new TreeSet();
059 }
060
061 public ByteArrayOutputStream getBody() {
062 return body;
063 }
064
065 public String getClassName() {
066 return className;
067 }
068
069 public String getDirectory() {
070 return directory;
071 }
072
073 public String getFileName() {
074 return fileName;
075 }
076
077 public String getPackageName() {
078 return packageName;
079 }
080
081 public String getRootDirectory() {
082 return rootDirectory;
083 }
084
085 public void importsAdd(String pack) {
086 this.imports.add(pack);
087 }
088
089 public void writeHeader() {
090 indentPrintln("/*");
091 indentPrintln(" * Copyright 2011 The Kuali Foundation");
092 indentPrintln(" *");
093 indentPrintln(" * Licensed under the Educational Community License, Version 2.0 (the \"License\");");
094 indentPrintln(" * you may not use this file except in compliance with the License.");
095 indentPrintln(" * You may obtain a copy of the License at");
096 indentPrintln(" *");
097 indentPrintln(" * http://www.osedu.org/licenses/ECL-2.0");
098 indentPrintln(" *");
099 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 // exclude imports from same package
117 if (imprt.startsWith(packageName)) {
118 // don't exclude imports for same package that are including nested classes
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 //System.out.println ("Writing java class: " + fileName + " to " + dir.getAbsolutePath ());
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 // suppresss blank lines
167 if (m.group().equals("")) {
168 continue;
169 }
170 indentPrint("* ");
171 println(m.group());
172 }
173 }
174 }