teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.exports; ingo@1986: ingo@1994: import javax.xml.xpath.XPathConstants; ingo@1994: ingo@1994: import org.w3c.dom.Node; ingo@1994: import org.w3c.dom.NodeList; ingo@1994: ingo@1994: import org.apache.log4j.Logger; ingo@1994: teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils; ingo@1994: teichmann@5831: import org.dive4elements.artifactdatabase.state.DefaultSection; teichmann@5831: import org.dive4elements.artifactdatabase.state.DefaultSettings; teichmann@5831: import org.dive4elements.artifactdatabase.state.Section; ingo@1986: ingo@1986: ingo@1986: /** ingo@1986: * @author Ingo Weinzierl ingo@1986: */ ingo@1986: public class ChartSettings extends DefaultSettings { ingo@1986: teichmann@8202: private static final Logger log = Logger.getLogger(ChartSettings.class); ingo@1994: ingo@2046: protected ChartSection chartSection; ingo@2046: protected LegendSection legendSection; ingo@2056: protected ExportSection exportSection; ingo@2046: protected Section axesSection; ingo@1986: ingo@1986: ingo@1986: public ChartSettings() { ingo@1986: super(); ingo@1986: ingo@1986: axesSection = new DefaultSection("axes"); ingo@1986: addSection(axesSection); ingo@1986: } ingo@1986: ingo@1986: ingo@1986: /** ingo@1986: * Sets the chart section. Old chart sections are removed. ingo@1986: * ingo@1986: * @param chartSection A new Section that stores chart specific attributes. ingo@1986: */ ingo@2046: public void setChartSection(ChartSection chartSection) { ingo@2046: ChartSection oldChartSection = getChartSection(); ingo@1986: ingo@1986: if (oldChartSection != null) { ingo@1986: removeSection(oldChartSection); ingo@1986: } ingo@1986: ingo@1986: this.chartSection = chartSection; ingo@1986: addSection(chartSection); ingo@1986: } ingo@1986: ingo@1986: ingo@1986: /** ingo@1986: * Returns the Section that stores chart specific attributes. ingo@1986: * ingo@1986: * @return the Section that stores chart specific attributes. ingo@1986: */ ingo@2046: public ChartSection getChartSection() { ingo@1986: return chartSection; ingo@1986: } ingo@1986: ingo@1986: ingo@1986: /** ingo@1990: * Sets the legend section. Old legend sections are removed. ingo@1990: * ingo@1990: * @param legendSection A new Section that stores legend specific ingo@1990: * attributes. ingo@1990: */ ingo@2046: public void setLegendSection(LegendSection legendSection) { ingo@2046: LegendSection oldLegendSection = getLegendSection(); ingo@1990: ingo@1990: if (oldLegendSection != null) { ingo@1990: removeSection(oldLegendSection); ingo@1990: } ingo@1990: ingo@1990: this.legendSection = legendSection; ingo@1990: addSection(legendSection); ingo@1990: } ingo@1990: ingo@1990: ingo@1990: /** ingo@1990: * Returns the Section that stores legend specific attributes. ingo@1990: * ingo@1990: * @return the Section that stores legend specific attributes. ingo@1990: */ ingo@2046: public LegendSection getLegendSection() { ingo@1990: return legendSection; ingo@1990: } ingo@1990: ingo@1990: ingo@1990: /** ingo@2056: * Sets the export section. Old export sections are removed. ingo@2056: * ingo@2056: * @param exportSection A new Section that stores export specific ingo@2056: * attributes. ingo@2056: */ ingo@2056: public void setExportSection(ExportSection exportSection) { ingo@2056: ExportSection oldExportSection = getExportSection(); ingo@2056: ingo@2056: if (oldExportSection != null) { ingo@2056: removeSection(oldExportSection); ingo@2056: } ingo@2056: ingo@2056: this.exportSection = exportSection; ingo@2056: addSection(exportSection); ingo@2056: } ingo@2056: ingo@2056: ingo@2056: /** ingo@2056: * Returns the Section that stores export specific attributes. ingo@2056: * ingo@2056: * @return the Section that stores export specific attributes. ingo@2056: */ ingo@2056: public ExportSection getExportSection() { ingo@2056: return exportSection; ingo@2056: } ingo@2056: ingo@2056: ingo@2056: /** ingo@1986: * Adds a Section for a new axis of the chart. ingo@1986: * ingo@1986: * @param axisSection The Section specific for a chart axis. ingo@1986: */ ingo@2050: public void addAxisSection(AxisSection axisSection) { ingo@1986: if (axisSection != null) { ingo@1986: axesSection.addSubsection(axisSection); ingo@1986: } ingo@1986: } ingo@1994: ingo@1994: ingo@1994: /** ingo@2050: * This method returns an AxisSection specified by axisId or null if ingo@2050: * no AxisSection is existing with identifier axisId. ingo@2050: * ingo@2050: * @param axisId The identifier of the wanted AxisSection. ingo@2050: * ingo@2050: * @return the AxisSection specified by axisId or null. ingo@2050: */ ingo@2050: public AxisSection getAxisSection(String axisId) { ingo@2050: for (int i = 0, n = axesSection.getSubsectionCount(); i < n; i++) { ingo@2050: AxisSection as = (AxisSection) axesSection.getSubsection(i); ingo@2050: String id = as.getIdentifier(); ingo@2050: ingo@2050: if (id != null && id.equals(axisId)) { ingo@2050: return as; ingo@2050: } ingo@2050: } ingo@2050: ingo@2050: return null; ingo@2050: } ingo@2050: ingo@2050: ingo@2050: /** ingo@1994: * Parses the settings from settings. The result is a new ingo@1994: * ChartSettings instance. ingo@1994: * ingo@1994: * @param settings A settings node. ingo@1994: * ingo@1994: * @return a new ChartSettings instance. ingo@1994: */ ingo@1994: public static ChartSettings parse(Node settings) { ingo@1994: if (settings == null) { teichmann@8202: log.warn("Tried to parse ChartSettings from empty Node!"); ingo@1994: return null; ingo@1994: } ingo@1994: ingo@1994: ChartSettings chartSettings = new ChartSettings(); ingo@1994: ingo@1994: parseAxes(chartSettings, settings); ingo@1994: parseChart(chartSettings, settings); ingo@1994: parseLegend(chartSettings, settings); ingo@2056: parseExport(chartSettings, settings); ingo@1994: ingo@1994: return chartSettings; ingo@1994: } ingo@1994: ingo@1994: ingo@1994: protected static void parseAxes(ChartSettings target, Node settings) { ingo@1994: NodeList axesList = (NodeList) XMLUtils.xpath( ingo@1994: settings, "axes/axis", XPathConstants.NODESET, null); ingo@1994: ingo@1994: int num = axesList != null ? axesList.getLength() : 0; ingo@1994: ingo@1994: if (num <= 0) { teichmann@8202: log.debug("No axis sections found."); ingo@1994: return; ingo@1994: } ingo@1994: ingo@1994: for (int i = 0; i < num; i++) { ingo@1994: parseAxis(target, axesList.item(i)); ingo@1994: } ingo@1994: } ingo@1994: ingo@1994: ingo@1994: protected static void parseAxis(ChartSettings target, Node axis) { ingo@1994: AxisSection section = new AxisSection(); ingo@1994: ingo@1994: String id = XMLUtils.xpathString(axis, "id", null); ingo@1994: String label = XMLUtils.xpathString(axis, "label", null); ingo@1994: String fSize = XMLUtils.xpathString(axis, "font-size", null); ingo@1994: String fixation = XMLUtils.xpathString(axis, "fixation", null); ingo@1994: String low = XMLUtils.xpathString(axis, "lower", null); ingo@1994: String up = XMLUtils.xpathString(axis, "upper", null); aheinecke@7597: String sugLabel = XMLUtils.xpathString(axis, "suggested-label", null); ingo@1994: teichmann@8202: if (log.isDebugEnabled()) { teichmann@8202: log.debug("Found axis id: '" + id + "'"); teichmann@8202: log.debug("Found axis label: '" + label + "'"); teichmann@8202: log.debug("Found axis font size: '" + fSize + "'"); teichmann@8202: log.debug("Found axis fixation: '" + fixation + "'"); teichmann@8202: log.debug("Found axis lower: '" + low + "'"); teichmann@8202: log.debug("Found axis upper: '" + up + "'"); teichmann@8202: log.debug("Found axis sug. label:'" + sugLabel + "'"); ingo@1994: } ingo@1994: ingo@1994: section.setIdentifier(id); ingo@1994: section.setLabel(label); sascha@3405: section.setFontSize(Integer.parseInt(fSize.length() > 0 ? fSize : "-1")); ingo@1994: section.setFixed(Boolean.valueOf(fixation)); sascha@3405: section.setLowerRange(Double.parseDouble(low.length() > 0 ? low : "0")); sascha@3405: section.setUpperRange(Double.parseDouble(up.length() > 0 ? up : "0")); aheinecke@7597: section.setSuggestedLabel(sugLabel); ingo@1994: ingo@1994: target.addAxisSection(section); ingo@1994: } ingo@1994: ingo@1994: felix@3615: /** felix@3615: * From document chart create ChartSection and populate it with attributes. felix@3615: * Give this object to target as chartsection. felix@3615: */ ingo@1994: protected static void parseChart(ChartSettings target, Node chart) { ingo@1994: ChartSection chartSection = new ChartSection(); ingo@1994: ingo@1994: String title = XMLUtils.xpathString(chart, "chart/title", null); ingo@1994: String sub = XMLUtils.xpathString(chart, "chart/subtitle", null); ingo@1994: String grid = XMLUtils.xpathString(chart, "chart/display-grid", null); felix@3615: String logo = XMLUtils.xpathString(chart, "chart/display-logo", null); felix@3618: String placeh = XMLUtils.xpathString(chart, "chart/logo-placeh", null); felix@3618: String placev = XMLUtils.xpathString(chart, "chart/logo-placev", null); ingo@1994: teichmann@8202: if (log.isDebugEnabled()) { teichmann@8202: log.debug("Found chart title: '" + title + "'"); teichmann@8202: log.debug("Found chart subtitle: '" + sub + "'"); teichmann@8202: log.debug("Found chart grid: '" + grid + "'"); teichmann@8202: log.debug("Found chart logo: '" + logo + "'"); teichmann@8202: log.debug("Found chart logo placeh: '" + placeh + "'"); teichmann@8202: log.debug("Found chart logo placev: '" + placev + "'"); ingo@1994: } ingo@1994: ingo@1994: chartSection.setTitle(title); ingo@1994: chartSection.setSubtitle(sub); felix@3613: chartSection.setDisplayGrid(Boolean.valueOf(grid)); felix@3615: chartSection.setDisplayLogo(logo); felix@3618: chartSection.setLogoHPlacement(placeh); felix@3618: chartSection.setLogoVPlacement(placev); ingo@1994: ingo@1994: target.setChartSection(chartSection); ingo@1994: } ingo@1994: ingo@1994: ingo@1994: protected static void parseLegend(ChartSettings target, Node legend) { ingo@1994: LegendSection section = new LegendSection(); ingo@1994: ingo@1994: String vis = XMLUtils.xpathString(legend, "legend/visibility", null); ingo@1994: String fSize = XMLUtils.xpathString(legend, "legend/font-size", null); felix@3153: String lthre = XMLUtils.xpathString(legend, "legend/aggregation-threshold", null); ingo@1994: teichmann@8202: if (log.isDebugEnabled()) { teichmann@8202: log.debug("Found legend visibility: '" + vis + "'"); teichmann@8202: log.debug("Found legend font size : '" + fSize + "'"); teichmann@8202: log.debug("Found legend aggregation threshold : '" + lthre + "'"); ingo@1994: } ingo@1994: ingo@1994: section.setVisibility(Boolean.valueOf(vis)); ingo@1994: section.setFontSize(Integer.valueOf(fSize.length() > 0 ? fSize : "-1")); felix@3153: section.setAggregationThreshold(Integer.valueOf(lthre.length() >0 ? lthre : "-1")); ingo@1994: ingo@1994: target.setLegendSection(section); ingo@1994: } ingo@2056: ingo@2056: ingo@2056: protected static void parseExport(ChartSettings target, Node export) { ingo@2056: ExportSection section = new ExportSection(); ingo@2056: ingo@2056: String width = XMLUtils.xpathString(export, "export/width", null); ingo@2056: String height = XMLUtils.xpathString(export, "export/height", null); ingo@2056: teichmann@8202: if (log.isDebugEnabled()) { teichmann@8202: log.debug("Found export width : '" + width + "'"); teichmann@8202: log.debug("Found export height: '" + height + "'"); ingo@2056: } ingo@2056: ingo@2056: section.setWidth(Integer.valueOf(width.length() > 0 ? width : "-1")); ingo@2056: section.setHeight(Integer.valueOf(height.length() > 0 ? height : "-1")); ingo@2056: ingo@2056: target.setExportSection(section); ingo@2056: } ingo@1986: } ingo@1986: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :