001 /** 002 * Copyright 2004-2013 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.opensource.org/licenses/ecl2.php 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 License is distributed on an \"AS IS\" BASIS,"); 111 indentPrintln(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied."); 112 indentPrintln(" * See the License for the specific language governing permissions and"); 113 indentPrintln(" * limitations under the License."); 114 indentPrintln(" */"); 115 indentPrintln("package " + packageName + ";"); 116 indentPrintln(""); 117 } 118 119 public void writeImports() { 120 if (imports.size() == 0) { 121 return; 122 } 123 124 125 for (String imprt : imports) { 126 // exclude imports from same package 127 if (imprt.startsWith(packageName)) { 128 // don't exclude imports for same package that are including nested classes 129 if (!imprt.substring(packageName.length() + 1).contains(".")) { 130 continue; 131 } 132 } 133 indentPrintln("import " + imprt + ";"); 134 } 135 indentPrintln(""); 136 } 137 138 public void writeJavaClassAndImportsOutToFile() { 139 140 File dir = new File(this.directory); 141 log.debug ("Writing java class: " + fileName + " to " + dir.getAbsolutePath ()); 142 143 if (!dir.exists()) { 144 if (!dir.mkdirs()) { 145 throw new DictionaryExecutionException("Could not create directory " 146 + this.directory); 147 } 148 } 149 try { 150 PrintStream out = new PrintStream(new FileOutputStream(fileName, false)); 151 this.setOut(out); 152 } catch (FileNotFoundException ex) { 153 throw new DictionaryExecutionException(ex); 154 } 155 writeHeader(); 156 indentPrintln(""); 157 writeImports(); 158 indentPrintln(""); 159 indentPrintln(body.toString()); 160 } 161 162 public void openBrace() { 163 indentPrintln("{"); 164 incrementIndent(); 165 } 166 167 public void closeBrace() { 168 decrementIndent(); 169 indentPrintln("}"); 170 } 171 172 public void indentPrintWrappedComment(String str) { 173 Pattern pattern = Pattern.compile(".{0,79}(?:\\S(?:-| |$)|$)"); 174 Matcher m = pattern.matcher(str); 175 while (m.find()) { 176 // suppresss blank lines 177 if (m.group().equals("")) { 178 continue; 179 } 180 indentPrint("* "); 181 println(m.group()); 182 } 183 } 184 }