3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
;
12 import java
.io
.InputStream
;
14 import java
.util
.Date
;
15 import java
.util
.List
;
17 import javax
.xml
.parsers
.DocumentBuilder
;
18 import javax
.xml
.parsers
.DocumentBuilderFactory
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.joda
.time
.DateTime
;
22 import org
.w3c
.dom
.Document
;
23 import org
.w3c
.dom
.Element
;
24 import org
.w3c
.dom
.Node
;
25 import org
.w3c
.dom
.NodeList
;
27 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
30 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
37 public class AbcdParseUtility
{
39 private static final Logger logger
= Logger
.getLogger(AbcdParseUtility
.class);
42 public static URI
parseFirstUri(NodeList nodeList
, Abcd206ImportReport report
){
44 String textContent
= parseFirstTextContent(nodeList
);
45 if(textContent
!=null){
47 uri
= URI
.create(textContent
);
48 } catch (IllegalArgumentException e
) {
50 report
.addException("Exception during URI parsing!", e
);
57 public static String
parseFirstTextContent(NodeList nodeList
){
58 return parseFirstTextContent(nodeList
, true);
61 public static String
parseFirstTextContent(NodeList nodeList
, boolean cleanUpWhiteSpaces
){
63 if(nodeList
.getLength()>0){
64 string
= nodeList
.item(0).getTextContent();
65 if(cleanUpWhiteSpaces
){
66 string
= string
.replace("\n", "").replaceAll("( )+", " ").trim();
72 public static Double
parseFirstDouble(NodeList nodeList
, Abcd206ImportReport report
){
73 if(nodeList
.getLength()>0){
74 return parseDouble(nodeList
.item(0), report
);
79 public static Double
parseDouble(Node node
, Abcd206ImportReport report
){
80 String message
= "Could not parse double value for node " + node
.getNodeName();
81 Double doubleValue
= null;
83 String textContent
= node
.getTextContent();
85 textContent
= textContent
.replace(".","");
87 textContent
= textContent
.replace(",",".");
88 doubleValue
= Double
.parseDouble(textContent
);
89 } catch (NullPointerException npe
){
90 logger
.error(message
, npe
);
92 report
.addException(message
, npe
);
94 } catch (NumberFormatException nfe
){
95 logger
.error(message
, nfe
);
97 report
.addException(message
, nfe
);
103 public static DateTime
parseFirstDateTime(NodeList nodeList
) {
104 DateTime dateTime
= null;
105 String textContent
= parseFirstTextContent(nodeList
);
106 if(textContent
!=null){
107 dateTime
= DateTime
.parse(textContent
);
112 public static Date
parseFirstDate(NodeList nodeList
) {
114 DateTime dateTime
= parseFirstDateTime(nodeList
);
116 date
= dateTime
.toDate();
121 public static Reference
parseFirstReference(NodeList referenceNodeList
, ICdmApplicationConfiguration cdmAppController
){
122 String referenceCitation
= AbcdParseUtility
.parseFirstTextContent(referenceNodeList
);
123 //check if reference already exists
124 List
<Reference
> matchingReferences
= cdmAppController
.getReferenceService().findByTitle(Reference
.class, referenceCitation
, MatchMode
.EXACT
, null, null, null, null, null).getRecords();
125 Reference
<?
> reference
;
126 if(matchingReferences
.size()==1){
127 reference
= matchingReferences
.iterator().next();
130 reference
= ReferenceFactory
.newGeneric();
131 reference
.setTitle(referenceCitation
);
132 cdmAppController
.getReferenceService().saveOrUpdate(reference
);
138 * Return the wrapper with the list of root nodes for an ABCD XML file
139 * @param fileName: the file's location
140 * @return a wrapper with a list of root nodes ("Unit")
142 public static UnitAssociationWrapper
parseUnitsNodeList(InputStream inputStream
, Abcd206ImportReport report
) {
143 UnitAssociationWrapper unitAssociationWrapper
= new UnitAssociationWrapper();
144 NodeList unitList
= null;
146 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
147 DocumentBuilder builder
= factory
.newDocumentBuilder();
149 Document document
= builder
.parse(inputStream
);
150 Element root
= document
.getDocumentElement();
151 unitList
= root
.getElementsByTagName("Unit");
152 if (unitList
.getLength()>0) {
153 unitAssociationWrapper
.setPrefix("");
154 unitAssociationWrapper
.setAssociatedUnits(unitList
);
155 return unitAssociationWrapper
;
157 unitList
= root
.getElementsByTagName("abcd:Unit");
158 if (unitList
.getLength()>0) {
159 unitAssociationWrapper
.setPrefix("abcd:");
160 unitAssociationWrapper
.setAssociatedUnits(unitList
);
161 return unitAssociationWrapper
;
163 unitList
= root
.getElementsByTagName("abcd21:Unit");
164 if (unitList
.getLength()>0) {
165 unitAssociationWrapper
.setPrefix("abcd21:");
166 unitAssociationWrapper
.setAssociatedUnits(unitList
);
168 } catch (Exception e
) {
171 report
.addException("Exception during parsing of nodeList!", e
);
174 return unitAssociationWrapper
;