1 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 ; 31 import java.io.FileInputStream ; 32 import java.io.FileWriter ; 33 import java.io.IOException ; 34 import java.io.PrintWriter ; 35 import java.text.SimpleDateFormat ; 36 import java.util.Date ; 37 import java.util.Iterator ; 38 import java.util.Properties ; 39 import java.util.StringTokenizer ; 40 import java.util.TreeSet ; 41 import java.util.List ; 42 43 public class PropertyConstantBuilder extends Task { 44 public static void main(String 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 prefix = ""; 51 if(args.length > 2) { 52 prefix = args[2]; 53 } 54 55 try { 56 new PropertyConstantBuilder().buildClass(args[0], new File (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 propertiesFile = null; 65 private String stubFile = null; 66 private String prefix = ""; 67 68 public void execute() throws BuildException { 69 buildClass(propertiesFile, new File (stubFile), prefix); 70 } 71 72 public void setFile(String file) { 73 this.stubFile = file; 74 } 75 76 public void setProperties(String file) { 77 this.propertiesFile = file; 78 } 79 80 public void setPrefix(String prefix) { 81 this.prefix = prefix; 82 } 83 84 private void buildClass(String propertiesFile, File file, String prefix) throws BuildException { 85 Properties defaults = new Properties (); 86 String [] files = propertiesFile.split(" *, *"); 87 for(int fileCount = 0; fileCount < files.length; fileCount++) { 88 try { 89 defaults.load(new FileInputStream (files[fileCount])); 90 } catch (IOException e) { 91 throw new BuildException("properties file '" + files[fileCount] + "' not found."); 92 } 93 } 94 try { 95 PrintWriter stubWriter = new PrintWriter (new FileWriter (stubFile)); 96 stubWriter.println(); 97 stubWriter.println(" // automatically created interface/constants stub from"); 98 stubWriter.println(" // "+propertiesFile); 99 stubWriter.println(" // generated on " + new SimpleDateFormat ().format(new Date ())); 100 101 createConstants(stubWriter, defaults, prefix); 102 103 stubWriter.close(); 104 } catch (IOException e) { 105 throw new BuildException("error writing class sources: " + e.getMessage()); 106 } 107 } 108 109 private void createConstants(PrintWriter stubWriter, Properties properties, String prefix) { 110 TreeSet sorted = new TreeSet (properties.keySet()); 111 Iterator it = sorted.iterator(); 112 while (it.hasNext()) { 113 String property = (String ) it.next(); 114 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 getCamlCase(String name, String prefix) { 124 if(name.startsWith(prefix)) { 125 name = name.substring(prefix.length()); 126 } 127 StringTokenizer tokenizer = new StringTokenizer (name, ".", false); 128 StringBuffer result = new StringBuffer (); 129 while (tokenizer.hasMoreTokens()) { 130 String 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 |