ingo@535: package de.intevation.flys.client.server;
ingo@535:
ingo@535: import java.io.InputStream;
ingo@535: import java.io.IOException;
ingo@549: import java.util.Map;
ingo@535:
ingo@535: import javax.xml.xpath.XPathConstants;
ingo@535:
ingo@535: import org.w3c.dom.Document;
ingo@535: import org.w3c.dom.Node;
ingo@535:
ingo@535: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
ingo@535:
ingo@535: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@535: import de.intevation.artifacts.common.utils.ClientProtocolUtils;
ingo@535: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@535:
ingo@535: import de.intevation.artifacts.httpclient.http.HttpClient;
ingo@535: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
ingo@535:
ingo@535: import de.intevation.flys.client.shared.Transform2D;
ingo@535: import de.intevation.flys.client.shared.exceptions.ServerException;
ingo@535: import de.intevation.flys.client.shared.model.Collection;
ingo@535:
ingo@535: import de.intevation.flys.client.client.services.ChartInfoService;
ingo@535:
ingo@535:
ingo@535: /**
ingo@535: * This service fetches a document that contains meta information for a specific
ingo@535: * chart.
ingo@535: *
ingo@535: * @author Ingo Weinzierl
ingo@535: */
ingo@535: public class ChartInfoServiceImpl
ingo@535: extends RemoteServiceServlet
ingo@535: implements ChartInfoService
ingo@535: {
ingo@535: public static final String XPATH_TRANSFORM_MATRIX =
ingo@535: "/art:chartinfo/art:transformation-matrix";
ingo@535:
ingo@535: public static final String EXCEPTION_STRING = "error_chart_info_service";
ingo@535:
ingo@535:
ingo@535: public Transform2D getChartInfo(
ingo@549: Collection collection,
ingo@549: String url,
ingo@549: String locale,
ingo@549: String type,
ingo@549: Map attr)
ingo@535: throws ServerException
ingo@535: {
ingo@535: System.out.println("ChartInfoServiceImpl.getChartInfo");
ingo@535:
ingo@535: Document request = ClientProtocolUtils.newOutCollectionDocument(
ingo@535: collection.identifier(),
ingo@535: type,
ingo@535: type,
ingo@549: ChartServiceHelper.getChartAttributes(attr));
ingo@535:
ingo@535: try {
ingo@535: HttpClient client = new HttpClientImpl(url, locale);
ingo@535: InputStream in = client.collectionOut(
ingo@535: request,
ingo@535: collection.identifier(),
ingo@535: type + "_chartinfo");
ingo@535:
ingo@535: Document info = XMLUtils.parseDocument(in);
ingo@535:
ingo@535: return parseInfoDocument(info);
ingo@535: }
ingo@535: catch (IOException ioe) {
ingo@535: // do nothing
ingo@535: }
ingo@535:
ingo@535: System.err.println("Error while fetching chart info.");
ingo@535:
ingo@535: throw new ServerException(EXCEPTION_STRING);
ingo@535: }
ingo@535:
ingo@535:
ingo@535: /**
ingo@535: * Parses the chart info document and extract the Transform2D values.
ingo@535: *
ingo@535: * @param doc The chart info document.
ingo@535: *
ingo@535: * @return a Transform2D object to transfrom pixel coordinates into chart
ingo@535: * coordinates.
ingo@535: */
ingo@535: protected Transform2D parseInfoDocument(Document doc) {
ingo@535: System.out.println("ChartInfoServiceImpl.parseInfoDocument");
ingo@535:
ingo@535: Node matrix = (Node) XMLUtils.xpath(
ingo@535: doc,
ingo@535: XPATH_TRANSFORM_MATRIX,
ingo@535: XPathConstants.NODE,
ingo@535: ArtifactNamespaceContext.INSTANCE);
ingo@535:
ingo@535: if (matrix == null) {
ingo@535: return new Transform2D(1d, 1d, 0d, 0d);
ingo@535: }
ingo@535:
ingo@535: String sx = XMLUtils.xpathString(
ingo@535: matrix, "@art:sx", ArtifactNamespaceContext.INSTANCE);
ingo@535:
ingo@535: String sy = XMLUtils.xpathString(
ingo@535: matrix, "@art:sy", ArtifactNamespaceContext.INSTANCE);
ingo@535:
ingo@535: String tx = XMLUtils.xpathString(
ingo@535: matrix, "@art:tx", ArtifactNamespaceContext.INSTANCE);
ingo@535:
ingo@535: String ty = XMLUtils.xpathString(
ingo@535: matrix, "@art:ty", ArtifactNamespaceContext.INSTANCE);
ingo@535:
ingo@535: if (sx != null && sy != null && tx != null && ty != null) {
ingo@535: try {
ingo@535: return new Transform2D(
ingo@535: Double.parseDouble(sx),
ingo@535: Double.parseDouble(sy),
ingo@535: Double.parseDouble(tx),
ingo@535: Double.parseDouble(ty));
ingo@535: }
ingo@535: catch (NumberFormatException nfe) {
ingo@535: System.err.println("Error while parsing matrix values.");
ingo@535: }
ingo@535: }
ingo@535:
ingo@535: System.err.println("No matrix values found.");
ingo@535:
ingo@535: return new Transform2D(1d, 1d, 0d, 0d);
ingo@535: }
ingo@535: }
ingo@535: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :