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