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