Author: aaime Date: 2012-01-21 03:58:28 -0800 (Sat, 21 Jan 2012) New Revision: 38508 Added: trunk/modules/library/api/src/main/java/org/geotools/styling/ResourceLocator.java Modified: trunk/modules/library/main/src/main/java/org/geotools/styling/SLDParser.java trunk/modules/library/main/src/test/java/org/geotools/styling/SLDParserTest.java Log: [GEOT-4000] Configurable resource locator in SLDParser (patch by Jan De Moerloose) Added: trunk/modules/library/api/src/main/java/org/geotools/styling/ResourceLocator.java =================================================================== --- trunk/modules/library/api/src/main/java/org/geotools/styling/ResourceLocator.java (rev 0) +++ trunk/modules/library/api/src/main/java/org/geotools/styling/ResourceLocator.java 2012-01-21 11:58:28 UTC (rev 38508) @@ -0,0 +1,35 @@ +/* + * GeoTools - The Open Source Java GIS Toolkit + * http://geotools.org + * + * (C) 2002-2008, Open Source Geospatial Foundation (OSGeo) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +package org.geotools.styling; + +import java.net.URL; + +/** + * Provides a hook to locate online resources in {@link ExternalGraphic} instances. To be used by parser implementors. + * + * @source $URL: http://svn.osgeo.org/geotools/tags/2.7.4/modules/library/api/src/main/java/org/geotools/styling/ResourceLocator.java $ + */ +public interface ResourceLocator { + + /** + * Locate the specified resource. + * + * @param uri uri of the resource + * @return the fully resolved URL of the resource or null, if the resource cannot be located. + */ + URL locateResource(String uri); +} Modified: trunk/modules/library/main/src/main/java/org/geotools/styling/SLDParser.java =================================================================== --- trunk/modules/library/main/src/main/java/org/geotools/styling/SLDParser.java 2012-01-21 11:57:16 UTC (rev 38507) +++ trunk/modules/library/main/src/main/java/org/geotools/styling/SLDParser.java 2012-01-21 11:58:28 UTC (rev 38508) @@ -114,6 +114,9 @@ /** useful for detecting relative onlineresources */ private URL sourceUrl; + + /** provides complete control for detecting relative onlineresources */ + private ResourceLocator onlineResourceLocator; /** * Create a Stylereader - use if you already have a dom to parse. @@ -128,6 +131,7 @@ public SLDParser(StyleFactory factory, FilterFactory filterFactory) { this.factory = factory; this.ff = filterFactory; + this.onlineResourceLocator = new DefaultResourceLocator(); } /** @@ -276,6 +280,10 @@ public void setInput(java.io.Reader in) { source = new InputSource(in); } + + public void setOnLineResourceLocator(ResourceLocator onlineResourceLocator) { + this.onlineResourceLocator = onlineResourceLocator; + } /** * Read the xml inputsource provided and create a Style object for each user style found @@ -1659,26 +1667,7 @@ } } - URL url = null; - try { - url = new URL(uri); - } catch (MalformedURLException mfe) { - LOGGER.fine("Looks like " + uri + " is a relative path.."); - if (sourceUrl != null) { - try { - url = new URL(sourceUrl, uri); - } catch (MalformedURLException e) { - LOGGER.warning("can't parse " + uri + " as relative to" - + sourceUrl.toExternalForm()); - } - } - if (url == null) - { - url = getClass().getResource(uri); - if (url == null) - LOGGER.warning("can't parse " + uri + " as a java resource present in the classpath"); - } - } + URL url = onlineResourceLocator.locateResource(uri); ExternalGraphic extgraph; if (url == null) { @@ -2364,4 +2353,39 @@ return halo; } + + /** + * Default locator for online resources. Searches by absolute URL, relative + * path w.r.t. to SLD document or classpath. + * + * @author Jan De Moerloose + * + */ + class DefaultResourceLocator implements ResourceLocator { + + public URL locateResource(String uri) { + URL url = null; + try { + url = new URL(uri); + } catch (MalformedURLException mfe) { + LOGGER.fine("Looks like " + uri + " is a relative path.."); + if (sourceUrl != null) { + try { + url = new URL(sourceUrl, uri); + } catch (MalformedURLException e) { + LOGGER.warning("can't parse " + uri + " as relative to" + + sourceUrl.toExternalForm()); + } + } + if (url == null) + { + url = getClass().getResource(uri); + if (url == null) + LOGGER.warning("can't parse " + uri + " as a java resource present in the classpath"); + } + } + return url; + } + + } } Modified: trunk/modules/library/main/src/test/java/org/geotools/styling/SLDParserTest.java =================================================================== --- trunk/modules/library/main/src/test/java/org/geotools/styling/SLDParserTest.java 2012-01-21 11:57:16 UTC (rev 38507) +++ trunk/modules/library/main/src/test/java/org/geotools/styling/SLDParserTest.java 2012-01-21 11:58:28 UTC (rev 38508) @@ -21,8 +21,12 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; import java.util.List; +import junit.framework.Assert; + import org.geotools.factory.CommonFactoryFinder; import org.junit.Test; import org.opengis.style.GraphicalSymbol; @@ -76,6 +80,36 @@ " </UserStyle>\n" + "</StyledLayerDescriptor>"; + static String SLD_EXTERNAL_GRAPHIC = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<StyledLayerDescriptor version=\"1.0.0\" \n" + + " xsi:schemaLocation=\"http://www.opengis.net/sld StyledLayerDescriptor.xsd\" \n" + + " xmlns=\"http://www.opengis.net/sld\" xmlns:ogc=\"http://www.opengis.net/ogc\" \n" + + " xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n" + + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + + " <UserStyle>\n" + + " <Name>Default Styler</Name>\n" + + " <Title>Default Styler</Title>\n" + + " <Abstract></Abstract>\n" + + " <FeatureTypeStyle>\n" + + " <FeatureTypeName>Feature</FeatureTypeName>\n" + + " <Rule>\n" + + " <PointSymbolizer>\n" + + " <Graphic>\n" + + " <ExternalGraphic>\n" + + " <OnlineResource xlink:type=\"simple\"\n" + + " xlink:href=\"test-data/blob.gif\">\n" + + " </OnlineResource>\n" + + " <Format>image/png</Format>\n" + + " </ExternalGraphic>\n" + + " <Size>20</Size>\n" + + " </Graphic>\n" + + " </PointSymbolizer>\n" + + " </Rule>\n" + + " </FeatureTypeStyle>\n" + + " </UserStyle>\n" + + "</StyledLayerDescriptor>"; + static StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null); @Test @@ -122,6 +156,33 @@ assertEquals(mark, CommonFactoryFinder.getStyleFactory(null).createMark()); } + @Test + public void testOnlineResourceLocator() throws MalformedURLException { + // test whether the configured resolver is called and the url is used correctly + SLDParser parser = new SLDParser(styleFactory, input(SLD_EXTERNAL_GRAPHIC)); + parser.setOnLineResourceLocator(new ResourceLocator() { + + public URL locateResource(String uri) { + Assert.assertEquals("test-data/blob.gif", uri); + return getClass().getResource(uri); + } + }); + Style[] styles = parser.readXML(); + assertEquals(1, styles.length); + List<FeatureTypeStyle> fts = styles[0].featureTypeStyles(); + assertEquals(1, fts.size()); + List<Rule> rules = fts.get(0).rules(); + assertEquals(1, rules.size()); + List<Symbolizer> symbolizers = rules.get(0).symbolizers(); + assertEquals(1, symbolizers.size()); + PointSymbolizer ps = (PointSymbolizer) symbolizers.get(0); + // here we would have had two instead of one + List<GraphicalSymbol> graphicalSymbols = ps.getGraphic().graphicalSymbols(); + assertEquals(1, graphicalSymbols.size()); + ExternalGraphic graphic = (ExternalGraphic) graphicalSymbols.get(0); + assertEquals(getClass().getResource("test-data/blob.gif"), graphic.getLocation()); + } + void assertStyles(Style[] styles) { assertEquals(1, styles.length); assertEquals("style", styles[0].getName()); |