cleanup
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / specimen / abcd206 / in / AbcdParseUtility.java
1 /**
2 * Copyright (C) 2015 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 package eu.etaxonomy.cdm.io.specimen.abcd206.in;
10
11 import java.io.InputStream;
12 import java.net.URI;
13 import java.util.Date;
14 import java.util.List;
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 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;
31
32 /**
33 * @author pplitzner
34 * @since 16.06.2015
35 */
36 public class AbcdParseUtility {
37
38 private static final Logger logger = Logger.getLogger(AbcdParseUtility.class);
39
40 public static URI parseFirstUri(NodeList nodeList, SpecimenImportReport report){
41 URI uri = null;
42 String textContent = parseFirstTextContent(nodeList);
43 if(textContent!=null){
44 try {
45 uri = URI.create(textContent);
46 } catch (IllegalArgumentException e) {
47 if(report!=null){
48 report.addException("Exception during URI parsing!", e);
49 }
50 }
51 }
52 return uri;
53 }
54
55 public static String parseFirstTextContent(NodeList nodeList){
56 return parseFirstTextContent(nodeList, true);
57 }
58
59 public static String parseFirstTextContent(NodeList nodeList, boolean cleanUpWhiteSpaces){
60 String string = null;
61 if(nodeList!=null && nodeList.getLength()>0){
62 string = nodeList.item(0).getTextContent();
63 if(cleanUpWhiteSpaces){
64 string = string.replace("\n", "").replaceAll("( )+", " ").trim();
65 }
66 }
67 return string;
68 }
69
70 public static Double parseFirstDouble(NodeList nodeList, SpecimenImportReport report){
71 if(nodeList.getLength()>0){
72 return parseDouble(nodeList.item(0), report);
73 }
74 return null;
75 }
76
77 public static Double parseDouble(Node node, SpecimenImportReport report){
78 String message = "Could not parse double value for node " + node.getNodeName();
79 Double doubleValue = null;
80 try{
81 String textContent = node.getTextContent();
82 //remove 1000 dots
83 textContent = textContent.replace(".","");
84 //convert commmas
85 textContent = textContent.replace(",",".");
86 if (!CdmUtils.isBlank(textContent)){
87 doubleValue = Double.parseDouble(textContent);
88 }
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, ICdmRepository cdmAppController){
122 String referenceCitation = AbcdParseUtility.parseFirstTextContent(referenceNodeList);
123 //check if reference already exists
124 List<Reference> matchingReferences = cdmAppController.getReferenceService().findByTitleWithRestrictions(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, SpecimenImportReport 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 }