1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 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
|
package eu.etaxonomy.cdm.io.specimen.abcd206.in;
|
11
|
|
12
|
import java.io.InputStream;
|
13
|
import java.net.URI;
|
14
|
import java.util.Date;
|
15
|
|
16
|
import javax.xml.parsers.DocumentBuilder;
|
17
|
import javax.xml.parsers.DocumentBuilderFactory;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.joda.time.DateTime;
|
21
|
import org.w3c.dom.Document;
|
22
|
import org.w3c.dom.Element;
|
23
|
import org.w3c.dom.Node;
|
24
|
import org.w3c.dom.NodeList;
|
25
|
|
26
|
/**
|
27
|
* @author pplitzner
|
28
|
* @date 16.06.2015
|
29
|
*
|
30
|
*/
|
31
|
public class AbcdParseUtility {
|
32
|
|
33
|
private static final Logger logger = Logger.getLogger(AbcdParseUtility.class);
|
34
|
|
35
|
|
36
|
public static URI parseFirstUri(NodeList nodeList){
|
37
|
URI uri = null;
|
38
|
String textContent = parseFirstTextContent(nodeList);
|
39
|
if(textContent!=null){
|
40
|
try {
|
41
|
uri = URI.create(textContent);
|
42
|
} catch (IllegalArgumentException e) {
|
43
|
//nothing
|
44
|
}
|
45
|
}
|
46
|
return uri;
|
47
|
}
|
48
|
|
49
|
public static String parseFirstTextContent(NodeList nodeList){
|
50
|
return parseFirstTextContent(nodeList, true);
|
51
|
}
|
52
|
|
53
|
public static String parseFirstTextContent(NodeList nodeList, boolean cleanUpWhiteSpaces){
|
54
|
String string = null;
|
55
|
if(nodeList.getLength()>0){
|
56
|
string = nodeList.item(0).getTextContent();
|
57
|
if(cleanUpWhiteSpaces){
|
58
|
string = string.replace("\n", "").replaceAll("( )+", " ").trim();
|
59
|
}
|
60
|
}
|
61
|
return string;
|
62
|
}
|
63
|
|
64
|
public static Double parseFirstDouble(NodeList nodeList){
|
65
|
if(nodeList.getLength()>0){
|
66
|
return parseDouble(nodeList.item(0));
|
67
|
}
|
68
|
return null;
|
69
|
}
|
70
|
|
71
|
public static Double parseDouble(Node node){
|
72
|
String message = "Could not parse double value for node " + node.getNodeName();
|
73
|
Double doubleValue = null;
|
74
|
try{
|
75
|
String textContent = node.getTextContent();
|
76
|
//remove 1000 dots
|
77
|
textContent = textContent.replace(".","");
|
78
|
//convert commmas
|
79
|
textContent = textContent.replace(",",".");
|
80
|
doubleValue = Double.parseDouble(textContent);
|
81
|
} catch (NullPointerException npe){
|
82
|
logger.error(message, npe);
|
83
|
} catch (NumberFormatException nfe){
|
84
|
logger.error(message, nfe);
|
85
|
}
|
86
|
return doubleValue;
|
87
|
}
|
88
|
|
89
|
public static DateTime parseFirstDateTime(NodeList nodeList) {
|
90
|
DateTime dateTime = null;
|
91
|
String textContent = parseFirstTextContent(nodeList);
|
92
|
if(textContent!=null){
|
93
|
dateTime = DateTime.parse(textContent);
|
94
|
}
|
95
|
return dateTime;
|
96
|
}
|
97
|
|
98
|
public static Date parseFirstDate(NodeList nodeList) {
|
99
|
Date date = null;
|
100
|
DateTime dateTime = parseFirstDateTime(nodeList);
|
101
|
date = dateTime.toDate();
|
102
|
return date;
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Return the list of root nodes for an ABCD XML file
|
107
|
* @param fileName: the file's location
|
108
|
* @return the list of root nodes ("Unit")
|
109
|
*/
|
110
|
public static NodeList getUnitsNodeList(Abcd206ImportState state) {
|
111
|
InputStream inputStream = state.getConfig().getSource();
|
112
|
NodeList unitList = null;
|
113
|
try {
|
114
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
115
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
116
|
|
117
|
Document document = builder.parse(inputStream);
|
118
|
Element root = document.getDocumentElement();
|
119
|
unitList = root.getElementsByTagName("Unit");
|
120
|
if (unitList.getLength() == 0) {
|
121
|
unitList = root.getElementsByTagName("abcd:Unit");
|
122
|
}
|
123
|
if (unitList.getLength() == 0) {
|
124
|
unitList = root.getElementsByTagName("abcd21:Unit");
|
125
|
}
|
126
|
} catch (Exception e) {
|
127
|
logger.warn(e);
|
128
|
}
|
129
|
return unitList;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Return the prefix an ABCD XML file
|
134
|
* @param fileName: the file's location
|
135
|
* @return the prefix
|
136
|
*/
|
137
|
public static String getPrefix(Abcd206ImportState state) {
|
138
|
InputStream inputStream = state.getConfig().getSource();
|
139
|
NodeList unitList = null;
|
140
|
try {
|
141
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
142
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
143
|
|
144
|
Document document = builder.parse(inputStream);
|
145
|
Element root = document.getDocumentElement();
|
146
|
unitList = root.getElementsByTagName("Unit");
|
147
|
if (unitList.getLength()>0) {
|
148
|
return null;
|
149
|
}
|
150
|
unitList = root.getElementsByTagName("abcd:Unit");
|
151
|
if (unitList.getLength()>0) {
|
152
|
return "abcd:";
|
153
|
}
|
154
|
unitList = root.getElementsByTagName("abcd21:Unit");
|
155
|
if (unitList.getLength() == 0) {
|
156
|
return "abcd21:";
|
157
|
}
|
158
|
} catch (Exception e) {
|
159
|
logger.warn(e);
|
160
|
}
|
161
|
return null;
|
162
|
}
|
163
|
|
164
|
}
|