1 package com.protomatter.util; 2 3 52 53 import java.io.*; 54 import java.util.*; 55 56 66 public class ProtoProperties 67 extends Properties 68 { 69 74 public ProtoProperties() 75 { 76 super(); 77 } 78 79 84 public ProtoProperties(Properties props) 85 { 86 super(props); 87 } 88 89 public synchronized void load(InputStream in) 90 throws IOException 91 { 92 read(new BufferedReader(new InputStreamReader(in))); 93 } 94 95 99 private void read(BufferedReader reader) 100 throws IOException 101 { 102 String line = null; 103 while (true) 104 { 105 if (line == null) 106 line = reader.readLine(); 107 if (line == null) 108 return; 109 110 if ((line.length() > 0) && (line.charAt(0) != '#')) 111 { 112 if (line.startsWith("Import ")) 113 { 114 String file = line.substring(7); read(new BufferedReader(new FileReader(new File(file)))); 116 line = null; } 118 else if (line.charAt(line.length() -1) == '\\') 119 { 120 line = line.substring(0, line.length() -1); 121 StringBuffer b = new StringBuffer (); 122 b.append(line); 123 124 boolean done = false; 125 while (!done) 126 { 127 line = reader.readLine(); 128 if (line != null) 129 line = line.trim(); 130 if (line == null || line.length() == 0) 131 { 132 line = null; done = true; 134 } 135 else if (line.charAt(0) == '#') { 137 ; } 139 else 140 { 141 if (line.charAt(line.length() -1) == '\\') 142 { 143 line = line.substring(0, line.length() -1); 144 b.append(line); 145 } 146 else 147 { 148 b.append(line); 149 line = null; done = true; 151 } 152 } 153 } 154 155 addProp(b.toString()); 156 } 157 else { 159 addProp(line); 160 line = null; 161 } 162 } 163 else 164 { 165 line = null; } 167 } 168 } 169 170 private void addProp(String line) 171 { 172 int index = line.indexOf("="); 173 if (index != -1) 174 { 175 String key = line.substring(0, index).trim(); 176 String val = line.substring(index +1).trim(); 177 put(key, val); 178 } 179 } 180 181 186 public static void main(String args[]) 187 { 188 if (args.length != 1) 189 { 190 System.out.println("Usage: com.protomatter.util.ProtoProperties file"); 191 System.exit(0); 192 } 193 try 194 { 195 Properties p = new ProtoProperties(); 196 p.load(new FileInputStream(new File(args[0]))); 197 198 Enumeration e = p.keys(); 199 while (e.hasMoreElements()) 200 { 201 String key = (String )e.nextElement(); 202 String val = p.getProperty(key); 203 System.out.println("'" + key + "' = '" + val + "'"); 204 System.out.println(""); 205 } 206 } 207 catch (Exception x) 208 { 209 x.printStackTrace(); 210 } 211 } 212 } 213 | Popular Tags |