1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.tools; |
17 | |
18 | import java.io.File; |
19 | import java.io.FileWriter; |
20 | import java.io.IOException; |
21 | import java.io.Writer; |
22 | |
23 | import javax.xml.parsers.DocumentBuilder; |
24 | import javax.xml.parsers.DocumentBuilderFactory; |
25 | import javax.xml.parsers.ParserConfigurationException; |
26 | |
27 | import org.apache.xml.serialize.OutputFormat; |
28 | import org.apache.xml.serialize.XMLSerializer; |
29 | import org.w3c.dom.Document; |
30 | import org.w3c.dom.Element; |
31 | import org.xml.sax.SAXException; |
32 | |
33 | /** |
34 | * Various utility methods to simplify processing XML data. |
35 | * |
36 | * @author Thomas Aglassinger |
37 | */ |
38 | public final class XmlTools |
39 | { |
40 | private static final int PRETTY_INDENT = 2; |
41 | private static final int PRETTY_LINE_WIDTH = 100; |
42 | private static XmlTools instance; |
43 | private DocumentBuilder documentBuilder; |
44 | private DocumentBuilderFactory documentBuilderFactory; |
45 | |
46 | private XmlTools() |
47 | throws ParserConfigurationException { |
48 | documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
49 | documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
50 | } |
51 | |
52 | public static synchronized XmlTools instance() { |
53 | if (instance == null) { |
54 | try { |
55 | instance = new XmlTools(); |
56 | } catch (Exception error) { |
57 | // Tunnel checked exception |
58 | LocaleTools localeTools = LocaleTools.instance(); |
59 | String message = localeTools.getMessage("errors.cannotSetUpXmlProcessing"); |
60 | IllegalStateException error2 = new IllegalStateException(message); |
61 | |
62 | error2.initCause(error); |
63 | throw error2; |
64 | } |
65 | } |
66 | return instance; |
67 | } |
68 | |
69 | /** |
70 | * Create new XML document. |
71 | * |
72 | * @param rootElementName name of the root element to add, or <code>null</code> if the |
73 | * document should not have any root just yet |
74 | */ |
75 | public Document createEmptyDocument(String rootElementName) { |
76 | Document result = documentBuilder.newDocument(); |
77 | |
78 | if (rootElementName != null) { |
79 | Element rootElement = result.createElement(rootElementName); |
80 | |
81 | result.appendChild(rootElement); |
82 | } |
83 | return result; |
84 | } |
85 | |
86 | /** |
87 | * Pretty print <code>xmlFile</code>, replacing the original. |
88 | */ |
89 | public void prettyPrint(File xmlFile) |
90 | throws IOException, SAXException { |
91 | assert xmlFile != null; |
92 | Document document = readDocument(xmlFile); |
93 | Writer writer = new FileWriter(xmlFile); |
94 | |
95 | try { |
96 | prettyPrint(document, writer); |
97 | } finally { |
98 | writer.close(); |
99 | } |
100 | } |
101 | |
102 | /** |
103 | * Write <code>document</code> to <code>writer</code>. |
104 | */ |
105 | public void write(Document document, Writer writer) |
106 | throws IOException { |
107 | assert document != null; |
108 | assert writer != null; |
109 | OutputFormat format = new OutputFormat(document); |
110 | XMLSerializer serial = new XMLSerializer(writer, format); |
111 | |
112 | serial.asDOMSerializer(); |
113 | serial.serialize(document.getDocumentElement()); |
114 | } |
115 | |
116 | /** |
117 | * Pretty print <code>document</code> to <code>writer</code>. |
118 | */ |
119 | private void prettyPrint(Document document, Writer writer) |
120 | throws IOException { |
121 | OutputFormat format = new OutputFormat(document); |
122 | |
123 | format.setLineWidth(PRETTY_LINE_WIDTH); |
124 | format.setIndenting(true); |
125 | format.setIndent(PRETTY_INDENT); |
126 | |
127 | XMLSerializer serializer = new XMLSerializer(writer, format); |
128 | |
129 | serializer.serialize(document); |
130 | } |
131 | |
132 | /** |
133 | * Read whole document from <code>xmlFile</code>. |
134 | */ |
135 | private Document readDocument(File xmlFile) |
136 | throws IOException, SAXException { |
137 | return documentBuilder.parse(xmlFile); |
138 | } |
139 | |
140 | } |