001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/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
049 import javax.media.jai.JAI;
050 import javax.media.jai.RenderedOp;
051
052 import org.apache.batik.transcoder.SVGAbstractTranscoder;
053 import org.apache.batik.transcoder.Transcoder;
054 import org.apache.batik.transcoder.TranscoderException;
055 import org.apache.batik.transcoder.TranscoderInput;
056 import org.apache.batik.transcoder.TranscoderOutput;
057 import org.apache.batik.transcoder.image.PNGTranscoder;
058 import org.deegree.framework.log.ILogger;
059 import org.deegree.framework.log.LoggerFactory;
060 import org.deegree.framework.util.StringTools;
061 import org.deegree.framework.xml.Marshallable;
062 import org.deegree.model.feature.Feature;
063 import org.deegree.model.feature.FeatureProperty;
064
065 import com.sun.media.jai.codec.MemoryCacheSeekableStream;
066
067 /**
068 * The ExternalGraphic element allows a reference to be made to an external graphic file with a Web
069 * URL. The OnlineResource sub-element gives the URL and the Format sub-element identifies the
070 * expected document MIME type of a successful fetch. Knowing the MIME type in advance allows the
071 * styler to select the best- supported format from the list of URLs with equivalent content.
072 *
073 *
074 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
075 * @author last edited by: $Author: mschneider $
076 *
077 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
078 */
079 public class ExternalGraphic implements Marshallable {
080
081 private static final ILogger LOG = LoggerFactory.getLogger( ExternalGraphic.class );
082
083 private BufferedImage image = null;
084
085 private String format = null;
086
087 private URL onlineResource = null;
088
089 private TranscoderInput input = null;
090
091 private ByteArrayOutputStream bos = null;
092
093 private TranscoderOutput output = null;
094
095 private Transcoder trc = null;
096
097 /**
098 * Creates a new ExternalGraphic_Impl object.
099 *
100 * @param format
101 * @param onlineResource
102 */
103 ExternalGraphic( String format, URL onlineResource ) {
104 setFormat( format );
105 setOnlineResource( onlineResource );
106 }
107
108 /**
109 * the Format sub-element identifies the expected document MIME type of a successful fetch.
110 *
111 * @return Format of the external graphic
112 *
113 */
114 public String getFormat() {
115 return format;
116 }
117
118 /**
119 * sets the format (MIME type)
120 *
121 * @param format
122 * Format of the external graphic
123 *
124 */
125 public void setFormat( String format ) {
126 this.format = format;
127 }
128
129 /**
130 * The OnlineResource gives the URL of the external graphic
131 *
132 * @return URL of the external graphic
133 *
134 */
135 public URL getOnlineResource() {
136 return onlineResource;
137 }
138
139 /**
140 * sets the online resource / URL of the external graphic
141 *
142 * @param onlineResource
143 * URL of the external graphic
144 *
145 */
146 public void setOnlineResource( URL onlineResource ) {
147 this.onlineResource = onlineResource;
148 // TODO
149 // removing this is just experimental; possibly initializeOnlineResource( Feature feature )
150 // must be adapted
151 // String file = onlineResource.getFile();
152 // int idx = file.indexOf( "$" );
153 // if ( idx == -1 ) {
154 // retrieveImage( onlineResource );
155 // }
156 }
157
158 /**
159 * @param onlineResource
160 */
161 private void retrieveImage( URL onlineResource ) {
162
163 try {
164 String t =onlineResource.toURI().toASCIIString();
165 if ( t.trim().toLowerCase().endsWith( ".svg" ) ) {
166 // initialize the the classes required for svg handling
167 bos = new ByteArrayOutputStream( 2000 );
168 output = new TranscoderOutput( bos );
169 // PNGTranscoder is needed to handle transparent parts
170 // of a SVG
171 trc = new PNGTranscoder();
172 try {
173 input = new TranscoderInput( t );
174 } catch ( Exception e ) {
175 e.printStackTrace();
176 }
177 } else {
178 InputStream is = onlineResource.openStream();
179 MemoryCacheSeekableStream mcss = new MemoryCacheSeekableStream( is );
180 RenderedOp rop = JAI.create( "stream", mcss );
181 image = rop.getAsBufferedImage();
182 mcss.close();
183 is.close();
184 }
185 } catch ( Exception e ) {
186 LOG.logError( e.getMessage(), e );
187 }
188 }
189
190 /**
191 * returns the external graphic as an image. this method is not part of the sld specifications
192 * but it is added for speed up applications
193 * @param targetSizeX
194 * @param targetSizeY
195 * @param feature
196 *
197 * @return the external graphic as BufferedImage
198 */
199 public BufferedImage getAsImage( int targetSizeX, int targetSizeY, Feature feature ) {
200
201 if ( ( ( this.input == null ) && ( this.image == null ) ) || feature != null ) {
202 URL onlineResource = initializeOnlineResource( feature );
203 retrieveImage( onlineResource );
204 }
205
206 if ( input != null ) {
207 if ( targetSizeX <= 0 ) {
208 targetSizeX = 1;
209 }
210 if ( targetSizeY <= 0 ) {
211 targetSizeY = 1;
212 }
213
214 trc.addTranscodingHint( SVGAbstractTranscoder.KEY_HEIGHT, new Float( targetSizeX ) );
215 trc.addTranscodingHint( SVGAbstractTranscoder.KEY_WIDTH, new Float( targetSizeY ) );
216 try {
217 trc.transcode( input, output );
218 try {
219 bos.flush();
220 bos.close();
221 } catch ( IOException e3 ) {
222 e3.printStackTrace();
223 }
224 } catch ( TranscoderException e ) {
225 LOG.logError( e.getMessage(), e );
226 }
227 try {
228 ByteArrayInputStream is = new ByteArrayInputStream( bos.toByteArray() );
229 MemoryCacheSeekableStream mcss = new MemoryCacheSeekableStream( is );
230 RenderedOp rop = JAI.create( "stream", mcss );
231 image = rop.getAsBufferedImage();
232 mcss.close();
233 } catch ( IOException e1 ) {
234 LOG.logError( e1.getMessage(), e1 );
235 }
236 }
237
238 return image;
239 }
240
241 /**
242 * @param feature
243 * @return online resource URL
244 */
245 private URL initializeOnlineResource( Feature feature ) {
246
247 String file = null;
248 try {
249 file = this.onlineResource.toURI().toASCIIString();
250 LOG.logDebug( "external graphic pattern: ", file );
251 } catch ( URISyntaxException e1 ) {
252 e1.printStackTrace();
253 }
254 String[] tags = StringTools.extractStrings( file, "$", "$" );
255
256 if ( tags != null ) {
257 FeatureProperty[] properties = feature.getProperties();
258 for ( int i = 0; i < tags.length; i++ ) {
259 String tag = tags[i].substring( 1, tags[i].length() - 1 );
260 for ( int j = 0; j < properties.length; j++ ) {
261 if ( properties[j].getName().getLocalName().equals( tag ) ) {
262 String to = properties[j].getValue().toString();
263 file = StringTools.replace( file, tags[i], to, true );
264 }
265 }
266 }
267 }
268 URL onlineResource = null;
269 try {
270 onlineResource = new URL( file );
271 LOG.logDebug( "external graphic URL: ", file );
272 } catch ( MalformedURLException e ) {
273 LOG.logError( e.getMessage(), e );
274 }
275 return onlineResource;
276 }
277
278 /**
279 * sets the external graphic as an image.
280 *
281 * @param image
282 * the external graphic as BufferedImage
283 */
284 public void setAsImage( BufferedImage image ) {
285 this.image = image;
286 }
287
288 /**
289 * exports the content of the ExternalGraphic as XML formated String
290 *
291 * @return xml representation of the ExternalGraphic
292 */
293 public String exportAsXML() {
294
295 StringBuffer sb = new StringBuffer( 200 );
296 sb.append( "<ExternalGraphic>" );
297 sb.append( "<OnlineResource xmlns:xlink='http://www.w3.org/1999/xlink' " );
298 sb.append( "xlink:type='simple' xlink:href='" );
299 try {
300 sb.append( onlineResource.toURI().toASCIIString() + "'/>" );
301 } catch ( URISyntaxException e ) {
302 e.printStackTrace();
303 }
304 sb.append( "<Format>" ).append( escape( format ) ).append( "</Format>" );
305 sb.append( "</ExternalGraphic>" );
306 return sb.toString();
307 }
308
309 }