Project

General

Profile

Download (4.9 KB) Statistics
| Branch: | Tag: | Revision:
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.print;
11

    
12
import java.util.UUID;
13

    
14
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
15
import org.jdom.Element;
16
import org.jdom.JDOMException;
17
import org.jdom.Text;
18
import org.jdom.xpath.XPath;
19

    
20
/**
21
 * @author n.hoffmann
22
 * @since Apr 9, 2010
23
 * @version 1.0
24
 */
25
public class XMLHelper {
26
	private static final Logger logger = LogManager.getLogger(XMLHelper.class);
27
	
28
	public enum EntityType{
29
		CLASSIFICATION("Classification"), 
30
		TAXON_NODE("TaxonNode"),
31
		EMPTY("");
32
		
33
		
34
		private String cdmClassName;
35
		
36
		EntityType(String cdmClassName){
37
			this.cdmClassName = cdmClassName;
38
		}
39
		
40
		public static EntityType getEntityType(String cdmClassName){
41
			for(EntityType entityType : values()){
42
				if(entityType.getCdmClassName().equals(cdmClassName)){
43
					return entityType;
44
				}
45
			}
46
			return EMPTY;
47
		}
48

    
49
		private String getCdmClassName() {
50
			return cdmClassName;
51
		}
52
	}
53
	
54
	public static final String ELEMENT_CLASS = "class";
55
	public static final String ELEMENT_UUID = "uuid";
56
	public static final String ELEMENT_TITLE_CACHE = "titleCache";
57

    
58
	public static EntityType getEntityType(Element element) {
59
		return EntityType.getEntityType(getClazz(element));
60
	}
61
	
62
	public static UUID getUuid(Element element) {
63
		String content = getContent(element, ELEMENT_UUID);
64
		
65
		UUID uuid = null;
66
		
67
		try{
68
			uuid = UUID.fromString(content);
69
		}catch(IllegalArgumentException e){
70
			throw new RuntimeException(e);
71
		}
72
		
73
		return uuid;
74
	}
75
	
76
	public static String getClazz(Element element) {
77
		return getContent(element, ELEMENT_CLASS);
78
	}
79
	
80
	public static String getTitleCache(Element element) {
81
		return getContent(element, ELEMENT_TITLE_CACHE);
82
	}
83
	
84
	public static String getTitleCacheOfTaxonNode(Element treeNodeElement) {
85
		if(EntityType.TAXON_NODE.equals(getEntityType(treeNodeElement))){
86
			Element element;
87
			try {
88
				element = (Element) XPath.selectSingleNode(treeNodeElement, "taxon/name");
89
				if(element != null){
90
					return getContent(element, ELEMENT_TITLE_CACHE);
91
				}else{
92
					logger.error("Could not find title cache for taxonNode/taxon/name path");
93
				}
94
				
95
			} catch (JDOMException e) {
96
				logger.error("Could not find title cache for taxonNode/taxon/name path", e);
97
			} catch (Exception e) {
98
				logger.error("Exception while trying to retrieve title cache for taxon", e);
99
			}			
100
		}
101
		return getTitleCache(treeNodeElement);
102
	}
103
	
104
	public static Element getTaxonFromTaxonNode(Element taxonNodeElement) {
105
		if(EntityType.TAXON_NODE.equals(getEntityType(taxonNodeElement))){
106
			Element element;
107
			try {
108
				element = (Element) XPath.selectSingleNode(taxonNodeElement, "taxon");
109
				return element;
110
			} catch (JDOMException e) {
111
				logger.error("Could not find title cache for taxonNode/taxon/name path", e);
112
			} catch (Exception e) {
113
				logger.error("Exception while trying to retrieve title cache for taxon", e);
114
			}			
115
		}
116
		return null;
117
	}
118

    
119
	/**
120
	 * Adds an element as a child to another element
121
	 * 
122
	 * @param elementToAdd
123
	 * @param parentElement
124
	 */
125
	public static void addContent(Element elementToAdd, Element parentElement){
126
		elementToAdd.detach();
127
		parentElement.addContent(elementToAdd);
128
	}
129
	
130
	/**
131
	 * Adds an element to the child element of another element using the given name to select the child element
132
	 * 
133
	 * @param elementToAdd
134
	 * @param childElementName
135
	 * @param parentElement
136
	 */
137
	public static void addContent(Element elementToAdd, String childElementName, Element parentElement){
138
				
139
		if(elementToAdd == null){
140
			logger.warn("Retrieved a null element. Not adding it.");
141
			return;
142
		}
143
		if(parentElement == null){
144
			throw new IllegalArgumentException("Target element may not be null");
145
		}
146
		
147
		// create element if it did not exist before
148
		if(parentElement.getChild(childElementName) == null){
149
			parentElement.addContent(new Element(childElementName));
150
		}
151
		
152
		Element elementToAddClone = (Element) elementToAdd.clone();
153
		elementToAddClone.detach();
154
		
155
		Element childElement = parentElement.getChild(childElementName);
156
		childElement.addContent(elementToAddClone);
157
	}
158
	
159
	/*********** PRIVATE METHODS ********************/
160
	
161
	/**
162
	 * Returns the content of a child of <code>this</code> element 
163
	 * with the given element name.
164
	 * 
165
	 * @param elementName the name of a child element
166
	 * @return a String represting the content of the XML element with the given name
167
	 */
168
	private static String getContent(Element element, String elementName){
169
		if(element == null){
170
			logger.error("Element is null");
171
			return "";
172
		}
173
		
174
		Element contentElement = element.getChild(elementName);
175
		if(contentElement != null){
176
			Text content = (Text) contentElement.getContent().iterator().next();
177
			return content.getText();
178
		}
179
		return null;
180
	}
181
}
(9-9/10)