1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.common;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.FileNotFoundException;
|
14
|
import java.io.FileOutputStream;
|
15
|
import java.io.IOException;
|
16
|
import java.io.InputStream;
|
17
|
import java.io.OutputStream;
|
18
|
import java.util.ArrayList;
|
19
|
import java.util.List;
|
20
|
|
21
|
import org.apache.logging.log4j.LogManager;
|
22
|
import org.apache.logging.log4j.Logger;
|
23
|
import org.jdom.Attribute;
|
24
|
import org.jdom.Document;
|
25
|
import org.jdom.Element;
|
26
|
import org.jdom.JDOMException;
|
27
|
import org.jdom.Namespace;
|
28
|
import org.jdom.input.SAXBuilder;
|
29
|
import org.jdom.output.Format;
|
30
|
import org.jdom.output.XMLOutputter;
|
31
|
|
32
|
public class XmlHelp {
|
33
|
private static final Logger logger = LogManager.getLogger(XmlHelp.class);
|
34
|
|
35
|
public final static Format prettyFormat = Format.getPrettyFormat();
|
36
|
/**
|
37
|
* Writes the Document doc to the specified file
|
38
|
* @param doc
|
39
|
* @param path
|
40
|
* @param fileName
|
41
|
* @return true, if no error
|
42
|
*
|
43
|
* TODO throw the FileNotFoundException and handle in the calling method. That is more likely the place where you can do
|
44
|
* something about the problem
|
45
|
*/
|
46
|
static public boolean saveToXml(Document doc, String path, String fileName, Format format ){
|
47
|
try {
|
48
|
if (! fileName.endsWith(".xml")){
|
49
|
fileName += ".xml";
|
50
|
}
|
51
|
FileOutputStream outFile = new FileOutputStream(path + File.separator + fileName);
|
52
|
return saveToXml(doc, outFile, format);
|
53
|
} catch (FileNotFoundException e) {
|
54
|
logger.error("FileNotFoundException in saveToXml()");
|
55
|
return false;
|
56
|
}
|
57
|
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* Writes the Document doc to the specified file
|
62
|
* @param doc
|
63
|
* @param path
|
64
|
* @param fileName
|
65
|
* @return true, if no error
|
66
|
*
|
67
|
* TODO throw the IOException and handle in the calling method. That is more likely the place where you can do
|
68
|
* something about the problem
|
69
|
*/
|
70
|
static public boolean saveToXml(Document doc, OutputStream outStream, Format format ){
|
71
|
try {
|
72
|
XMLOutputter out = new XMLOutputter(format);
|
73
|
out.output(doc, outStream);
|
74
|
return true;
|
75
|
} catch (IOException e) {
|
76
|
logger.error("IOException in saveToXml()");
|
77
|
return false;
|
78
|
}
|
79
|
}
|
80
|
|
81
|
static public Element getFirstAttributedChild(Element parent, String elementName, String attributeName, String attributeValue){
|
82
|
Namespace ns = parent.getNamespace();
|
83
|
|
84
|
List<Element> elList = getChildren(parent, elementName, ns);
|
85
|
for (Element el : elList){
|
86
|
Attribute attr = el.getAttribute(attributeName);
|
87
|
if (attr != null && attr.getValue().equalsIgnoreCase(attributeValue)){
|
88
|
return el;
|
89
|
}
|
90
|
}
|
91
|
return null;
|
92
|
}
|
93
|
|
94
|
static public List<Element> getAttributedChildList(Element parent, String elementName, String attributeName){
|
95
|
List<Element> resultList = new ArrayList<Element>();
|
96
|
Namespace ns = parent.getNamespace();
|
97
|
List<Element> elList = getChildren(parent, elementName, ns);
|
98
|
for (Element el : elList){
|
99
|
Attribute attr = el.getAttribute(attributeName);
|
100
|
if (attr != null){
|
101
|
resultList.add(el);
|
102
|
}
|
103
|
}
|
104
|
return resultList;
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Returns a list of children with the given element name and with a given attribute name and
|
109
|
* a given value for this attribute.<BR>
|
110
|
* The value comparison is case insensitive.
|
111
|
* @param parent
|
112
|
* @param elementName
|
113
|
* @param attributeName
|
114
|
* @param value
|
115
|
* @return
|
116
|
*/
|
117
|
static public List<Element> getAttributedChildListWithValue(Element parent, String elementName, String attributeName, String value){
|
118
|
List<Element> resultList = new ArrayList<Element>();
|
119
|
Namespace ns = parent.getNamespace();
|
120
|
List<Element> elList = getChildren(parent, elementName, ns);
|
121
|
for (Element el : elList){
|
122
|
Attribute attr = el.getAttribute(attributeName);
|
123
|
if (attr != null){
|
124
|
if (attr.getValue().equalsIgnoreCase(value)){
|
125
|
resultList.add(el);
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
return resultList;
|
130
|
}
|
131
|
|
132
|
@SuppressWarnings("unchecked")
|
133
|
private static List<Element> getChildren(Element parent, String elementName,Namespace ns) {
|
134
|
return parent.getChildren(elementName, ns);
|
135
|
}
|
136
|
|
137
|
public static String getChildAttributeValue(Element element, String childElementName, Namespace childElementNamespace, String childAttributeName, Namespace childAttributeNamespace){
|
138
|
Element child = element.getChild(childElementName, childElementNamespace);
|
139
|
if (child == null){
|
140
|
return null;
|
141
|
}
|
142
|
Attribute childAttribute = child.getAttribute(childAttributeName, childAttributeNamespace);
|
143
|
if (childAttribute == null){
|
144
|
return null;
|
145
|
}
|
146
|
return childAttribute.getValue();
|
147
|
}
|
148
|
|
149
|
public static String getChildContent(Element element, String childElementName, Namespace childElementNamespace, String childAttributeName, Namespace childAttributeNamespace){
|
150
|
Element child = element.getChild(childElementName, childElementNamespace);
|
151
|
if (child == null){
|
152
|
return null;
|
153
|
}
|
154
|
List childContent = child.getContent();
|
155
|
if (childContent.isEmpty()){
|
156
|
return null;
|
157
|
}
|
158
|
for (Object content:childContent){
|
159
|
if (content instanceof Element){
|
160
|
Element contentEl = (Element)content;
|
161
|
if (contentEl.getName().equals(childAttributeName)){
|
162
|
return contentEl.getText();
|
163
|
}
|
164
|
}
|
165
|
}
|
166
|
return null;
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* @param parent
|
171
|
* @param elementName
|
172
|
* @param attributeName
|
173
|
* @param attributeValue
|
174
|
* @return
|
175
|
*/
|
176
|
static public Element getOrAddChild(Element parent, String elementName, String attributeName, String attributeValue){
|
177
|
Element result = null;
|
178
|
if (parent != null){
|
179
|
if (attributeName != null){
|
180
|
result = getFirstAttributedChild(parent, elementName, attributeName, attributeValue);
|
181
|
}else{
|
182
|
result = parent.getChild(elementName, parent.getNamespace());
|
183
|
}
|
184
|
if (result == null){
|
185
|
result = new Element(elementName, parent.getNamespace());
|
186
|
if (attributeName != null){
|
187
|
Attribute attr = new Attribute(attributeName, attributeValue);
|
188
|
result.setAttribute(attr);
|
189
|
}
|
190
|
}
|
191
|
if (result.getParent()== null){
|
192
|
parent.addContent(result);
|
193
|
}
|
194
|
}
|
195
|
return result;
|
196
|
}
|
197
|
|
198
|
static public Element insertXmlRefProperty(Element parent, String strName, String strValue){
|
199
|
Namespace ns = parent.getNamespace();
|
200
|
Element property = new Element("property", ns);
|
201
|
Attribute name = new Attribute("name", strName);
|
202
|
property.setAttribute(name);
|
203
|
Attribute value = new Attribute("value", strValue);
|
204
|
property.setAttribute(value);
|
205
|
parent.addContent(property);
|
206
|
return property;
|
207
|
}
|
208
|
|
209
|
static public Element insertXmlValueProperty(Element parent, String strName, String strValue){
|
210
|
Namespace ns = parent.getNamespace();
|
211
|
Element property = new Element("property", ns);
|
212
|
Attribute name = new Attribute("name", strName);
|
213
|
property.setAttribute(name);
|
214
|
Attribute value = new Attribute("value", strValue);
|
215
|
property.setAttribute(value);
|
216
|
parent.addContent(property);
|
217
|
return property;
|
218
|
}
|
219
|
|
220
|
|
221
|
static public Element insertXmlBean(Element parent, String strId, String strClass){
|
222
|
Namespace ns = parent.getNamespace();
|
223
|
Element bean = new Element("bean", ns);
|
224
|
Attribute id = new Attribute("id", strId);
|
225
|
bean.setAttribute(id);
|
226
|
Attribute clazz = new Attribute("class", strClass);
|
227
|
bean.setAttribute(clazz);
|
228
|
parent.addContent(bean);
|
229
|
return bean;
|
230
|
}
|
231
|
|
232
|
|
233
|
/**
|
234
|
* returns the root Element in the File xmlFile
|
235
|
* @param xmlInput
|
236
|
* @return
|
237
|
* @throws JDOMException
|
238
|
* @throws IOException
|
239
|
*/
|
240
|
static public Element getRoot(InputStream xmlInput) throws JDOMException, IOException{
|
241
|
SAXBuilder builder = new SAXBuilder();
|
242
|
Document doc = builder.build(xmlInput);
|
243
|
Element root = doc.getRootElement();
|
244
|
return root;
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* returns the root Element in the File xmlFile
|
249
|
*
|
250
|
* @param xmlInput
|
251
|
* @param elementName
|
252
|
* @return
|
253
|
* TODO throw the JDOMException and the IOException and handle in the calling method. That is more likely the place where you can do
|
254
|
* something about the problem
|
255
|
*/
|
256
|
static public Element getRoot(InputStream xmlInput, String elementName){
|
257
|
try {
|
258
|
SAXBuilder builder = new SAXBuilder();
|
259
|
Document doc = builder.build(xmlInput);
|
260
|
Element root = doc.getRootElement();
|
261
|
if (root.getName() != elementName){
|
262
|
return null;
|
263
|
}else{
|
264
|
return root;
|
265
|
}
|
266
|
} catch (JDOMException e) {
|
267
|
e.printStackTrace();
|
268
|
return null;
|
269
|
} catch (IOException e) {
|
270
|
e.printStackTrace();
|
271
|
return null;
|
272
|
}
|
273
|
}
|
274
|
|
275
|
/**
|
276
|
* returns the root Element in the File xmlFile
|
277
|
* @param xmlInput
|
278
|
* @return
|
279
|
*
|
280
|
* TODO throw the IOException and handle in the calling method. That is more likely the place where you can do
|
281
|
* something about the problem
|
282
|
*/
|
283
|
static public Element getBeansRoot(InputStream xmlInput){
|
284
|
try {
|
285
|
SAXBuilder builder = new SAXBuilder();
|
286
|
Document doc = builder.build(xmlInput);
|
287
|
Element root = doc.getRootElement();
|
288
|
if (root.getName() != "beans"){
|
289
|
return null;
|
290
|
}else{
|
291
|
return root;
|
292
|
}
|
293
|
} catch (JDOMException e) {
|
294
|
e.printStackTrace();
|
295
|
return null;
|
296
|
} catch (IOException e) {
|
297
|
e.printStackTrace();
|
298
|
return null;
|
299
|
}
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* Gets the child element and tests if there is no other child element exists having the same name.
|
304
|
* The result is returned as a pair of thd child element and a boolean value that indicates if the
|
305
|
* elements cardinality was correct. <BR>
|
306
|
* If there is more then one child element with the child element name
|
307
|
* or if there is no such element and obligatory is <code>true</code> the second part of the result is <code>false</code>
|
308
|
* Otherwise it is <code>true</code>.
|
309
|
* @param parentElement the parent element
|
310
|
* @param childName name of the child element
|
311
|
* @param nsChild the namespace for the child element
|
312
|
* @param obligatory if <code>true</code>, return value is only <code>true</code> if exactly 1 child element with
|
313
|
* the given name exists
|
314
|
* @return
|
315
|
*/
|
316
|
static public DoubleResult<Element, Boolean> getSingleChildElement(Element parentElement, String childName, Namespace nsChild, boolean obligatory){
|
317
|
DoubleResult<Element, Boolean> result = new DoubleResult<Element, Boolean>();
|
318
|
result.setSecondResult(false);
|
319
|
|
320
|
if (parentElement == null){
|
321
|
logger.warn("Parent element is null");
|
322
|
return result;
|
323
|
}
|
324
|
List<Element> elList = getChildren(parentElement, childName, nsChild);
|
325
|
if (elList.size() > 1){
|
326
|
logger.error("Multiple '" + childName + "' elements.");
|
327
|
return result;
|
328
|
}else if (elList.size() == 0){
|
329
|
logger.info("There is no '" + childName + "' element");
|
330
|
if (! obligatory){
|
331
|
result.setSecondResult(true);
|
332
|
}
|
333
|
return result;
|
334
|
}
|
335
|
Element childElement = elList.get(0);
|
336
|
result.setFirstResult(childElement);
|
337
|
result.setSecondResult(true);
|
338
|
return result;
|
339
|
}
|
340
|
|
341
|
static public Element getSingleChildElement(ResultWrapper<Boolean> success, Element parentElement, String childName, Namespace nsChild, boolean obligatory){
|
342
|
|
343
|
if (parentElement == null){
|
344
|
logger.warn("Parent element is null");
|
345
|
success.setValue(false);
|
346
|
return null;
|
347
|
}
|
348
|
List<Element> elList = getChildren(parentElement, childName, nsChild);
|
349
|
if (elList.size() > 1){
|
350
|
logger.error("Multiple '" + childName + "' elements.");
|
351
|
success.setValue(false);
|
352
|
return null;
|
353
|
}else if (elList.size() == 0){
|
354
|
elList = getChildren(parentElement, childName, null);
|
355
|
logger.info("There is no '" + childName + "' element");
|
356
|
if (obligatory){
|
357
|
success.setValue(false);
|
358
|
}
|
359
|
return null;
|
360
|
}
|
361
|
|
362
|
Element childElement = elList.get(0);
|
363
|
return childElement;
|
364
|
}
|
365
|
|
366
|
static public List<Element> getMultipleChildElement(Element parentElement, String childName, Namespace nsChild, boolean obligatory){
|
367
|
|
368
|
if (parentElement == null){
|
369
|
logger.warn("Parent element is null");
|
370
|
return null;
|
371
|
}
|
372
|
|
373
|
List<Element> elList = getChildren(parentElement, childName.trim(), nsChild);
|
374
|
|
375
|
if (elList.size() == 0){
|
376
|
logger.info("There is no '" + childName + "' element");
|
377
|
return null;
|
378
|
}
|
379
|
return elList;
|
380
|
}
|
381
|
|
382
|
public static String getChildContentAttributeValue(Element element,
|
383
|
String childAttributeName, Namespace taxonNameNamespace, String childElementName,
|
384
|
Namespace childElementNamespace) {
|
385
|
Element child = element.getChild(childAttributeName, taxonNameNamespace);
|
386
|
if (child == null){
|
387
|
return null;
|
388
|
}
|
389
|
List childContent = child.getContent();
|
390
|
if (childContent.isEmpty()){
|
391
|
return null;
|
392
|
}
|
393
|
for (Object content:childContent){
|
394
|
if (content instanceof Element){
|
395
|
Element contentEl = (Element)content;
|
396
|
if (contentEl == null){
|
397
|
return null;
|
398
|
}
|
399
|
if (!(contentEl.getAttributeValue(childElementName) == null)){
|
400
|
Attribute at = contentEl.getAttribute("ressource");
|
401
|
if (at != null) {
|
402
|
return at.getValue();
|
403
|
}
|
404
|
}
|
405
|
}
|
406
|
}
|
407
|
return null;
|
408
|
}
|
409
|
|
410
|
}
|