1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.print;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Arrays;
|
15
|
import java.util.List;
|
16
|
import java.util.Locale;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.jdom.Element;
|
20
|
import org.jdom.input.SAXBuilder;
|
21
|
|
22
|
/**
|
23
|
* Abstract implementation of an IXMLEntityFactory
|
24
|
*
|
25
|
* @see IXMLEntityFactory
|
26
|
*
|
27
|
* @author n.hoffmann
|
28
|
* @created Jul 16, 2010
|
29
|
* @version 1.0
|
30
|
*/
|
31
|
public abstract class XmlEntityFactoryBase implements IXMLEntityFactory {
|
32
|
private static final Logger logger = Logger
|
33
|
.getLogger(XmlEntityFactoryBase.class);
|
34
|
|
35
|
/**
|
36
|
* A list of XML element names that are used to store collections
|
37
|
*/
|
38
|
private static final List<String> COLLECTION_ELEMENTS = Arrays.asList(new String[]{"arraylist", "hashtable", "hashset", "persistentset"});
|
39
|
|
40
|
private static final String MODEL_AND_VIEW = "ModelAndView";
|
41
|
|
42
|
/**
|
43
|
* The SAXBuilder used to parse the results from cdmlib-remote API calls
|
44
|
*/
|
45
|
protected SAXBuilder builder = new SAXBuilder();
|
46
|
|
47
|
/**
|
48
|
* This method will transform the response of a CDM REST request into a list of
|
49
|
* XMLEntityContainer objects for convenient handling.
|
50
|
*
|
51
|
* @param rootElement
|
52
|
* @return
|
53
|
*/
|
54
|
protected List<Element> processElementList(Element rootElement) {
|
55
|
logger.trace("Processing element list: " + rootElement);
|
56
|
|
57
|
List<Element> result = new ArrayList<Element>();
|
58
|
|
59
|
if(rootElement == null){
|
60
|
return result;
|
61
|
}
|
62
|
|
63
|
if(rootElement.getName().equals(MODEL_AND_VIEW)){
|
64
|
Element model = rootElement.getChild("model");
|
65
|
rootElement = (Element) model.getChildren().get(0);
|
66
|
|
67
|
logger.warn("ModelAndView detected");
|
68
|
|
69
|
}
|
70
|
|
71
|
String rootElementName = rootElement.getName().toLowerCase(Locale.ENGLISH);
|
72
|
if(! COLLECTION_ELEMENTS.contains(rootElementName)){
|
73
|
logger.error("Given element is not of a processable collection type. RootElementName: " + rootElementName);
|
74
|
return result;
|
75
|
}
|
76
|
|
77
|
|
78
|
for(Object child : rootElement.getChildren()){
|
79
|
if(child instanceof Element){
|
80
|
Element childElement = (Element) ((Element)child).clone();
|
81
|
|
82
|
String processedElementName = XMLHelper.getClazz(childElement);
|
83
|
|
84
|
childElement.detach();
|
85
|
childElement.setName(processedElementName);
|
86
|
childElement.removeAttribute("class");
|
87
|
|
88
|
result.add(childElement);
|
89
|
}
|
90
|
}
|
91
|
|
92
|
return result;
|
93
|
}
|
94
|
}
|