/*
* Copyright (C) 2006 http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.chaidb.db.helper;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
/**
* @author justin
* XmlUtils
* Description:
*/
public class XmlUtils {
public static Document newDocument() {
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return db.newDocument();
} catch (Exception e) {
return null;
}
}
/**
* Because different DOM implementation can not perform importion, I write
* this utils to import node by copy the node to desination
*
* @param parent Target
* @param nd source
* @return
*/
public static Node importNodeByCopy(Element parent, Node nd) {
Node ret = null;
if (nd == null) return null;
Document doc = parent.getOwnerDocument();
String ns = nd.getNamespaceURI();
switch (nd.getNodeType()) {
case Node.ELEMENT_NODE: {
Element ele = null;
if (ns != null) {
ele = doc.createElementNS(ns, nd.getNodeName());
Attr attr = null;
if (nd.getPrefix() != null && nd.getPrefix().length() > 0) {
attr = doc.createAttributeNS(ns, "xmlns:" + nd.getPrefix());
attr.setNodeValue(ns);
} else {
attr = doc.createAttribute(ns);
attr.setNodeValue(ns);
}
if (attr != null) {
ele.setAttributeNode(attr);
}
} else {
ele = doc.createElement(nd.getNodeName());
}
NodeList list = nd.getChildNodes();
if (list.getLength() > 0) {
for (int i = 0; i < list.getLength(); i++) {
importNodeByCopy(ele, list.item(i));
}
}
ret = ele;
}
break;
case Node.ATTRIBUTE_NODE: {
Attr src = (Attr) nd;
Attr attr = null;
if (ns != null) {
attr = doc.createAttributeNS(src.getNamespaceURI(), src
.getName());
} else {
attr = doc.createAttribute(src.getName());
}
attr.setValue(src.getValue());
ret = attr;
}
break;
case Node.CDATA_SECTION_NODE: {
CDATASection dss = (CDATASection) nd;
CDATASection ds = doc.createCDATASection(dss.getData());
ret = ds;
}
break;
case Node.COMMENT_NODE: {
Comment cs = (Comment) nd;
Comment c = doc.createComment(cs.getData());
ret = c;
}
break;
case Node.TEXT_NODE: {
Text ts = (Text) nd;
Text t = doc.createTextNode(ts.getData());
ret = t;
}
break;
default:
throw new UnsupportedOperationException();
}
parent.appendChild(ret);
return ret;
}
public static String getAttributeValue(Node node, String sAttributeName) {
try {
return node.getAttributes().getNamedItem(sAttributeName)
.getNodeValue();
} catch (Exception e) {
return "";
}
}
/**
* Return the text value of an node. Return emtpy string otherwise.
*/
public static String getTextValue(Node node) {
if (node == null) {
return "";
}
Node c = node.getFirstChild();
if (c == null) {
return "";
}
if (c.getNodeType() == Node.TEXT_NODE) {
return c.getNodeValue().trim();
}
return "";
}
}