1 package org.apache.maven.javacc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
187
188
189
190
191
192
193
194
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 }