view flys-client/src/main/java/de/intevation/flys/client/server/ChartInfoServiceImpl.java @ 539:fea93eebd2fa

Improved the layout of the mouse position panel. flys-client/trunk@2039 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 01 Jun 2011 11:59:59 +0000
parents fe2860653f8d
children 95a7f9cb3d58
line wrap: on
line source
package de.intevation.flys.client.server;

import java.io.InputStream;
import java.io.IOException;

import javax.xml.xpath.XPathConstants;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.intevation.artifacts.common.ArtifactNamespaceContext;
import de.intevation.artifacts.common.utils.ClientProtocolUtils;
import de.intevation.artifacts.common.utils.XMLUtils;
import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;

import de.intevation.artifacts.httpclient.http.HttpClient;
import de.intevation.artifacts.httpclient.http.HttpClientImpl;

import de.intevation.flys.client.shared.Transform2D;
import de.intevation.flys.client.shared.exceptions.ServerException;
import de.intevation.flys.client.shared.model.Collection;

import de.intevation.flys.client.client.services.ChartInfoService;


/**
 * This service fetches a document that contains meta information for a specific
 * chart.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ChartInfoServiceImpl
extends      RemoteServiceServlet
implements   ChartInfoService
{
    public static final String XPATH_TRANSFORM_MATRIX =
        "/art:chartinfo/art:transformation-matrix";

    public static final String EXCEPTION_STRING = "error_chart_info_service";


    public Transform2D getChartInfo(
        Collection collection,
        String     url,
        String     locale,
        String     type,
        int        width,
        int        height)
    throws ServerException
    {
        System.out.println("ChartInfoServiceImpl.getChartInfo");

        Document request = ClientProtocolUtils.newOutCollectionDocument(
                collection.identifier(),
                type,
                type,
                getChartAttributes(width, height));

        try {
            HttpClient client = new HttpClientImpl(url, locale);
            InputStream in    = client.collectionOut(
                request,
                collection.identifier(),
                type + "_chartinfo");

            Document info = XMLUtils.parseDocument(in);

            return parseInfoDocument(info);
        }
        catch (IOException ioe) {
            // do nothing
        }

        System.err.println("Error while fetching chart info.");

        throw new ServerException(EXCEPTION_STRING);
    }


    /**
     * This method returns a document which might contain parameters to adjust
     * chart settings. The document is created using the information that are
     * contained in the request object.
     *
     * @param req The request document.
     *
     * @return a document to adjust chart settings.
     */
    protected Document getChartAttributes(int width, int height) {
        Document doc = XMLUtils.newDocument();

        ElementCreator ec = new ElementCreator(
            doc,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        Element size = ec.create("size");

        ec.addAttr(size, "width", String.valueOf(width), true);
        ec.addAttr(size, "height", String.valueOf(height), true);

        doc.appendChild(size);

        return doc;
    }


    /**
     * Parses the chart info document and extract the Transform2D values.
     *
     * @param doc The chart info document.
     *
     * @return a Transform2D object to transfrom pixel coordinates into chart
     * coordinates.
     */
    protected Transform2D parseInfoDocument(Document doc) {
        System.out.println("ChartInfoServiceImpl.parseInfoDocument");

        Node matrix = (Node) XMLUtils.xpath(
            doc,
            XPATH_TRANSFORM_MATRIX,
            XPathConstants.NODE,
            ArtifactNamespaceContext.INSTANCE);

        if (matrix == null) {
            return new Transform2D(1d, 1d, 0d, 0d);
        }

        String sx = XMLUtils.xpathString(
            matrix, "@art:sx", ArtifactNamespaceContext.INSTANCE);

        String sy = XMLUtils.xpathString(
            matrix, "@art:sy", ArtifactNamespaceContext.INSTANCE);

        String tx = XMLUtils.xpathString(
            matrix, "@art:tx", ArtifactNamespaceContext.INSTANCE);

        String ty = XMLUtils.xpathString(
            matrix, "@art:ty", ArtifactNamespaceContext.INSTANCE);

        if (sx != null && sy != null && tx != null && ty != null) {
            try {
                return new Transform2D(
                    Double.parseDouble(sx),
                    Double.parseDouble(sy),
                    Double.parseDouble(tx),
                    Double.parseDouble(ty));
            }
            catch (NumberFormatException nfe) {
                System.err.println("Error while parsing matrix values.");
            }
        }

        System.err.println("No matrix values found.");

        return new Transform2D(1d, 1d, 0d, 0d);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org