1   /* FOREGEJ - FOrmatting REfactoring GEnerating Java
2    *
3    * Copyright (C) 2003 Andreas Arrgard
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package com.octagroup.foregej.ant;
20  import antlr.ASTFactory;
21  import antlr.RecognitionException;
22  import antlr.TokenStreamException;
23  import antlr.collections.AST;
24  import antlr.debug.misc.ASTFrame;
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.DirectoryScanner;
27  import org.apache.tools.ant.Task;
28  import org.apache.tools.ant.types.FileSet;
29  import java.awt.event.WindowAdapter;
30  import java.awt.event.WindowEvent;
31  import java.io.BufferedReader;
32  import java.io.File;
33  import java.io.FileReader;
34  import java.io.IOException;
35  import java.io.Reader;
36  import com.octagroup.foregej.antlr.NodeWriter;
37  import com.octagroup.foregej.java.lang.JavaAST;
38  import com.octagroup.foregej.java.lang.JavaASTFactory;
39  import com.octagroup.foregej.java.lang.JavaRecognizer;
40  import com.octagroup.foregej.java.lang.ast.CompilationUnit;
41  /***
42   * DOCUMENT ME! Date Author Description
43   * 
44   * @author 2003 -02-12 $author$ Created
45   */
46  public class JavaRewriterTask extends Task 
47  {
48      /***
49       * the files to process
50       */
51      private FileSet _files=null;
52      /***
53       * Sets the files to process.
54       * 
55       * @param fs the files to process.
56       */
57      public void addSrc(FileSet fs)
58      {
59          _files=fs;
60      }
61      /***
62       * DOCUMENT ME!
63       * 
64       * @param f DOCUMENT ME!
65       * @param t DOCUMENT ME!
66       * @param tokenNames DOCUMENT ME!
67       * @throws InterruptedException DOCUMENT ME!
68       * @throws RecognitionException DOCUMENT ME!
69       */
70      public void doTreeAction(String f,AST t,String[] tokenNames) throws RecognitionException, InterruptedException
71      {
72          if(t==null) {
73              System.out.println(f+":"+null);
74              return;
75          } else {
76          }
77          ASTFactory factory=JavaASTFactory.newInstance();
78          AST r=factory.create(0, "AST ROOT");
79          r.setFirstChild(t);
80          final JavaRewriterTask jrt=this;
81          final ASTFrame frame=new ASTFrame("Java AST", r);
82          frame.setVisible(true);
83          frame.addWindowListener(new WindowAdapter()
84          {
85              public void windowClosing(WindowEvent e)
86              {
87                  frame.setVisible(false);
88                  frame.dispose();
89                  synchronized(jrt) {
90                      jrt.notify();
91                  }
92              }
93          }
94          );
95          /*
96           JavaTreeParser tparse = new JavaTreeParser();
97          
98           try
99           {
100          tparse.compilationUnit(t);
101          }
102          catch(RecognitionException e)
103          {
104          System.err.println(e.getMessage());
105          e.printStackTrace();
106          }
107          */
108         synchronized(jrt) {
109             jrt.wait();
110         }
111         System.out.println("Root AST:"+t.getClass().getName()+":"+t.getType());
112         System.out.println(t.toStringTree());
113         CompilationUnit cu=new CompilationUnit((JavaAST)t);
114         NodeWriter nw=new NodeWriter();
115         cu.write(nw);
116         System.out.println(nw.toString());
117     }
118     /***
119      * DOCUMENT ME!
120      */
121     public void execute()
122     {
123         try{
124             if(_files==null) {
125                 throw new BuildException("Must supply some source files...");
126             }
127             DirectoryScanner ds=_files.getDirectoryScanner(this.getProject());
128             String[] files=ds.getIncludedFiles();
129             for(int i=0; i<files.length; i++){
130                 File file=new File(ds.getBasedir(), files[i]);
131                 if(file.exists()==false) {
132                     throw new IOException("File not found:"+file);
133                 }
134                 File backup=null;
135                 for(int bIdx=0; true; bIdx++){
136                     backup=new File(file.getAbsoluteFile()+".old"+bIdx);
137                     if(backup.exists()==false) {
138                         break;
139                     }
140                 }
141                 JavaFormatter.format(file, file);
142             }
143         }catch (Exception e) {
144             e.printStackTrace();
145             throw new BuildException(e.getMessage());
146         }
147     }
148     /***
149      * DOCUMENT ME!
150      * 
151      * @param f DOCUMENT ME!
152      * @throws IOException DOCUMENT ME!
153      * @throws InterruptedException DOCUMENT ME!
154      * @throws RecognitionException DOCUMENT ME!
155      * @throws TokenStreamException DOCUMENT ME!
156      */
157     private void processJavaFile(File f) throws TokenStreamException, RecognitionException, IOException, InterruptedException
158     {
159         FileReader r=null;
160         try{
161             r=new FileReader(f);
162             processJavaFile(f.toString(), new BufferedReader(r));
163         }finally {
164             if(r!=null) {
165                 r.close();
166             }
167         }
168     }
169     /***
170      * DOCUMENT ME!
171      * 
172      * @param f DOCUMENT ME!
173      * @param r DOCUMENT ME!
174      * @throws IOException DOCUMENT ME!
175      * @throws InterruptedException DOCUMENT ME!
176      * @throws RecognitionException DOCUMENT ME!
177      * @throws TokenStreamException DOCUMENT ME!
178      */
179     private void processJavaFile(String f,Reader r) throws IOException, TokenStreamException, RecognitionException, InterruptedException
180     {
181         JavaRecognizer parser=JavaRecognizer.setup(f, r);
182         parser.compilationUnit();
183         doTreeAction(f, parser.getAST(), parser.getTokenNames());
184     }
185 }
This page was automatically generated by Maven