Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java @ 710:cded0924193d
Added generator and facet for error reports.
flys-artifacts/trunk@2166 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 20 Jun 2011 13:47:59 +0000 |
parents | 45cd58a2a2bb |
children | 035c0095b427 |
line wrap: on
line source
package de.intevation.flys.exports; import java.io.IOException; import java.io.OutputStream; import javax.xml.xpath.XPathConstants; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.jfree.data.Range; import de.intevation.artifacts.Artifact; import de.intevation.artifacts.CallContext; import de.intevation.artifacts.ArtifactNamespaceContext; import de.intevation.artifacts.common.utils.XMLUtils; import de.intevation.artifactdatabase.state.Facet; import de.intevation.flys.model.River; import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.resources.Resources; /** * The base class for chart creation. It should provide some basic things that * equal in all chart types. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public abstract class ChartGenerator implements OutGenerator { private static Logger logger = Logger.getLogger(ChartGenerator.class); /** The default chart width, if no other width is set.*/ public static final int DEFAULT_CHART_WIDTH = 600; /** The default chart height, if no other height is set.*/ public static final int DEFAULT_CHART_HEIGHT = 400; /** The XPath that points to the chart size of the incoming request * document.*/ public static final String XPATH_CHART_SIZE = "/art:action/art:attributes/art:size"; public static final String XPATH_CHART_X_RANGE = "/art:action/art:attributes/art:xrange"; public static final String XPATH_CHART_Y_RANGE = "/art:action/art:attributes/art:yrange"; /** 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 artifact that is used to decorate the chart with meta information.*/ protected Artifact master; public void init(Document request, OutputStream out, CallContext context) { logger.debug("ChartGenerator.init"); this.request = request; this.out = out; this.context = context; } public void setMasterArtifact(Artifact master) { this.master = master; } protected String msg(String key, String def) { return Resources.getMsg(context.getMeta(), key, def); } protected String msg(String key, String def, Object[] args) { return Resources.getMsg(context.getMeta(), key, def, args); } protected String getRiverName() { FLYSArtifact flys = (FLYSArtifact) master; River river = flys.getRiver(); return river != null ? river.getName() : ""; } protected double[] getRange() { FLYSArtifact flys = (FLYSArtifact) master; return flys.getDistance(); } /** * Returns the size of a chart export as array which has been specified by * the incoming request document. * * @return the size of a chart as [width, height] or the result of * getDefaultSize() if no width or height are given in the request document. */ protected int[] getSize() { int[] size = new int[2]; Node sizeEl = (Node) XMLUtils.xpath( request, XPATH_CHART_SIZE, XPathConstants.NODE, ArtifactNamespaceContext.INSTANCE); if (sizeEl != null) { String w = XMLUtils.xpathString( sizeEl, "@art:width", ArtifactNamespaceContext.INSTANCE); String h = XMLUtils.xpathString( sizeEl, "@art:height", ArtifactNamespaceContext.INSTANCE); if (w != null && w.length() > 0 && h != null && h.length() > 0) { try { size[0] = Integer.parseInt(w); size[1] = Integer.parseInt(h); } catch (NumberFormatException nfe) { logger.warn("Wrong values for chart width/height."); } } } return size[0] > 0 && size[1] > 0 ? size : getDefaultSize(); } protected Range getDomainAxisRange() { Node xrange = (Node) XMLUtils.xpath( request, XPATH_CHART_X_RANGE, XPathConstants.NODE, ArtifactNamespaceContext.INSTANCE); if (xrange == null) { return null; } String lower = XMLUtils.xpathString( xrange, "@art:from", ArtifactNamespaceContext.INSTANCE); String upper = XMLUtils.xpathString( xrange, "@art:to", ArtifactNamespaceContext.INSTANCE); logger.debug("FOUND X RANGE: " + lower + " -> " + upper); if (lower != null && upper != null) { try { double from = Double.parseDouble(lower); double to = Double.parseDouble(upper); if (from == 0 && to == 0) { logger.debug("No range specified. Lower and upper X == 0"); return null; } if (from > to) { double tmp = to; to = from; from = tmp; } return new Range(from, to); } catch (NumberFormatException nfe) { logger.warn("Wrong values for domain axis range."); } } return null; } protected Range getValueAxisRange() { Node yrange = (Node) XMLUtils.xpath( request, XPATH_CHART_Y_RANGE, XPathConstants.NODE, ArtifactNamespaceContext.INSTANCE); if (yrange == null) { return null; } String lower = XMLUtils.xpathString( yrange, "@art:from", ArtifactNamespaceContext.INSTANCE); String upper = XMLUtils.xpathString( yrange, "@art:to", ArtifactNamespaceContext.INSTANCE); if (lower != null && upper != null) { try { double from = Double.parseDouble(lower); double to = Double.parseDouble(upper); if (from == 0 && to == 0) { logger.debug("No range specified. Lower and upper Y == 0"); return null; } if (from > to) { double tmp = to; to = from; from = tmp; } return new Range(from, to); } catch (NumberFormatException nfe) { logger.warn("Wrong values for value axis range."); } } return null; } /** * Returns the default size of a chart export as array. * * @return the default size of a chart as [width, height]. */ protected int[] getDefaultSize() { return new int[] { DEFAULT_CHART_WIDTH, DEFAULT_CHART_HEIGHT }; } public abstract void doOut(Artifact artifact, Facet facet, Document attr); public abstract void generate() throws IOException; } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :