1 package spoon.support; 2 3 import java.io.File ; 4 import java.io.FileNotFoundException ; 5 import java.io.IOException ; 6 import java.io.PrintStream ; 7 import java.util.ArrayList ; 8 import java.util.List ; 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 19 public class JavaOutputProcessor extends AbstractProcessor<CtSimpleType<?>> 20 implements FileGenerator<CtSimpleType<?>> { 21 File directory; 22 23 List <File > printedFiles = new ArrayList <File >(); 24 25 boolean writePackageAnnotationFile = true; 26 27 33 public JavaOutputProcessor(File outputDirectory) { 34 this.directory = outputDirectory; 35 } 36 37 public List <File > getCreatedFiles() { 38 return printedFiles; 39 } 40 41 public File getOutputDirectory() { 42 return directory; 43 } 44 45 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 stream = null; 56 57 if (directory == null) 59 throw new RuntimeException ( 60 "You should set output directory before printing"); 61 if (directory.isFile()) 63 throw new RuntimeException ("Output must be a directory"); 64 if (!directory.exists()) { 65 if (!directory.mkdirs()) 66 throw new RuntimeException ("Error creating output directory"); 67 } 68 69 File packageDir; 71 if (pack.getQualifiedName().equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) { 72 packageDir = new File (directory.getAbsolutePath()); 73 } else { 74 packageDir = new File (directory.getAbsolutePath() 76 + File.separatorChar 77 + pack.getQualifiedName().replace('.', File.separatorChar)); 78 } 79 if (!packageDir.exists()) { 80 if (!packageDir.mkdirs()) 81 throw new RuntimeException ("Error creating output directory"); 82 } 83 84 if (writePackageAnnotationFile 86 && element.getPackage().getAnnotations().size() > 0) { 87 File packageAnnot = new File (packageDir.getAbsolutePath() 88 + File.separatorChar 89 + JavaPrettyPrinter.PACKAGE_DECLARATION); 90 if (!printedFiles.contains(packageAnnot)) 91 printedFiles.add(packageAnnot); 92 try { 93 stream = new PrintStream (packageAnnot); 94 stream.println(printer.getPackageDeclaration()); 95 stream.close(); 96 } catch (FileNotFoundException e) { 97 e.printStackTrace(); 98 } finally { 99 if (stream != null) 100 stream.close(); 101 } 102 } 103 104 try { 106 File file = new File (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 (file); 113 stream.print(printer.getResult()); 114 stream.close(); 115 } catch (FileNotFoundException e) { 116 e.printStackTrace(); 117 } catch (IOException e) { 118 e.printStackTrace(); 119 } finally { 120 if (stream != null) 121 stream.close(); 122 } 123 124 } 125 126 130 public void process(CtSimpleType<?> type) { 131 if (type.isTopLevel()) 132 createJavaFile(type); 133 } 134 135 public void setOutputDirectory(File directory) { 136 this.directory = directory; 137 } 138 139 } 140 | Popular Tags |