Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XMLWriter |
|
| 1.0;1 |
1 | package org.jtheque.core.utils.file; | |
2 | ||
3 | import org.jdom.Document; | |
4 | import org.jdom.Element; | |
5 | ||
6 | /* | |
7 | * This file is part of JTheque. | |
8 | * | |
9 | * JTheque is free software: you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation, either version 3 of the License. | |
12 | * | |
13 | * JTheque is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | /** | |
23 | * An XML writer. | |
24 | * | |
25 | * @author Baptiste Wicht | |
26 | */ | |
27 | public final class XMLWriter { | |
28 | private final Document document; | |
29 | ||
30 | private Element current; | |
31 | ||
32 | /** | |
33 | * Construct a new XMLWriter. | |
34 | */ | |
35 | public XMLWriter() { | |
36 | 0 | super(); |
37 | ||
38 | 0 | document = new Document(); |
39 | 0 | } |
40 | ||
41 | /** | |
42 | * Construct a new XML writer. | |
43 | * | |
44 | * @param root The name of the root element. | |
45 | */ | |
46 | public XMLWriter(String root) { | |
47 | 0 | super(); |
48 | ||
49 | 0 | document = new Document(new Element(root)); |
50 | ||
51 | 0 | current = getRoot(); |
52 | 0 | } |
53 | ||
54 | /** | |
55 | * Add the element to the document and set the new element as the current element. | |
56 | * | |
57 | * @param element The element to add. | |
58 | */ | |
59 | public void add(String element) { | |
60 | 0 | Element newElement = new Element(element); |
61 | ||
62 | 0 | current.addContent(newElement); |
63 | ||
64 | 0 | current = newElement; |
65 | 0 | } |
66 | ||
67 | /** | |
68 | * Add an element to the document and set the new element as the current element. | |
69 | * | |
70 | * @param element The name of the element to add. | |
71 | * @param text The text of the element. | |
72 | */ | |
73 | public void add(String element, String text) { | |
74 | 0 | add(element); |
75 | ||
76 | 0 | current.setText(text); |
77 | 0 | } |
78 | ||
79 | /** | |
80 | * Add the element. | |
81 | * | |
82 | * @param element The name of the element. | |
83 | * @param text The text of the element. | |
84 | */ | |
85 | public void addOnly(String element, String text) { | |
86 | 0 | Element newElement = new Element(element); |
87 | ||
88 | 0 | newElement.setText(text); |
89 | ||
90 | 0 | current.addContent(newElement); |
91 | 0 | } |
92 | ||
93 | /** | |
94 | * Add only the element with the specified value. | |
95 | * | |
96 | * @param element The element. | |
97 | * @param value The value of the element. | |
98 | */ | |
99 | public void addOnly(String element, int value) { | |
100 | 0 | addOnly(element, Integer.toString(value)); |
101 | 0 | } |
102 | ||
103 | /** | |
104 | * Add an attribute to the current element. | |
105 | * | |
106 | * @param key The key of the attribute. | |
107 | * @param value The value of the attribute. | |
108 | */ | |
109 | public void addAttribute(String key, String value) { | |
110 | 0 | current.setAttribute(key, value); |
111 | 0 | } |
112 | ||
113 | /** | |
114 | * Return the root element of the document. | |
115 | * | |
116 | * @return The root element of the document. | |
117 | */ | |
118 | public Element getRoot() { | |
119 | 0 | return document.getRootElement(); |
120 | } | |
121 | ||
122 | /** | |
123 | * Set the current element. | |
124 | * | |
125 | * @param element The current element. | |
126 | */ | |
127 | public void setCurrent(Element element) { | |
128 | 0 | current = element; |
129 | 0 | } |
130 | ||
131 | /** | |
132 | * Write the XML document to the file path. | |
133 | * | |
134 | * @param filePath The file path. | |
135 | */ | |
136 | public void write(String filePath) { | |
137 | 0 | XMLUtils.writeXml(document, filePath); |
138 | 0 | } |
139 | ||
140 | /** | |
141 | * Switch to parent. | |
142 | */ | |
143 | public void switchToParent() { | |
144 | 0 | current = current.getParentElement(); |
145 | 0 | } |
146 | } |