001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program 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
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.utils;
017:
018: import java.io.File;
019: import java.io.FileInputStream;
020: import java.sql.Timestamp;
021: import javax.xml.parsers.DocumentBuilderFactory;
022: import org.apache.commons.lang.StringEscapeUtils;
023: import org.w3c.dom.Document;
024: import org.w3c.dom.Node;
025: import org.xml.sax.InputSource;
026: import org.xml.sax.SAXException;
027:
028: /**
029: * Provides utility methods useful for parsing and processing XML data.
030: */
031: public class XMLUtil {
032:
033: /** Logger */
034: public static final WikiLogger logger = WikiLogger
035: .getLogger(XMLUtil.class.getName());
036:
037: /**
038: *
039: */
040: private XMLUtil() {
041: }
042:
043: /**
044: * Utiltiy method for building an XML tag of the form <tagName>value</tagName>.
045: *
046: * @param tagName The name of the XML tag, such as <tagName>value</tagName>.
047: * @param tagValue The value of the XML tag, such as <tagName>value</tagName>.
048: * @param escape If <code>true</code> then any less than, greater than, quotation mark,
049: * apostrophe or ampersands in tagValue will be XML-escaped.
050: * @return An XML representations of the tagName and tagValue parameters.
051: */
052: public static String buildTag(String tagName, String tagValue,
053: boolean escape) {
054: if (tagValue == null) {
055: return "";
056: }
057: StringBuffer buffer = new StringBuffer();
058: buffer.append('<').append(tagName).append('>');
059: if (escape) {
060: tagValue = StringEscapeUtils.escapeXml(tagValue);
061: }
062: buffer.append(tagValue);
063: buffer.append("</").append(tagName).append('>');
064: return buffer.toString();
065: }
066:
067: /**
068: * Utiltiy method for building an XML tag of the form <tagName>value</tagName>.
069: *
070: * @param tagName The name of the XML tag, such as <tagName>value</tagName>.
071: * @param tagValue The value of the XML tag, such as <tagName>value</tagName>.
072: * @return An XML representations of the tagName and tagValue parameters.
073: */
074: public static String buildTag(String tagName, int tagValue) {
075: return XMLUtil.buildTag(tagName, Integer.toString(tagValue),
076: false);
077: }
078:
079: /**
080: * Utiltiy method for building an XML tag of the form <tagName>value</tagName>.
081: *
082: * @param tagName The name of the XML tag, such as <tagName>value</tagName>.
083: * @param tagValue The value of the XML tag, such as <tagName>value</tagName>.
084: * @return An XML representations of the tagName and tagValue parameters.
085: */
086: public static String buildTag(String tagName, Integer tagValue) {
087: return (tagValue == null) ? "" : XMLUtil.buildTag(tagName,
088: tagValue.toString(), false);
089: }
090:
091: /**
092: * Utiltiy method for building an XML tag of the form <tagName>value</tagName>.
093: *
094: * @param tagName The name of the XML tag, such as <tagName>value</tagName>.
095: * @param tagValue The value of the XML tag, such as <tagName>value</tagName>.
096: * @return An XML representations of the tagName and tagValue parameters.
097: */
098: public static String buildTag(String tagName, boolean tagValue) {
099: return XMLUtil.buildTag(tagName, Boolean.toString(tagValue),
100: false);
101: }
102:
103: /**
104: * Utiltiy method for building an XML tag of the form <tagName>value</tagName>.
105: *
106: * @param tagName The name of the XML tag, such as <tagName>value</tagName>.
107: * @param tagValue The value of the XML tag, such as <tagName>value</tagName>.
108: * @return An XML representations of the tagName and tagValue parameters.
109: */
110: public static String buildTag(String tagName, Timestamp tagValue) {
111: return (tagValue == null) ? "" : XMLUtil.buildTag(tagName,
112: tagValue.toString(), false);
113: }
114:
115: /**
116: * Utiltiy method for building an XML tag of the form <tagName>value</tagName>.
117: *
118: * @param tagName The name of the XML tag, such as <tagName>value</tagName>.
119: * @param tagValue The value of the XML tag, such as <tagName>value</tagName>.
120: * @return An XML representations of the tagName and tagValue parameters.
121: */
122: public static String buildTag(String tagName, long tagValue) {
123: return XMLUtil
124: .buildTag(tagName, Long.toString(tagValue), false);
125: }
126:
127: /**
128: * Get XML <code>Node</code> text content. This method duplicates the
129: * org.w3c.dom.Node.getTextContent() method in JDK 1.5.
130: *
131: * @param baseNode The XML node from which the content is being retrieved.
132: * @return The text content of the XML node.
133: */
134: public static String getTextContent(Node baseNode) throws Exception {
135: // if element, first child will be a text element with content
136: Node child = baseNode.getFirstChild();
137: if (child != null && child.getNodeType() == Node.TEXT_NODE) {
138: return child.getNodeValue();
139: }
140: return "";
141: }
142:
143: /**
144: * Given a <code>File</code> object that points to an XML file, parse the
145: * XML and return a parsed <code>Document</code> object.
146: *
147: * @param file The File object that points to the XML file.
148: * @param validating Set to <code>true</code> if the parser should
149: * validate against a DTD.
150: * @return A parsed Document object.
151: */
152: public static Document parseXML(File file, boolean validating)
153: throws Exception {
154: if (!file.exists()) {
155: throw new Exception("File " + file.getAbsolutePath()
156: + " does not exist");
157: }
158: FileInputStream stream = null;
159: try {
160: stream = new FileInputStream(file);
161: InputSource source = new InputSource(stream);
162: try {
163: return XMLUtil.parseXML(source, validating);
164: } catch (SAXException e) {
165: // invalid XML
166: logger.severe("The file " + file.getAbsolutePath()
167: + " contains invalid XML", e);
168: throw new Exception("The file "
169: + file.getAbsolutePath()
170: + " contains invalid XML: " + e.getMessage(), e);
171: }
172: } finally {
173: if (stream != null) {
174: stream.close();
175: }
176: }
177: }
178:
179: /**
180: * Given an <code>InputSource</code> object that points to XML data, parse
181: * the XML and return a parsed <code>Document</code> object.
182: *
183: * @param source The InputSource object that points to XML data.
184: * @param validating Set to <code>true</code> if the parser should
185: * validate against a DTD.
186: * @return A parsed Document object.
187: */
188: public static Document parseXML(InputSource source,
189: boolean validating) throws Exception {
190: // Create a builder factory
191: DocumentBuilderFactory factory = DocumentBuilderFactory
192: .newInstance();
193: factory.setValidating(validating);
194: // Create the builder and parse the file
195: return factory.newDocumentBuilder().parse(source);
196: }
197: }
|