View Javadoc

1   package org.apache.maven.javacc;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  import java.io.File;
21  
22  import java.util.ArrayList;
23  
24  import org.javacc.jjtree.JJTree;
25  
26  /**
27   *
28   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
29   * @version $Id: JJTreeBean.java 373246 2006-01-28 21:11:33Z ltheussl $
30   */
31  public class JJTreeBean extends BaseBean
32  {
33      /**
34       * Generates AST and javacc grammar
35       * @return The value returned by org.javacc.jjtree.JJTree.main( String[] )
36       * @throws Exception Exception
37       */
38      public int generate() throws Exception
39      {
40          final ArrayList params = new ArrayList();
41  
42          final File outputDir = getJJTreeOutputDir();
43  
44          System.err.println( "-OUTPUT_DIRECTORY="
45              + outputDir.getCanonicalPath() );
46  
47          final long timestamp = System.currentTimeMillis();
48  
49          System.err.println( "Creating directory: "
50              + outputDir.getCanonicalPath() );
51  
52          if ( !outputDir.exists() )
53          {
54              System.out.println( "Creating directorty: "
55                  + outputDir.getAbsolutePath() );
56  
57              outputDir.mkdirs();
58          }
59          else
60          {
61              if ( !checkTimestamp() )
62              {
63                  return 0;
64              }
65          }
66  
67          params.add( "-OUTPUT_DIRECTORY=" + outputDir.getCanonicalPath() );
68  
69          params.add( getGrammar() );
70  
71          String[] args =
72              (String[]) params.toArray( new String[params.size()] );
73  
74          final JJTree jjtree = new JJTree();
75  
76          final int retValue = jjtree.main( args );
77  
78          moveJJTJavaParserState();
79  
80          //addHeader( outputDir );
81          createTimestampFile( timestamp, outputDir );
82  
83          return retValue;
84      }
85  
86      /**
87       *
88       */
89      private void moveJJTJavaParserState()
90      {
91          final File jjtJavaParserStateFileOrg =
92              new File( getJJTreeOutputDir(), "JJTJavaParserState.java" );
93  
94          final File jjtJavaParserStateFileNew =
95              new File( getJavaccOutputDir(), "JJTJavaParserState.java" );
96  
97          if ( !jjtJavaParserStateFileOrg.equals( jjtJavaParserStateFileNew ) )
98          {
99              final File dir = jjtJavaParserStateFileNew.getParentFile();
100 
101             if ( !dir.exists() )
102             {
103                 dir.mkdirs();
104             }
105 
106             System.out.println( "Moving: " + jjtJavaParserStateFileOrg
107                 + " to: " + jjtJavaParserStateFileNew );
108 
109             jjtJavaParserStateFileOrg.renameTo( jjtJavaParserStateFileNew );
110         }
111     }
112 
113     /**
114      * Returns the path to generated javacc grammar
115      * @return the path to generated javacc grammar
116      */
117     public String getJavaccGrammar()
118     {
119         final File grammarFile = new File( getGrammar() );
120 
121         String grammarName = grammarFile.getName();
122 
123         grammarName = grammarName.substring( 0, grammarName.length() - 1 );
124 
125         final String retValue =
126             getJJTreeOutputDir() + File.separator + grammarName;
127 
128         return retValue;
129     }
130 }