001    package calhoun.util;
002    
003    import java.io.BufferedInputStream;
004    import java.io.FileOutputStream;
005    import java.io.IOException;
006    import java.io.InputStream;
007    import java.io.OutputStream;
008    import java.io.StringWriter;
009    import java.util.zip.GZIPInputStream;
010    
011    import javax.xml.transform.Templates;
012    import javax.xml.transform.Transformer;
013    import javax.xml.transform.TransformerFactory;
014    import javax.xml.transform.stream.StreamResult;
015    import javax.xml.transform.stream.StreamSource;
016    
017    import org.apache.commons.logging.Log;
018    import org.apache.commons.logging.LogFactory;
019    import org.dom4j.Document;
020    import org.dom4j.DocumentException;
021    import org.dom4j.DocumentHelper;
022    import org.dom4j.Element;
023    import org.dom4j.Node;
024    import org.dom4j.io.OutputFormat;
025    import org.dom4j.io.SAXReader;
026    import org.dom4j.io.XMLWriter;
027    
028    /** Utility functions to simplify XML.  Wrapper around DOM4j.
029     */
030    public final class XmlUtil {
031            @SuppressWarnings("unused")
032            private static final Log log = LogFactory.getLog(XmlUtil.class);
033    
034            /** This class should not be instantiated. */
035            private XmlUtil() {
036            }
037    
038            public static void transform(String input, String output, String stylesheet) {
039                    try {
040                            TransformerFactory tFactory = TransformerFactory.newInstance();
041                            StreamSource source = new StreamSource(ResourceLoader.openInputStream(XmlUtil.class, stylesheet));
042                            Templates templates = tFactory.newTemplates(source);
043                            Transformer transformer = templates.newTransformer();
044                            StreamSource sourceXML = new StreamSource(input);
045                            StreamResult result = new StreamResult(output);
046                            
047                            transformer.transform(sourceXML, result);       
048                    }
049                    catch(Exception e) {
050                            throw new ConfigException(e);
051                    }
052            }
053            
054            public static Document newDocument() {
055                    return DocumentHelper.createDocument();
056            }
057                    
058            public static Element newElement(String name) {
059                    return DocumentHelper.createElement(name);
060            }
061            
062            /** Creates a dom4j Document from a file.  The filename given can be on the classpath or loaded from the filesystem.
063             * Converts exceptions into runtime ConfigExceptions.  
064             */
065            public static Document parseFile(String filename) {
066                    try {
067                            SAXReader xmlReader = new SAXReader();
068                            InputStream s = new BufferedInputStream(ResourceLoader.openInputStream(XmlUtil.class,filename));
069                            s.mark(100);
070                            boolean isGzip = FileUtil.isGzipStream(s);
071                            s.reset();
072                            if (isGzip) s = new GZIPInputStream(s);
073                            return xmlReader.read(s);
074                    } catch (IOException e) {
075                            throw new ErrorException(e);
076                    } catch (DocumentException ex) {
077                            throw new ConfigException("Invalid XML file: ", ex);
078                    }
079            }
080    
081            /** Creates a dom4j Document from a text string.
082             * Converts exceptions into runtime ErrorExceptions.  
083             */
084            public static Document parseString(String data) {
085                    try {
086                            return DocumentHelper.parseText(data);
087                    } catch (DocumentException ex) {
088                            throw new ErrorException("Invalid XML data: ", ex);
089                    }
090            }
091    
092            /** Pretty prints a document and returns the result as a String */
093            public static String prettyPrint(Document doc) {
094                    return prettyPrint((Node) doc);
095            }
096    
097            /** Pretty prints a dom4j Node and returns the result as a String */
098            public static String prettyPrint(Node node) {
099                    StringWriter sw = new StringWriter();
100                    try {
101                            OutputFormat outformat = OutputFormat.createPrettyPrint();
102                            XMLWriter writer = new XMLWriter(sw, outformat);
103                            writer.write(node);
104                            writer.flush();
105                    } catch (Exception ex) {
106                            throw new ErrorException("Error writing XML output ", ex);
107                    }
108                    return sw.toString();
109            }
110    
111            /** Pretty prints a document to a given OutputStream */
112            public static void prettyPrint(Document doc, OutputStream out) {
113                    try {
114                            OutputFormat outformat = OutputFormat.createPrettyPrint();
115                            XMLWriter writer = new XMLWriter(out, outformat);
116                            writer.write(doc);
117                            writer.flush();
118                    } catch (Exception ex) {
119                            throw new ErrorException("Error writing XML output ", ex);
120                    }
121            }
122    
123            /** Pretty prints a document to a given filename */
124            public static void prettyPrint(Document doc, String filename) {
125                    try {
126                            OutputStream out = new FileOutputStream(filename);
127                            prettyPrint(doc, out);
128                            out.close();
129                    } catch (IOException ex) {
130                            throw new ConfigException("Error saving XML to file ", ex);
131                    }
132            }
133    
134    }