KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > JavaOutputProcessor


1 package spoon.support;
2
3 import java.io.File JavaDoc;
4 import java.io.FileNotFoundException JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.PrintStream JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 import spoon.processing.AbstractProcessor;
11 import spoon.processing.FileGenerator;
12 import spoon.reflect.declaration.CtPackage;
13 import spoon.reflect.declaration.CtSimpleType;
14 import spoon.reflect.visitor.JavaPrettyPrinter;
15
16 /**
17  * A processor that generates compilable Java source files from the meta-model.
18  */

19 public class JavaOutputProcessor extends AbstractProcessor<CtSimpleType<?>>
20         implements FileGenerator<CtSimpleType<?>> {
21     File JavaDoc directory;
22
23     List JavaDoc<File JavaDoc> printedFiles = new ArrayList JavaDoc<File JavaDoc>();
24
25     boolean writePackageAnnotationFile = true;
26
27     /**
28      * Creates a new processor for generating Java source files.
29      *
30      * @param outputDirectory
31      * the root output directory
32      */

33     public JavaOutputProcessor(File JavaDoc outputDirectory) {
34         this.directory = outputDirectory;
35     }
36
37     public List JavaDoc<File JavaDoc> getCreatedFiles() {
38         return printedFiles;
39     }
40
41     public File JavaDoc getOutputDirectory() {
42         return directory;
43     }
44
45     /**
46      * Creates the Java file associated to the given element.
47      */

48     public void createJavaFile(CtSimpleType<?> element) {
49         JavaPrettyPrinter printer = new JavaPrettyPrinter();
50
51         printer.makeImports(element);
52         printer.scan(element);
53
54         CtPackage pack = element.getPackage();
55         PrintStream JavaDoc stream = null;
56
57         // Check output directory
58
if (directory == null)
59             throw new RuntimeException JavaDoc(
60                     "You should set output directory before printing");
61         // Create spooned dir
62
if (directory.isFile())
63             throw new RuntimeException JavaDoc("Output must be a directory");
64         if (!directory.exists()) {
65             if (!directory.mkdirs())
66                 throw new RuntimeException JavaDoc("Error creating output directory");
67         }
68
69         // create package directory
70
File JavaDoc packageDir;
71         if (pack.getQualifiedName().equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) {
72             packageDir = new File JavaDoc(directory.getAbsolutePath());
73         } else {
74             // Create current package dir
75
packageDir = new File JavaDoc(directory.getAbsolutePath()
76                     + File.separatorChar
77                     + pack.getQualifiedName().replace('.', File.separatorChar));
78         }
79         if (!packageDir.exists()) {
80             if (!packageDir.mkdirs())
81                 throw new RuntimeException JavaDoc("Error creating output directory");
82         }
83
84         // Create package annotation file
85
if (writePackageAnnotationFile
86                 && element.getPackage().getAnnotations().size() > 0) {
87             File JavaDoc packageAnnot = new File JavaDoc(packageDir.getAbsolutePath()
88                     + File.separatorChar
89                     + JavaPrettyPrinter.PACKAGE_DECLARATION);
90             if (!printedFiles.contains(packageAnnot))
91                 printedFiles.add(packageAnnot);
92             try {
93                 stream = new PrintStream JavaDoc(packageAnnot);
94                 stream.println(printer.getPackageDeclaration());
95                 stream.close();
96             } catch (FileNotFoundException JavaDoc e) {
97                 e.printStackTrace();
98             } finally {
99                 if (stream != null)
100                     stream.close();
101             }
102         }
103
104         // print type
105
try {
106             File JavaDoc file = new File JavaDoc(packageDir.getAbsolutePath()
107                     + File.separatorChar + element.getSimpleName()
108                     + JavaPrettyPrinter.FILE_EXTENSION);
109             file.createNewFile();
110             if (!printedFiles.contains(file))
111                 printedFiles.add(file);
112             stream = new PrintStream JavaDoc(file);
113             stream.print(printer.getResult());
114             stream.close();
115         } catch (FileNotFoundException JavaDoc e) {
116             e.printStackTrace();
117         } catch (IOException JavaDoc e) {
118             e.printStackTrace();
119         } finally {
120             if (stream != null)
121                 stream.close();
122         }
123
124     }
125
126     /**
127      * Creates a source file for each processed top-level type and pretty prints
128      * its contents.
129      */

130     public void process(CtSimpleType<?> type) {
131         if (type.isTopLevel())
132             createJavaFile(type);
133     }
134
135     public void setOutputDirectory(File JavaDoc directory) {
136         this.directory = directory;
137     }
138
139 }
140
Popular Tags