teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.shared; ingo@1328: ingo@2440: import java.util.Date; ingo@2440: ingo@1328: ingo@1328: public class MapUtils { ingo@1328: ingo@1328: public static final String GET_LEGEND_GRAPHIC_TEMPLATE = ingo@1328: "${SERVER}SERVICE=WMS&VERSION=1.1.1&layer=${LAYER}" + ingo@1328: "&REQUEST=getLegendGraphic&FORMAT=image/png"; ingo@1328: ingo@1328: ingo@1328: private MapUtils() { ingo@1328: } ingo@1328: aheinecke@5676: public static String getLegendGraphicUrl(String server, String layer) { aheinecke@5676: return getLegendGraphicUrl(server, layer, -1); aheinecke@5676: } ingo@1328: aheinecke@5676: public static String getLegendGraphicUrl(String server, String layer, int dpi) { ingo@1328: if (server == null || layer == null) { ingo@1328: return null; ingo@1328: } ingo@1328: aheinecke@5676: if (server.contains("osm.intevation.de")) { aheinecke@5676: // GetLegend is not implemented at osm.intevation.de aheinecke@5676: // This avoids an error in the print log aheinecke@5676: return null; aheinecke@5676: } ingo@1328: server = server.indexOf("?") >= 0 ? server : server + "?"; ingo@1328: ingo@1328: String url = GET_LEGEND_GRAPHIC_TEMPLATE; ingo@1328: url = url.replace("${SERVER}", server); ingo@1328: url = url.replace("${LAYER}", layer); ingo@2440: url = url + "×tamp=" + new Date().getTime(); aheinecke@5676: if (dpi != -1) { aheinecke@5676: url+="&legend_options=dpi:" + dpi; aheinecke@5676: } ingo@1328: ingo@1328: return url; ingo@1328: } aheinecke@6384: aheinecke@6384: aheinecke@6384: public static String toJavaEncodedString(String str) { aheinecke@6384: if (str == null) { aheinecke@6384: return null; aheinecke@6384: } aheinecke@6384: StringBuilder sb = new StringBuilder(); aheinecke@6384: for (int i = 0, len = str.length(); i < len; i++) { aheinecke@6384: int unipoint = Character.codePointAt(str, i); aheinecke@6384: if ((unipoint < 32) || (unipoint > 127)) { aheinecke@6384: sb.append("\\u"); aheinecke@6384: sb.append(Integer.toHexString((unipoint >> 3*4) & 0xf)); aheinecke@6384: sb.append(Integer.toHexString((unipoint >> 2*4) & 0xf)); aheinecke@6384: sb.append(Integer.toHexString((unipoint >> 1*4) & 0xf)); aheinecke@6384: sb.append(Integer.toHexString((unipoint >> 0*4) & 0xf)); aheinecke@6384: } else { aheinecke@6384: sb.append(str.charAt(i)); aheinecke@6384: } aheinecke@6384: } aheinecke@6384: return sb.toString(); aheinecke@6384: } ingo@1328: } ingo@1328: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :