KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > PropertyConstantBuilder


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.util;
26
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.Task;
29
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileWriter JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.text.SimpleDateFormat JavaDoc;
36 import java.util.Date JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Properties JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40 import java.util.TreeSet JavaDoc;
41 import java.util.List JavaDoc;
42
43 public class PropertyConstantBuilder extends Task {
44   public static void main(String JavaDoc args[]) {
45     if (args.length < 2) {
46       System.err.println("usage: " + PropertyConstantBuilder.class.getName() +
47                          " propertiesfile interface [prefix]");
48       System.exit(-1);
49     }
50     String JavaDoc prefix = "";
51     if(args.length > 2) {
52       prefix = args[2];
53     }
54
55     try {
56       new PropertyConstantBuilder().buildClass(args[0], new File JavaDoc(args[1]), prefix);
57     } catch (BuildException e) {
58       System.err.println("class build failed: " + e.getMessage());
59       System.exit(-1);
60     }
61     System.exit(0);
62   }
63
64   private String JavaDoc propertiesFile = null;
65   private String JavaDoc stubFile = null;
66   private String JavaDoc prefix = "";
67
68   public void execute() throws BuildException {
69     buildClass(propertiesFile, new File JavaDoc(stubFile), prefix);
70   }
71
72   public void setFile(String JavaDoc file) {
73     this.stubFile = file;
74   }
75
76   public void setProperties(String JavaDoc file) {
77     this.propertiesFile = file;
78   }
79
80   public void setPrefix(String JavaDoc prefix) {
81     this.prefix = prefix;
82   }
83
84   private void buildClass(String JavaDoc propertiesFile, File JavaDoc file, String JavaDoc prefix) throws BuildException {
85     Properties JavaDoc defaults = new Properties JavaDoc();
86     String JavaDoc[] files = propertiesFile.split(" *, *");
87     for(int fileCount = 0; fileCount < files.length; fileCount++) {
88       try {
89         defaults.load(new FileInputStream JavaDoc(files[fileCount]));
90       } catch (IOException JavaDoc e) {
91         throw new BuildException("properties file '" + files[fileCount] + "' not found.");
92       }
93     }
94     try {
95       PrintWriter JavaDoc stubWriter = new PrintWriter JavaDoc(new FileWriter JavaDoc(stubFile));
96       stubWriter.println();
97       stubWriter.println(" // automatically created interface/constants stub from");
98       stubWriter.println(" // "+propertiesFile);
99       stubWriter.println(" // generated on " + new SimpleDateFormat JavaDoc().format(new Date JavaDoc()));
100
101       createConstants(stubWriter, defaults, prefix);
102
103       stubWriter.close();
104     } catch (IOException JavaDoc e) {
105       throw new BuildException("error writing class sources: " + e.getMessage());
106     }
107   }
108
109   private void createConstants(PrintWriter JavaDoc stubWriter, Properties JavaDoc properties, String JavaDoc prefix) {
110     TreeSet JavaDoc sorted = new TreeSet JavaDoc(properties.keySet());
111     Iterator JavaDoc it = sorted.iterator();
112     while (it.hasNext()) {
113       String JavaDoc property = (String JavaDoc) it.next();
114       // write constants
115
stubWriter.println(" // constant/getter for '"+property+"'");
116       stubWriter.print(" public final static String " + property.toUpperCase().replace('.', '_'));
117       stubWriter.println(" = \"" + property + "\";");
118       stubWriter.println(" public String get" + getCamlCase(property, prefix) + "();");
119       stubWriter.println(" public String set" + getCamlCase(property, prefix) + "(String value);");
120     }
121   }
122
123   private String JavaDoc getCamlCase(String JavaDoc name, String JavaDoc prefix) {
124     if(name.startsWith(prefix)) {
125       name = name.substring(prefix.length());
126     }
127     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(name, ".", false);
128     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
129     while (tokenizer.hasMoreTokens()) {
130       String JavaDoc token = tokenizer.nextToken();
131       result.append(token.substring(0, 1).toUpperCase());
132       if (token.length() > 1) {
133         result.append(token.substring(1));
134       }
135     }
136     return result.toString();
137   }
138 }
139
Popular Tags