1 | |
package org.jtheque.core.managers.core.application; |
2 | |
|
3 | |
import org.jdom.Element; |
4 | |
import org.jtheque.core.managers.resource.ImageDescriptor; |
5 | |
import org.jtheque.core.managers.resource.ImageType; |
6 | |
import org.jtheque.core.utils.SystemProperty; |
7 | |
import org.jtheque.core.utils.file.XMLException; |
8 | |
import org.jtheque.core.utils.file.XMLReader; |
9 | |
import org.jtheque.utils.StringUtils; |
10 | |
import org.jtheque.utils.bean.InternationalString; |
11 | |
import org.jtheque.utils.bean.Version; |
12 | |
import org.jtheque.utils.io.FileUtils; |
13 | |
|
14 | |
import java.io.File; |
15 | |
import java.util.ArrayList; |
16 | |
import java.util.Collection; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public final class XMLApplicationReader { |
40 | |
private XMLReader reader; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public Application readApplication(String filePath){ |
50 | 0 | XMLApplication application = new XMLApplication(); |
51 | |
|
52 | 0 | reader = new XMLReader(); |
53 | |
|
54 | 0 | openFile(filePath); |
55 | |
|
56 | |
try { |
57 | 0 | readFile(application); |
58 | 0 | } catch (XMLException e){ |
59 | 0 | throw new IllegalArgumentException("Unable to read the file " + filePath, e); |
60 | |
} finally { |
61 | 0 | FileUtils.close(reader); |
62 | 0 | } |
63 | |
|
64 | 0 | return application; |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private void openFile(String filePath){ |
73 | |
try { |
74 | 0 | reader.openFile(filePath); |
75 | 0 | } catch (XMLException e){ |
76 | 0 | throw new IllegalArgumentException("Unable to read the file " + filePath, e); |
77 | 0 | } |
78 | 0 | } |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
private void readFile(XMLApplication application) throws XMLException{ |
88 | 0 | readVersion(application); |
89 | 0 | readApplicationValues(application); |
90 | 0 | readInternationalization(application); |
91 | 0 | application.setImages(readImageDescriptor("logo"), readImageDescriptor("icon")); |
92 | 0 | readOptions(application); |
93 | 0 | readProperties(application); |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
private void readVersion(XMLApplication application) throws XMLException{ |
104 | 0 | String versionStr = reader.readString("@version", reader.getRootElement()); |
105 | |
|
106 | 0 | application.setVersion(new Version(versionStr)); |
107 | 0 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
private void readApplicationValues(XMLApplication application) throws XMLException{ |
117 | 0 | String folder = reader.readString("folder", reader.getRootElement()); |
118 | |
|
119 | 0 | if (StringUtils.isEmpty(folder) || !new File(folder).exists()){ |
120 | 0 | application.setProperty("application.folder.path", new File(SystemProperty.USER_DIR.get()).getParentFile().getAbsolutePath()); |
121 | |
} else { |
122 | 0 | application.setProperty("application.folder.path", new File(folder).getAbsolutePath()); |
123 | |
} |
124 | |
|
125 | 0 | application.setProperty("application.repository", reader.readString("repository", reader.getRootElement())); |
126 | 0 | application.setProperty("application.messages", reader.readString("messages", reader.getRootElement())); |
127 | 0 | } |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
private void readInternationalization(XMLApplication application) throws XMLException{ |
137 | 0 | Object i18nElement = reader.getNode("i18n", reader.getRootElement()); |
138 | |
|
139 | 0 | readLanguages(i18nElement, application); |
140 | 0 | readApplicationProperties(i18nElement, application); |
141 | 0 | } |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
private void readLanguages(Object i18nElement, XMLApplication application) throws XMLException{ |
152 | 0 | if(reader.existsNode("languages", i18nElement)){ |
153 | 0 | Collection<Element> nodes = reader.getNodes("languages/language", i18nElement); |
154 | |
|
155 | 0 | Collection<String> languages = new ArrayList<String>(nodes.size()); |
156 | |
|
157 | 0 | for (Element languageElement : nodes){ |
158 | 0 | languages.add(languageElement.getText()); |
159 | |
} |
160 | |
|
161 | 0 | nodes = reader.getNodes("languages/*", i18nElement); |
162 | |
|
163 | 0 | for (Element languageElement : nodes){ |
164 | 0 | languages.add(languageElement.getName()); |
165 | |
} |
166 | |
|
167 | 0 | application.setSupportedLanguages(languages.toArray(new String[languages.size()])); |
168 | |
} |
169 | 0 | } |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
private void readApplicationProperties(Object i18nElement, XMLApplication application) throws XMLException{ |
180 | 0 | if (reader.getNode("files", i18nElement) != null || reader.getNode("name", i18nElement) == null){ |
181 | 0 | application.setApplicationProperties(new I18nAplicationProperties()); |
182 | |
} else { |
183 | 0 | DirectValuesApplicationProperties props = new DirectValuesApplicationProperties(); |
184 | |
|
185 | 0 | props.setName(readInternationalString("name", i18nElement)); |
186 | 0 | props.setAuthor(readInternationalString("author", i18nElement)); |
187 | 0 | props.setSite(readInternationalString("site", i18nElement)); |
188 | 0 | props.setEmail(readInternationalString("email", i18nElement)); |
189 | 0 | props.setCopyright(readInternationalString("copyright", i18nElement)); |
190 | |
|
191 | 0 | application.setApplicationProperties(props); |
192 | |
} |
193 | 0 | } |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
private InternationalString readInternationalString(String path, Object parentElement) throws XMLException{ |
206 | 0 | InternationalString internationalString = new InternationalString(); |
207 | |
|
208 | 0 | Collection<Element> elements = reader.getNodes(path + "/*", parentElement); |
209 | |
|
210 | 0 | for (Element child : elements){ |
211 | 0 | internationalString.put(child.getName(), child.getText()); |
212 | |
} |
213 | |
|
214 | 0 | return internationalString; |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
private ImageDescriptor readImageDescriptor(String node) throws XMLException{ |
228 | 0 | if(reader.existsNode(node, reader.getRootElement())){ |
229 | 0 | Object iconElement = reader.getNode(node, reader.getRootElement()); |
230 | |
|
231 | 0 | StringBuilder path = new StringBuilder(SystemProperty.USER_DIR.get()); |
232 | 0 | path.append("images/"); |
233 | 0 | path.append(reader.readString("image", iconElement)); |
234 | |
|
235 | |
|
236 | 0 | if(reader.existsValue("@image", iconElement)){ |
237 | 0 | path.append(reader.readString("@image", iconElement)); |
238 | |
} else { |
239 | 0 | path.append(reader.readString("image", iconElement)); |
240 | |
} |
241 | |
|
242 | |
ImageType type; |
243 | |
|
244 | 0 | if(reader.existsNode("type", iconElement)){ |
245 | 0 | String typeStr = reader.readString("type", iconElement); |
246 | 0 | type = StringUtils.isEmpty(typeStr) ? ImageType.PNG : ImageType.resolve(typeStr); |
247 | 0 | } else if(reader.existsValue("@type", iconElement)){ |
248 | 0 | String typeStr = reader.readString("@type", iconElement); |
249 | 0 | type = StringUtils.isEmpty(typeStr) ? ImageType.PNG : ImageType.resolve(typeStr); |
250 | 0 | } else { |
251 | 0 | type = ImageType.PNG; |
252 | |
} |
253 | |
|
254 | 0 | return new ImageDescriptor(path.toString(), type); |
255 | |
} |
256 | |
|
257 | 0 | return new ImageDescriptor(node, ImageType.PNG); |
258 | |
} |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
private void readOptions(XMLApplication application) throws XMLException{ |
268 | 0 | if(reader.existsNode("options", reader.getRootElement())){ |
269 | 0 | Object optionsElement = reader.getNode("options", reader.getRootElement()); |
270 | |
|
271 | 0 | if(reader.existsValue("licence", optionsElement) && StringUtils.isNotEmpty(reader.readString("licence", optionsElement))){ |
272 | 0 | application.displayLicence(); |
273 | 0 | application.setProperty("application.licence", SystemProperty.USER_DIR.get() + reader.readString("licence", optionsElement)); |
274 | |
} |
275 | |
} |
276 | 0 | } |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
private void readProperties(XMLApplication application) throws XMLException{ |
286 | 0 | Collection<Element> nodes = reader.getNodes("properties/*", reader.getRootElement()); |
287 | |
|
288 | 0 | for (Element propertyElement : nodes){ |
289 | 0 | application.setProperty(propertyElement.getName(), propertyElement.getText()); |
290 | |
} |
291 | 0 | } |
292 | |
} |