📄 xmlserializer.java
字号:
/* * $Id: XmlSerializer.java 5462 2005-08-05 18:35:48Z jonesde $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.entity.serialize;import java.io.FileNotFoundException;import java.io.IOException;import java.io.Serializable;import java.lang.ref.WeakReference;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Hashtable;import java.util.Iterator;import java.util.LinkedList;import java.util.Locale;import java.util.Map;import java.util.Properties;import java.util.Stack;import java.util.TreeMap;import java.util.TreeSet;import java.util.Vector;import java.util.WeakHashMap;import javax.xml.parsers.ParserConfigurationException;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilXml;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilObject;import org.ofbiz.base.util.StringUtil;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericPK;import org.ofbiz.entity.GenericValue;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.xml.sax.SAXException;/** * <p><b>Title:</b> XmlSerializer * <p><b>Description:</b> Simple XML serialization/deserialization routines with embedded type information * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @version $Rev: 5462 $ * @since 2.0 */public class XmlSerializer { public static final String module = XmlSerializer.class.getName(); private static WeakReference simpleDateFormatter; public static String serialize(Object object) throws SerializeException, FileNotFoundException, IOException { Document document = UtilXml.makeEmptyXmlDocument("ofbiz-ser"); Element rootElement = document.getDocumentElement(); rootElement.appendChild(serializeSingle(object, document)); return UtilXml.writeXmlDocument(document); } public static Object deserialize(String content, GenericDelegator delegator) throws SerializeException, SAXException, ParserConfigurationException, IOException { // readXmlDocument with false second parameter to disable validation Document document = UtilXml.readXmlDocument(content, false); if (document != null) { Element rootElement = document.getDocumentElement(); // find the first element below the root element, that should be the object Node curChild = rootElement.getFirstChild(); while (curChild != null && curChild.getNodeType() != Node.ELEMENT_NODE) { curChild = curChild.getNextSibling(); } if (curChild == null) return null; Element element = (Element) curChild; return deserializeSingle(element, delegator); } else { Debug.logWarning("Serialized document came back null", module); return null; } } public static Element serializeSingle(Object object, Document document) throws SerializeException { if (document == null) return null; if (object == null) return document.createElement("null"); // - Standard Objects - if (object instanceof String) { return makeElement("std-String", object, document); } else if (object instanceof Integer) { return makeElement("std-Integer", object, document); } else if (object instanceof Long) { return makeElement("std-Long", object, document); } else if (object instanceof Float) { return makeElement("std-Float", object, document); } else if (object instanceof Double) { return makeElement("std-Double", object, document); } else if (object instanceof Boolean) { return makeElement("std-Boolean", object, document); } else if (object instanceof Locale) { return makeElement("std-Locale", object, document); // - SQL Objects - } else if (object instanceof java.sql.Timestamp) { return makeElement("sql-Timestamp", object, document); } else if (object instanceof java.sql.Date) { return makeElement("sql-Date", object, document); } else if (object instanceof java.sql.Time) { return makeElement("sql-Time", object, document); } else if (object instanceof java.util.Date) { // NOTE: make sure this is AFTER the java.sql date/time objects since they inherit from java.util.Date DateFormat formatter = getDateFormat(); String stringValue = null; synchronized (formatter) { stringValue = formatter.format((java.util.Date) object); } return makeElement("std-Date", stringValue, document); // return makeElement("std-Date", object, document); } else if (object instanceof Collection) { // - Collections - String elementName = null; // these ARE order sensitive; for instance Stack extends Vector, so if Vector were first we would lose the stack part if (object instanceof ArrayList) { elementName = "col-ArrayList"; } else if (object instanceof LinkedList) { elementName = "col-LinkedList"; } else if (object instanceof Stack) { elementName = "col-Stack"; } else if (object instanceof Vector) { elementName = "col-Vector"; } else if (object instanceof TreeSet) { elementName = "col-TreeSet"; } else if (object instanceof HashSet) { elementName = "col-HashSet"; } else { // no specific type found, do general Collection, will deserialize as LinkedList elementName = "col-Collection"; } // if (elementName == null) return serializeCustom(object, document); Collection value = (Collection) object; Element element = document.createElement(elementName); Iterator iter = value.iterator(); while (iter.hasNext()) { element.appendChild(serializeSingle(iter.next(), document)); } return element; } else if (object instanceof GenericPK) { // Do GenericEntity objects as a special case, use std XML import/export routines GenericPK value = (GenericPK) object; return value.makeXmlElement(document, "eepk-"); } else if (object instanceof GenericValue) { GenericValue value = (GenericValue) object; return value.makeXmlElement(document, "eeval-"); } else if (object instanceof Map) { // - Maps - String elementName = null; // these ARE order sensitive; for instance Properties extends Hashtable, so if Hashtable were first we would lose the Properties part if (object instanceof HashMap) { elementName = "map-HashMap"; } else if (object instanceof Properties) { elementName = "map-Properties"; } else if (object instanceof Hashtable) { elementName = "map-Hashtable"; } else if (object instanceof WeakHashMap) { elementName = "map-WeakHashMap"; } else if (object instanceof TreeMap) { elementName = "map-TreeMap"; } else { // serialize as a simple Map implementation if nothing else applies, these will deserialize as a HashMap elementName = "map-Map"; } Element element = document.createElement(elementName); Map value = (Map) object; Iterator iter = value.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Element entryElement = document.createElement("map-Entry"); element.appendChild(entryElement); Element key = document.createElement("map-Key"); entryElement.appendChild(key); key.appendChild(serializeSingle(entry.getKey(), document)); Element mapValue = document.createElement("map-Value"); entryElement.appendChild(mapValue); mapValue.appendChild(serializeSingle(entry.getValue(), document)); } return element; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -