ingo@549: package de.intevation.flys.client.server;
ingo@549:
ingo@549: import java.util.Map;
ingo@549:
ingo@549: import org.w3c.dom.Document;
ingo@549: import org.w3c.dom.Element;
ingo@549:
ingo@549: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@549: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@549: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
ingo@549:
ingo@549:
ingo@549: /**
ingo@549: * @author Ingo Weinzierl
ingo@549: */
ingo@549: public class ChartServiceHelper {
ingo@549:
ingo@549: /** The default chart width if no value is specified in the request.*/
ingo@549: public static final int DEFAULT_CHART_WIDTH = 600;
ingo@549:
ingo@549: /** The default chart height if no value is specified in the request.*/
ingo@549: public static final int DEFAULT_CHART_HEIGHT = 400;
ingo@549:
ingo@549:
ingo@549: private ChartServiceHelper() {
ingo@549: }
ingo@549:
ingo@549: /**
ingo@549: * This method returns a document which might contain parameters to adjust
ingo@549: * chart settings. The document is created using the information that are
ingo@549: * contained in the request object.
ingo@549: *
ingo@549: * @param req The request document.
ingo@549: *
ingo@549: * @return a document to adjust chart settings.
ingo@549: */
ingo@549: protected static Document getChartAttributes(Map req) {
ingo@549: System.out.println("ChartServiceHelper.getChartAttributes");
ingo@549:
ingo@549: Document doc = XMLUtils.newDocument();
ingo@549:
ingo@549: ElementCreator ec = new ElementCreator(
ingo@549: doc,
ingo@549: ArtifactNamespaceContext.NAMESPACE_URI,
ingo@549: ArtifactNamespaceContext.NAMESPACE_PREFIX);
ingo@549:
ingo@549: Element attributes = ec.create("attributes");
ingo@549:
ingo@549: appendChartSize(req, attributes, ec);
ingo@1351: appendFormat(req, attributes, ec);
ingo@549: appendXRange(req, attributes, ec);
ingo@549: appendYRange(req, attributes, ec);
ingo@549:
ingo@549: doc.appendChild(attributes);
ingo@549:
ingo@549: return doc;
ingo@549: }
ingo@549:
ingo@549:
ingo@549: /**
ingo@549: * This method extracts the size (width/height) of a chart from request
ingo@549: * object and append those values - if they exist - to the attribute
ingo@549: * document used to adjust chart settings.
ingo@549: *
ingo@549: * @param req The request object that might contain the chart size.
ingo@549: * @param attributes The attributes element used to adjust chart settings.
ingo@549: * @param ec The ElementCreator that might be used to create new Elements.
ingo@549: */
ingo@549: protected static void appendChartSize(
ingo@549: Map req,
ingo@549: Element attributes,
ingo@549: ElementCreator ec)
ingo@549: {
ingo@549: System.out.println("ChartServiceHelper.appendChartSize");
ingo@549:
ingo@549: Element size = ec.create("size");
ingo@549:
ingo@549: String width = req.get("width");
ingo@549: String height = req.get("height");
ingo@549:
ingo@549: if (width == null || height == null) {
ingo@549: width = String.valueOf(DEFAULT_CHART_WIDTH);
ingo@549: height = String.valueOf(DEFAULT_CHART_HEIGHT);
ingo@549: }
ingo@549:
ingo@549: ec.addAttr(size, "width", width, true);
ingo@549: ec.addAttr(size, "height", height, true);
ingo@549:
ingo@549: attributes.appendChild(size);
ingo@549: }
ingo@549:
ingo@549:
ingo@549: /**
ingo@549: * This method extracts the x range for the chart from request object and
ingo@549: * appends those range - if it exists - to the attribute document used to
ingo@549: * adjust the chart settings.
ingo@549: *
ingo@549: * @param req The request object that might contain the chart size.
ingo@549: * @param doc The attribute document used to adjust chart settings.
ingo@549: * @param ec The ElementCreator that might be used to create new Elements.
ingo@549: */
ingo@549: protected static void appendXRange(
ingo@549: Map req,
ingo@549: Element attributes,
ingo@549: ElementCreator ec)
ingo@549: {
ingo@549: System.out.println("ChartServiceHelper.appendXRange");
ingo@549:
ingo@549: Element range = ec.create("xrange");
ingo@549:
ingo@549: String from = req.get("minx");
ingo@549: String to = req.get("maxx");
ingo@549:
ingo@549: if (from != null && to != null) {
ingo@549: ec.addAttr(range, "from", from, true);
ingo@549: ec.addAttr(range, "to", to, true);
ingo@549:
ingo@549: attributes.appendChild(range);
ingo@549: }
ingo@549: }
ingo@549:
ingo@549:
ingo@549: /**
ingo@549: * This method extracts the x range for the chart from request object and
ingo@549: * appends those range - if it exists - to the attribute document used to
ingo@549: * adjust the chart settings.
ingo@549: *
ingo@549: * @param req The request object that might contain the chart size.
ingo@549: * @param doc The attribute document used to adjust chart settings.
ingo@549: * @param ec The ElementCreator that might be used to create new Elements.
ingo@549: */
ingo@549: protected static void appendYRange(
ingo@549: Map req,
ingo@549: Element attributes,
ingo@549: ElementCreator ec)
ingo@549: {
ingo@549: System.out.println("ChartServiceHelper.appendYRange");
ingo@549:
ingo@549: Element range = ec.create("yrange");
ingo@549:
ingo@549: String from = req.get("miny");
ingo@549: String to = req.get("maxy");
ingo@549:
ingo@549: if (from != null && to != null) {
ingo@549: ec.addAttr(range, "from", from, true);
ingo@549: ec.addAttr(range, "to", to, true);
ingo@549:
ingo@549: attributes.appendChild(range);
ingo@549: }
ingo@549: }
ingo@1351:
ingo@1351:
ingo@1351: /**
ingo@1351: * This method extracts the format string from request object and appends
ingo@1351: * those format - if existing - to the attribute document used to adjust
ingo@1351: * the chart settings.
ingo@1351: *
ingo@1351: * @param req The request object that might contain the chart format.
ingo@1351: * @param doc The attribute document used to adjust chart settings.
ingo@1351: * @param ec The ElementCreator that might be used to create new Elements.
ingo@1351: */
ingo@1351: protected static void appendFormat(
ingo@1351: Map req,
ingo@1351: Element attributes,
ingo@1351: ElementCreator ec
ingo@1351:
ingo@1351: ) {
ingo@1351: System.out.println("ChartServiceHelper.appendFormat");
ingo@1351:
ingo@1351: String formatStr = req.get("format");
ingo@1351: if (formatStr == null || formatStr.length() == 0) {
ingo@1351: return;
ingo@1351: }
ingo@1351:
ingo@1351: Element format = ec.create("format");
ingo@1351: ec.addAttr(format, "value", formatStr, true);
ingo@1351:
ingo@1351: attributes.appendChild(format);
ingo@1351: }
ingo@549: }
ingo@549: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :