001/**
002 * Copyright 2004-2014 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 */
016package org.kuali.student.contract.writer;
017
018import org.kuali.student.contract.exception.DictionaryExecutionException;
019import org.kuali.student.contract.model.impl.ServiceContractModelPescXsdLoader;
020import org.slf4j.Logger;
021import org.slf4j.LoggerFactory;
022
023import java.io.ByteArrayOutputStream;
024import java.io.File;
025import java.io.FileNotFoundException;
026import java.io.FileOutputStream;
027import java.io.PrintStream;
028import java.util.Calendar;
029import java.util.Set;
030import java.util.TreeSet;
031import java.util.regex.Matcher;
032import java.util.regex.Pattern;
033
034/**
035 *
036 * @author nwright
037 */
038public 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        if (pack.startsWith("java.lang.")) {
097            return;
098        }
099        this.imports.add(pack);
100    }
101
102    public void writeHeader() {
103        indentPrintln("/*");
104        indentPrintln(" * Copyright " + Calendar.getInstance().get(Calendar.YEAR)  + " The Kuali Foundation");
105        indentPrintln(" *");
106        indentPrintln(" * Licensed under the Educational Community License, Version 2.0 (the \"License\");");
107        indentPrintln(" * you may not use this file except in compliance with the License.");
108        indentPrintln(" * You may obtain a copy of the License at");
109        indentPrintln(" *");
110        indentPrintln(" *       http://www.osedu.org/licenses/ECL-2.0");
111        indentPrintln(" *");
112        indentPrintln(" * Unless required by applicable law or agreed to in writing, software");
113        indentPrintln(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
114        indentPrintln(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
115        indentPrintln(" * See the License for the specific language governing permissions and");
116        indentPrintln(" * limitations under the License.");
117        indentPrintln(" */");
118        indentPrintln("package " + packageName + ";");
119        indentPrintln("");
120    }
121
122    public void writeImports() {
123        if (imports.isEmpty()) {
124            return;
125        }
126
127
128        for (String imprt : imports) {
129            // exclude imports from same package
130            if (imprt.startsWith(packageName)) {
131                // don't exclude imports for same package that are including nested classes
132                if (!imprt.substring(packageName.length() + 1).contains(".")) {
133                    continue;
134                }
135            }
136            indentPrintln("import " + imprt + ";");
137        }
138        indentPrintln("");
139    }
140
141    public void writeJavaClassAndImportsOutToFile() {
142
143        File dir = new File(this.directory);
144        log.debug ("Writing java class: " + fileName + " to " + dir.getAbsolutePath ());
145
146        if (!dir.exists()) {
147            if (!dir.mkdirs()) {
148                throw new DictionaryExecutionException("Could not create directory "
149                        + this.directory);
150            }
151        }
152        try {
153            PrintStream out = new PrintStream(new FileOutputStream(fileName, false));
154            this.setOut(out);
155        } catch (FileNotFoundException ex) {
156            throw new DictionaryExecutionException(ex);
157        }
158        writeHeader();
159        indentPrintln("");
160        writeImports();
161        indentPrintln("");
162        indentPrintln(body.toString());
163    }
164
165    public void openBrace() {
166        indentPrintln("{");
167        incrementIndent();
168    }
169
170    public void closeBrace() {
171        decrementIndent();
172        indentPrintln("}");
173    }
174
175    public void indentPrintWrappedComment(String str) {
176        Pattern pattern = Pattern.compile(".{0,79}(?:\\S(?:-| |$)|$)");
177        Matcher m = pattern.matcher(str);
178        while (m.find()) {
179            // suppresss blank lines
180            if (m.group().equals("")) {
181                continue;
182            }
183            indentPrint("* ");
184            println(m.group());
185        }
186    }
187}