KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > macro > ImageMacro


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.render.macro;
26
27 import org.radeox.api.engine.ImageRenderEngine;
28 import org.radeox.api.engine.RenderEngine;
29 import org.radeox.util.Encoder;
30 import org.radeox.util.i18n.ResourceManager;
31 import org.snipsnap.app.Application;
32 import org.snipsnap.config.Configuration;
33 import org.snipsnap.render.macro.parameter.SnipMacroParameter;
34 import org.snipsnap.snip.Snip;
35 import org.snipsnap.snip.SnipLink;
36 import org.snipsnap.snip.SnipSpaceFactory;
37
38 import java.io.IOException JavaDoc;
39 import java.io.Writer JavaDoc;
40 import java.util.ResourceBundle JavaDoc;
41
42 /*
43  * Macro that displays images
44  *
45  * @author stephan
46  * @team sonicteam
47  * @version $Id: ImageMacro.java 1606 2004-05-17 10:56:18Z leo $
48  */

49
50 public class ImageMacro extends SnipMacro {
51   Configuration config;
52
53   public String JavaDoc getName() {
54     return "image";
55   }
56
57   public String JavaDoc getDescription() {
58     return ResourceManager.getString("i18n.messages", "macro.image.description");
59   }
60
61   public String JavaDoc[] getParamDescription() {
62     return ResourceManager.getString("i18n.messages", "macro.image.params").split(";");
63   }
64
65   public ImageMacro() {
66     config = Application.get().getConfiguration();
67   }
68
69   public void execute(Writer JavaDoc writer, SnipMacroParameter params)
70     throws IllegalArgumentException JavaDoc, IOException JavaDoc {
71
72     RenderEngine engine = params.getContext().getRenderEngine();
73
74     if (engine instanceof ImageRenderEngine) {
75       ImageRenderEngine imageEngine = (ImageRenderEngine) engine;
76
77       if (params.getLength() > 0) {
78         String JavaDoc img = params.get("img");
79         String JavaDoc alt = null, ext = null, align = null, target = null;
80         boolean qualifiedParams = img != null;
81         if (qualifiedParams) {
82           alt = params.get("alt");
83           ext = params.get("ext");
84           align = params.get("align");
85           target = params.get("target");
86         } else {
87           img = params.get(0);
88           alt = params.get(1);
89           ext = params.get(2);
90           align = params.get(3);
91           target = params.get(4);
92         }
93
94         String JavaDoc link = params.get("link");
95         if (link != null) {
96           writer.write("<a HREF=\"" + Encoder.escape(link) + "\"");
97           if (target != null) {
98             writer.write("target=\""+ Encoder.escape(target) +"\"");
99           }
100           writer.write(">");
101         }
102
103         String JavaDoc imageName = img;
104
105         if (imageName.startsWith("http://")) {
106           if (config.allow(Configuration.APP_PERM_EXTERNALIMAGES)) {
107             appendExternalImage(writer, imageName, align);
108           }
109         } else {
110           // Does the name contain an extension?
111
int dotIndex = imageName.lastIndexOf('.');
112           if (-1 != dotIndex) {
113             ext = imageName.substring(dotIndex + 1);
114             imageName = imageName.substring(0, dotIndex);
115           }
116
117           Snip snip = params.getSnipRenderContext().getSnip();
118           int slashIndex = imageName.lastIndexOf('/');
119           if(-1 != slashIndex) {
120             String JavaDoc snipName = imageName.substring(0, slashIndex);
121             snip = SnipSpaceFactory.getInstance().load(snipName);
122             imageName = imageName.substring(slashIndex + 1);
123           }
124
125           if ("svg".equals(ext)) {
126             // SVG cannot be used with <image>
127
writer.write("<object data=\"");
128             writer.write(snip.getNameEncoded() + "/" + imageName);
129             writer.write(".");
130             writer.write(ext);
131             writer.write("\" type=\"image/svg+xml\" width=\"400\" height=\"400\"></object>");
132           } else {
133             SnipLink.appendImage(writer, snip, imageName, alt, ext, align);
134           }
135         }
136
137         if (link != null) {
138           writer.write("</a>");
139         }
140       }
141     } else {
142       throw new IllegalArgumentException JavaDoc("Number of arguments does not match");
143     }
144     return;
145   }
146
147
148   public static Writer JavaDoc appendExternalImage(Writer JavaDoc writer, String JavaDoc url, String JavaDoc position) throws IOException JavaDoc {
149     writer.write("<img SRC=\"");
150     writer.write(url);
151     writer.write("\" ");
152     if (position != null) {
153       writer.write("class=\"");
154       writer.write(position);
155       writer.write("\" ");
156     }
157     writer.write("border=\"0\"/>");
158     return writer;
159   }
160 }
161
Popular Tags