|
Ant example source code file (PropertyFile.java)
The PropertyFile.java source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.apache.tools.ant.taskdefs.optional; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.Vector; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.types.EnumeratedAttribute; /** *Modifies settings in a property file. * * <p> *The following is an example of its usage: * <ul><target name="setState"> key was
* not contained in the property file.
*/
private void executeDate(String oldValue) throws BuildException {
Calendar currentValue = Calendar.getInstance();
if (pattern == null) {
pattern = "yyyy/MM/dd HH:mm";
}
DateFormat fmt = new SimpleDateFormat(pattern);
String currentStringValue = getCurrentValue(oldValue);
if (currentStringValue == null) {
currentStringValue = DEFAULT_DATE_VALUE;
}
if ("now".equals(currentStringValue)) {
currentValue.setTime(new Date());
} else {
try {
currentValue.setTime(fmt.parse(currentStringValue));
} catch (ParseException pe) {
// swallow
}
}
if (operation != Operation.EQUALS_OPER) {
int offset = 0;
try {
offset = Integer.parseInt(value);
if (operation == Operation.DECREMENT_OPER) {
offset = -1 * offset;
}
} catch (NumberFormatException e) {
throw new BuildException("Value not an integer on " + key);
}
currentValue.add(field, offset);
}
newValue = fmt.format(currentValue.getTime());
}
/**
* Handle operations for type <code>int.
*
* @param oldValue the current value read from the property file or
* <code>null if the key was
* not contained in the property file.
*/
private void executeInteger(String oldValue) throws BuildException {
int currentValue = DEFAULT_INT_VALUE;
int newV = DEFAULT_INT_VALUE;
DecimalFormat fmt = (pattern != null) ? new DecimalFormat(pattern)
: new DecimalFormat();
try {
String curval = getCurrentValue(oldValue);
if (curval != null) {
currentValue = fmt.parse(curval).intValue();
} else {
currentValue = 0;
}
} catch (NumberFormatException nfe) {
// swallow
} catch (ParseException pe) {
// swallow
}
if (operation == Operation.EQUALS_OPER) {
newV = currentValue;
} else {
int operationValue = 1;
if (value != null) {
try {
operationValue = fmt.parse(value).intValue();
} catch (NumberFormatException nfe) {
// swallow
} catch (ParseException pe) {
// swallow
}
}
if (operation == Operation.INCREMENT_OPER) {
newV = currentValue + operationValue;
} else if (operation == Operation.DECREMENT_OPER) {
newV = currentValue - operationValue;
}
}
this.newValue = fmt.format(newV);
}
/**
* Handle operations for type <code>string.
*
* @param oldValue the current value read from the property file or
* <code>null if the key was
* not contained in the property file.
*/
private void executeString(String oldValue) throws BuildException {
String newV = DEFAULT_STRING_VALUE;
String currentValue = getCurrentValue(oldValue);
if (currentValue == null) {
currentValue = DEFAULT_STRING_VALUE;
}
if (operation == Operation.EQUALS_OPER) {
newV = currentValue;
} else if (operation == Operation.INCREMENT_OPER) {
newV = currentValue + value;
}
this.newValue = newV;
}
/**
* Check if parameter combinations can be supported
* @todo make sure the 'unit' attribute is only specified on date
* fields
*/
private void checkParameters() throws BuildException {
if (type == Type.STRING_TYPE
&& operation == Operation.DECREMENT_OPER) {
throw new BuildException("- is not supported for string "
+ "properties (key:" + key + ")");
}
if (value == null && defaultValue == null) {
throw new BuildException("\"value\" and/or \"default\" "
+ "attribute must be specified (key:" + key + ")");
}
if (key == null) {
throw new BuildException("key is mandatory");
}
if (type == Type.STRING_TYPE && pattern != null) {
throw new BuildException("pattern is not supported for string "
+ "properties (key:" + key + ")");
}
}
private String getCurrentValue(String oldValue) {
String ret = null;
if (operation == Operation.EQUALS_OPER) {
// If only value is specified, the property is set to it
// regardless of its previous value.
if (value != null && defaultValue == null) {
ret = value;
}
// If only default is specified and the property previously
// existed in the property file, it is unchanged.
if (value == null && defaultValue != null && oldValue != null) {
ret = oldValue;
}
// If only default is specified and the property did not
// exist in the property file, the property is set to default.
if (value == null && defaultValue != null && oldValue == null) {
ret = defaultValue;
}
// If value and default are both specified and the property
// previously existed in the property file, the property
// is set to value.
if (value != null && defaultValue != null && oldValue != null) {
ret = value;
}
// If value and default are both specified and the property
// did not exist in the property file, the property is set
// to default.
if (value != null && defaultValue != null && oldValue == null) {
ret = defaultValue;
}
} else {
ret = (oldValue == null) ? defaultValue : oldValue;
}
return ret;
}
/**
* Enumerated attribute with the values "+", "-", "="
*/
public static class Operation extends EnumeratedAttribute {
// Property type operations
/** + */
public static final int INCREMENT_OPER = 0;
/** - */
public static final int DECREMENT_OPER = 1;
/** = */
public static final int EQUALS_OPER = 2;
/** {@inheritDoc}. */
public String[] getValues() {
return new String[] {"+", "-", "="};
}
/**
* Convert string to index.
* @param oper the string to convert.
* @return the index.
*/
public static int toOperation(String oper) {
if ("+".equals(oper)) {
return INCREMENT_OPER;
} else if ("-".equals(oper)) {
return DECREMENT_OPER;
}
return EQUALS_OPER;
}
}
/**
* Enumerated attribute with the values "int", "date" and "string".
*/
public static class Type extends EnumeratedAttribute {
// Property types
/** int */
public static final int INTEGER_TYPE = 0;
/** date */
public static final int DATE_TYPE = 1;
/** string */
public static final int STRING_TYPE = 2;
/** {@inheritDoc} */
public String[] getValues() {
return new String[] {"int", "date", "string"};
}
/**
* Convert string to index.
* @param type the string to convert.
* @return the index.
*/
public static int toType(String type) {
if ("int".equals(type)) {
return INTEGER_TYPE;
} else if ("date".equals(type)) {
return DATE_TYPE;
}
return STRING_TYPE;
}
}
}
/**
* Borrowed from Tstamp
* @todo share all this time stuff across many tasks as a datetime datatype
* @since Ant 1.5
*/
public static class Unit extends EnumeratedAttribute {
private static final String MILLISECOND = "millisecond";
private static final String SECOND = "second";
private static final String MINUTE = "minute";
private static final String HOUR = "hour";
private static final String DAY = "day";
private static final String WEEK = "week";
private static final String MONTH = "month";
private static final String YEAR = "year";
private static final String[] UNITS
= {MILLISECOND, SECOND, MINUTE, HOUR,
DAY, WEEK, MONTH, YEAR };
private Map calendarFields = new HashMap();
/** no arg constructor */
public Unit() {
calendarFields.put(MILLISECOND,
new Integer(Calendar.MILLISECOND));
calendarFields.put(SECOND, new Integer(Calendar.SECOND));
calendarFields.put(MINUTE, new Integer(Calendar.MINUTE));
calendarFields.put(HOUR, new Integer(Calendar.HOUR_OF_DAY));
calendarFields.put(DAY, new Integer(Calendar.DATE));
calendarFields.put(WEEK, new Integer(Calendar.WEEK_OF_YEAR));
calendarFields.put(MONTH, new Integer(Calendar.MONTH));
calendarFields.put(YEAR, new Integer(Calendar.YEAR));
}
/**
* Convert the value to a Calendar field index.
* @return the calander value.
*/
public int getCalendarField() {
String key = getValue().toLowerCase();
Integer i = (Integer) calendarFields.get(key);
return i.intValue();
}
/** {@inheritDoc}. */
public String[] getValues() {
return UNITS;
}
}
}
Other Ant examples (source code examples)Here is a short list of links related to this Ant PropertyFile.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.