001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.plugin;
021:
022: import java.util.*;
023: import com.ecyrd.jspwiki.*;
024: import com.ecyrd.jspwiki.attachment.AttachmentManager;
025: import com.ecyrd.jspwiki.attachment.Attachment;
026: import com.ecyrd.jspwiki.providers.ProviderException;
027:
028: /**
029: * Provides an image plugin for better control than is possible with
030: * a simple image inclusion.
031: *
032: * @author Janne Jalkanen
033: * @since 2.1.4.
034: */
035: // FIXME: It is not yet possible to do wiki internal links. In order to
036: // do this cleanly, a TranslatorReader revamp is needed.
037: public class Image implements WikiPlugin {
038: public static final String PARAM_SRC = "src";
039: public static final String PARAM_ALIGN = "align";
040: public static final String PARAM_HEIGHT = "height";
041: public static final String PARAM_WIDTH = "width";
042: public static final String PARAM_ALT = "alt";
043: public static final String PARAM_CAPTION = "caption";
044: public static final String PARAM_LINK = "link";
045: public static final String PARAM_TARGET = "target";
046: public static final String PARAM_STYLE = "style";
047: public static final String PARAM_CLASS = "class";
048: // public static final String PARAM_MAP = "map";
049: public static final String PARAM_BORDER = "border";
050:
051: /**
052: * This method is used to clean away things like quotation marks which
053: * a malicious user could use to stop processing and insert javascript.
054: */
055: private static final String getCleanParameter(Map params,
056: String paramId) {
057: return TextUtil.replaceEntities((String) params.get(paramId));
058: }
059:
060: public String execute(WikiContext context, Map params)
061: throws PluginException {
062: WikiEngine engine = context.getEngine();
063: String src = getCleanParameter(params, PARAM_SRC);
064: String align = getCleanParameter(params, PARAM_ALIGN);
065: String ht = getCleanParameter(params, PARAM_HEIGHT);
066: String wt = getCleanParameter(params, PARAM_WIDTH);
067: String alt = getCleanParameter(params, PARAM_ALT);
068: String caption = getCleanParameter(params, PARAM_CAPTION);
069: String link = getCleanParameter(params, PARAM_LINK);
070: String target = getCleanParameter(params, PARAM_TARGET);
071: String style = getCleanParameter(params, PARAM_STYLE);
072: String cssclass = getCleanParameter(params, PARAM_CLASS);
073: // String map = getCleanParameter( params, PARAM_MAP );
074: String border = getCleanParameter(params, PARAM_BORDER);
075:
076: if (src == null) {
077: throw new PluginException(
078: "Parameter 'src' is required for Image plugin");
079: }
080:
081: if (cssclass == null)
082: cssclass = "imageplugin";
083:
084: if (target != null && !validTargetValue(target)) {
085: target = null; // not a valid value so ignore
086: }
087:
088: try {
089: AttachmentManager mgr = engine.getAttachmentManager();
090: Attachment att = mgr.getAttachmentInfo(context, src);
091:
092: if (att != null) {
093: src = context.getURL(WikiContext.ATTACH, att.getName());
094: }
095: } catch (ProviderException e) {
096: throw new PluginException("Attachment info failed: "
097: + e.getMessage());
098: }
099:
100: StringBuffer result = new StringBuffer();
101:
102: result.append("<table border=\"0\" class=\"" + cssclass + "\"");
103: //if( align != null ) result.append(" align=\""+align+"\"");
104: //if( style != null ) result.append(" style=\""+style+"\"");
105:
106: //
107: // Do some magic to make sure centering also work on FireFox
108: //
109: if (style != null) {
110: result.append(" style=\"" + style);
111:
112: // Make sure that we add a ";" to the end of the style string
113: if (result.charAt(result.length() - 1) != ';')
114: result.append(";");
115:
116: if (align != null && align.equals("center")) {
117: result
118: .append(" margin-left: auto; margin-right: auto;");
119: }
120:
121: result.append("\"");
122: } else {
123: if (align != null && align.equals("center"))
124: result
125: .append(" style=\"margin-left: auto; margin-right: auto;\"");
126: }
127:
128: if (align != null && !(align.equals("center")))
129: result.append(" align=\"" + align + "\"");
130:
131: result.append(">\n");
132:
133: if (caption != null) {
134: result.append("<caption align=bottom>"
135: + TextUtil.replaceEntities(caption)
136: + "</caption>\n");
137: }
138:
139: result.append("<tr><td>");
140:
141: if (link != null) {
142: result.append("<a href=\"" + link + "\"");
143: if (target != null) {
144: result.append(" target=\"" + target + "\"");
145: }
146: result.append(">");
147: }
148:
149: result.append("<img src=\"" + src + "\"");
150:
151: if (ht != null)
152: result.append(" height=\"" + ht + "\"");
153: if (wt != null)
154: result.append(" width=\"" + wt + "\"");
155: if (alt != null)
156: result.append(" alt=\"" + alt + "\"");
157: if (border != null)
158: result.append(" border=\"" + border + "\"");
159: // if( map != null ) result.append(" map=\""+map+"\"");
160:
161: result.append(" />");
162: if (link != null)
163: result.append("</a>");
164: result.append("</td></tr>\n");
165:
166: result.append("</table>\n");
167:
168: return result.toString();
169: }
170:
171: private boolean validTargetValue(String s) {
172: if (s.equals("_blank") || s.equals("_self")
173: || s.equals("_parent") || s.equals("_top")) {
174: return true;
175: } else if (s.length() > 0) // check [a-zA-z]
176: {
177: char c = s.charAt(0);
178: return Character.isLowerCase(c) || Character.isUpperCase(c);
179: }
180: return false;
181: }
182:
183: }
|