001 //$HeadURL: http://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/graphics/sld/ExternalGraphic.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 package org.deegree.graphics.sld; 037 038 import static org.deegree.framework.xml.XMLTools.escape; 039 040 import java.awt.image.BufferedImage; 041 import java.io.ByteArrayInputStream; 042 import java.io.ByteArrayOutputStream; 043 import java.io.IOException; 044 import java.io.InputStream; 045 import java.net.MalformedURLException; 046 import java.net.URISyntaxException; 047 import java.net.URL; 048 import java.util.HashMap; 049 import java.util.Map; 050 051 import javax.media.jai.JAI; 052 import javax.media.jai.RenderedOp; 053 054 import org.apache.batik.transcoder.SVGAbstractTranscoder; 055 import org.apache.batik.transcoder.Transcoder; 056 import org.apache.batik.transcoder.TranscoderException; 057 import org.apache.batik.transcoder.TranscoderInput; 058 import org.apache.batik.transcoder.TranscoderOutput; 059 import org.apache.batik.transcoder.image.PNGTranscoder; 060 import org.deegree.framework.log.ILogger; 061 import org.deegree.framework.log.LoggerFactory; 062 import org.deegree.framework.util.StringTools; 063 import org.deegree.framework.xml.Marshallable; 064 import org.deegree.model.feature.Feature; 065 import org.deegree.model.feature.FeatureProperty; 066 067 import com.sun.media.jai.codec.MemoryCacheSeekableStream; 068 069 /** 070 * The ExternalGraphic element allows a reference to be made to an external graphic file with a Web URL. The 071 * OnlineResource sub-element gives the URL and the Format sub-element identifies the expected document MIME type of a 072 * successful fetch. Knowing the MIME type in advance allows the styler to select the best- supported format from the 073 * list of URLs with equivalent content. 074 * 075 * 076 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 077 * @author last edited by: $Author: apoth $ 078 * 079 * @version $Revision: 31461 $, $Date: 2011-08-08 17:20:30 +0200 (Mo, 08 Aug 2011) $ 080 */ 081 public class ExternalGraphic implements Marshallable { 082 083 private static final ILogger LOG = LoggerFactory.getLogger( ExternalGraphic.class ); 084 085 private static BufferedImage defaultImage = null; 086 static { 087 if ( defaultImage == null ) { 088 defaultImage = new BufferedImage( 1, 1, BufferedImage.TYPE_INT_ARGB ); 089 } 090 } 091 092 private BufferedImage image = null; 093 094 private String format = null; 095 096 private URL onlineResource = null; 097 098 private TranscoderInput input = null; 099 100 private ByteArrayOutputStream bos = null; 101 102 private TranscoderOutput output = null; 103 104 private Transcoder trc = null; 105 106 private static Map<String, BufferedImage> cache = null; 107 static { 108 if ( cache == null ) { 109 cache = new HashMap<String, BufferedImage>( 500 ); 110 } 111 } 112 113 /** 114 * Creates a new ExternalGraphic_Impl object. 115 * 116 * @param format 117 * @param onlineResource 118 */ 119 ExternalGraphic( String format, URL onlineResource ) { 120 setFormat( format ); 121 setOnlineResource( onlineResource ); 122 } 123 124 /** 125 * the Format sub-element identifies the expected document MIME type of a successful fetch. 126 * 127 * @return Format of the external graphic 128 * 129 */ 130 public String getFormat() { 131 return format; 132 } 133 134 /** 135 * sets the format (MIME type) 136 * 137 * @param format 138 * Format of the external graphic 139 * 140 */ 141 public void setFormat( String format ) { 142 this.format = format; 143 } 144 145 /** 146 * The OnlineResource gives the URL of the external graphic 147 * 148 * @return URL of the external graphic 149 * 150 */ 151 public URL getOnlineResource() { 152 return onlineResource; 153 } 154 155 /** 156 * sets the online resource / URL of the external graphic 157 * 158 * @param onlineResource 159 * URL of the external graphic 160 * 161 */ 162 public void setOnlineResource( URL onlineResource ) { 163 this.onlineResource = onlineResource; 164 // TODO 165 // removing this is just experimental; possibly initializeOnlineResource( Feature feature ) 166 // must be adapted 167 // String file = onlineResource.getFile(); 168 // int idx = file.indexOf( "$" ); 169 // if ( idx == -1 ) { 170 // retrieveImage( onlineResource ); 171 // } 172 } 173 174 /** 175 * @param onlineResource 176 */ 177 private void retrieveImage( URL onlineResource ) { 178 179 try { 180 String t = onlineResource.toURI().toASCIIString(); 181 if ( t.trim().toLowerCase().endsWith( ".svg" ) ) { 182 // initialize the the classes required for svg handling 183 bos = new ByteArrayOutputStream( 2000 ); 184 output = new TranscoderOutput( bos ); 185 // PNGTranscoder is needed to handle transparent parts 186 // of a SVG 187 trc = new PNGTranscoder(); 188 try { 189 input = new TranscoderInput( t ); 190 } catch ( Exception e ) { 191 e.printStackTrace(); 192 } 193 } else { 194 InputStream is = onlineResource.openStream(); 195 MemoryCacheSeekableStream mcss = new MemoryCacheSeekableStream( is ); 196 RenderedOp rop = JAI.create( "stream", mcss ); 197 image = rop.getAsBufferedImage(); 198 mcss.close(); 199 is.close(); 200 } 201 } catch ( Exception e ) { 202 LOG.logError( e.getMessage(), e ); 203 } 204 } 205 206 /** 207 * returns the external graphic as an image. this method is not part of the sld specifications but it is added for 208 * speed up applications 209 * 210 * @param targetSizeX 211 * @param targetSizeY 212 * @param feature 213 * 214 * @return the external graphic as BufferedImage 215 */ 216 public BufferedImage getAsImage( int targetSizeX, int targetSizeY, Feature feature ) { 217 218 if ( ( ( this.input == null ) && ( this.image == null ) ) || feature != null ) { 219 URL onlineResource = initializeOnlineResource( feature ); 220 if ( onlineResource != null ) { 221 LOG.logDebug( "image URL:", onlineResource ); 222 image = cache.get( onlineResource.getFile() ); 223 if ( image == null ) { 224 retrieveImage( onlineResource ); 225 cache.put( onlineResource.getFile(), image ); 226 } 227 } else { 228 LOG.logDebug( "image URL can not be created return default image instead" ); 229 return defaultImage; 230 } 231 } 232 233 if ( input != null ) { 234 image = cache.get( targetSizeX + '_' + targetSizeY + '_' + onlineResource.getFile() ); 235 if ( image == null ) { 236 if ( targetSizeX <= 0 ) { 237 targetSizeX = 1; 238 } 239 if ( targetSizeY <= 0 ) { 240 targetSizeY = 1; 241 } 242 243 trc.addTranscodingHint( SVGAbstractTranscoder.KEY_HEIGHT, new Float( targetSizeX ) ); 244 trc.addTranscodingHint( SVGAbstractTranscoder.KEY_WIDTH, new Float( targetSizeY ) ); 245 try { 246 trc.transcode( input, output ); 247 try { 248 bos.flush(); 249 bos.close(); 250 } catch ( IOException e3 ) { 251 e3.printStackTrace(); 252 } 253 } catch ( TranscoderException e ) { 254 LOG.logError( e.getMessage(), e ); 255 } 256 try { 257 ByteArrayInputStream is = new ByteArrayInputStream( bos.toByteArray() ); 258 MemoryCacheSeekableStream mcss = new MemoryCacheSeekableStream( is ); 259 RenderedOp rop = JAI.create( "stream", mcss ); 260 image = rop.getAsBufferedImage(); 261 cache.put( targetSizeX + '_' + targetSizeY + '_' + onlineResource.getFile(), image ); 262 mcss.close(); 263 } catch ( IOException e1 ) { 264 LOG.logError( e1.getMessage(), e1 ); 265 } 266 } 267 } 268 if ( image == null ) { 269 LOG.logDebug( "image URL can not be created return default image instead" ); 270 image = defaultImage; 271 } 272 273 return image; 274 } 275 276 /** 277 * @param feature 278 * @return online resource URL 279 */ 280 private URL initializeOnlineResource( Feature feature ) { 281 282 String file = null; 283 try { 284 file = this.onlineResource.toURI().toASCIIString(); 285 LOG.logDebug( "external graphic pattern: ", file ); 286 } catch ( URISyntaxException e1 ) { 287 e1.printStackTrace(); 288 } 289 String[] tags = StringTools.extractStrings( file, "$", "$" ); 290 291 if ( tags != null ) { 292 FeatureProperty[] properties = feature.getProperties(); 293 if ( properties != null ) { 294 for ( int i = 0; i < tags.length; i++ ) { 295 String tag = tags[i].substring( 1, tags[i].length() - 1 ); 296 for ( int j = 0; j < properties.length; j++ ) { 297 if ( properties[j] != null && properties[j].getValue() != null 298 && properties[j].getName().getLocalName().equals( tag ) ) { 299 String to = properties[j].getValue().toString(); 300 file = StringTools.replace( file, tags[i], to, true ); 301 } 302 } 303 } 304 } 305 } 306 URL onlineResource = null; 307 try { 308 onlineResource = new URL( file ); 309 LOG.logDebug( "external graphic URL: ", file ); 310 } catch ( MalformedURLException e ) { 311 LOG.logError( e.getMessage(), e ); 312 } 313 return onlineResource; 314 } 315 316 /** 317 * sets the external graphic as an image. 318 * 319 * @param image 320 * the external graphic as BufferedImage 321 */ 322 public void setAsImage( BufferedImage image ) { 323 this.image = image; 324 } 325 326 /** 327 * exports the content of the ExternalGraphic as XML formated String 328 * 329 * @return xml representation of the ExternalGraphic 330 */ 331 public String exportAsXML() { 332 333 StringBuffer sb = new StringBuffer( 200 ); 334 sb.append( "<ExternalGraphic>" ); 335 sb.append( "<OnlineResource xmlns:xlink='http://www.w3.org/1999/xlink' " ); 336 sb.append( "xlink:type='simple' xlink:href='" ); 337 try { 338 sb.append( onlineResource.toURI().toASCIIString() + "'/>" ); 339 } catch ( URISyntaxException e ) { 340 e.printStackTrace(); 341 } 342 sb.append( "<Format>" ).append( escape( format ) ).append( "</Format>" ); 343 sb.append( "</ExternalGraphic>" ); 344 return sb.toString(); 345 } 346 347 }