/**
* Creates a Map from an encoded name/value pair string
* @param str The string to decode and format
* @param trim Trim whitespace off fields
* @return a Map of name/value pairs
*/
public static Map strToMap(String str, boolean trim) {
if (str == null) return null;
Map decodedMap = new HashMap();
List elements = split(str, "|");
Iterator i = elements.iterator();
while (i.hasNext()) {
String s = (String) i.next();
List e = split(s, "=");
if (e.size() != 2) {
continue;
}
String name = (String) e.get(0);
String value = (String) e.get(1);
if (trim) {
if (name != null) {
name = name.trim();
}
if (value != null) {
value = value.trim();
}
}
try {
decodedMap.put(URLDecoder.decode(name, "UTF-8"), URLDecoder.decode(value, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
}
}
return decodedMap;
}
/**
* Creates a Map from an encoded name/value pair string
* @param str The string to decode and format
* @return a Map of name/value pairs
*/
public static Map strToMap(String str) {
return strToMap(str, false);
}
/**
* Creates an encoded String from a Map of name/value pairs (MUST BE STRINGS!)
* @param map The Map of name/value pairs
* @return String The encoded String
*/
public static String mapToStr(Map map) {
if (map == null) return null;
StringBuffer buf = new StringBuffer();
Set keySet = map.keySet();
Iterator i = keySet.iterator();
boolean first = true;
while (i.hasNext()) {
Object key = i.next();
Object value = map.get(key);
if (!(key instanceof String) || !(value instanceof String))
continue;
String encodedName = null;
try {
encodedName = URLEncoder.encode((String) key, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
String encodedValue = null;
try {
encodedValue = URLEncoder.encode((String) value, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
if (first)
first = false;
else
buf.append("|");
buf.append(encodedName);
buf.append("=");
buf.append(encodedValue);
}
return buf.toString();
}
/**
* Create a Map from a List of keys and a List of values
* @param keys List of keys
* @param values List of values
* @return Map of combined lists
* @throws IllegalArgumentException When either List is null or the sizes do not equal
*/
public static Map createMap(List keys, List values) {
if (keys == null || values == null || keys.size() != values.size()) {
throw new IllegalArgumentException("Keys and Values cannot be null and must be the same size");
}
Map newMap = new HashMap();
for (int i = 0; i < keys.size(); i++) {
newMap.put(keys.get(i), values.get(i));
}
return newMap;
}
/** Make sure the string starts with a forward slash but does not end with one; converts back-slashes to forward-slashes; if in String is null or empty, returns zero length string. */
public static String cleanUpPathPrefix(String prefix) {
if (prefix == null || prefix.length() == 0) return "";
StringBuffer cppBuff = new StringBuffer(prefix.replace('\\', '/'));
if (cppBuff.charAt(0) != '/') {
cppBuff.insert(0, '/');
}
if (cppBuff.charAt(cppBuff.length() - 1) == '/') {
cppBuff.deleteCharAt(cppBuff.length() - 1);
}
return cppBuff.toString();
}
* Creates a Map from an encoded name/value pair string
* @param str The string to decode and format
* @param trim Trim whitespace off fields
* @return a Map of name/value pairs
*/
public static Map strToMap(String str, boolean trim) {
if (str == null) return null;
Map decodedMap = new HashMap();
List elements = split(str, "|");
Iterator i = elements.iterator();
while (i.hasNext()) {
String s = (String) i.next();
List e = split(s, "=");
if (e.size() != 2) {
continue;
}
String name = (String) e.get(0);
String value = (String) e.get(1);
if (trim) {
if (name != null) {
name = name.trim();
}
if (value != null) {
value = value.trim();
}
}
try {
decodedMap.put(URLDecoder.decode(name, "UTF-8"), URLDecoder.decode(value, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
}
}
return decodedMap;
}
/**
* Creates a Map from an encoded name/value pair string
* @param str The string to decode and format
* @return a Map of name/value pairs
*/
public static Map strToMap(String str) {
return strToMap(str, false);
}
/**
* Creates an encoded String from a Map of name/value pairs (MUST BE STRINGS!)
* @param map The Map of name/value pairs
* @return String The encoded String
*/
public static String mapToStr(Map map) {
if (map == null) return null;
StringBuffer buf = new StringBuffer();
Set keySet = map.keySet();
Iterator i = keySet.iterator();
boolean first = true;
while (i.hasNext()) {
Object key = i.next();
Object value = map.get(key);
if (!(key instanceof String) || !(value instanceof String))
continue;
String encodedName = null;
try {
encodedName = URLEncoder.encode((String) key, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
String encodedValue = null;
try {
encodedValue = URLEncoder.encode((String) value, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
if (first)
first = false;
else
buf.append("|");
buf.append(encodedName);
buf.append("=");
buf.append(encodedValue);
}
return buf.toString();
}
/**
* Create a Map from a List of keys and a List of values
* @param keys List of keys
* @param values List of values
* @return Map of combined lists
* @throws IllegalArgumentException When either List is null or the sizes do not equal
*/
public static Map createMap(List keys, List values) {
if (keys == null || values == null || keys.size() != values.size()) {
throw new IllegalArgumentException("Keys and Values cannot be null and must be the same size");
}
Map newMap = new HashMap();
for (int i = 0; i < keys.size(); i++) {
newMap.put(keys.get(i), values.get(i));
}
return newMap;
}
/** Make sure the string starts with a forward slash but does not end with one; converts back-slashes to forward-slashes; if in String is null or empty, returns zero length string. */
public static String cleanUpPathPrefix(String prefix) {
if (prefix == null || prefix.length() == 0) return "";
StringBuffer cppBuff = new StringBuffer(prefix.replace('\\', '/'));
if (cppBuff.charAt(0) != '/') {
cppBuff.insert(0, '/');
}
if (cppBuff.charAt(cppBuff.length() - 1) == '/') {
cppBuff.deleteCharAt(cppBuff.length() - 1);
}
return cppBuff.toString();
}