Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java @ 1106:e9f66d63bdd0
Write user defined barriers into a shapefile that is placed in the Artifact's directory.
flys-artifacts/trunk@2609 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 30 Aug 2011 11:09:54 +0000 |
parents | cded0924193d |
children | efe1b8545f5c |
line wrap: on
line source
package de.intevation.flys.exports; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import org.w3c.dom.Document; import org.apache.log4j.Logger; import au.com.bytecode.opencsv.CSVWriter; import de.intevation.artifacts.Artifact; import de.intevation.artifacts.CallContext; import de.intevation.artifactdatabase.state.Facet; import de.intevation.artifacts.common.ArtifactNamespaceContext; import de.intevation.artifacts.common.utils.XMLUtils; import de.intevation.flys.artifacts.resources.Resources; import de.intevation.flys.artifacts.FLYSArtifact; /** * An abstract exporter that implements some basic methods for exporting data of * artifacts. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public abstract class AbstractExporter implements OutGenerator { /** The logger used in this exporter.*/ private static Logger logger = Logger.getLogger(AbstractExporter.class); /** The name of the CSV facet which triggers the CSV creation.*/ public static final String FACET_CSV = "csv"; /** The default charset for the CSV export.*/ public static final String DEFAULT_CSV_CHARSET = "UTF-8"; /** The default separator for the CSV export.*/ public static final char DEFAULT_CSV_SEPARATOR = ','; /** XPath that points to the desired export facet.*/ public static final String XPATH_FACET = "/art:action/@art:type"; /** The document of the incoming out() request.*/ protected Document request; /** The output stream where the data should be written to.*/ protected OutputStream out; /** The CallContext object.*/ protected CallContext context; /** The selected facet.*/ protected String facet; /** The master artifact.*/ protected Artifact master; /** * Concrete subclasses need to use this method to write their special data * objects into the CSV document. * * @param writer The CSVWriter. */ protected abstract void writeCSVData(CSVWriter writer); /** * This method enables concrete subclasses to collected its own special * data. * * @param artifacts The artifact that stores the data that has to be * exported. */ protected abstract void addData(Object data); @Override public void init(Document request, OutputStream out, CallContext context) { logger.debug("AbstractExporter.init"); this.request = request; this.out = out; this.context = context; } @Override public void setMasterArtifact(Artifact master) { this.master = master; } /** * This doOut() just collects the data of multiple artifacts. Therefore, it * makes use of the addData() method which enables concrete subclasses to * store its data on its own. The real output creation takes place in the * concrete generate() methods. * * @param artifact The artifact. * @param facet The facet to add - NOTE: the facet needs to fit to the first * facet inserted into this exporter. Otherwise this artifact/facet is * skipped. * @param attr The attr document. */ @Override public void doOut(Artifact artifact, Facet facet, Document attr) { String name = facet.getName(); logger.debug("AbstractExporter.doOut: " + name); if (!isFacetValid(name)) { logger.warn("Facet '" + name + "' not valid. No output created!"); return; } FLYSArtifact flys = (FLYSArtifact)artifact; Facet nativeFacet = flys.getNativeFacet(facet); if (nativeFacet != null) { addData(nativeFacet.getData(flys, context)); } } /** * Generates an export based on a specified facet. */ @Override public void generate() throws IOException { logger.debug("AbstractExporter.generate"); if (facet != null && facet.equals(FACET_CSV)) { generateCSV(); } else { throw new IOException("invalid facet for exporter."); } } /** * Determines if the desired facet is valid for this exporter. If no facet * is currently set, <i>facet</i> is set. * * @param facet The desired facet. * * @return true, if <i>facet</i> is valid, otherwise false. */ protected boolean isFacetValid(String facet) { logger.debug("AbstractExporter.isFacetValid"); String thisFacet = getFacet(); if (thisFacet == null || thisFacet.length() == 0) { return false; } else if (facet == null || facet.length() == 0) { return false; } else { return thisFacet.equals(facet); } } /** * Returns the name of the desired facet. * * @return the name of the desired facet. */ protected String getFacet() { if (facet == null) { facet = getFacetFromRequest(); } return facet; } /** * Extracts the name of the requested facet from request document. * * @return the name of the requested facet. */ protected String getFacetFromRequest() { return XMLUtils.xpathString( request, XPATH_FACET, ArtifactNamespaceContext.INSTANCE); } protected String msg(String key, String def) { return Resources.getMsg(context.getMeta(), key, def); } /** * This method starts CSV creation. It makes use of writeCSVData() which has * to be implemented by concrete subclasses. */ protected void generateCSV() throws IOException { logger.info("AbstractExporter.generateCSV"); CSVWriter writer = new CSVWriter( new OutputStreamWriter( out, DEFAULT_CSV_CHARSET), DEFAULT_CSV_SEPARATOR); writeCSVData(writer); writer.close(); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :