// **********************************************************************
//
//
* * @param markerList a string containing a space delimited list of marker * names. * @return Vector of marker names. */ public static Vector parseSpacedMarkers(String markerList) { return parseMarkers(markerList, " "); } /** * Parse a list of marker names from a space separated list within a String. *
*
* @param markerList a string containing a space delimited list of marker
* names.
* @param delim the list of tokens to look for which separate the list
* elements.
* @return Vector of marker names.
*/
public static Vector parseMarkers(String markerList, String delim) {
Vector vector = null;
if (markerList == null) {
Debug.message("propertiesdetail", "PropUtils: marker list null!");
return new Vector(0);
}
if (Debug.debugging("propertiesdetail")) {
Debug.output("PropertyHandler: parsing marker list |" + markerList
+ "|");
}
// First, get rid of the quotation marks;
markerList = markerList.replace('\"', '\0');
// Next, tokenize the space delimited string
StringTokenizer tokens = new StringTokenizer(markerList, delim);
vector = new Vector(tokens.countTokens());
while (tokens.hasMoreTokens()) {
String name = tokens.nextToken().trim();
vector.addElement(name);
}
return vector;
}
/** Borrowed from Properites.java */
public static final String keyValueSeparators = "=: \t\r\n\f";
/** Borrowed from Properites.java */
public static final String strictKeyValueSeparators = "=:";
/** Borrowed from Properites.java */
public static final String whiteSpaceChars = " \t\r\n\f";
/** As defined in the OGC Web Mapping Testbed. */
public static final String propertySeparators = "&";
/**
* Take a property list, defined in a single string, and return a Properties
* object. The properties, as key-value pairs, are separated by another type
* of symbol. In this method, the key-values are assumed to be separated
* from other key-value pairs by PropUtils.propertySeparators String
* characters, and each key is separated from its value by any character in
* the PropUtils.keyValueSeparators list.
*
* @param list the properties list string.
* @return Properties object containing keys and values.
* @throws PropertyStringFormatException if a key doesn't have a value.
*/
public static Properties parsePropertyList(String list)
throws PropertyStringFormatException {
return parsePropertyList(list, propertySeparators, keyValueSeparators);
}
/**
* Take a property list, defined in a single string, and return a Properties
* object. The properties, as key-value pairs, are separated by another type
* of symbol.
*
* @param list the properties list string.
* @return Properties object containing keys and values.
* @param propertySeparators the key-values are assumed to be separated from
* other key-value pairs by any character in this String.
* @param keyValueSeparators each key is separated from its value by any
* character in this String.
* @throws PropertyStringFormatException if a key doesn't have a value.
*/
public static Properties parsePropertyList(String list,
String propertySeparators,
String keyValueSeparators)
throws PropertyStringFormatException {
Properties props = new Properties();
Vector keyValuePairs = parseMarkers(list, propertySeparators);
for (int i = 0; i < keyValuePairs.size(); i++) {
// Next, tokenize the space delimited string
StringTokenizer tokens = new StringTokenizer((String) keyValuePairs.elementAt(i), keyValueSeparators);
try {
String key = tokens.nextToken().trim();
String value = tokens.nextToken().trim();
props.put(key, value);
} catch (NoSuchElementException nsee) {
throw new PropertyStringFormatException(list);
}
}
return props;
}
/**
* Copy the contents from one properties object to another.
*
* @param from the source Properties object.
* @param to the destination Properties object.
*/
public static void copyProperties(Properties from, Properties to) {
Enumeration keys = from.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
to.put(key, from.getProperty(key));
}
}
/**
* Load the named file from the named directory into the given
* Properties instance. If the file is not found a warning is
* issued. If an IOException occurs, a fatal error is printed.
*
* @param props the instance to receive the loaded properties
* @param dir the directory where the properties file resides
* @param file the name of the file
* @return true if the properties file exists and was loaded.
*/
public static boolean loadProperties(Properties props, String dir,
String file) {
File propsFile = new File(dir, file);
try {
InputStream propsStream = new FileInputStream(propsFile);
props.load(propsStream);
if (Debug.debugging("properties")) {
Debug.output("PropUtils: Found " + propsFile);
}
return true;
} catch (java.io.FileNotFoundException e) {
if (Debug.debugging("properties")) {
Debug.output("PropUtils: File not found - \"" + propsFile
+ "\"");
}
} catch (java.io.IOException e) {
Debug.error("PropUtils: Caught IO Exception reading \"" + propsFile
+ "\"");
e.printStackTrace();
} catch (java.security.AccessControlException ace) {
}
return false;
}
/**
* Loads properties from a java resource. This will load the named resource
* identifier into the given properties instance.
*
* @param properties the Properties instance to receive the properties.
* @param propsIn an InputStream to read properties from
* @return true if the properties file exists and was loaded.
*/
public static boolean loadProperties(Properties properties,
InputStream propsIn) {
try {
properties.load(propsIn);
return true;
} catch (java.io.IOException e) {
if (Debug.debugging("properties")) {
Debug.error("PropUtils: Caught IOException loading properties from InputStream.");
}
return false;
}
}
/**
* A function that brings up a file chooser window in order to have the user
* look for a valid Java properties file.
*
* @return properties object with selected file contents.
*/
public static Properties promptUserForProperties() {
JFileChooser fileChooser = new JFileChooser();
int retvalue = fileChooser.showOpenDialog(null);
Properties props = new Properties();
if (retvalue != JFileChooser.APPROVE_OPTION) {
return props;
}
try {
FileInputStream inputStream = new FileInputStream(fileChooser.getSelectedFile());
props.load(inputStream);
return props;
} catch (Exception ioe) {
System.err.println("PropUtils.promptUserForProperties: Exception reading properties file.");
System.err.println(ioe.getMessage());
ioe.printStackTrace();
return props;
}
}
/**
* It seems like every PropertyConsumer wrestles with having a prefix or
* not. This method lets you just get the prefix with a period on the end
* (for scoping purposes), or just returns an empty String. Either way, you
* get a String you can slap on the beginning of your defined propery names
* to get a valid property based on what the prefix is.
*/
public static String getScopedPropertyPrefix(PropertyConsumer pc) {
return getScopedPropertyPrefix(pc.getPropertyPrefix());
}
/**
* Given the string, check if it's null or empty. If it is, return an empty
* string. If it isn't, check to see if it ends with a period, and do
* nothing if it does. If it doesn't end in a period, add one, and then
* return that. The returned string should be good for prepending to other
* properties.
*/
public static String getScopedPropertyPrefix(String pre) {
if ((pre == null) || (pre.length() == 0)) {
return "";
} else if (pre.endsWith(".")) {
return pre;
} else {
return pre + ".";
}
}
/**
* It kills Properties to have null values set. You can wrap a property
* value in this in PropertyConsumer.getProperties() to not worry about it.
* Returns "" if prop == null, else returns what was passed in.
*/
public static String unnull(String prop) {
if (prop == null) {
return "";
}
return prop;
}
/**
* Takes a string of `;' separated paths and returns an array of parsed
* strings. NOTE: this method currently doesn't support appropriate quoting
* of the `;' character, although it probably should...
*
* @param p properties
* @param propName the name of the property
* @return Array of strings representing paths.
*/
public static String[] initPathsFromProperties(Properties p, String propName) {
return initPathsFromProperties(p, propName, null);
}
/**
* Takes a string of `;' separated paths and returns an array of parsed
* strings. NOTE: this method currently doesn't support appropriate quoting
* of the `;' character, although it probably should...
*
* @param p properties
* @param propName the name of the property
* @param defaultPaths the value of the paths to set if the property doesn't
* exist, or if is doesn't contain anything.
* @return Array of strings representing paths.
*/
public static String[] initPathsFromProperties(Properties p,
String propName,
String[] defaultPaths) {
String[] ret = stringArrayFromProperties(p, propName, ";");
if (ret == null) {
ret = defaultPaths;
}
return ret;
}
/**
* Gets an integer out of a properties object. Returns the default value if
* something goes wrong.
*
* @param p properties
* @param propName name of the property associated with the wanted value.
* @param defaultValue what to return if the property name doesn't exist, or
* if the value isn't a numerical value.
* @return integer value associated with the property.
*/
public static int intFromProperties(Properties p, String propName,
int defaultValue) {
int ret = defaultValue;
String intString = p.getProperty(propName);
if (intString != null) {
try {
ret = Integer.parseInt(intString.trim());
} catch (NumberFormatException e) {
ret = defaultValue;
}
}
return ret;
}
/**
* Gets an float out of a properties object. Returns the default value if
* something goes wrong.
*
* @param p properties
* @param propName name of the property associated with the wanted value.
* @param defaultValue what to return if the property name doesn't exist, or
* if the value isn't a numerical value.
* @return float value associated with the property.
*/
public static float floatFromProperties(Properties p, String propName,
float defaultValue) {
float ret = defaultValue;
String floatString = p.getProperty(propName);
if (floatString != null) {
try {
ret = Float.parseFloat(floatString.trim());
} catch (NumberFormatException e) {
ret = defaultValue;
}
}
return ret;
}
/**
* Gets an boolean out of a properties object. Returns the default value if
* something goes wrong.
*
* @param p properties
* @param propName name of the property associated with the wanted value.
* @param defaultValue what to return if the property name doesn't exist, or
* if the value isn't a numerical value.
* @return boolean value associated with the property.
*/
public static boolean booleanFromProperties(Properties p, String propName,
boolean defaultValue) {
boolean ret = defaultValue;
String booleanString = p.getProperty(propName);
if (booleanString != null) {
ret = booleanString.trim().toLowerCase().equals("true");
}
return ret;
}
/**
* Creates an object out of a property name. If anything fails, return null.
*
* @param p properties
* @param propName name of class to instantiate.
* @return null on failure, otherwise, a default constructed instance of the
* class named in the property.
*/
public static Object objectFromProperties(Properties p, String propName) {
Object ret = null;
String objectName = p.getProperty(propName);
if (objectName != null) {
ret = ComponentFactory.create(objectName);
}
return ret;
}
/**
* Takes a string of representing token separated properties and returns an
* array of parsed strings. NOTE: this method currently doesn't support
* appropriate quoting of the token, although it probably should...
*
* @param p properties
* @param propName the name of the property
* @param tok the characters separating the strings.
* @return Array of strings between the tokens.
*/
public static String[] stringArrayFromProperties(Properties p,
String propName, String tok) {
String[] ret = null;
String raw = p.getProperty(propName);
if (raw != null && raw.length() != 0) {
try {
StringTokenizer token = new StringTokenizer(raw, tok);
int numPaths = token.countTokens();
ret = new String[numPaths];
for (int i = 0; i < numPaths; i++) {
ret[i] = token.nextToken();
}
return ret;
} catch (java.util.NoSuchElementException e) {
e.printStackTrace();
}
}
return ret;
}
/**
* Gets a double out of a properties object. Returns the default value if
* something goes wrong.
*
* @param p properties
* @param propName name of the property associated with the wanted value.
* @param defaultValue what to return if the property name doesn't exist, or
* if the value isn't a numerical value.
* @return double value associated with the property.
*/
public static double doubleFromProperties(Properties p, String propName,
double defaultValue) {
double ret = defaultValue;
String doubleString = p.getProperty(propName);
if (doubleString != null) {
try {
ret = Double.parseDouble(doubleString.trim());
} catch (NumberFormatException e) {
ret = defaultValue;
}
}
return ret;
}
/**
* Gets a long out of a properties object. Returns the default value if
* something goes wrong.
*
* @param p properties
* @param propName name of the property associated with the wanted value.
* @param defaultValue what to return if the property name doesn't exist, or
* if the value isn't a numerical value.
* @return long value associated with the property.
*/
public static long longFromProperties(Properties p, String propName,
long defaultValue) {
long ret = defaultValue;
String longString = p.getProperty(propName);
if (longString != null) {
try {
ret = Long.parseLong(longString.trim());
} catch (NumberFormatException e) {
ret = defaultValue;
}
}
return ret;
}
/**
* Take a string from a properties file, representing the 24bit RGB or 32bit
* ARGB hex values for a color, and convert it to a java.awt.Color.
*
* @param p properties
* @param propName the name of the property
* @param dfault color to use if the property value doesn't work
* @return java.awt.Color
* @exception NumberFormatException if the specified string cannot be
* interpreted as a hexidecimal integer
* @see ColorFactory#parseColorFromProperties(Properties, String, String,
* boolean)
*/
public static Color parseColorFromProperties(Properties p, String propName,
String dfault)
throws NumberFormatException {
return ColorFactory.parseColorFromProperties(p, propName, dfault, false);
}
/**
* Take a string from a properties file, representing the 24bit RGB or 32bit
* ARGB hex values for a color, and convert it to a java.awt.Color.
*
* @param p properties
* @param propName the name of the property
* @param dfault color to use if the property value doesn't work
* @return java.awt.Color
* @see ColorFactory#parseColorFromProperties(Properties, String, String,
* boolean)
*/
public static Paint parseColorFromProperties(Properties p, String propName,
Paint dfault) {
return ColorFactory.parseColorFromProperties(p, propName, dfault);
}
/**
* Convert a string representing a 24/32bit hex color value into a Color
* value. NOTE:
*