XmlUtil



import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.w3c.dom.traversal.NodeIterator;


import com.sun.org.apache.xpath.internal.XPathAPI;


public class XmlUtil {

private static Log logger = LogFactory.getLog(XmlUtil.class);


private static javax.xml.transform.TransformerFactory transformerFactory = javax.xml.transform.TransformerFactory.newInstance();


private static Map<String, Object> transformerTemplates = new HashMap<String, Object>();


private static boolean isTemplatesCacheOn = true;


private static boolean isValidation = false;


private static String[] urlProtocols = new String[] {
"http://", "file://", "ftp://", "https://"
};


protected XmlUtil() {}


public static void setTemplateCache(boolean cacheOn) {
isTemplatesCacheOn = cacheOn;
}


public static void setValidation(boolean validationOn) {
isValidation = validationOn;
}


public static Document stringToDocument(String xmlString) throws Exception {
return(createDocument(xmlString));
}


public static Document createDocument(String xmlString) throws Exception {
return(createDocument(new org.xml.sax.InputSource(new StringReader(xmlString))));
}


public static Document createDocument(InputStream xmlInputStream) throws Exception {
return(createDocument(new org.xml.sax.InputSource(xmlInputStream)));
}


public static Document createDocument(InputStream xmlInputStream, String encoding) throws Exception {
org.xml.sax.InputSource src = new org.xml.sax.InputSource(xmlInputStream);
if (encoding != null) src.setEncoding(encoding);
return(createDocument(src));
}


public static Document createDocument(Reader xmlReader) throws Exception {
return(createDocument(new org.xml.sax.InputSource(xmlReader)));
}


public static Document createDocument(org.xml.sax.InputSource xmlInputSource) throws Exception {
try {
Document doc = null;
javax.xml.parsers.DocumentBuilderFactory fact = javax.xml.parsers.DocumentBuilderFactory.newInstance();
fact.setValidating(isValidation);
doc = fact.newDocumentBuilder().parse(xmlInputSource);
return(doc);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static Document createDocument(String docTypeName, String systemID) throws Exception {
try {
javax.xml.parsers.DocumentBuilderFactory fact = javax.xml.parsers.DocumentBuilderFactory.newInstance();
DOMImplementation domImpl = fact.newDocumentBuilder().getDOMImplementation();
DocumentType docType = domImpl.createDocumentType(docTypeName, null, systemID);
Document doc = domImpl.createDocument(null, docTypeName, docType);
return(doc);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static Document createDocument() throws Exception {
try {
javax.xml.parsers.DocumentBuilderFactory fact = javax.xml.parsers.DocumentBuilderFactory.newInstance();
Document doc = null;
doc = fact.newDocumentBuilder().newDocument();
return(doc);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static String serializeDocument(Document doc) throws Exception {
try {
return(serializeNode(doc));
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static String serializeNode(Node node) throws Exception {
try {
return(transformToString(node, null));
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static String serializeNode(Node node, boolean includeXmlHeader) throws Exception {
try {
if (includeXmlHeader) {
return(transformToString(node, null));
} else {
javax.xml.transform.Transformer transformer = createTransformer(null);
transformer.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
javax.xml.transform.Source source = new javax.xml.transform.dom.DOMSource(node);
javax.xml.transform.Result result = new javax.xml.transform.stream.StreamResult(writer);
transformer.transform(source, result);
writer.flush();
return(writer.getBuffer().toString());
}
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void streamToNode(InputStream xmlStream, Node holderNode) throws Exception {
javax.xml.transform.Result result = new javax.xml.transform.dom.DOMResult(holderNode);
transformToResult(xmlStream, null, result);
}


public static void readerToNode(Reader xmlReader, Node holderNode) throws Exception {
javax.xml.transform.Result result = new javax.xml.transform.dom.DOMResult(holderNode);
transformToResult(xmlReader, null, result);
}


public static void stringToNode(String xmlString, Node holderNode) throws Exception {
javax.xml.transform.Result result = new javax.xml.transform.dom.DOMResult(holderNode);
transformToResult(xmlString, null, result);
}


public static String nodeToString(Node node) throws Exception {
return(serializeNode(node));
}


public static String nodeToString(Node node, boolean includeXmlHeader) throws Exception {
return(serializeNode(node, includeXmlHeader));
}


public static String documentToString(Document doc) throws Exception {
return(serializeDocument(doc));
}


public static Node createChildPath(Node parent, StringTokenizer childPathToken) throws Exception {
if (parent == null || childPathToken == null) return(null);
Document doc = parent.getOwnerDocument();
if (doc == null) doc = (Document) parent;
while (childPathToken.hasMoreTokens()) {
String name = childPathToken.nextToken();
if (name.length() == 0) continue;
Node nod = selectSingleNode(parent, name);
if (nod == null) {
nod = doc.createElement(name);
parent.appendChild(nod);
}
parent = nod;
}
return(parent);
}


public static Node createChildPath(Node parent, String childPath) throws Exception {
if (parent == null || childPath == null || childPath.length() == 0) return(null);
return(createChildPath(parent, childPath, (String)null));
}


public static Node createChildPath(Node parent, String childPath, String pathSeparator) throws Exception {
if (parent == null || childPath == null || childPath.length() == 0) return(null);
if (pathSeparator == null) pathSeparator = "/";
StringTokenizer tok = new StringTokenizer(childPath, pathSeparator);
return(createChildPath(parent, tok));
}


public static Element addSingleElement(Node contextNode, String elmName) throws Exception {
Element elm = contextNode.getOwnerDocument().createElement(elmName);
contextNode.appendChild(elm);
return(elm);
}


public static Element addSingleElement(Node contextNode, String elmName, String elmValue) throws Exception {
Element elm = addSingleElement(contextNode, elmName);
if (elmValue != null) elm.appendChild(contextNode.getOwnerDocument().createTextNode(elmValue));
return(elm);
}


public static void addElements(Node contextNode, String elmName, String elmValues[]) throws Exception {
if (elmValues == null) return;
for (int i = 0; i < elmValues.length; ++i)
addSingleElement(contextNode, elmName, elmValues[i]);
}


public static void addElements(Node contextNode, String elmName, List<Object> elmValues) throws Exception {
if (elmValues == null) return;
for (int i = 0; i < elmValues.size(); ++i)
addSingleElement(contextNode, elmName, (String) elmValues.get(i));
}


public static void removeNodes(Node contextNode, String childPath) throws Exception {
NodeList nl = selectNodeList(contextNode, childPath);
if (nl != null && nl.getLength() > 0) {
Node pn = nl.item(0).getParentNode();
for (int i = 0; i < nl.getLength(); ++i) pn.removeChild(nl.item(i));
}
}


public static void removeNode(Node contextNode, String childPath) throws Exception {
Node n = selectSingleNode(contextNode, childPath);
if (n != null) n.getParentNode().removeChild(n);
}


public static Text setElementValue(Element elm, String value) throws Exception {
Text valNode = (Text)selectSingleNode(elm, "text()");
if (valNode != null) {
if (value != null) {
valNode.setNodeValue(value);
} else {
elm.removeChild(valNode);
valNode = null;
}
} else if (value != null) {
valNode = elm.getOwnerDocument().createTextNode(value);
elm.appendChild(valNode);
}
return(valNode);
}


public static void setElementValue(Element elm, String[] values) throws Exception {
NodeList valNodes = selectNodeList(elm.getParentNode(), elm.getNodeName());
int i = 0;
while (i < valNodes.getLength() && i < values.length) {
setElementValue((Element)(valNodes.item(i)), values[i]);
++i;
}
if (i == valNodes.getLength()) {
Document doc = elm.getOwnerDocument();
Node parent = elm.getParentNode();
String elmName = elm.getNodeName();
while (i < values.length) {
elm = doc.createElement(elmName);
parent.appendChild(elm);
elm.appendChild(doc.createTextNode(values[i]));
++i;
}
} else {
Node parent = elm.getParentNode();
while (i < valNodes.getLength()) {
parent.removeChild(valNodes.item(i));
++i;
}
}
}


public static void setElementValue(Element elm, List<Object> values) throws Exception {
String[] strValues = new String[values.size()];
values.toArray(strValues);
setElementValue(elm, strValues);
}


public static Element setElementValue(Node contextNode, String elmName, String value) throws Exception {
Element elm = (Element)selectSingleNode(contextNode, elmName);
if (elm == null) elm = (Element)createChildPath(contextNode, elmName);
setElementValue(elm, value);
return(elm);
}


public static void setElementValue(Node contextNode, String elmName, String[] values) throws Exception {
Element elm = (Element)selectSingleNode(contextNode, elmName);
if (elm == null) elm = (Element)createChildPath(contextNode, elmName);
setElementValue(elm, values);
}


public static void setElementValue(Node contextNode, String elmName, List<Object> values) throws Exception {
Element elm = (Element)selectSingleNode(contextNode, elmName);
if (elm == null) elm = (Element)createChildPath(contextNode, elmName);
setElementValue(elm, values);
}


public static NodeList selectNodeList(Node contextNode, String childNodePath) throws Exception {
return(XPathAPI.selectNodeList(contextNode, childNodePath));
}


public static NodeList selectNodeList(Node contextNode, String childNodePath, Node namespaceNode) throws Exception {
return(XPathAPI.selectNodeList(contextNode, childNodePath, namespaceNode));
}


public static Node selectSingleNode(Node contextNode, String childNodePath) throws Exception {
return(XPathAPI.selectSingleNode(contextNode, childNodePath));
}


public static Node selectSingleNode(Node contextNode, String childNodePath, Node namespaceNode) throws Exception {
return(XPathAPI.selectSingleNode(contextNode, childNodePath, namespaceNode));
}


public static NodeIterator selectNodeIterator(Node contextNode, String childNodePath) throws Exception {
return(XPathAPI.selectNodeIterator(contextNode, childNodePath));
}


public static NodeIterator selectNodeIterator(Node contextNode, String childNodePath, Node namespaceNode) throws Exception {
return(XPathAPI.selectNodeIterator(contextNode, childNodePath, namespaceNode));
}


public static boolean doesNodeExist(Node contextNode, String childNodePath) {
try {
return(selectSingleNode(contextNode, childNodePath) != null);
} catch (Throwable e) {
logger.error("error", e);
return(false);
}
}

public static boolean doesNodeExist(Node contextNode, String childNodePath, Node namespaceNode) {
try {
return(selectSingleNode(contextNode, childNodePath, namespaceNode) != null);
} catch (Throwable e) {
logger.error("error", e);
return(false);
}
}


public static String getSingleValue(Node contextNode, String elmPath) {
if (contextNode == null || elmPath == null) return(null);
try {
Node nd = selectSingleNode(contextNode, elmPath + "/text()");
return(nd == null ? null : nd.getNodeValue());
} catch (Throwable e) {
logger.error("error", e);
return(null);
}
}


public static List<String> getValues(Node contextNode, String elmPath) {
if (contextNode == null || elmPath == null) return(null);
List<String> values = new ArrayList<String>();
try {
NodeList nodeList = selectNodeList(contextNode, elmPath);
if (nodeList != null) {
for (int i = 0; i < nodeList.getLength(); ++i) {
Node nd = selectSingleNode(nodeList.item(i), "text()");
if (nd != null) {
String val = nd.getNodeValue();
if (val != null) values.add(val);
}
}
}
return(values);
} catch (Throwable e) {
logger.error("error", e);
return(null);
}
}


public static String getAttributeValue(Node contextNode, String childNodePath, String attrName) {
if (contextNode == null || childNodePath == null || attrName == null) return(null);
try {
Element nd = (Element)selectSingleNode(contextNode, childNodePath);
return(nd == null ? null : nd.getAttribute(attrName));
} catch (Throwable e) {
logger.error("error", e);
return(null);
}
}


public static void setAttributeValue(Element elm, String attrName, String attrValue) throws Exception {
if (elm == null || attrName == null) return;
try {
if (attrValue == null) {
elm.removeAttribute(attrName);
return;
}
elm.setAttribute(attrName, attrValue);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void setAttributeValue(Node contextNode, String childNodePath, String attrName, String attrValue) throws Exception {
if (contextNode == null || childNodePath == null || attrName == null) return;
try {
if (attrValue == null) {
removeAttribute(contextNode, childNodePath, attrName);
return;
}
Element elm = (Element)selectSingleNode(contextNode, childNodePath);
if (elm == null) elm = (Element)createChildPath(contextNode, childNodePath);
elm.setAttribute(attrName, attrValue);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void removeAttribute(Node contextNode, String childNodePath, String attrName) throws Exception {
if (contextNode == null || childNodePath == null || attrName == null) return;
try {
Element nd = (Element)selectSingleNode(contextNode, childNodePath);
if (nd != null) nd.removeAttribute(attrName);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void removeAttribute(Node node, String attrName) throws Exception {
if (node == null || attrName == null) return;
try {
((Element)node).removeAttribute(attrName);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static Document transformToDOM(String xmlString, String xslSystemID) throws Exception {
try {
Document doc = createDocument();
transformToResult(xmlString, xslSystemID, new javax.xml.transform.dom.DOMResult(doc));
return(doc);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static Document transformToDOM(InputStream xmlStream, String xslSystemID) throws Exception {
try {
Document doc = createDocument();
transformToResult(xmlStream, xslSystemID, new javax.xml.transform.dom.DOMResult(doc));
return(doc);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static Document transformToDOM(Reader xmlReader, String xslSystemID) throws Exception {
try {
Document doc = createDocument();
transformToResult(xmlReader, xslSystemID, new javax.xml.transform.dom.DOMResult(doc));
return(doc);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static Document transformToDOM(Node xmlNode, String xslSystemID) throws Exception {
try {
Document doc = createDocument();
transformToResult(xmlNode, xslSystemID, new javax.xml.transform.dom.DOMResult(doc));
return(doc);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void transformToWriter(String xmlString, String xslSystemID, Writer outputWriter) throws Exception {
try {
transformToResult(xmlString, xslSystemID, new javax.xml.transform.stream.StreamResult(outputWriter));
outputWriter.flush();
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void transformToWriter(InputStream xmlStream, String xslSystemID, Writer outputWriter) throws Exception {
try {
transformToResult(xmlStream, xslSystemID, new javax.xml.transform.stream.StreamResult(outputWriter));
outputWriter.flush();
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void transformToWriter(Reader xmlReader, String xslSystemID, Writer outputWriter) throws Exception {
try {
transformToResult(xmlReader, xslSystemID, new javax.xml.transform.stream.StreamResult(outputWriter));
outputWriter.flush();
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void transformToWriter(Node xmlNode, String xslSystemID, Writer outputWriter) throws Exception {
try {
transformToResult(xmlNode, xslSystemID, new javax.xml.transform.stream.StreamResult(outputWriter));
outputWriter.flush();
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void transformToStream(String xmlString, String xslSystemID, OutputStream outputStream) throws Exception {
try {
transformToResult(xmlString, xslSystemID, new javax.xml.transform.stream.StreamResult(outputStream));
outputStream.flush();
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void transformToStream(InputStream xmlStream, String xslSystemID, OutputStream outputStream) throws Exception {
try {
transformToResult(xmlStream, xslSystemID, new javax.xml.transform.stream.StreamResult(outputStream));
outputStream.flush();
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void transformToStream(Reader xmlReader, String xslSystemID, OutputStream outputStream) throws Exception {
try {
transformToResult(xmlReader, xslSystemID, new javax.xml.transform.stream.StreamResult(outputStream));
outputStream.flush();
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static void transformToStream(Node xmlNode, String xslSystemID, OutputStream outputStream) throws Exception {
try {
transformToResult(xmlNode, xslSystemID, new javax.xml.transform.stream.StreamResult(outputStream));
outputStream.flush();
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static String transformToString(String xmlString, String xslSystemID) throws Exception {
try {
StringWriter writer = new StringWriter();
transformToWriter(xmlString, xslSystemID, writer);
return(writer.getBuffer().toString());
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static String transformToString(InputStream xmlStream, String xslSystemID) throws Exception {
try {
StringWriter writer = new StringWriter();
transformToWriter(xmlStream, xslSystemID, writer);
return(writer.getBuffer().toString());
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static String transformToString(Reader xmlReader, String xslSystemID) throws Exception {
try {
StringWriter writer = new StringWriter();
transformToWriter(xmlReader, xslSystemID, writer);
return(writer.getBuffer().toString());
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


public static String transformToString(Node xmlNode, String xslSystemID) throws Exception {
try {
StringWriter writer = new StringWriter();
transformToWriter(xmlNode, xslSystemID, writer);
return(writer.getBuffer().toString());
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


private static void transformToResult(String xmlString, String xslSystemID, javax.xml.transform.Result result) throws Exception {
transformToResult((new javax.xml.transform.stream.StreamSource(new StringReader(xmlString))), xslSystemID, result);
}


private static void transformToResult(InputStream xmlStream, String xslSystemID, javax.xml.transform.Result result) throws Exception {
transformToResult((new javax.xml.transform.stream.StreamSource(xmlStream)), xslSystemID, result);
}


private static void transformToResult(Reader xmlReader, String xslSystemID, javax.xml.transform.Result result) throws Exception {
transformToResult((new javax.xml.transform.stream.StreamSource(xmlReader)), xslSystemID, result);
}


private static void transformToResult(Node xmlNode, String xslSystemID, javax.xml.transform.Result result) throws Exception {
transformToResult((new javax.xml.transform.dom.DOMSource(xmlNode)), xslSystemID, result);
}

private static void transformToResult(javax.xml.transform.Source source, String xslSystemID, javax.xml.transform.Result result) throws Exception {
try {
javax.xml.transform.Transformer transformer = createTransformer(xslSystemID);
transformer.transform(source, result);
} catch (Exception e) {
logger.error("error", e);
throw e;
}
}


private static javax.xml.transform.Transformer createTransformer(String xslSystemID) throws Exception {
if (xslSystemID == null) {
return(transformerFactory.newTransformer());
}
javax.xml.transform.Templates templates = (javax.xml.transform.Templates)transformerTemplates.get(xslSystemID);
if (templates == null) {
if (logger.isDebugEnabled()) logger.debug("getting XSL source for " + xslSystemID + " ...");
javax.xml.transform.stream.StreamSource xslSrc = null;
InputStream xslIS = null;
if (isValidUrl(xslSystemID)) {
xslSrc = new javax.xml.transform.stream.StreamSource(xslSystemID);
} else {
ClassLoader clsLoader = XmlUtil.class.getClassLoader();
if (clsLoader == null) xslIS = ClassLoader.getSystemResourceAsStream(xslSystemID);
else xslIS = clsLoader.getResourceAsStream(xslSystemID);
xslSrc = new javax.xml.transform.stream.StreamSource(xslIS);
}
if (logger.isDebugEnabled()) logger.debug("getting XSL template for " + xslSystemID + " ...");
synchronized(transformerFactory) {
templates = transformerFactory.newTemplates(xslSrc);
}
if (isTemplatesCacheOn) {
synchronized(transformerTemplates) {
Object tmp = transformerTemplates.get(xslSystemID);
if (tmp == null) {
transformerTemplates.put(xslSystemID, templates);
if (logger.isDebugEnabled()) logger.debug("XSL template " + xslSystemID + " is added into cache");
} else {
templates = (javax.xml.transform.Templates)tmp;
}
}
}
if (xslIS != null) try { xslIS.close(); } catch (Exception e) {}
}
return(templates.newTransformer());
}


protected static boolean isValidUrl(String str) {
if (str == null) return(false);
for (int i = 0; i < urlProtocols.length; ++i)
if (str.startsWith(urlProtocols[i])) return(true);
return(false);
}

public static Map<String, Object> parseParams(String strParams) throws Exception {
return(parseProperties(strParams, "param"));
}


public static Map<String, Object> parseParameters(String strParameters) throws Exception {
return(parseProperties(strParameters, "parameter"));
}


public static Map<String, Object> parseProps(String strProps) throws Exception {
return(parseProperties(strProps, "prop"));
}


public static Map<String, Object> parseProperties(String strProperties) throws Exception {
return(parseProperties(strProperties, "property"));
}


public static Map<String, Object> parseProperties(String strProps, String tagName) throws Exception {
if (strProps == null || tagName == null || strProps.length() == 0) return(null);
Document doc = XmlUtil.stringToDocument(strProps);
if (doc == null) return(null);
Element docElm = doc.getDocumentElement();
return(parseProperties(docElm, tagName));
}


public static Map<String, Object> parseProperties(Element ctx, String tagName) throws Exception {
if (ctx == null || tagName == null) return(null);
NodeList nl = XmlUtil.selectNodeList(ctx, tagName);
if (nl == null || nl.getLength() == 0) return(null);


String typeClass = ctx.getAttribute("type");
if (typeClass != null && typeClass.length() == 0) typeClass = null;
String format = ctx.getAttribute("format");
if (format != null && format.length() == 0) format = null;

Map<String, Object> props = new HashMap<String, Object>();
for (int i = 0; i < nl.getLength(); ++i) {
Element propElm = (Element) nl.item(i);
String propName = propElm.getAttribute("name");
if (propName != null && propName.length() > 0) {
Object propValue = XmlUtil.getSingleValue(propElm, ".");
if (propValue == null) propValue = "";
String propTypeClass = propElm.getAttribute("type");
if (propTypeClass == null || propTypeClass.length() == 0) propTypeClass = typeClass;
String propFormat = propElm.getAttribute("format");
if (propFormat == null || propFormat.length() == 0) propFormat = format;
if (propTypeClass != null) propValue = parseStringToObject((String) propValue, propTypeClass, propFormat);
props.put(propName, propValue);
}
}
if (props.size() == 0) props = null;
return(props);
}


public static Object parseStringToObject(String strValue, String valueType, String format) throws Exception {
if ("java.util.Date".equals(valueType)) {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
return(fmt.parse(strValue));
} else if ("java.sql.Date".equals(valueType)) {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
return(new java.sql.Date(fmt.parse(strValue).getTime()));
} else if ("java.sql.Timestamp".equals(valueType)) {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return(new java.sql.Timestamp(fmt.parse(strValue).getTime()));
} else if ("java.sql.Time".equals(valueType)) {
DateFormat fmt = new SimpleDateFormat("HH:mm:ss");
return(new java.sql.Time(fmt.parse(strValue).getTime()));
} else if ("java.lang.Boolean".equals(valueType)) {
return(Boolean.valueOf(strValue));
} else if ("java.lang.Double".equals(valueType)) {
return(Double.valueOf(strValue));
} else if ("java.lang.Long".equals(valueType)) {
return(Long.valueOf(strValue));
} else if ("java.lang.Short".equals(valueType)) {
return(Short.valueOf(strValue));
} else if ("java.lang.Integer".equals(valueType)) {
return(Integer.valueOf(strValue));
} else if ("java.lang.Byte".equals(valueType)) {
return(Byte.valueOf(strValue));
} else if ("java.lang.Float".equals(valueType)) {
return(Float.valueOf(strValue));
} else if ("java.math.BigInteger".equals(valueType)) {
return(new java.math.BigInteger(strValue));
} else if ("java.math.BigDecimal".equals(valueType)) {
return(new java.math.BigDecimal(strValue));
} else {
return(strValue);
}
}


public static Map<String, Object> parseDeepProperties(String strProps, String tagName) throws Exception {
if (strProps == null || tagName == null || strProps.length() == 0) return(null);
Document doc = XmlUtil.stringToDocument(strProps);
if (doc == null) return(null);
Element docElm = doc.getDocumentElement();
return(parseDeepProperties(docElm, tagName));
}


public static Map<String, Object> parseDeepProperties(Element ctx, String tagName) throws Exception {
if (ctx == null || tagName == null) return(null);
NodeList nl = XmlUtil.selectNodeList(ctx, tagName);
if (nl == null || nl.getLength() == 0) return(null);

String typeClass = ctx.getAttribute("type");
if (typeClass != null && typeClass.length() == 0) typeClass = null;
String format = ctx.getAttribute("format");
if (format != null && format.length() == 0) format = null;

Map<String, Object> props = new HashMap<String, Object>();
for (int i = 0; i < nl.getLength(); ++i) {
Element propElm = (Element) nl.item(i);
String propName = propElm.getAttribute("name");
if (propName != null && propName.length() > 0) {
Object val = parseDeepProperties(propElm, tagName);
if (val == null) {
val = XmlUtil.getSingleValue(propElm, ".");
if (val == null) val = "";
String propTypeClass = propElm.getAttribute("type");
if (propTypeClass == null || propTypeClass.length() == 0) propTypeClass = typeClass;
String propFormat = propElm.getAttribute("format");
if (propFormat == null || propFormat.length() == 0) propFormat = format;
if (propTypeClass != null) val = parseStringToObject((String) val, propTypeClass, propFormat);
}
Object oldVal = props.get(propName);
if (oldVal == null) {
props.put(propName, val);
} else {
if (oldVal instanceof ArrayList) {
List<Object> arrayList = Arrays.asList(oldVal);
arrayList.add(val);
} else {
List<Object> lv = new ArrayList<Object>();
lv.add(oldVal);
lv.add(val);
props.put(propName, lv);
}
}
}
}
return(props.size() > 0 ? props : null);
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值