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 org.kuali.student.contract.exception.DictionaryExecutionException;
019 import org.kuali.student.contract.model.impl.ServiceContractModelPescXsdLoader;
020 import org.slf4j.Logger;
021 import org.slf4j.LoggerFactory;
022
023 import java.io.ByteArrayOutputStream;
024 import java.io.File;
025 import java.io.FileNotFoundException;
026 import java.io.FileOutputStream;
027 import java.io.PrintStream;
028 import java.util.Calendar;
029 import java.util.Set;
030 import java.util.TreeSet;
031 import java.util.regex.Matcher;
032 import java.util.regex.Pattern;
033
034 /**
035 *
036 * @author nwright
037 */
038 public abstract class JavaClassWriter extends XmlWriter {
039
040 private static Logger log = LoggerFactory.getLogger(JavaClassWriter.class);
041
042 private String rootDirectory;
043 private String packageName;
044 private String className;
045 private String fileName;
046 private String directory;
047 private ByteArrayOutputStream body;
048 private Set<String> imports;
049
050 public JavaClassWriter(String rootDirectory, String packageName,
051 String className) {
052 super();
053 this.body = new ByteArrayOutputStream(1000);
054 this.setOut(new PrintStream(body));
055 this.setIndent(0);
056 this.rootDirectory = rootDirectory;
057
058 this.packageName = packageName;
059 this.className = className;
060 this.fileName =
061 new JavaClassFileNameBuilder(rootDirectory, packageName, className).build();
062 this.directory =
063 new JavaClassFileNameBuilder(rootDirectory, packageName, className).buildDirectory();
064 this.imports = new TreeSet();
065 }
066
067 public ByteArrayOutputStream getBody() {
068 return body;
069 }
070
071 public String getClassName() {
072 return className;
073 }
074
075 public String getDirectory() {
076 return directory;
077 }
078
079 public String getFileName() {
080 return fileName;
081 }
082
083 public String getPackageName() {
084 return packageName;
085 }
086
087 protected void setPackageName(String packageName) {
088 this.packageName = packageName;
089 }
090
091 public String getRootDirectory() {
092 return rootDirectory;
093 }
094
095 public void importsAdd(String pack) {
096 this.imports.add(pack);
097 }
098
099 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 // exclude imports from same package
128 if (imprt.startsWith(packageName)) {
129 // don't exclude imports for same package that are including nested classes
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 // suppresss blank lines
178 if (m.group().equals("")) {
179 continue;
180 }
181 indentPrint("* ");
182 println(m.group());
183 }
184 }
185 }