KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > save > Reformat


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.source.save;
21
22 import org.netbeans.modules.java.source.engine.EngineEnvironment;
23 import org.netbeans.modules.java.source.engine.EngineEnvironment;
24 import com.sun.source.tree.CompilationUnitTree;
25 import org.netbeans.modules.java.source.engine.EngineEnvironment;
26 import org.netbeans.api.java.source.query.QueryEnvironment;
27 import org.netbeans.api.java.source.query.Query;
28 import org.netbeans.modules.java.source.pretty.VeryPretty;
29 import org.netbeans.modules.java.source.pretty.ImportAnalysis;
30
31 import com.sun.tools.javac.tree.*;
32 import com.sun.tools.javac.tree.JCTree.*;
33 import com.sun.tools.javac.util.*;
34
35 import java.io.*;
36
37 public class Reformat extends Query<Void JavaDoc,Object JavaDoc> {
38     public String JavaDoc destinationDirectory;
39     boolean displayedError = false;
40     Context context;
41     PrintWriter log;
42     int nWritten;
43     private EngineEnvironment ee;
44
45     @Override JavaDoc
46     public void attach(QueryEnvironment env) {
47     super.attach(env);
48         ee = (EngineEnvironment)env;
49     context = ee.getContext();
50     Options options = Options.instance(context);
51     String JavaDoc saveDir = options.get("-save_directory");
52     if (saveDir != null)
53         destinationDirectory = saveDir;
54     }
55     
56     @Override JavaDoc
57     public void release() {
58         super.release();
59         ee = null;
60         context = null;
61         log = null;
62     }
63
64     public void apply() {
65     nWritten = 0;
66         log = env.getOutputWriter("Formatting Files");
67         super.apply();
68         log.println("Wrote "+nWritten+" files");
69         log.close();
70     log = null;
71     }
72     
73     @Override JavaDoc
74     public Void JavaDoc visitCompilationUnit(CompilationUnitTree node, Object JavaDoc p) {
75         JCCompilationUnit topLevel = (JCCompilationUnit)node;
76         String JavaDoc sourceName = Commit.getSourceFileName(topLevel);
77         File dfn = new File(destinationDirectory, sourceName);
78         log.println("Formatting "+dfn);
79         PrintWriter out;
80         try {
81             out = new PrintWriter(new BufferedWriter(new FileWriter(dfn)));
82         } catch (IOException ioe) {
83             if (!dfn.getParentFile().mkdirs()) {
84                 if (!displayedError)
85                     error("Couldn\'t create directory:\n" + dfn.getParent() +
86                           "\n" + ioe.getMessage());
87                 displayedError = true;
88                 return null;
89             }
90         try {
91         out = new PrintWriter(new BufferedWriter(new FileWriter(dfn)));
92         }
93         catch (IOException ioe2) {
94         if (!displayedError)
95             error("Couldn\'t create file:\n" + dfn + "\n" +
96               ioe2.getMessage());
97         displayedError = true;
98         return null;
99         }
100     }
101
102         try {
103         VeryPretty pretty = new VeryPretty(context);
104             ImportAnalysis imports = new ImportAnalysis(pretty.options.starThreshold,
105                                                         pretty.options.useThreshold,
106                                                         topLevel, ee.getSymbolTable(),
107                                                         ee.getJavacTypes());
108         pretty.setImports(imports);
109         if (imports.containingClass != null)
110         pretty.enclClassName = imports.containingClass.name;
111         pretty.printImports();
112             for (List<JCTree> defs = topLevel.defs; defs.tail != null; defs = defs.tail) {
113                 defs.head.accept(pretty);
114                 if (defs.tail != null)
115                     pretty.print('\n');
116             }
117             pretty.writeTo(out);
118         out.close();
119         nWritten++;
120     } catch(Throwable JavaDoc err) {
121         String JavaDoc msg = "Error writing " + sourceName + ": " + err;
122         try {
123                 log.println(msg);
124                 msg = null;
125         } catch(Throwable JavaDoc t) {}
126         if(msg != null) error(msg);
127         displayedError = true;
128     }
129         return null;
130     }
131 }
132
Popular Tags