Source Code Cross Referenced for ExternalGraphic.java in  » GIS » deegree » org » deegree » graphics » sld » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » deegree » org.deegree.graphics.sld 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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