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