Improved: General refactoring and code improvements, package
org.apache.ofbiz.entity.serialize. (OFBIZ-9951) Thanks Dennis Balkir for reporting and providing the patch. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1818476 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+88
-87
@@ -82,7 +82,7 @@ public class XmlSerializer {
|
||||
/** Deserialize a Java object from an XML string. <p>This method should be used with caution.
|
||||
* If the XML string contains a serialized <code>GenericValue</code> or <code>GenericPK</code>
|
||||
* then it is possible to unintentionally corrupt the database.</p>
|
||||
*
|
||||
*
|
||||
* @param content the content
|
||||
* @param delegator the delegator
|
||||
* @return return a deserialized object from XML string
|
||||
@@ -100,17 +100,16 @@ public class XmlSerializer {
|
||||
return UtilXml.fromXml(content);
|
||||
}
|
||||
return deserialize(document, delegator);
|
||||
} else {
|
||||
Debug.logWarning("Serialized document came back null", module);
|
||||
return null;
|
||||
}
|
||||
Debug.logWarning("Serialized document came back null", module);
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Deserialize a Java object from a DOM <code>Document</code>.
|
||||
* <p>This method should be used with caution. If the DOM <code>Document</code>
|
||||
* contains a serialized <code>GenericValue</code> or <code>GenericPK</code>
|
||||
* then it is possible to unintentionally corrupt the database.</p>
|
||||
*
|
||||
*
|
||||
* @param document the document
|
||||
* @param delegator the delegator
|
||||
* @return returns a deserialized object from a DOM document
|
||||
@@ -130,10 +129,13 @@ public class XmlSerializer {
|
||||
}
|
||||
|
||||
public static Element serializeSingle(Object object, Document document) throws SerializeException {
|
||||
if (document == null) return null;
|
||||
if (document == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (object == null)
|
||||
if (object == null) {
|
||||
return makeElement("null", null, document);
|
||||
}
|
||||
|
||||
// - Standard Objects -
|
||||
if (object instanceof String) {
|
||||
@@ -151,7 +153,7 @@ public class XmlSerializer {
|
||||
} else if (object instanceof Locale) {
|
||||
return makeElement("std-Locale", object, document);
|
||||
} else if (object instanceof BigDecimal) {
|
||||
String stringValue = ((BigDecimal) object).setScale(10, RoundingMode.HALF_UP).toString();
|
||||
String stringValue = ((BigDecimal) object).setScale(10, RoundingMode.HALF_UP).toString();
|
||||
return makeElement("std-BigDecimal", stringValue, document);
|
||||
// - SQL Objects -
|
||||
} else if (object instanceof java.sql.Timestamp) {
|
||||
@@ -193,8 +195,6 @@ public class XmlSerializer {
|
||||
elementName = "col-Collection";
|
||||
}
|
||||
|
||||
// if (elementName == null) return serializeCustom(object, document);
|
||||
|
||||
Collection<?> value = UtilGenerics.cast(object);
|
||||
Element element = document.createElement(elementName);
|
||||
Iterator<?> iter = value.iterator();
|
||||
@@ -263,24 +263,22 @@ public class XmlSerializer {
|
||||
byte[] objBytes = UtilObject.getBytes(object);
|
||||
if (objBytes == null) {
|
||||
throw new SerializeException("Unable to serialize object; null byte array returned");
|
||||
} else {
|
||||
String byteHex = StringUtil.toHexString(objBytes);
|
||||
Element element = document.createElement("cus-obj");
|
||||
// this is hex encoded so does not need to be in a CDATA block
|
||||
element.appendChild(document.createTextNode(byteHex));
|
||||
return element;
|
||||
}
|
||||
} else {
|
||||
throw new SerializeException("Cannot serialize object of class " + object.getClass().getName());
|
||||
String byteHex = StringUtil.toHexString(objBytes);
|
||||
Element element = document.createElement("cus-obj");
|
||||
// this is hex encoded so does not need to be in a CDATA block
|
||||
element.appendChild(document.createTextNode(byteHex));
|
||||
return element;
|
||||
}
|
||||
throw new SerializeException("Cannot serialize object of class " + object.getClass().getName());
|
||||
}
|
||||
|
||||
public static Element makeElement(String elementName, Object value, Document document) {
|
||||
if (value == null) {
|
||||
Element element = document.createElement("null");
|
||||
element.setAttribute("xsi:nil", "true");
|
||||
// I tried to put the schema in the envelope header (in createAndSendSOAPResponse)
|
||||
// resEnv.declareNamespace("http://www.w3.org/2001/XMLSchema-instance", null);
|
||||
// I tried to put the schema in the envelope header (in createAndSendSOAPResponse)
|
||||
// resEnv.declareNamespace("http://www.w3.org/2001/XMLSchema-instance", null);
|
||||
// But it gets prefixed and that does not work. So adding in each instance
|
||||
element.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
|
||||
return element;
|
||||
@@ -294,7 +292,9 @@ public class XmlSerializer {
|
||||
public static Object deserializeSingle(Element element, Delegator delegator) throws SerializeException {
|
||||
String tagName = element.getLocalName();
|
||||
|
||||
if ("null".equals(tagName)) return null;
|
||||
if ("null".equals(tagName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (tagName.startsWith("std-")) {
|
||||
// - Standard Objects -
|
||||
@@ -364,96 +364,98 @@ public class XmlSerializer {
|
||||
Collection<Object> value = null;
|
||||
|
||||
if ("col-ArrayList".equals(tagName)) {
|
||||
value = new ArrayList<Object>();
|
||||
value = new ArrayList<>();
|
||||
} else if ("col-LinkedList".equals(tagName)) {
|
||||
value = new LinkedList<Object>();
|
||||
value = new LinkedList<>();
|
||||
} else if ("col-Stack".equals(tagName)) {
|
||||
value = new Stack<Object>();
|
||||
value = new Stack<>();
|
||||
} else if ("col-Vector".equals(tagName)) {
|
||||
value = new Vector<Object>();
|
||||
value = new Vector<>();
|
||||
} else if ("col-TreeSet".equals(tagName)) {
|
||||
value = new TreeSet<Object>();
|
||||
value = new TreeSet<>();
|
||||
} else if ("col-HashSet".equals(tagName)) {
|
||||
value = new HashSet<Object>();
|
||||
value = new HashSet<>();
|
||||
} else if ("col-Collection".equals(tagName)) {
|
||||
value = new LinkedList<Object>();
|
||||
value = new LinkedList<>();
|
||||
}
|
||||
|
||||
if (value == null) {
|
||||
return deserializeCustom(element);
|
||||
} else {
|
||||
Node curChild = element.getFirstChild();
|
||||
|
||||
while (curChild != null) {
|
||||
if (curChild.getNodeType() == Node.ELEMENT_NODE) {
|
||||
value.add(deserializeSingle((Element) curChild, delegator));
|
||||
}
|
||||
curChild = curChild.getNextSibling();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
Node curChild = element.getFirstChild();
|
||||
|
||||
while (curChild != null) {
|
||||
if (curChild.getNodeType() == Node.ELEMENT_NODE) {
|
||||
value.add(deserializeSingle((Element) curChild, delegator));
|
||||
}
|
||||
curChild = curChild.getNextSibling();
|
||||
}
|
||||
return value;
|
||||
} else if (tagName.startsWith("map-")) {
|
||||
// - Maps -
|
||||
Map<Object, Object> value = null;
|
||||
|
||||
if ("map-HashMap".equals(tagName)) {
|
||||
value = new HashMap<Object, Object>();
|
||||
value = new HashMap<>();
|
||||
} else if ("map-Properties".equals(tagName)) {
|
||||
value = new Properties();
|
||||
} else if ("map-Hashtable".equals(tagName)) {
|
||||
value = new Hashtable<Object, Object>();
|
||||
value = new Hashtable<>();
|
||||
} else if ("map-WeakHashMap".equals(tagName)) {
|
||||
value = new WeakHashMap<Object, Object>();
|
||||
value = new WeakHashMap<>();
|
||||
} else if ("map-TreeMap".equals(tagName)) {
|
||||
value = new TreeMap<Object, Object>();
|
||||
value = new TreeMap<>();
|
||||
} else if ("map-Map".equals(tagName)) {
|
||||
value = new HashMap<Object, Object>();
|
||||
value = new HashMap<>();
|
||||
}
|
||||
|
||||
if (value == null) {
|
||||
return deserializeCustom(element);
|
||||
} else {
|
||||
Node curChild = element.getFirstChild();
|
||||
|
||||
while (curChild != null) {
|
||||
if (curChild.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element curElement = (Element) curChild;
|
||||
|
||||
if ("map-Entry".equals(curElement.getLocalName())) {
|
||||
|
||||
Element mapKeyElement = UtilXml.firstChildElement(curElement, "map-Key");
|
||||
Element keyElement = null;
|
||||
Node tempNode = mapKeyElement.getFirstChild();
|
||||
|
||||
while (tempNode != null) {
|
||||
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
keyElement = (Element) tempNode;
|
||||
break;
|
||||
}
|
||||
tempNode = tempNode.getNextSibling();
|
||||
}
|
||||
if (keyElement == null) throw new SerializeException("Could not find an element under the map-Key");
|
||||
|
||||
Element mapValueElement = UtilXml.firstChildElement(curElement, "map-Value");
|
||||
Element valueElement = null;
|
||||
|
||||
tempNode = mapValueElement.getFirstChild();
|
||||
while (tempNode != null) {
|
||||
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
valueElement = (Element) tempNode;
|
||||
break;
|
||||
}
|
||||
tempNode = tempNode.getNextSibling();
|
||||
}
|
||||
if (valueElement == null) throw new SerializeException("Could not find an element under the map-Value");
|
||||
|
||||
value.put(deserializeSingle(keyElement, delegator), deserializeSingle(valueElement, delegator));
|
||||
}
|
||||
}
|
||||
curChild = curChild.getNextSibling();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
Node curChild = element.getFirstChild();
|
||||
|
||||
while (curChild != null) {
|
||||
if (curChild.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element curElement = (Element) curChild;
|
||||
|
||||
if ("map-Entry".equals(curElement.getLocalName())) {
|
||||
|
||||
Element mapKeyElement = UtilXml.firstChildElement(curElement, "map-Key");
|
||||
Element keyElement = null;
|
||||
Node tempNode = mapKeyElement.getFirstChild();
|
||||
|
||||
while (tempNode != null) {
|
||||
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
keyElement = (Element) tempNode;
|
||||
break;
|
||||
}
|
||||
tempNode = tempNode.getNextSibling();
|
||||
}
|
||||
if (keyElement == null) {
|
||||
throw new SerializeException("Could not find an element under the map-Key");
|
||||
}
|
||||
|
||||
Element mapValueElement = UtilXml.firstChildElement(curElement, "map-Value");
|
||||
Element valueElement = null;
|
||||
|
||||
tempNode = mapValueElement.getFirstChild();
|
||||
while (tempNode != null) {
|
||||
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
valueElement = (Element) tempNode;
|
||||
break;
|
||||
}
|
||||
tempNode = tempNode.getNextSibling();
|
||||
}
|
||||
if (valueElement == null) {
|
||||
throw new SerializeException("Could not find an element under the map-Value");
|
||||
}
|
||||
|
||||
value.put(deserializeSingle(keyElement, delegator), deserializeSingle(valueElement, delegator));
|
||||
}
|
||||
}
|
||||
curChild = curChild.getNextSibling();
|
||||
}
|
||||
return value;
|
||||
} else if (tagName.startsWith("eepk-")) {
|
||||
return delegator.makePK(element);
|
||||
} else if (tagName.startsWith("eeval-")) {
|
||||
@@ -477,9 +479,8 @@ public class XmlSerializer {
|
||||
}
|
||||
}
|
||||
throw new SerializeException("Problem deserializing object from byte array + " + element.getLocalName());
|
||||
} else {
|
||||
throw new SerializeException("Cannot deserialize element named " + element.getLocalName());
|
||||
}
|
||||
throw new SerializeException("Cannot deserialize element named " + element.getLocalName());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -498,7 +499,7 @@ public class XmlSerializer {
|
||||
}
|
||||
if (formatter == null) {
|
||||
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
|
||||
simpleDateFormatter = new WeakReference<DateFormat>(formatter);
|
||||
simpleDateFormatter = new WeakReference<>(formatter);
|
||||
}
|
||||
return formatter;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user