Project

General

Profile

Download (6.47 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.Representation;
24
import eu.etaxonomy.cdm.model.description.CategoricalData;
25
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26
import eu.etaxonomy.cdm.model.description.Feature;
27
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
28
import eu.etaxonomy.cdm.model.description.QuantitativeData;
29
import eu.etaxonomy.cdm.model.description.StateData;
30
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
31
import eu.etaxonomy.cdm.model.description.TaxonDescription;
32
import eu.etaxonomy.cdm.model.description.TextData;
33
import eu.etaxonomy.cdm.model.name.TaxonName;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36

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

    
45
public class PilotOutputDocumentBuilder {
46

    
47
	private SDDDataSet cdmSource;
48

    
49
	private static final Logger logger = Logger.getLogger(PilotOutputDocumentBuilder.class);
50

    
51
	// private SDDContext sddContext;
52

    
53
	public PilotOutputDocumentBuilder() throws IOException {
54

    
55
	}
56

    
57
	public void marshal(SDDDataSet cdmSource, String sddDestination) throws IOException {
58

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

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

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

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

    
81
			Taxon taxon = (Taxon) tb.next();
82
			texte = writeTaxonDescriptionsToHTML(taxon, texte);
83

    
84
		}
85
		texte += "</body>\n</html>\n";
86

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

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

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

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

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

    
123
			texte += "<ul type=\"circle\">\n";
124

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

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

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

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

    
172
			}
173

    
174
			texte += "</ul>\n";
175
		}
176
		return texte;
177
	}
178
}
179

    
180

    
(2-2/5)