2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
;
11 import java
.io
.InputStream
;
13 import java
.util
.Date
;
14 import java
.util
.List
;
16 import javax
.xml
.parsers
.DocumentBuilder
;
17 import javax
.xml
.parsers
.DocumentBuilderFactory
;
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
;
26 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
27 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
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
, SpecimenImportReport 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
!=null && 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
, SpecimenImportReport report
){
73 if(nodeList
.getLength()>0){
74 return parseDouble(nodeList
.item(0), report
);
79 public static Double
parseDouble(Node node
, SpecimenImportReport 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 if (!CdmUtils
.isBlank(textContent
)){
89 doubleValue
= Double
.parseDouble(textContent
);
91 } catch (NullPointerException npe
){
92 logger
.error(message
, npe
);
94 report
.addException(message
, npe
);
96 } catch (NumberFormatException nfe
){
97 logger
.error(message
, nfe
);
99 report
.addException(message
, nfe
);
105 public static DateTime
parseFirstDateTime(NodeList nodeList
) {
106 DateTime dateTime
= null;
107 String textContent
= parseFirstTextContent(nodeList
);
108 if(textContent
!=null){
109 dateTime
= DateTime
.parse(textContent
);
114 public static Date
parseFirstDate(NodeList nodeList
) {
116 DateTime dateTime
= parseFirstDateTime(nodeList
);
118 date
= dateTime
.toDate();
123 public static Reference
parseFirstReference(NodeList referenceNodeList
, ICdmRepository cdmAppController
){
124 String referenceCitation
= AbcdParseUtility
.parseFirstTextContent(referenceNodeList
);
125 //check if reference already exists
126 List
<Reference
> matchingReferences
= cdmAppController
.getReferenceService().findByTitle(Reference
.class, referenceCitation
, MatchMode
.EXACT
, null, null, null, null, null).getRecords();
128 if(matchingReferences
.size()==1){
129 reference
= matchingReferences
.iterator().next();
132 reference
= ReferenceFactory
.newGeneric();
133 reference
.setTitle(referenceCitation
);
134 cdmAppController
.getReferenceService().saveOrUpdate(reference
);
140 * Return the wrapper with the list of root nodes for an ABCD XML file
141 * @param fileName: the file's location
142 * @return a wrapper with a list of root nodes ("Unit")
144 public static UnitAssociationWrapper
parseUnitsNodeList(InputStream inputStream
, SpecimenImportReport report
) {
145 UnitAssociationWrapper unitAssociationWrapper
= new UnitAssociationWrapper();
146 NodeList unitList
= null;
148 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
149 DocumentBuilder builder
= factory
.newDocumentBuilder();
151 Document document
= builder
.parse(inputStream
);
152 Element root
= document
.getDocumentElement();
153 unitList
= root
.getElementsByTagName("Unit");
154 if (unitList
.getLength()>0) {
155 unitAssociationWrapper
.setPrefix("");
156 unitAssociationWrapper
.setAssociatedUnits(unitList
);
157 return unitAssociationWrapper
;
159 unitList
= root
.getElementsByTagName("abcd:Unit");
160 if (unitList
.getLength()>0) {
161 unitAssociationWrapper
.setPrefix("abcd:");
162 unitAssociationWrapper
.setAssociatedUnits(unitList
);
163 return unitAssociationWrapper
;
165 unitList
= root
.getElementsByTagName("abcd21:Unit");
166 if (unitList
.getLength()>0) {
167 unitAssociationWrapper
.setPrefix("abcd21:");
168 unitAssociationWrapper
.setAssociatedUnits(unitList
);
170 } catch (Exception e
) {
173 report
.addException("Exception during parsing of nodeList!", e
);
176 return unitAssociationWrapper
;