001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui;
023:
024: import java.io.StringWriter;
025:
026: import org.apache.xml.serialize.OutputFormat;
027: import org.apache.xml.serialize.XMLSerializer;
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032:
033: /**
034: * Miscellaneous XML utility functions
035: */
036: public class XMLUtils {
037: /**
038: * Extract all text children of an element
039: */
040: public static String extractTextChildren(Element parentNode) {
041: NodeList childNodes = parentNode.getChildNodes();
042: String result = new String();
043: for (int i = 0; i < childNodes.getLength(); i++) {
044: Node node = childNodes.item(i);
045: if (node.getNodeType() == Node.TEXT_NODE) {
046: result += node.getNodeValue();
047: }
048: }
049: return result;
050: }
051:
052: /**
053: * Get a child element
054: */
055: public static Element getChild(Element parentNode, String nodeName) {
056: NodeList childNodes = parentNode.getChildNodes();
057:
058: for (int i = 0; i < childNodes.getLength(); i++) {
059: Node node = childNodes.item(i);
060: if (node.getNodeType() == Node.ELEMENT_NODE
061: && nodeName.equals(node.getNodeName())) {
062: return (Element) node;
063: }
064: }
065: return null;
066: }
067:
068: /**
069: * Get the text content of a child element
070: */
071: public static String getStringFromChild(Element parentNode,
072: String nodeName) {
073: Element child = getChild(parentNode, nodeName);
074: if (child == null)
075: return null;
076: else
077: return extractTextChildren(child);
078: }
079:
080: /**
081: * String replacement utility function
082: */
083: public static String replace(String string, String from, String to) {
084: if (string.indexOf(from) > -1) {
085: StringBuffer sb = new StringBuffer();
086: int ix = -1;
087: while ((ix = string.indexOf((from))) >= 0) {
088: sb.append(string.substring(0, ix)).append(to);
089: string = string.substring(ix + from.length());
090: }
091: if (string.length() > 1)
092: sb.append(string);
093: return sb.toString();
094: } else {
095: return string;
096: }
097: }
098:
099: /**
100: * Convert an XML document to a latin-1 string
101: */
102: public static String serializeXML(Document document)
103: throws GUIException {
104: try {
105: StringWriter stringWriter = new StringWriter();
106: OutputFormat format = new OutputFormat("XML", "ISO-8859-1",
107: true);
108: format.setLineWidth(0);
109: XMLSerializer serializer = new XMLSerializer(stringWriter,
110: format);
111: serializer.asDOMSerializer().serialize(document);
112: /* We don't want the newline character at the end */
113: String string = stringWriter.toString();
114: return string.substring(0, string.length() - 1);
115: } catch (Exception e) {
116: throw new GUIException("Could not serialize XML", e);
117: }
118: }
119:
120: }
|