Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java @ 445:a7947972fdeb
Added a new class that supports formatters for different types of data.
flys-artifacts/trunk@1937 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 17 May 2011 14:22:27 +0000 |
parents | e0fec407a280 |
children | 45cd58a2a2bb |
comparison
equal
deleted
inserted
replaced
444:932a5e3c7fa1 | 445:a7947972fdeb |
---|---|
1 package de.intevation.flys.exports; | 1 package de.intevation.flys.exports; |
2 | 2 |
3 import java.io.IOException; | 3 import java.io.IOException; |
4 import java.io.OutputStream; | 4 import java.io.OutputStream; |
5 import java.io.OutputStreamWriter; | 5 import java.io.OutputStreamWriter; |
6 import java.text.NumberFormat; | |
7 import java.util.Locale; | |
8 | 6 |
9 import org.w3c.dom.Document; | 7 import org.w3c.dom.Document; |
10 | 8 |
11 import org.apache.log4j.Logger; | 9 import org.apache.log4j.Logger; |
12 | 10 |
13 import au.com.bytecode.opencsv.CSVWriter; | 11 import au.com.bytecode.opencsv.CSVWriter; |
14 | 12 |
15 import de.intevation.artifacts.Artifact; | 13 import de.intevation.artifacts.Artifact; |
16 import de.intevation.artifacts.CallContext; | 14 import de.intevation.artifacts.CallContext; |
15 | |
16 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
17 import de.intevation.artifacts.common.utils.XMLUtils; | |
17 | 18 |
18 import de.intevation.flys.artifacts.resources.Resources; | 19 import de.intevation.flys.artifacts.resources.Resources; |
19 | 20 |
20 | 21 |
21 /** | 22 /** |
37 public static final String DEFAULT_CSV_CHARSET = "UTF-8"; | 38 public static final String DEFAULT_CSV_CHARSET = "UTF-8"; |
38 | 39 |
39 /** The default separator for the CSV export.*/ | 40 /** The default separator for the CSV export.*/ |
40 public static final char DEFAULT_CSV_SEPARATOR = ','; | 41 public static final char DEFAULT_CSV_SEPARATOR = ','; |
41 | 42 |
43 /** XPath that points to the desired export facet.*/ | |
44 public static final String XPATH_FACET = "/art:action/@art:type"; | |
45 | |
42 | 46 |
43 /** The document of the incoming out() request.*/ | 47 /** The document of the incoming out() request.*/ |
44 protected Document request; | 48 protected Document request; |
45 | 49 |
46 /** The output stream where the data should be written to.*/ | 50 /** The output stream where the data should be written to.*/ |
139 * @return true, if <i>facet</i> is valid, otherwise false. | 143 * @return true, if <i>facet</i> is valid, otherwise false. |
140 */ | 144 */ |
141 protected boolean isFacetValid(String facet) { | 145 protected boolean isFacetValid(String facet) { |
142 logger.debug("AbstractExporter.isFacetValid"); | 146 logger.debug("AbstractExporter.isFacetValid"); |
143 | 147 |
144 if (facet == null || facet.length() == 0) { | 148 String thisFacet = getFacet(); |
149 | |
150 if (thisFacet == null || thisFacet.length() == 0) { | |
145 return false; | 151 return false; |
146 } | 152 } |
147 else if (this.facet == null || this.facet.length() == 0) { | 153 else if (facet == null || facet.length() == 0) { |
148 logger.debug("Set the facet of this export: " + facet); | 154 return false; |
149 this.facet = facet; | |
150 | |
151 return true; | |
152 } | 155 } |
153 else { | 156 else { |
154 return this.facet.equals(facet); | 157 return thisFacet.equals(facet); |
155 } | 158 } |
156 } | 159 } |
157 | 160 |
161 | |
162 /** | |
163 * Returns the name of the desired facet. | |
164 * | |
165 * @return the name of the desired facet. | |
166 */ | |
167 protected String getFacet() { | |
168 if (facet == null) { | |
169 facet = getFacetFromRequest(); | |
170 } | |
171 | |
172 return facet; | |
173 } | |
174 | |
175 | |
176 /** | |
177 * Extracts the name of the requested facet from request document. | |
178 * | |
179 * @return the name of the requested facet. | |
180 */ | |
181 protected String getFacetFromRequest() { | |
182 return XMLUtils.xpathString( | |
183 request, XPATH_FACET, ArtifactNamespaceContext.INSTANCE); | |
184 } | |
158 | 185 |
159 | 186 |
160 protected String msg(String key, String def) { | 187 protected String msg(String key, String def) { |
161 return Resources.getMsg(context.getMeta(), key, def); | 188 return Resources.getMsg(context.getMeta(), key, def); |
162 } | 189 } |
179 | 206 |
180 writeCSVData(writer); | 207 writeCSVData(writer); |
181 | 208 |
182 writer.close(); | 209 writer.close(); |
183 } | 210 } |
184 | |
185 | |
186 protected NumberFormat getFormatter(int min, int max) { | |
187 Locale locale = Resources.getLocale(context.getMeta()); | |
188 NumberFormat nf = NumberFormat.getInstance(locale); | |
189 | |
190 nf.setMaximumFractionDigits(max); | |
191 nf.setMinimumFractionDigits(min); | |
192 | |
193 return nf; | |
194 } | |
195 } | 211 } |
196 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 212 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |