Skip to content

Commit

Permalink
[GEOS-8494] Allow TextDecoration template to pick from request parame…
Browse files Browse the repository at this point in the history
…ters too
  • Loading branch information
aaime committed Dec 15, 2017
1 parent 43a1799 commit ce2c282
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -227,7 +228,7 @@ public void paint(Graphics2D g2d, Rectangle rect, WMSMapContent mapContent)
*
* @see {Block}
*/
private List<Block> blocks;
List<Block> blocks;

/**
* Create a new MapDecorationLayout with no decorations in it yet.
Expand All @@ -252,10 +253,38 @@ public static MapDecorationLayout fromFile(Resource f, boolean tiled) throws Exc

Document confFile = new SAXBuilder().build(f.file());

return fromDocument(dl, confFile);
}

/**
* Read an XML layout file and populate a new MapDecorationLayout with the MapDecorations specified
* therein.
*
* @param definition The layout definition as a string
* @param tiled is this map metatiled?
* @return a new MapDecorationLayout containing the MapDecorations specified
* @throws Exception if the configuration is invalid or other errors occur while parsing
*/
public static MapDecorationLayout fromString(String definition, boolean tiled) throws Exception {
MapDecorationLayout dl = tiled
? new MetatiledMapDecorationLayout()
: new MapDecorationLayout();

Document confFile = new SAXBuilder().build(new StringReader(definition));

return fromDocument(dl, confFile);
}

private static MapDecorationLayout fromDocument(MapDecorationLayout dl, Document confFile) throws Exception {
for (Element e : (List<Element>)confFile.getRootElement().getChildren("decoration")){
Map<String, String> m = new HashMap<String,String>();
for (Element option : (List<Element>)e.getChildren("option")){
m.put(option.getAttributeValue("name"), option.getAttributeValue("value"));
String value = option.getAttributeValue("value");
if(value == null) {
// pick from body, useful if the content is large
value = option.getValue();
}
m.put(option.getAttributeValue("name"), value);
}

MapDecoration decoration = getDecoration(e.getAttributeValue("type"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,6 @@
*/
package org.geoserver.wms.decoration;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.geoserver.wms.WMSMapContent;
import org.geotools.geometry.jts.TransformedShape;
import org.geotools.renderer.style.FontCache;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.StringModel;
import freemarker.template.Configuration;
Expand All @@ -36,6 +13,23 @@
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import org.geoserver.ows.Dispatcher;
import org.geoserver.ows.Request;
import org.geoserver.wms.WMSMapContent;
import org.geotools.renderer.style.FontCache;
import org.geotools.util.Converters;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.awt.*;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* A map decoration showing a text message driven by a Freemarker template
Expand Down Expand Up @@ -154,6 +148,20 @@ public boolean isEmpty() throws TemplateModelException {
@Override
public TemplateModel get(String key) throws TemplateModelException {
String value = (String) env.get(key);
// also allow using the other request parameters
if (value == null) {
Request request = Dispatcher.REQUEST.get();
if (request != null && request.getRawKvp() != null) {
Object obj = request.getRawKvp().get(key);
value = Converters.convert(obj, String.class);
if (obj != null && value == null) {
// try also Spring converters as a fallback, can do some thing GT converters
// cannot do (e.g array -> string). Did not create a bridge to GT converters as it might
// have global consequences
DefaultConversionService.getSharedInstance().convert(obj, String.class);
}
}
}
if(value != null) {
return new StringModel(value, bw);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Map;

import org.geoserver.platform.GeoServerExtensionsHelper;
import org.geoserver.wms.WMSMapContent;
import org.hamcrest.CoreMatchers;
import org.junit.Test;

public class MapDecorationLayoutTest {
Expand Down Expand Up @@ -403,4 +406,27 @@ public Dimension findOptimalSize(Graphics2D g2d, WMSMapContent mapContent) throw
dl.paint(g2d, new Rectangle(0, 0, 100, 100), null);
}

@Test
public void testLoadLargeValue() throws Exception {
String messageTemplate = "<#setting datetime_format=\"yyyy-MM-dd'T'HH:mm:ss.SSSX\">\n" +
"<#setting locale=\"en_US\">\n" +
"<#if time??>\n" +
"${time?datetime?string[\"dd-MM-yyyy\"]}\n" +
"</#if>";
String definition = "<layout>\n" +
" <decoration type=\"text\" affinity=\"bottom,right\" offset=\"6,6\" size=\"auto\">\n" +
" <option name=\"message\"><![CDATA[" + messageTemplate + "]]></option>\n" +
" <option name=\"font-family\" value=\"Bitstream Vera Sans\"/>\n" +
" <option name=\"font-size\" value=\"12\"/>\n" +
" <option name=\"halo-radius\" value=\"2\"/>\n" +
" </decoration>\n" +
"</layout>\n";
GeoServerExtensionsHelper.singleton("text", new TextDecoration());
MapDecorationLayout layout = MapDecorationLayout.fromString(definition, false);
List<MapDecorationLayout.Block> blocks = layout.blocks;
assertEquals(1, blocks.size());
assertThat(blocks.get(0).decoration, CoreMatchers.instanceOf(TextDecoration.class));
TextDecoration text = (TextDecoration) blocks.get(0).decoration;
assertEquals(messageTemplate, text.messageTemplate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.wms.decoration;

import org.geoserver.ows.Dispatcher;
import org.geoserver.ows.Request;
import org.geoserver.ows.util.CaseInsensitiveMap;
import org.geoserver.wms.GetMapRequest;
import org.geoserver.wms.WMSMapContent;
import org.junit.After;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class TextDecorationTest {

@After
public void clearRequest() {
Dispatcher.REQUEST.remove();
}

@Test
public void testExpandRequestVariable() throws Exception {
// setup environment
Request request = new Request();
Map kvp = new CaseInsensitiveMap(new HashMap());
kvp.put("time", "2008-10-31T00:00:00.000Z");
request.setRawKvp(kvp);
Dispatcher.REQUEST.set(request);

TextDecoration decoration = new TextDecoration();
Map<String, String> options = new HashMap<>();
options.put("message", "<#setting datetime_format=\"yyyy-MM-dd'T'HH:mm:ss.SSSX\">\n" +
"<#setting locale=\"en_US\">\n" +
"<#if time??>\n" +
"${time?datetime?string[\"dd.MM.yyyy\"]}" +
"</#if>");
decoration.loadOptions(options);

GetMapRequest getMap = new GetMapRequest();
WMSMapContent wmsMapContent = new WMSMapContent(getMap);
String message = decoration.evaluateMessage(wmsMapContent);
assertEquals("31.10.2008", message);
}
}

0 comments on commit ce2c282

Please sign in to comment.