Merge branch 'release/5.45.0'
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / taxonx2013 / TaxonXXMLFieldGetter.java
1 /**
2 * Copyright (C) 2007 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.taxonx2013;
10
11 import java.util.Map;
12
13 import org.apache.logging.log4j.LogManager;
14 import org.apache.logging.log4j.Logger;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Node;
17 import org.w3c.dom.NodeList;
18
19 import eu.etaxonomy.cdm.common.URI;
20 import eu.etaxonomy.cdm.model.common.CdmBase;
21 import eu.etaxonomy.cdm.model.description.Feature;
22 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
23 import eu.etaxonomy.cdm.model.reference.Reference;
24 import eu.etaxonomy.cdm.model.taxon.Classification;
25
26 public class TaxonXXMLFieldGetter {
27
28 private static final Logger logger = LogManager.getLogger();
29
30 private final Document doc;
31
32 private final NomenclaturalCode nomenclaturalCode;
33 private Classification classification;
34 private final TaxonXImport importer;
35 private final TaxonXImportState taxonXstate;
36 private TaxonXModsExtractor modsextractor;
37 private TaxonXTreatmentExtractor treatmentextractor ;
38
39 public TaxonXXMLFieldGetter(TaxonXDataHolder dataholder, String prefix,Document document, TaxonXImport taxonXImport,
40 TaxonXImportState taxonXstate, Classification classif, Map<String,Feature> featuresMap){
41 this.doc = document;
42 this.importer = taxonXImport;
43 this.nomenclaturalCode = taxonXstate.getConfig().getNomenclaturalCode();
44 this.classification = classif;
45 logger.info("CLASSIFICATION "+classification);
46 this.taxonXstate=taxonXstate;
47 modsextractor = new TaxonXModsExtractor(importer);
48 Reference originalSourceUrl =taxonXstate.getConfig().getOriginalSourceURL();
49 treatmentextractor = new TaxonXTreatmentExtractor(nomenclaturalCode,classification,importer, taxonXstate,featuresMap,originalSourceUrl );
50 }
51
52
53 /**
54 * parse the Mods from the TaxonX file
55 *
56 *@return the created Reference object
57 **/
58 public Reference parseMods(){
59 // System.out.println("PARSEMODS");
60 //taxonx
61 Node root = doc.getFirstChild();
62
63
64 //taxonHeader, taxonBody
65 NodeList nodes = root.getChildNodes();
66 Reference ref = null;
67 for (int i=0; i< nodes.getLength();i++) {
68 // System.out.println(nodes.item(i).getNodeName());
69 if (nodes.item(i).getNodeName().equalsIgnoreCase("tax:taxonxheader")){
70 NodeList nodes2 = nodes.item(i).getChildNodes();
71 for (int j=0; j< nodes2.getLength();j++){
72 if (nodes2.item(j).getNodeName().equalsIgnoreCase("mods:mods")){
73 ref = modsextractor.extractMods(nodes2.item(j));
74 importer.getReferenceService().saveOrUpdate(ref);
75 }
76 }
77 }
78 }
79 if (ref!=null) {
80 taxonXstate.getConfig().setClassificationName(ref.getCitation());
81 } else {
82 taxonXstate.getConfig().setClassificationName("no reference title");
83 }
84 ref=CdmBase.deproxy(ref, Reference.class);
85 return ref;
86 }
87
88
89 /**
90 * Foreach treatment section, launches the treatment "extractor"
91 * @param ref : the current reference, extracted from the mods
92 * @param sourcename: the URI of the TaxonX document
93 */
94 public void parseTreatment(Reference ref, URI sourceName){
95 System.out.println("PARSETREATMENT "+ref);
96 //taxonx
97 Node root = doc.getFirstChild();
98 //taxonHeader, taxonBody
99 NodeList nodes = root.getChildNodes();
100
101 for ( int i=0; i< nodes.getLength();i++) {
102 // System.out.println(nodes.item(i).getNodeName());
103 if (nodes.item(i).getNodeName().equalsIgnoreCase("tax:taxonxBody")){
104 NodeList nodes2 = nodes.item(i).getChildNodes();
105 for (int j=0; j< nodes2.getLength();j++){
106 if (nodes2.item(j).getNodeName().equalsIgnoreCase("tax:treatment")){
107 try {
108 treatmentextractor.extractTreatment(nodes2.item(j), ref,sourceName);
109 } catch (Exception e) {
110 logger.error("Unhandled exception occurred in treatment. Treatment not fully imported.");
111 e.printStackTrace();
112 }
113 }
114 }
115 }
116 }
117 }
118
119
120
121 /**
122 * updates the classification in the treatment extractor
123 * @param classification2
124 */
125 public void updateClassification(Classification classification2) {
126 //System.out.println("UPDATECLASSIFICATIONS "+classification2);
127 classification=classification2;
128 if (treatmentextractor != null) {
129 treatmentextractor.updateClassification(classification);
130 }
131
132 }
133
134
135 /**
136 * @return
137 */
138 public Map<String,Feature> getFeaturesUsed() {
139 return treatmentextractor.getFeaturesUsed();
140 }
141
142
143 }