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