Merge branch 'release/3.12.0'
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / specimen / abcd206 / in / AbcdParseUtility.java
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 import java.util.List;
16
17 import javax.xml.parsers.DocumentBuilder;
18 import javax.xml.parsers.DocumentBuilderFactory;
19
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;
26
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;
31
32 /**
33 * @author pplitzner
34 * @date 16.06.2015
35 *
36 */
37 public class AbcdParseUtility {
38
39 private static final Logger logger = Logger.getLogger(AbcdParseUtility.class);
40
41
42 public static URI parseFirstUri(NodeList nodeList, Abcd206ImportReport report){
43 URI uri = null;
44 String textContent = parseFirstTextContent(nodeList);
45 if(textContent!=null){
46 try {
47 uri = URI.create(textContent);
48 } catch (IllegalArgumentException e) {
49 if(report!=null){
50 report.addException("Exception during URI parsing!", e);
51 }
52 }
53 }
54 return uri;
55 }
56
57 public static String parseFirstTextContent(NodeList nodeList){
58 return parseFirstTextContent(nodeList, true);
59 }
60
61 public static String parseFirstTextContent(NodeList nodeList, boolean cleanUpWhiteSpaces){
62 String string = null;
63 if(nodeList.getLength()>0){
64 string = nodeList.item(0).getTextContent();
65 if(cleanUpWhiteSpaces){
66 string = string.replace("\n", "").replaceAll("( )+", " ").trim();
67 }
68 }
69 return string;
70 }
71
72 public static Double parseFirstDouble(NodeList nodeList, Abcd206ImportReport report){
73 if(nodeList.getLength()>0){
74 return parseDouble(nodeList.item(0), report);
75 }
76 return null;
77 }
78
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;
82 try{
83 String textContent = node.getTextContent();
84 //remove 1000 dots
85 textContent = textContent.replace(".","");
86 //convert commmas
87 textContent = textContent.replace(",",".");
88 doubleValue = Double.parseDouble(textContent);
89 } catch (NullPointerException npe){
90 logger.error(message, npe);
91 if(report!=null){
92 report.addException(message, npe);
93 }
94 } catch (NumberFormatException nfe){
95 logger.error(message, nfe);
96 if(report!=null){
97 report.addException(message, nfe);
98 }
99 }
100 return doubleValue;
101 }
102
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);
108 }
109 return dateTime;
110 }
111
112 public static Date parseFirstDate(NodeList nodeList) {
113 Date date = null;
114 DateTime dateTime = parseFirstDateTime(nodeList);
115 if(dateTime!=null){
116 date = dateTime.toDate();
117 }
118 return date;
119 }
120
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();
128 }
129 else{
130 reference = ReferenceFactory.newGeneric();
131 reference.setTitle(referenceCitation);
132 cdmAppController.getReferenceService().saveOrUpdate(reference);
133 }
134 return reference;
135 }
136
137 /**
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")
141 */
142 public static UnitAssociationWrapper parseUnitsNodeList(InputStream inputStream, Abcd206ImportReport report) {
143 UnitAssociationWrapper unitAssociationWrapper = new UnitAssociationWrapper();
144 NodeList unitList = null;
145 try {
146 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
147 DocumentBuilder builder = factory.newDocumentBuilder();
148
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;
156 }
157 unitList = root.getElementsByTagName("abcd:Unit");
158 if (unitList.getLength()>0) {
159 unitAssociationWrapper.setPrefix("abcd:");
160 unitAssociationWrapper.setAssociatedUnits(unitList);
161 return unitAssociationWrapper;
162 }
163 unitList = root.getElementsByTagName("abcd21:Unit");
164 if (unitList.getLength()>0) {
165 unitAssociationWrapper.setPrefix("abcd21:");
166 unitAssociationWrapper.setAssociatedUnits(unitList);
167 }
168 } catch (Exception e) {
169 logger.warn(e);
170 if(report!=null){
171 report.addException("Exception during parsing of nodeList!", e);
172 }
173 }
174 return unitAssociationWrapper;
175 }
176
177 }