ingo@1986: package de.intevation.flys.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: ingo@1994: import de.intevation.artifacts.common.utils.XMLUtils; ingo@1994: ingo@1986: import de.intevation.artifactdatabase.state.DefaultSection; ingo@1986: import de.intevation.artifactdatabase.state.DefaultSettings; ingo@1986: import de.intevation.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: ingo@1994: private static final Logger logger = Logger.getLogger(ChartSettings.class); ingo@1994: ingo@1986: protected Section chartSection; ingo@1990: protected Section legendSection; ingo@1986: 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@1986: public void setChartSection(Section chartSection) { ingo@1986: Section 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@1986: public Section 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@1990: public void setLegendSection(Section legendSection) { ingo@1990: Section 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@1990: public Section getLegendSection() { ingo@1990: return legendSection; ingo@1990: } ingo@1990: ingo@1990: ingo@1990: /** 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@1986: public void addAxisSection(Section axisSection) { ingo@1986: if (axisSection != null) { ingo@1986: axesSection.addSubsection(axisSection); ingo@1986: } ingo@1986: } ingo@1994: ingo@1994: ingo@1994: /** 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) { ingo@1994: logger.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@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) { ingo@1994: logger.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); ingo@1994: ingo@1994: if (logger.isDebugEnabled()) { ingo@1994: logger.debug("Fount axis id: '" + id + "'"); ingo@1994: logger.debug("Fount axis label: '" + label + "'"); ingo@1994: logger.debug("Fount axis font size: '" + fSize + "'"); ingo@1994: logger.debug("Fount axis fixation: '" + fixation + "'"); ingo@1994: logger.debug("Fount axis lower: '" + low + "'"); ingo@1994: logger.debug("Fount axis upper: '" + up + "'"); ingo@1994: } ingo@1994: ingo@1994: section.setIdentifier(id); ingo@1994: section.setLabel(label); ingo@1994: section.setFontSize(Integer.valueOf(fSize.length() > 0 ? fSize : "-1")); ingo@1994: section.setFixed(Boolean.valueOf(fixation)); ingo@1994: section.setLowerRange(Double.valueOf(low.length() > 0 ? low : "0")); ingo@1994: section.setUpperRange(Double.valueOf(up.length() > 0 ? up : "0")); ingo@1994: ingo@1994: target.addAxisSection(section); ingo@1994: } ingo@1994: ingo@1994: 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); ingo@1994: ingo@1994: if (logger.isDebugEnabled()) { ingo@1994: logger.debug("Found chart title: '" + title + "'"); ingo@1994: logger.debug("Found chart subtitle: '" + sub + "'"); ingo@1994: logger.debug("Found chart grid: '" + grid + "'"); ingo@1994: } ingo@1994: ingo@1994: chartSection.setTitle(title); ingo@1994: chartSection.setSubtitle(sub); ingo@1994: chartSection.setDisplayGird(Boolean.valueOf(grid)); 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); ingo@1994: ingo@1994: if (logger.isDebugEnabled()) { ingo@1994: logger.debug("Found legend visibility: '" + vis + "'"); ingo@1994: logger.debug("Found legend font size : '" + fSize + "'"); ingo@1994: } ingo@1994: ingo@1994: section.setVisibility(Boolean.valueOf(vis)); ingo@1994: section.setFontSize(Integer.valueOf(fSize.length() > 0 ? fSize : "-1")); ingo@1994: ingo@1994: target.setLegendSection(section); ingo@1994: } ingo@1986: } ingo@1986: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :