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