001: /*
002: * $Id: XMLUtils.java,v 1.6 2006/04/30 22:25:56 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/utils/XMLUtils.java,v $
004: * SQLUnit - a test harness for unit testing database stored procedures.
005: * Copyright (C) 2003 The SQLUnit Team
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: package net.sourceforge.sqlunit.utils;
022:
023: import net.sourceforge.sqlunit.HandlerFactory;
024: import net.sourceforge.sqlunit.IHandler;
025: import net.sourceforge.sqlunit.SymbolTable;
026:
027: import org.apache.log4j.Logger;
028: import org.jdom.Element;
029: import org.jdom.output.XMLOutputter;
030: import org.jdom.output.Format;
031:
032: import java.io.ByteArrayOutputStream;
033: import java.io.IOException;
034:
035: /**
036: * Provides commonly used functionality for methods which have to deal
037: * with JDOM elements.
038: * @author Sujit Pal (spal@users.sourceforge.net)
039: * @version $Revision: 1.6 $
040: */
041: public final class XMLUtils {
042:
043: private static final Logger LOG = Logger.getLogger(XMLUtils.class);
044:
045: /**
046: * Private Constructor. Cannot be instantiated.
047: */
048: private XMLUtils() {
049: // private constructor, cannot instantiate
050: }
051:
052: /**
053: * Wrapper over Element.getAttributeValue(attributeName) which tests
054: * to see if the attributeName is a variable, if so, does another
055: * lookup of the symbol table to return the value of the variable.
056: * @param element the JDOM Element.
057: * @param attribute the name of the attribute.
058: * @return the value of the attribute, or the value for the variable
059: * stored in the SymbolTable if the value is a variable.
060: */
061: public static String getAttributeValue(final Element element,
062: final String attribute) {
063: LOG.debug(">> getAttributeValue(Element " + element.getName()
064: + "," + attribute + ")");
065: String value = element.getAttributeValue(attribute);
066: if (SymbolTable.isVariableName(value)) {
067: String symValue = SymbolTable.getValue(value);
068: if (symValue == null) {
069: return value;
070: } else {
071: return symValue;
072: }
073: } else {
074: return value;
075: }
076: }
077:
078: /**
079: * Wrapper over Element.getText() which tests to see if the body text
080: * returned is a variable, if so, does another lookup of the symbol
081: * table to return the value of the variable.
082: * @param element the JDOM Element.
083: * @return the value of the body text, or the value for the variable
084: * stored in the SymbolTable if the value is a variable.
085: */
086: public static String getText(final Element element) {
087: LOG.debug(">> getText(Element "
088: + (element == null ? "NULL" : element.getName()) + ")");
089: if (element == null) {
090: return null;
091: }
092: String text = element.getText();
093: if (SymbolTable.isVariableName(text)) {
094: String symValue = SymbolTable.getValue(text);
095: if (symValue != null) {
096: return symValue;
097: } else {
098: return text;
099: }
100: } else {
101: return text;
102: }
103: }
104:
105: /**
106: * Converts the Element to a formatted XML String.
107: * @param element the JDOM Element.
108: * @return a formatted XML String representing the Element.
109: */
110: public static String toXMLString(final Element element) {
111: LOG.debug(">> toXMLString(Element " + element.getName() + ")");
112: XMLOutputter outputter = new XMLOutputter(Format
113: .getPrettyFormat());
114: ByteArrayOutputStream bos = new ByteArrayOutputStream();
115: try {
116: outputter.output(element, bos);
117: } catch (IOException e) {
118: return "** IOException from XMLUtils.toXMLString("
119: + element.getName() + ")";
120: }
121: return bos.toString();
122: }
123:
124: /**
125: * Replaces all occurences of CRLF, CR and LF with a single space
126: * and returns the trimmed result string.
127: * @param unstripped the unstripped string.
128: * @return the stripped string.
129: */
130: public static String stripCRLF(final String unstripped) {
131: if (unstripped == null) {
132: return null;
133: }
134: return unstripped.replaceAll("\r\n", " ").replaceAll("\r", " ")
135: .replaceAll("\n", " ").trim();
136: }
137: }
|