1 18 19 package org.apache.tools.ant.taskdefs.optional; 20 21 import java.io.BufferedInputStream ; 22 import java.io.BufferedOutputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.text.DateFormat ; 28 import java.text.DecimalFormat ; 29 import java.text.ParseException ; 30 import java.text.SimpleDateFormat ; 31 import java.util.Calendar ; 32 import java.util.Date ; 33 import java.util.Enumeration ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 import java.util.Properties ; 37 import java.util.Vector ; 38 import org.apache.tools.ant.BuildException; 39 import org.apache.tools.ant.Task; 40 import org.apache.tools.ant.util.FileUtils; 41 import org.apache.tools.ant.types.EnumeratedAttribute; 42 43 109 public class PropertyFile extends Task { 110 111 115 116 private String comment; 118 119 private Properties properties; 120 private File propertyfile; 121 122 private Vector entries = new Vector (); 123 124 128 129 133 134 138 public void execute() throws BuildException { 139 checkParameters(); 140 readFile(); 141 executeOperation(); 142 writeFile(); 143 } 144 145 149 public Entry createEntry() { 150 Entry e = new Entry(); 151 entries.addElement(e); 152 return e; 153 } 154 155 private void executeOperation() throws BuildException { 156 for (Enumeration e = entries.elements(); e.hasMoreElements();) { 157 Entry entry = (Entry) e.nextElement(); 158 entry.executeOn(properties); 159 } 160 } 161 162 private void readFile() throws BuildException { 163 properties = new Properties (); 165 try { 166 if (propertyfile.exists()) { 167 log("Updating property file: " 168 + propertyfile.getAbsolutePath()); 169 FileInputStream fis = null; 170 try { 171 fis = new FileInputStream (propertyfile); 172 BufferedInputStream bis = new BufferedInputStream (fis); 173 properties.load(bis); 174 } finally { 175 if (fis != null) { 176 fis.close(); 177 } 178 } 179 } else { 180 log("Creating new property file: " 181 + propertyfile.getAbsolutePath()); 182 FileOutputStream out = null; 183 try { 184 out = new FileOutputStream (propertyfile.getAbsolutePath()); 185 out.flush(); 186 } finally { 187 if (out != null) { 188 out.close(); 189 } 190 } 191 } 192 } catch (IOException ioe) { 193 throw new BuildException(ioe.toString()); 194 } 195 } 196 197 private void checkParameters() throws BuildException { 198 if (!checkParam(propertyfile)) { 199 throw new BuildException("file token must not be null.", getLocation()); 200 } 201 } 202 203 207 public void setFile(File file) { 208 propertyfile = file; 209 } 210 211 215 public void setComment(String hdr) { 216 comment = hdr; 217 } 218 219 private void writeFile() throws BuildException { 220 BufferedOutputStream bos = null; 221 try { 222 bos = new BufferedOutputStream (new FileOutputStream (propertyfile)); 223 properties.store(bos, comment); 224 } catch (IOException ioe) { 225 throw new BuildException(ioe, getLocation()); 226 } finally { 227 FileUtils.close(bos); 228 } 229 } 230 231 private boolean checkParam(File param) { 232 return !(param == null); 233 } 234 235 239 public static class Entry { 240 private static final int DEFAULT_INT_VALUE = 0; 241 private static final String DEFAULT_DATE_VALUE = "now"; 242 private static final String DEFAULT_STRING_VALUE = ""; 243 244 private String key = null; 245 private int type = Type.STRING_TYPE; 246 private int operation = Operation.EQUALS_OPER; 247 private String value = null; 248 private String defaultValue = null; 249 private String newValue = null; 250 private String pattern = null; 251 private int field = Calendar.DATE; 252 253 257 public void setKey(String value) { 258 this.key = value; 259 } 260 261 265 public void setValue(String value) { 266 this.value = value; 267 } 268 269 275 public void setOperation(Operation value) { 276 this.operation = Operation.toOperation(value.getValue()); 277 } 278 279 283 public void setType(Type value) { 284 this.type = Type.toType(value.getValue()); 285 } 286 287 293 public void setDefault(String value) { 294 this.defaultValue = value; 295 } 296 297 302 public void setPattern(String value) { 303 this.pattern = value; 304 } 305 306 323 public void setUnit(PropertyFile.Unit unit) { 324 field = unit.getCalendarField(); 325 } 326 327 332 protected void executeOn(Properties props) throws BuildException { 333 checkParameters(); 334 335 String oldValue = (String ) props.get(key); 337 try { 338 if (type == Type.INTEGER_TYPE) { 339 executeInteger(oldValue); 340 } else if (type == Type.DATE_TYPE) { 341 executeDate(oldValue); 342 } else if (type == Type.STRING_TYPE) { 343 executeString(oldValue); 344 } else { 345 throw new BuildException("Unknown operation type: " 346 + type); 347 } 348 } catch (NullPointerException npe) { 349 npe.printStackTrace(); 352 } 353 354 if (newValue == null) { 355 newValue = ""; 356 } 357 358 props.put(key, newValue); 360 } 361 362 369 private void executeDate(String oldValue) throws BuildException { 370 Calendar currentValue = Calendar.getInstance(); 371 372 if (pattern == null) { 373 pattern = "yyyy/MM/dd HH:mm"; 374 } 375 DateFormat fmt = new SimpleDateFormat (pattern); 376 377 String currentStringValue = getCurrentValue(oldValue); 378 if (currentStringValue == null) { 379 currentStringValue = DEFAULT_DATE_VALUE; 380 } 381 382 if ("now".equals(currentStringValue)) { 383 currentValue.setTime(new Date ()); 384 } else { 385 try { 386 currentValue.setTime(fmt.parse(currentStringValue)); 387 } catch (ParseException pe) { 388 } 390 } 391 392 if (operation != Operation.EQUALS_OPER) { 393 int offset = 0; 394 try { 395 offset = Integer.parseInt(value); 396 if (operation == Operation.DECREMENT_OPER) { 397 offset = -1 * offset; 398 } 399 } catch (NumberFormatException e) { 400 throw new BuildException("Value not an integer on " + key); 401 } 402 currentValue.add(field, offset); 403 } 404 405 newValue = fmt.format(currentValue.getTime()); 406 } 407 408 409 416 private void executeInteger(String oldValue) throws BuildException { 417 int currentValue = DEFAULT_INT_VALUE; 418 int newV = DEFAULT_INT_VALUE; 419 420 421 DecimalFormat fmt = (pattern != null) ? new DecimalFormat (pattern) 422 : new DecimalFormat (); 423 try { 424 String curval = getCurrentValue(oldValue); 425 if (curval != null) { 426 currentValue = fmt.parse(curval).intValue(); 427 } else { 428 currentValue = 0; 429 } 430 } catch (NumberFormatException nfe) { 431 } catch (ParseException pe) { 433 } 435 436 if (operation == Operation.EQUALS_OPER) { 437 newV = currentValue; 438 } else { 439 int operationValue = 1; 440 if (value != null) { 441 try { 442 operationValue = fmt.parse(value).intValue(); 443 } catch (NumberFormatException nfe) { 444 } catch (ParseException pe) { 446 } 448 } 449 450 if (operation == Operation.INCREMENT_OPER) { 451 newV = currentValue + operationValue; 452 } else if (operation == Operation.DECREMENT_OPER) { 453 newV = currentValue - operationValue; 454 } 455 } 456 457 this.newValue = fmt.format(newV); 458 } 459 460 467 private void executeString(String oldValue) throws BuildException { 468 String newV = DEFAULT_STRING_VALUE; 469 470 String currentValue = getCurrentValue(oldValue); 471 472 if (currentValue == null) { 473 currentValue = DEFAULT_STRING_VALUE; 474 } 475 476 if (operation == Operation.EQUALS_OPER) { 477 newV = currentValue; 478 } else if (operation == Operation.INCREMENT_OPER) { 479 newV = currentValue + value; 480 } 481 this.newValue = newV; 482 } 483 484 489 private void checkParameters() throws BuildException { 490 if (type == Type.STRING_TYPE 491 && operation == Operation.DECREMENT_OPER) { 492 throw new BuildException("- is not supported for string " 493 + "properties (key:" + key + ")"); 494 } 495 if (value == null && defaultValue == null) { 496 throw new BuildException("\"value\" and/or \"default\" " 497 + "attribute must be specified (key:" + key + ")"); 498 } 499 if (key == null) { 500 throw new BuildException("key is mandatory"); 501 } 502 if (type == Type.STRING_TYPE && pattern != null) { 503 throw new BuildException("pattern is not supported for string " 504 + "properties (key:" + key + ")"); 505 } 506 } 507 508 private String getCurrentValue(String oldValue) { 509 String ret = null; 510 if (operation == Operation.EQUALS_OPER) { 511 if (value != null && defaultValue == null) { 514 ret = value; 515 } 516 517 if (value == null && defaultValue != null && oldValue != null) { 520 ret = oldValue; 521 } 522 523 if (value == null && defaultValue != null && oldValue == null) { 526 ret = defaultValue; 527 } 528 529 if (value != null && defaultValue != null && oldValue != null) { 533 ret = value; 534 } 535 536 if (value != null && defaultValue != null && oldValue == null) { 540 ret = defaultValue; 541 } 542 } else { 543 ret = (oldValue == null) ? defaultValue : oldValue; 544 } 545 546 return ret; 547 } 548 549 552 public static class Operation extends EnumeratedAttribute { 553 554 556 public static final int INCREMENT_OPER = 0; 557 558 public static final int DECREMENT_OPER = 1; 559 560 public static final int EQUALS_OPER = 2; 561 562 563 public String [] getValues() { 564 return new String [] {"+", "-", "="}; 565 } 566 567 572 public static int toOperation(String oper) { 573 if ("+".equals(oper)) { 574 return INCREMENT_OPER; 575 } else if ("-".equals(oper)) { 576 return DECREMENT_OPER; 577 } 578 return EQUALS_OPER; 579 } 580 } 581 582 585 public static class Type extends EnumeratedAttribute { 586 587 589 public static final int INTEGER_TYPE = 0; 590 591 public static final int DATE_TYPE = 1; 592 593 public static final int STRING_TYPE = 2; 594 595 596 public String [] getValues() { 597 return new String [] {"int", "date", "string"}; 598 } 599 600 605 public static int toType(String type) { 606 if ("int".equals(type)) { 607 return INTEGER_TYPE; 608 } else if ("date".equals(type)) { 609 return DATE_TYPE; 610 } 611 return STRING_TYPE; 612 } 613 } 614 } 615 616 621 public static class Unit extends EnumeratedAttribute { 622 623 private static final String MILLISECOND = "millisecond"; 624 private static final String SECOND = "second"; 625 private static final String MINUTE = "minute"; 626 private static final String HOUR = "hour"; 627 private static final String DAY = "day"; 628 private static final String WEEK = "week"; 629 private static final String MONTH = "month"; 630 private static final String YEAR = "year"; 631 632 private static final String [] UNITS 633 = {MILLISECOND, SECOND, MINUTE, HOUR, 634 DAY, WEEK, MONTH, YEAR }; 635 636 private Map calendarFields = new HashMap (); 637 638 639 public Unit() { 640 calendarFields.put(MILLISECOND, 641 new Integer (Calendar.MILLISECOND)); 642 calendarFields.put(SECOND, new Integer (Calendar.SECOND)); 643 calendarFields.put(MINUTE, new Integer (Calendar.MINUTE)); 644 calendarFields.put(HOUR, new Integer (Calendar.HOUR_OF_DAY)); 645 calendarFields.put(DAY, new Integer (Calendar.DATE)); 646 calendarFields.put(WEEK, new Integer (Calendar.WEEK_OF_YEAR)); 647 calendarFields.put(MONTH, new Integer (Calendar.MONTH)); 648 calendarFields.put(YEAR, new Integer (Calendar.YEAR)); 649 } 650 651 655 public int getCalendarField() { 656 String key = getValue().toLowerCase(); 657 Integer i = (Integer ) calendarFields.get(key); 658 return i.intValue(); 659 } 660 661 662 public String [] getValues() { 663 return UNITS; 664 } 665 } 666 } 667 | Popular Tags |