Project

General

Profile

Download (6.51 KB) Statistics
| Branch: | Tag: | Revision:
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

    
10
package eu.etaxonomy.cdm.io.pilotOutputHtml;
11

    
12
import java.io.FileWriter;
13
import java.io.IOException;
14
import java.util.Iterator;
15
import java.util.Set;
16

    
17
import javax.xml.bind.Marshaller;
18

    
19
import org.apache.log4j.Logger;
20

    
21
import eu.etaxonomy.cdm.io.jaxb.CdmMarshallerListener;
22
import eu.etaxonomy.cdm.io.sdd.out.SDDDataSet;
23
import eu.etaxonomy.cdm.model.common.Language;
24
import eu.etaxonomy.cdm.model.common.LanguageString;
25
import eu.etaxonomy.cdm.model.common.Representation;
26
import eu.etaxonomy.cdm.model.description.CategoricalData;
27
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
28
import eu.etaxonomy.cdm.model.description.Feature;
29
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
30
import eu.etaxonomy.cdm.model.description.QuantitativeData;
31
import eu.etaxonomy.cdm.model.description.StateData;
32
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
33
import eu.etaxonomy.cdm.model.description.TaxonDescription;
34
import eu.etaxonomy.cdm.model.description.TextData;
35
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38

    
39
/**
40
 * Writes the SDD XML file. 
41
 * 
42
 * @author h.fradin
43
 * @created 10.12.2008
44
 * @version 1.0
45
 */
46

    
47
public class PilotOutputDocumentBuilder {
48

    
49
	private SDDDataSet cdmSource;
50

    
51
	private static final Logger logger = Logger.getLogger(PilotOutputDocumentBuilder.class);
52

    
53
	// private SDDContext sddContext;
54

    
55
	public PilotOutputDocumentBuilder() throws IOException {
56

    
57
	}
58

    
59
	public void marshal(SDDDataSet cdmSource, String sddDestination) throws IOException {
60

    
61
		this.cdmSource = cdmSource;
62
		Marshaller marshaller;		
63
		CdmMarshallerListener marshallerListener = new CdmMarshallerListener();
64
		logger.info("Start marshalling");
65
		writeCDMtoHTML(sddDestination);
66
	}
67

    
68
	/**Write the HTML document.
69
	 * @param base
70
	 * @throws IOException
71
	 */
72
	public void writeCDMtoHTML(String sddDestination) throws IOException {
73

    
74
		FileWriter writer = null;
75
		String texte = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<html>\n<head>\n";
76
		texte += "<title>Taxon descriptions included in the CDM base</title>\n";
77
		texte += "</head>\n";
78
		texte += "<body bgcolor=\"#FFFFE8\" topmargin=\"5\">\n";
79
		texte += "<h1 align=\"center\">Taxon descriptions included in the CDM base</h1>\n";
80

    
81
		for (Iterator<? extends TaxonBase> tb = this.cdmSource.getTaxa().iterator() ; tb.hasNext() ;){
82

    
83
			Taxon taxon = (Taxon) tb.next();
84
			texte = writeTaxonDescriptionsToHTML(taxon, texte);
85

    
86
		}
87
		texte += "</body>\n</html>\n";
88

    
89
		try{
90
			writer = new FileWriter(sddDestination, true);
91
			writer.write(texte,0,texte.length());
92
		}catch(IOException ex){
93
			ex.printStackTrace();
94
		}finally{
95
			if(writer != null){
96
				writer.close();
97
			}
98
		}
99
	}
100

    
101
	/**Write the HTML part for one taxon.
102
	 * @param base
103
	 * @throws IOException
104
	 */
105
	public String writeTaxonDescriptionsToHTML(Taxon taxon, String texte) throws IOException {
106

    
107
		TaxonNameBase taxonName = taxon.getName();
108
		String name = taxonName.getTitleCache();
109
		Set<TaxonDescription> descriptions = taxon.getDescriptions();
110
		for (Iterator<TaxonDescription> td = descriptions.iterator() ; td.hasNext() ;){
111
			TaxonDescription description = td.next();
112

    
113
			if (!description.getTitleCache().equals("")) {
114
				texte += "<h3>Name of description: " + description.getTitleCache() + "</h3>\n";
115
			} else {
116
				texte += "No title for this description.<br/>\n";
117
			}
118

    
119
			if (!name.equals("")) {
120
				texte += "<h3>Taxon name: " + name + "</h3>\n";
121
			} else {
122
				texte += "No taxon name attached to this description.<br/><br/>\n";
123
			}
124

    
125
			texte += "<ul type=\"circle\">\n";
126

    
127
			for (Iterator<? extends DescriptionElementBase> dep = description.getElements().iterator() ; dep.hasNext() ;){
128
				DescriptionElementBase descriptionElement = (DescriptionElementBase) dep.next();
129

    
130
				if (descriptionElement instanceof CategoricalData) {
131
					CategoricalData categorical = (CategoricalData) descriptionElement;
132
					Feature feature = categorical.getFeature();
133
					Representation representation = (Representation) feature.getRepresentations().toArray()[0];
134
					texte += "<li>Categorical data associated with feature: <b>" + representation.getLabel() + "</b><br/>\nStates: ";
135
					for (Iterator<? extends StateData> sd = categorical.getStates().iterator() ; sd.hasNext() ;){
136
						StateData stateData = (StateData) sd.next();
137
						texte += ((Representation) stateData.getState().getRepresentations().toArray()[0]).getLabel();
138
						if (sd.hasNext()) texte += "; "; else texte += ".<br/>\n";
139
					}
140
					texte += "<br/>\n";
141
				}
142

    
143
				if (descriptionElement instanceof QuantitativeData) {
144
					QuantitativeData quantitative = (QuantitativeData) descriptionElement;
145
					Feature feature = quantitative.getFeature();
146
					Representation representation = (Representation) feature.getRepresentations().toArray()[0];
147
					texte += "<li>Quantitative data associated with feature: <b>" + representation.getLabel() + "</b><br/>\n";
148
					MeasurementUnit mu = quantitative.getUnit();
149
					String unit = ((Representation) mu.getRepresentations().toArray()[0]).getLabel();
150
					if (!unit.equals("")) texte += "Measurement unit: " + unit + "<br/>\n";
151
					for (Iterator<? extends StatisticalMeasurementValue> smv = quantitative.getStatisticalValues().iterator() ; smv.hasNext() ;){
152
						StatisticalMeasurementValue statistical = (StatisticalMeasurementValue) smv.next();
153
						texte += ((Representation) statistical.getType().getRepresentations().toArray()[0]).getLabel();
154
						texte += " = " + statistical.getValue() + "<br/>\n";
155
					}
156
					texte += "<br/>\n";
157
				}
158

    
159
				if (descriptionElement instanceof TextData) {
160
					TextData text = (TextData) descriptionElement;
161
					Feature feature = text.getFeature();
162
					Representation representation = (Representation) feature.getRepresentations().toArray()[0];
163
					texte += "<li>Text data associated with feature: <b>" + representation.getLabel() + "</b><br/>\n";
164
					texte += "Text: " + ((LanguageString) text.getMultilanguageText().get(((Language) text.getMultilanguageText().keySet().toArray()[0]))).getText() + "<br/>\n";
165
					texte += "<br/>\n";
166
				}
167

    
168
			}
169

    
170
			texte += "</ul>\n";
171
		}
172
		return texte;
173
	}
174
}
175

    
176

    
(2-2/5)