KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > util > OverwriteProperties


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.jetspeed.util;
17
18 import java.io.File JavaDoc;
19 import java.io.BufferedReader JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 /**
30  * Task to overwrite Properties: used for JRP, TRP and Torque.properties
31  *
32  * @author <a HREF="mailto:taylor@apache.org">David Sean Taylor</a>
33  * @author <a HREF="mailto:epugh@upstate.com">Eric Pugh</a>
34  * @created January 29, 2003
35  * @version $Id: OverwriteProperties.java,v 1.4 2004/02/23 03:23:42 jford Exp $
36  */

37 public class OverwriteProperties {
38     /** The file to merge properties into */
39     protected File JavaDoc baseProperties;
40     /** The file to pull the properties from */
41     protected File JavaDoc properties;
42     /** The directory to look in for include files */
43     protected File JavaDoc includeRoot;
44
45     /** Description of the Field */
46     public boolean verbose = false;
47
48     /** An array of all the properties */
49     protected ArrayList JavaDoc baseArray = new ArrayList JavaDoc(1024);
50     /** An array of all the properties that will be removed */
51     protected ArrayList JavaDoc removeArray = new ArrayList JavaDoc(128);
52     /** Description of the Field */
53     protected HashMap JavaDoc baseMap = new HashMap JavaDoc();
54     /** What to use as a line seperator */
55     protected String JavaDoc lineSeparator = System.getProperty("line.separator", "\r\n");
56
57
58     /**
59      * Sets the file to merge properties into
60      *
61      * @param baseProperties The file path to merge properties into
62      */

63     public void setBaseProperties(File JavaDoc baseProperties)
64     {
65         this.baseProperties = baseProperties;
66     }
67
68
69     /**
70      * Sets the file to pull properties from
71      *
72      * @param properties The file path to the pull the merge properties from
73      */

74     public void setProperties(File JavaDoc properties)
75     {
76         this.properties = properties;
77     }
78
79
80     /**
81      * Sets the directory to look for includes in.
82      *
83      * @param includeRoot the directory to look in.
84      */

85     public void setIncludeRoot(File JavaDoc includeRoot)
86     {
87         this.includeRoot = includeRoot;
88     }
89
90
91     /**
92      * Sets whether to output extra debugging info
93      *
94      * @param verbose The new verbose value
95      */

96     public void setVerbose(boolean verbose)
97     {
98         this.verbose = verbose;
99     }
100
101
102     /**
103      * Return the file to merge propertie into
104      *
105      * @return The baseProperties value
106      */

107     public File JavaDoc getBaseProperties()
108     {
109         return baseProperties;
110     }
111
112
113     /**
114      * Gets the properties attribute of the OverwriteProperties object
115      *
116      * @return The properties value
117      */

118     public File JavaDoc getProperties()
119     {
120         return properties;
121     }
122
123
124     /**
125      * Gets the includeRoot attribute of the OverwriteProperties object
126      *
127      * @return The includeRoot value
128      */

129     public File JavaDoc getIncludeRoot()
130     {
131         return includeRoot;
132     }
133
134
135     /**
136      * Gets the verbose attribute of the OverwriteProperties object
137      *
138      * @return The verbose value
139      */

140     public boolean getVerbose()
141     {
142         return verbose;
143     }
144
145
146     /**
147      * The main program for the OverwriteProperties class
148      *
149      * @param args The command line arguments
150      * @exception Exception Description of the Exception
151      */

152     public static void main(String JavaDoc[] args)
153         throws Exception JavaDoc
154     {
155         OverwriteProperties overwriteProperties = new OverwriteProperties();
156
157         try
158         {
159             if (args.length < 3)
160             {
161                 System.out.println("Usage: java OverwriteProperties c:/temp/File1.props c:/temp/File2.props c:/include-root/");
162                 System.out.println("Usage: File1 will be modified, new parameters from File 2 will be added,");
163                 System.out.println("Usage: and same parameters will be updated. The include-root is where include files are found.");
164                 throw new Exception JavaDoc("Incorrect number of arguments supplied");
165             }
166             overwriteProperties.setBaseProperties(new File JavaDoc(args[0]));
167             overwriteProperties.setProperties(new File JavaDoc(args[1]));
168             overwriteProperties.setIncludeRoot(new File JavaDoc(args[2]));
169
170             overwriteProperties.execute();
171
172         }
173         catch (FileNotFoundException JavaDoc ex)
174         {
175             System.err.println(ex.getMessage());
176         }
177         catch (IOException JavaDoc ex)
178         {
179             System.err.println(ex.getMessage());
180         }
181         catch (SecurityException JavaDoc ex)
182         {
183             System.err.println(ex.getMessage());
184         }
185     }
186
187
188     /** Description of the Method */
189     public void execute() throws FileNotFoundException JavaDoc, IOException JavaDoc, SecurityException JavaDoc
190     {
191
192             if (verbose)
193             {
194                 System.out.println("Merging into file " + getBaseProperties() + " file " + getProperties());
195             }
196
197             if (!getBaseProperties().exists())
198             {
199               throw new FileNotFoundException JavaDoc("Could not find file:" + getBaseProperties());
200             }
201
202             if (!getProperties().exists())
203             {
204               throw new FileNotFoundException JavaDoc("Could not find file:" + getProperties());
205             }
206
207             if (!getIncludeRoot().exists() || !getIncludeRoot().isDirectory())
208             {
209               throw new FileNotFoundException JavaDoc("Could not find directory:" + getIncludeRoot());
210             }
211
212             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(baseProperties));
213             int index = 0;
214             String JavaDoc key = null;
215             String JavaDoc line = null;
216             while ((line = reader.readLine()) != null)
217             {
218                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(line, "=");
219                 baseArray.add(index, line);
220                 if (verbose)
221                 {
222                     System.out.println("While reading baseArray[" + index + "] = " + line);
223                 }
224                 if (tokenizer.countTokens() >= 1
225                     && !line.startsWith("#")
226                     && !line.startsWith("include")
227                     && !line.startsWith("module.packages"))
228                 {
229                     key = tokenizer.nextToken().trim();
230                     if (key != null && key.length() > 0)
231                     {
232                         baseMap.put(key, new Integer JavaDoc(index));
233                         if (verbose)
234                         {
235                             System.out.println("baseMap[" + key + "," + index + "]");
236                         }
237                     }
238                 }
239                 index++;
240             }
241             reader.close();
242             if (verbose)
243             {
244                 System.out.println("\nOverwrite with Delta\n");
245             }
246
247             readProperties(properties, index);
248
249             boolean flags[] = removeProperties();
250
251             baseArray.trimToSize();
252             writeToFile(flags);
253
254
255     }
256
257
258     /**
259      * Description of the Method
260      *
261      * @param flags Description of the Parameter
262      * @exception FileNotFoundException Description of the Exception
263      * @exception IOException Description of the Exception
264      */

