ConfigurableObjects: fix value escaping

thobaben_serialize
Harald Wolff 2016-10-06 10:55:12 +02:00
parent 8066d1f497
commit 4e14febd21
1 changed files with 18 additions and 10 deletions

View File

@ -1,5 +1,7 @@
package org.hwo.configuration;
import static org.hwo.logging.Logging.log;
import static org.hwo.logging.LogLevel.*;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Field;
import java.util.Enumeration;
@ -7,6 +9,9 @@ import java.util.Hashtable;
import java.util.LinkedList;
import java.util.StringTokenizer;
import org.hwo.ByteArrayHelper;
import org.hwo.ByteArrayHexlifier;
public class ConfigurableObjects {
public static Field[] getConfigurableAttributes(Class<?> clazz){
@ -37,15 +42,12 @@ public class ConfigurableObjects {
public static String getValueAsString(Object value){
String s = value.toString();
s = s.replaceAll("\\\\","\\\\\\\\");
s = s.replaceAll("\\;", "\\\\;");
s = s.replaceAll("\\|", "\\\\|");
return s;
return ByteArrayHexlifier.hexlify(s);
}
public static Object getValueFromString(String value,Class<?> type) throws ClassCastException{
public static Object getValueFromString(String _value,Class<?> type) throws ClassCastException{
String value = ByteArrayHexlifier.unhexlify(_value);
if (type.equals(String.class))
return new String(value);
if (type.equals(Integer.class))
@ -121,11 +123,15 @@ public class ConfigurableObjects {
Hashtable<String,String> hash = new Hashtable<String, String>();
for (int n=0;n<l.size();n++){
String c = l.get(n).replaceAll("\\;", ";").replaceAll("\\|", "|").replaceAll("\\\\", "\\");
log(DEBUG,"l: %s",l.get(n));
String c = l.get(n);
String[] split = c.split("\\=",2);
if (split.length==1){
log(DEBUG,"split: (%s)",split[0]);
hash.put(split[0], "");
} else if (split.length==2){
log(DEBUG,"split: (%s:%s)",split[0],split[1]);
hash.put(split[0], split[1]);
}
}
@ -137,9 +143,11 @@ public class ConfigurableObjects {
try {
f.set(o, getValueFromString(hash.get(f.getName()), f.getType()));
} catch (IllegalArgumentException e) {
e.printStackTrace();
log(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
log(e);
} catch (ClassCastException e){
log(e);
}
}
}