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  
21  import java.io.File;
22  
23  import org.codehaus.plexus.util.FileUtils;
24  
25  /**
26   *
27   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
28   * @version $Id: BaseBean.java 373246 2006-01-28 21:11:33Z ltheussl $
29   */
30  public class BaseBean
31  {
32      /** jjtree or javacc grammar file*/
33      private String grammar;
34  
35      /** the name of the java package which will contain generated files */
36      private String javaccPackageName;
37      private String jjtreePackageName;
38      private String header;
39      private String generatedSourceDirectory;
40  
41      /**
42       * Returns grammar file (javacc of jjtree grammar)
43       * @return grammar file
44       */
45      public String getGrammar()
46      {
47          return grammar;
48      }
49  
50      /**
51       * Sets grammar file
52       * @param grammar
53       */
54      public void setGrammar( final String grammar )
55      {
56          this.grammar = grammar;
57      }
58  
59      /**
60       * Sets the name of the java package which will contain generated files
61       *
62       * @param javaccPackageName the name of java package e.g com.wombat.parser.ast
63       */
64      public void setJavaccPackageName( final String javaccPackageName )
65      {
66          this.javaccPackageName = javaccPackageName;
67      }
68  
69      /**
70       * @return Returns the header.
71       */
72      public String getHeader()
73      {
74          return header;
75      }
76  
77      /**
78       * @param header The header to set.
79       */
80      public void setHeader( final String header )
81      {
82          this.header = header;
83      }
84  
85      /**
86       *
87       * @return File
88       */
89      public File getJavaccOutputDir()
90      {
91          final String packagePath =
92              javaccPackageName.replace( '.', File.separatorChar );
93  
94          return new File( generatedSourceDirectory, packagePath );
95      }
96  
97      /**
98       *
99       * @return File
100      */
101     public File getJJTreeOutputDir()
102     {
103         final String packagePath =
104             jjtreePackageName.replace( '.', File.separatorChar );
105 
106         return new File( generatedSourceDirectory, packagePath );
107     }
108 
109     /**
110      *
111      * @throws Exception
112      */
113     protected void addHeader( final File directory )
114         throws Exception
115     {
116         final File headerFile = new File( getHeader() );
117 
118         if ( headerFile.exists() )
119         {
120             final String headerContent = getHeaderContent();
121 
122             final File[] generatedFiles = directory.listFiles();
123 
124             for ( int i = 0; i < generatedFiles.length; i++ )
125             {
126                 final File generatedFile = generatedFiles[i];
127 
128                 addHeader( generatedFile, headerContent );
129             }
130         }
131     }
132 
133     /**
134      *
135      * @return String
136      */
137     protected String getHeaderContent()
138     {
139         try
140         {
141             return FileUtils.fileRead( header );
142         }
143         catch ( Exception e )
144         {
145             return null;
146         }
147     }
148 
149     /**
150      *
151      * @param file
152      * @param headerContent
153      * @throws Exception
154      */
155     protected void addHeader( final File file, final String headerContent )
156         throws Exception
157     {
158         if ( !file.getName().endsWith( ".java" ) )
159         {
160             return;
161         }
162 
163         String content = FileUtils.fileRead( file.getCanonicalPath() );
164 
165         content = headerContent + content;
166 
167         FileUtils.fileWrite( file.getCanonicalPath(), content );
168     }
169 
170     public void createTimestampFile( final long timestamp, final File directory )
171         throws Exception
172     {
173         final File timestampFile = new File( directory, ".timestamp" );
174 
175         FileUtils.fileWrite( timestampFile.getAbsolutePath(), "" + timestamp );
176     }
177 
178     public long readTimestampFile( final File directory )
179     {
180         return 0;
181     }
182 
183     public boolean checkTimestamp()
184         throws Exception
185     {
186         //        final File outputDir = getOutputDir();
187         //        final File grammarFile = new File ( getGrammar() ).getCanonicalFile();
188         //        final File timestampFile = new File( outputDir, ".timestamp" );
189         //        if ( timestampFile.exists() )
190         //        {
191         //            long oldTimestamp = readTimestampFile( outputDir );
192         //            if ( oldTimestamp < grammarFile.lastModified() )
193         //            {
194         //                return false;
195         //            }
196         //        }
197         return true;
198     }
199 
200     /**
201      * @return Returns the jjtreePackageName.
202      */
203     public String getJjtreePackageName()
204     {
205         return jjtreePackageName;
206     }
207 
208     /**
209      * @param jjtreePackageName The jjtreePackageName to set.
210      */
211     public void setJjtreePackageName( final String jjtreePackageName )
212     {
213         this.jjtreePackageName = jjtreePackageName;
214     }
215 
216     public String getGeneratedSourceDirectory()
217     {
218         return generatedSourceDirectory;
219     }
220 
221     public void setGeneratedSourceDirectory( final String generatedSourceDirectory )
222     {
223         this.generatedSourceDirectory = generatedSourceDirectory;
224     }
225 }