265     public void writeToFile(boolean[] flags)
266         throws FileNotFoundException JavaDoc, IOException JavaDoc
267         {
268         FileOutputStream JavaDoc writer = new FileOutputStream JavaDoc(baseProperties);
269         writer.flush();
270         for (int i = 0; i < baseArray.size(); i++)
271         {
272             if (true == flags[i])
273             {
274                 if (verbose)
275                 {
276                     System.out.println("Skipping property[" + i + "] = " + baseArray.get(i));
277                 }
278                 continue;
279             }
280             if (verbose)
281             {
282                 System.out.println("Writing property[" + i + "] = " + baseArray.get(i));
283             }
284             writer.write(((String JavaDoc) baseArray.get(i)).getBytes());
285             writer.write(lineSeparator.getBytes());
286             writer.flush();
287         }
288         writer.close();
289
290     }
291
292
293     /**
294      * Description of the Method
295      *
296      * @return Description of the Return Value
297      */

298     public boolean[] removeProperties()
299     {
300
301         boolean flags[] = new boolean[baseArray.size()];
302
303         for (int i = 0; i < baseArray.size(); i++)
304         {
305             flags[i] = false;
306         }
307         for (int ix = 0; ix < removeArray.size(); ix++)
308         {
309             String JavaDoc prefix = (String JavaDoc) removeArray.get(ix);
310             for (int iy = 0; iy < baseArray.size(); iy++)
311             {
312                 String JavaDoc line = (String JavaDoc) baseArray.get(iy);
313                 if (line.startsWith(prefix))
314                 {
315                     flags[iy] = true;
316                     if (verbose)
317                     {
318                         System.out.println("flagging removal of property: " + line);
319                     }
320                 }
321             }
322         }
323         return flags;
324     }
325
326
327     /**
328      * Reads in the properties from the specified file
329      *
330      * @param propFile Description of the Parameter
331      * @param index Description of the Parameter
332      * @exception FileNotFoundException Description of the Exception
333      * @exception IOException Description of the Exception
334      */

