1 18 package org.apache.tools.ant.taskdefs.optional; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.io.OutputStreamWriter ; 28 import java.io.Writer ; 29 import java.util.Enumeration ; 30 import java.util.Hashtable ; 31 import java.util.Properties ; 32 import java.util.Vector ; 33 import java.util.List ; 34 import java.util.ArrayList ; 35 import java.util.Comparator ; 36 import java.util.Map ; 37 import java.util.Set ; 38 import java.util.TreeSet ; 39 import java.util.Collections ; 40 import java.util.Iterator ; 41 import javax.xml.parsers.DocumentBuilder ; 42 import javax.xml.parsers.DocumentBuilderFactory ; 43 import org.apache.tools.ant.BuildException; 44 import org.apache.tools.ant.Project; 45 import org.apache.tools.ant.Task; 46 import org.apache.tools.ant.types.EnumeratedAttribute; 47 import org.apache.tools.ant.types.PropertySet; 48 import org.apache.tools.ant.util.CollectionUtils; 49 import org.apache.tools.ant.util.DOMElementWriter; 50 import org.apache.tools.ant.util.FileUtils; 51 import org.apache.tools.ant.util.JavaEnvUtils; 52 import org.w3c.dom.Document ; 53 import org.w3c.dom.Element ; 54 55 88 public class EchoProperties extends Task { 89 90 93 private static final String PROPERTIES = "properties"; 94 95 98 private static final String PROPERTY = "property"; 99 100 103 private static final String ATTR_NAME = "name"; 104 105 108 private static final String ATTR_VALUE = "value"; 109 110 113 private File inFile = null; 114 115 119 private File destfile = null; 120 121 126 private boolean failonerror = true; 127 128 private Vector propertySets = new Vector (); 129 130 private String format = "text"; 131 132 private String prefix; 133 134 137 private String regex; 138 139 144 public void setSrcfile(File file) { 145 inFile = file; 146 } 147 148 154 public void setDestfile(File destfile) { 155 this.destfile = destfile; 156 } 157 158 159 166 public void setFailOnError(boolean failonerror) { 167 this.failonerror = failonerror; 168 } 169 170 171 184 public void setPrefix(String prefix) { 185 if (prefix != null && prefix.length() != 0) { 186 this.prefix = prefix; 187 PropertySet ps = new PropertySet(); 188 ps.setProject(getProject()); 189 ps.appendPrefix(prefix); 190 addPropertyset(ps); 191 } 192 } 193 194 209 public void setRegex(String regex) { 210 if (regex != null && regex.length() != 0) { 211 this.regex = regex; 212 PropertySet ps = new PropertySet(); 213 ps.setProject(getProject()); 214 ps.appendRegex(regex); 215 addPropertyset(ps); 216 } 217 } 218 219 224 public void addPropertyset(PropertySet ps) { 225 propertySets.addElement(ps); 226 } 227 228 232 public void setFormat(FormatAttribute ea) { 233 format = ea.getValue(); 234 } 235 236 240 public static class FormatAttribute extends EnumeratedAttribute { 241 private String [] formats = new String []{"xml", "text"}; 242 243 247 public String [] getValues() { 248 return formats; 249 } 250 } 251 252 257 public void execute() throws BuildException { 258 if (prefix != null && regex != null) { 259 throw new BuildException("Please specify either prefix" 260 + " or regex, but not both", getLocation()); 261 } 262 Hashtable allProps = new Hashtable (); 264 265 267 if (inFile == null && propertySets.size() == 0) { 268 allProps.putAll(getProject().getProperties()); 270 } else if (inFile != null) { 271 if (inFile.exists() && inFile.isDirectory()) { 272 String message = "srcfile is a directory!"; 273 if (failonerror) { 274 throw new BuildException(message, getLocation()); 275 } else { 276 log(message, Project.MSG_ERR); 277 } 278 return; 279 } 280 281 if (inFile.exists() && !inFile.canRead()) { 282 String message = "Can not read from the specified srcfile!"; 283 if (failonerror) { 284 throw new BuildException(message, getLocation()); 285 } else { 286 log(message, Project.MSG_ERR); 287 } 288 return; 289 } 290 291 FileInputStream in = null; 292 try { 293 in = new FileInputStream (inFile); 294 Properties props = new Properties (); 295 props.load(in); 296 allProps.putAll(props); 297 } catch (FileNotFoundException fnfe) { 298 String message = 299 "Could not find file " + inFile.getAbsolutePath(); 300 if (failonerror) { 301 throw new BuildException(message, fnfe, getLocation()); 302 } else { 303 log(message, Project.MSG_WARN); 304 } 305 return; 306 } catch (IOException ioe) { 307 String message = 308 "Could not read file " + inFile.getAbsolutePath(); 309 if (failonerror) { 310 throw new BuildException(message, ioe, getLocation()); 311 } else { 312 log(message, Project.MSG_WARN); 313 } 314 return; 315 } finally { 316 FileUtils.close(in); 317 } 318 } 319 320 Enumeration e = propertySets.elements(); 321 while (e.hasMoreElements()) { 322 PropertySet ps = (PropertySet) e.nextElement(); 323 allProps.putAll(ps.getProperties()); 324 } 325 326 OutputStream os = null; 327 try { 328 if (destfile == null) { 329 os = new ByteArrayOutputStream (); 330 saveProperties(allProps, os); 331 log(os.toString(), Project.MSG_INFO); 332 } else { 333 if (destfile.exists() && destfile.isDirectory()) { 334 String message = "destfile is a directory!"; 335 if (failonerror) { 336 throw new BuildException(message, getLocation()); 337 } else { 338 log(message, Project.MSG_ERR); 339 } 340 return; 341 } 342 343 if (destfile.exists() && !destfile.canWrite()) { 344 String message = 345 "Can not write to the specified destfile!"; 346 if (failonerror) { 347 throw new BuildException(message, getLocation()); 348 } else { 349 log(message, Project.MSG_ERR); 350 } 351 return; 352 } 353 os = new FileOutputStream (this.destfile); 354 saveProperties(allProps, os); 355 } 356 } catch (IOException ioe) { 357 if (failonerror) { 358 throw new BuildException(ioe, getLocation()); 359 } else { 360 log(ioe.getMessage(), Project.MSG_INFO); 361 } 362 } finally { 363 if (os != null) { 364 try { 365 os.close(); 366 } catch (IOException ex) { 367 } 369 } 370 } 371 } 372 373 374 385 protected void saveProperties(Hashtable allProps, OutputStream os) 386 throws IOException , BuildException { 387 final List keyList = new ArrayList (allProps.keySet()); 388 Collections.sort(keyList); 389 Properties props = new Properties () { 390 public Enumeration keys() { 391 return CollectionUtils.asEnumeration(keyList.iterator()); 392 } 393 public Set entrySet() { 394 Set result = super.entrySet(); 395 if (JavaEnvUtils.isKaffe()) { 396 TreeSet t = new TreeSet (new Comparator () { 397 public int compare(Object o1, Object o2) { 398 String key1 = (String ) ((Map.Entry ) o1).getKey(); 399 String key2 = (String ) ((Map.Entry ) o2).getKey(); 400 return key1.compareTo(key2); 401 } 402 }); 403 t.addAll(result); 404 result = t; 405 } 406 return result; 407 } 408 }; 409 for (int i = 0; i < keyList.size(); i++) { 410 String name = keyList.get(i).toString(); 411 String value = allProps.get(name).toString(); 412 props.setProperty(name, value); 413 } 414 if ("text".equals(format)) { 415 jdkSaveProperties(props, os, "Ant properties"); 416 } else if ("xml".equals(format)) { 417 xmlSaveProperties(props, os); 418 } 419 } 420 421 424 private static class Tuple implements Comparable { 425 private String key; 426 private String value; 427 428 private Tuple(String key, String value) { 429 this.key = key; 430 this.value = value; 431 } 432 433 441 public int compareTo(Object o) { 442 Tuple that = (Tuple) o; 443 return key.compareTo(that.key); 444 } 445 } 446 447 private List sortProperties(Properties props) { 448 List sorted = new ArrayList (props.size()); 450 Enumeration e = props.propertyNames(); 451 while (e.hasMoreElements()) { 452 String name = (String ) e.nextElement(); 453 sorted.add(new Tuple(name, props.getProperty(name))); 454 } 455 Collections.sort(sorted); 456 return sorted; 457 } 458 459 465 protected void xmlSaveProperties(Properties props, 466 OutputStream os) throws IOException { 467 Document doc = getDocumentBuilder().newDocument(); 469 Element rootElement = doc.createElement(PROPERTIES); 470 471 List sorted = sortProperties(props); 472 473 474 Iterator iten = sorted.iterator(); 476 while (iten.hasNext()) { 477 Tuple tuple = (Tuple) iten.next(); 478 Element propElement = doc.createElement(PROPERTY); 479 propElement.setAttribute(ATTR_NAME, tuple.key); 480 propElement.setAttribute(ATTR_VALUE, tuple.value); 481 rootElement.appendChild(propElement); 482 } 483 484 Writer wri = null; 485 try { 486 wri = new OutputStreamWriter (os, "UTF8"); 487 wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 488 (new DOMElementWriter()).write(rootElement, wri, 0, "\t"); 489 wri.flush(); 490 } catch (IOException ioe) { 491 throw new BuildException("Unable to write XML file", ioe); 492 } finally { 493 FileUtils.close(wri); 494 } 495 } 496 497 507 protected void jdkSaveProperties(Properties props, OutputStream os, 508 String header) throws IOException { 509 try { 510 props.store(os, header); 511 512 } catch (IOException ioe) { 513 throw new BuildException(ioe, getLocation()); 514 } finally { 515 if (os != null) { 516 try { 517 os.close(); 518 } catch (IOException ioex) { 519 log("Failed to close output stream"); 520 } 521 } 522 } 523 } 524 525 526 531 private static DocumentBuilder getDocumentBuilder() { 532 try { 533 return DocumentBuilderFactory.newInstance().newDocumentBuilder(); 534 } catch (Exception e) { 535 throw new ExceptionInInitializerError (e); 536 } 537 } 538 } 539 540 | Popular Tags |