001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util.xml;
019:
020: import java.io.StringWriter;
021:
022: import org.w3c.dom.DOMImplementation;
023: import org.w3c.dom.Document;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.NodeList;
027: import org.w3c.dom.ls.DOMImplementationLS;
028: import org.w3c.dom.ls.LSOutput;
029: import org.w3c.dom.ls.LSSerializer;
030:
031: final public class XMLUtil {
032:
033: public static String getOptionalElementValue(Element element,
034: String tag) {
035: if (element.getElementsByTagName(tag).getLength() <= 0)
036: return "";
037: if (element.getElementsByTagName(tag).item(0).getFirstChild() != null) {
038: return element.getElementsByTagName(tag).item(0)
039: .getFirstChild().getNodeValue();
040: } else {
041: return "";
042: }
043: }
044:
045: public static String getElementValue(Element element, String tag) {
046: if (element.getElementsByTagName(tag) != null
047: && element.getElementsByTagName(tag).item(0) != null
048: && element.getElementsByTagName(tag).item(0)
049: .getFirstChild() != null) {
050: return element.getElementsByTagName(tag).item(0)
051: .getFirstChild().getNodeValue();
052: } else {
053: // exception??
054: return null;
055: }
056: }
057:
058: public static String getOptionalAttributeValue(Node node,
059: String attribute) {
060: if (node.getAttributes().getNamedItem(attribute) == null) {
061: return "";
062: } else {
063: return node.getAttributes().getNamedItem(attribute)
064: .getNodeValue();
065: }
066: }
067:
068: public static String getOptionalAttributeValue(Node node,
069: String attribute, String defaultValue) {
070: if (node.getAttributes().getNamedItem(attribute) == null) {
071: return defaultValue;
072: } else {
073: return node.getAttributes().getNamedItem(attribute)
074: .getNodeValue();
075: }
076: }
077:
078: public static String getAttributeValue(Node node, String attribute) {
079: if (node.getAttributes().getNamedItem(attribute) == null) {
080: return null;
081: } else {
082: return node.getAttributes().getNamedItem(attribute)
083: .getNodeValue();
084: }
085: }
086:
087: public static String getContent(Node node) {
088: StringBuffer valBuf = new StringBuffer();
089: if (node != null) {
090: NodeList nodeList = node.getChildNodes();
091: for (int a = 0; a < nodeList.getLength(); a++) {
092: valBuf.append(nodeList.item(a).getNodeValue());
093: }
094: }
095: return valBuf.toString();
096: }
097:
098: public static boolean tagExists(Element element, String tag) {
099: if (element.getElementsByTagName(tag) == null)
100: return false;
101: if (element.getElementsByTagName(tag).getLength() <= 0)
102: return false;
103: return true;
104: }
105:
106: /**
107: * Prints a whole Document.
108: *
109: * @param xmlDocument
110: *
111: * @return
112: */
113: public final static String documentToString(Document xmlDocument) {
114:
115: DOMImplementation domImpl = xmlDocument.getImplementation();
116: DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl
117: .getFeature("LS", "3.0");
118:
119: LSSerializer serializer = domImplLS.createLSSerializer();
120: // Doesn't work properly:
121: //serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
122: LSOutput serializerOut = domImplLS.createLSOutput();
123:
124: StringWriter stringWriter = new StringWriter();
125: serializerOut.setCharacterStream(stringWriter);
126: serializer.write(xmlDocument, serializerOut);
127:
128: return stringWriter.toString();
129: }
130: }
|