335     public void readProperties(File JavaDoc propFile, int index)
336         throws FileNotFoundException JavaDoc, IOException JavaDoc
337         {
338         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(propFile));
339         String JavaDoc key = null;
340         String JavaDoc line = null;
341
342         while ((line = reader.readLine()) != null)
343         {
344             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(line, "=");
345
346             int count = tokenizer.countTokens();
347             if (count == 2 && line.startsWith("include"))
348             {
349                 key = tokenizer.nextToken().trim();
350                 File JavaDoc includeFile = new File JavaDoc(includeRoot + tokenizer.nextToken().trim());
351                 if (verbose)
352                 {
353                   System.out.println("include File = " + includeFile);
354                 }
355                 readProperties(includeFile, index);
356                 continue;
357             }
358             if (count >= 1 && line.startsWith("module.packages"))
359             {
360                 baseArray.add(index, line);
361                 if (verbose)
362                 {
363                     System.out.println("Adding module.package to baseArray[" + index + "] = " + line);
364                 }
365                 index++;
366
367                 key = line.trim();
368                 if (baseMap.containsKey(key))
369                 {
370                     int ix = ((Integer JavaDoc) baseMap.get(key)).intValue();
371                     baseArray.set(ix, line);
372                     if (verbose)
373                     {
374                         System.out.println("Resetting baseArray[" + ix + "] = " + line);
375                     }
376                 }
377
378                 continue;
379             }
380             if (count >= 1 && line.startsWith("-"))
381             {
382                 // remove from base
383

384                 String JavaDoc prefix = line.trim().substring(1);
385                 removeArray.add(prefix);
386                 if (verbose)
387                 {
388                     System.out.println("Flagging for removal = " + line);
389                 }
390                 continue;
391             }
392             if (count >= 1 && !line.startsWith("#"))
393             {
394                 key = tokenizer.nextToken().trim();
395                 if (key != null && key.length() > 0)
396                 {
397                     if (baseMap.containsKey(key))
398                     {
399                         int ix = ((Integer JavaDoc) baseMap.get(key)).intValue();
400                         baseArray.set(ix, line);
401                         if (verbose)
402                         {
403                             System.out.println("Resetting baseArray[" + ix + "] = " + line);
404                         }
405                     }
406                     else
407                     {
408                         baseArray.add(index, line);
409                         if (verbose)
410                         {
411                             System.out.println("Adding new entry to baseArray[" + index + "] = " + line);
412                         }
413                         baseMap.put(key, new Integer JavaDoc(index));
414                         if (verbose)
415                         {
416                             System.out.println("baseMap[" + key + "," + index + "]");
417                         }
418                         index++;
419                     }
420                 }
421             }
422
423         }
424         reader.close();
425
426     }
427
428 }
429
430
431
432
433
Popular Tags