View Javadoc

1   /*
2    * Copyright 2009 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may	obtain a copy of the License at
7    *
8    * 	http://www.osedu.org/licenses/ECL-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.contract.writer;
17  
18  import org.kuali.student.contract.exception.DictionaryExecutionException;
19  import org.kuali.student.contract.model.impl.ServiceContractModelPescXsdLoader;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.FileNotFoundException;
26  import java.io.FileOutputStream;
27  import java.io.PrintStream;
28  import java.util.Calendar;
29  import java.util.Set;
30  import java.util.TreeSet;
31  import java.util.regex.Matcher;
32  import java.util.regex.Pattern;
33  
34  /**
35   *
36   * @author nwright
37   */
38  public abstract class JavaClassWriter extends XmlWriter {
39  
40      private static Logger log = LoggerFactory.getLogger(JavaClassWriter.class);
41      
42      private String rootDirectory;
43      private String packageName;
44      private String className;
45      private String fileName;
46      private String directory;
47      private ByteArrayOutputStream body;
48      private Set<String> imports;
49  
50      public JavaClassWriter(String rootDirectory, String packageName,
51              String className) {
52          super();
53          this.body = new ByteArrayOutputStream(1000);
54          this.setOut(new PrintStream(body));
55          this.setIndent(0);
56          this.rootDirectory = rootDirectory;
57  
58          this.packageName = packageName;
59          this.className = className;
60          this.fileName =
61                  new JavaClassFileNameBuilder(rootDirectory, packageName, className).build();
62          this.directory =
63                  new JavaClassFileNameBuilder(rootDirectory, packageName, className).buildDirectory();
64          this.imports = new TreeSet();
65      }
66  
67      public ByteArrayOutputStream getBody() {
68          return body;
69      }
70  
71      public String getClassName() {
72          return className;
73      }
74  
75      public String getDirectory() {
76          return directory;
77      }
78  
79      public String getFileName() {
80          return fileName;
81      }
82  
83      public String getPackageName() {
84          return packageName;
85      }
86  
87      protected void setPackageName(String packageName) {
88          this.packageName = packageName;
89      }
90  
91      public String getRootDirectory() {
92          return rootDirectory;
93      }
94  
95      public void importsAdd(String pack) {
96          this.imports.add(pack);
97      }
98  
99      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 }