001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.util;
018:
019: import java.io.IOException;
020: import java.io.StringWriter;
021: import javax.xml.transform.Transformer;
022: import javax.xml.transform.TransformerFactory;
023:
024: import org.dom4j.Document;
025: import org.dom4j.DocumentException;
026: import org.dom4j.DocumentHelper;
027: import org.dom4j.Element;
028: import org.dom4j.io.DocumentResult;
029: import org.dom4j.io.DocumentSource;
030: import org.dom4j.io.OutputFormat;
031: import org.dom4j.io.XMLWriter;
032:
033: /**
034: * Utilities to parse strings into XML DOM Documents and vice versa
035: */
036: public final class XmlUtils {
037:
038: /**
039: * uses Dom4j to neatly format a given XML string
040: * by adding indents, newlines etc.
041: */
042: public static String getAsPrettyXml(String xmlString) {
043: return getAsPrettyXml(parse(xmlString));
044: }
045:
046: /**
047: * Override that accepts an XML DOM Document
048: * @param document XML as DOM Document
049: */
050: public static String getAsPrettyXml(Document document) {
051: OutputFormat format = new OutputFormat(" ", true);
052: format.setSuppressDeclaration(true);
053: StringWriter out = new StringWriter();
054: XMLWriter writer = new XMLWriter(out, format);
055: try {
056: try {
057: writer.write(document);
058: } finally {
059: writer.close();
060: }
061: } catch (IOException ioe) {
062: throw new RuntimeException(ioe);
063: }
064: return out.toString().trim();
065: }
066:
067: /**
068: * Converts a String into XML by parsing into a DOM Document
069: * uses Dom4j
070: */
071: public static Document parse(String xmlString) {
072: try {
073: return DocumentHelper.parseText(xmlString);
074: } catch (DocumentException de) {
075: throw new RuntimeException(de);
076: }
077: }
078:
079: public static Element getNewElement(String name) {
080: return DocumentHelper.createElement(name);
081: }
082:
083: public static Document getNewDocument(String rootElementName) {
084: Document d = DocumentHelper.createDocument();
085: d.addElement(rootElementName);
086: return d;
087: }
088:
089: public static Document transform(Document source,
090: Document stylesheet) {
091: TransformerFactory factory = TransformerFactory.newInstance();
092: try {
093: Transformer transformer = factory
094: .newTransformer(new DocumentSource(stylesheet));
095: DocumentResult result = new DocumentResult();
096: transformer.transform(new DocumentSource(source), result);
097: return result.getDocument();
098: } catch (Exception e) {
099: throw new RuntimeException(e);
100: }
101: }
102:
103: }
|