Project

General

Profile

Download (6.33 KB) Statistics
| Branch: | Tag: | Revision:
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
 */
37
public class AbcdParseUtility {
38

    
39
    private static final Logger logger = Logger.getLogger(AbcdParseUtility.class);
40

    
41

    
42
    public static URI parseFirstUri(NodeList nodeList, SpecimenImportReport 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!=null && 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, SpecimenImportReport 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, SpecimenImportReport 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
            if (!CdmUtils.isBlank(textContent)){
89
                doubleValue = Double.parseDouble(textContent);
90
            }
91
        } catch (NullPointerException npe){
92
            logger.error(message, npe);
93
            if(report!=null){
94
                report.addException(message, npe);
95
            }
96
        } catch (NumberFormatException nfe){
97
            logger.error(message, nfe);
98
            if(report!=null){
99
                report.addException(message, nfe);
100
            }
101
        }
102
        return doubleValue;
103
    }
104

    
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);
110
        }
111
        return dateTime;
112
    }
113

    
114
    public static Date parseFirstDate(NodeList nodeList) {
115
        Date date = null;
116
        DateTime dateTime = parseFirstDateTime(nodeList);
117
        if(dateTime!=null){
118
            date = dateTime.toDate();
119
        }
120
        return date;
121
    }
122

    
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();
127
        Reference reference;
128
        if(matchingReferences.size()==1){
129
            reference = matchingReferences.iterator().next();
130
        }
131
        else{
132
            reference = ReferenceFactory.newGeneric();
133
            reference.setTitle(referenceCitation);
134
            cdmAppController.getReferenceService().saveOrUpdate(reference);
135
        }
136
        return reference;
137
    }
138

    
139
    /**
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")
143
     */
144
    public static UnitAssociationWrapper parseUnitsNodeList(InputStream inputStream, SpecimenImportReport report) {
145
        UnitAssociationWrapper unitAssociationWrapper = new UnitAssociationWrapper();
146
        NodeList unitList = null;
147
        try {
148
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
149
            DocumentBuilder builder = factory.newDocumentBuilder();
150

    
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;
158
            }
159
            unitList = root.getElementsByTagName("abcd:Unit");
160
            if (unitList.getLength()>0) {
161
                unitAssociationWrapper.setPrefix("abcd:");
162
                unitAssociationWrapper.setAssociatedUnits(unitList);
163
                return unitAssociationWrapper;
164
            }
165
            unitList = root.getElementsByTagName("abcd21:Unit");
166
            if (unitList.getLength()>0) {
167
                unitAssociationWrapper.setPrefix("abcd21:");
168
                unitAssociationWrapper.setAssociatedUnits(unitList);
169
            }
170
        } catch (Exception e) {
171
            logger.warn(e);
172
            if(report!=null){
173
                report.addException("Exception during parsing of nodeList!", e);
174
            }
175
        }
176
        return unitAssociationWrapper;
177
    }
178

    
179
}
(8-8/15)