001/* 
002    Copyright (C) 2007 JSPWiki Developer Group
003
004    Licensed to the Apache Software Foundation (ASF) under one
005    or more contributor license agreements.  See the NOTICE file
006    distributed with this work for additional information
007    regarding copyright ownership.  The ASF licenses this file
008    to you under the Apache License, Version 2.0 (the
009    "License"); you may not use this file except in compliance
010    with the License.  You may obtain a copy of the License at
011
012       http://www.apache.org/licenses/LICENSE-2.0
013
014    Unless required by applicable law or agreed to in writing,
015    software distributed under the License is distributed on an
016    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017    KIND, either express or implied.  See the License for the
018    specific language governing permissions and limitations
019    under the License.  
020 */
021package org.apache.wiki.plugin;
022
023import java.util.Map;
024
025import org.apache.wiki.WikiContext;
026import org.apache.wiki.WikiEngine;
027import org.apache.wiki.api.exceptions.PluginException;
028import org.apache.wiki.api.plugin.WikiPlugin;
029import org.apache.wiki.util.TextUtil;
030
031/**
032 * Outputs an image with the supplied text as the <tt>title</tt> which is shown as a tooltip by
033 * most browsers. This is intended for short one line comments.
034 * <p>
035 * See http://www.456bereastreet.com/archive/200412/the_alt_and_title_attributes/ for discussion on
036 * alt and title attributes.
037 * <p>
038 * Adaption of the CommentPlugin written by Scott Hulbert, cleaned up and generalized, but basically
039 * his concept.
040 * <p>
041 * 
042 *  <p>Parameters : </p>
043 *  <ul>
044 *  <li><b>_cmdline</b> - the commentText</li>
045 *  </ul>
046 *  
047 */
048public class Note implements WikiPlugin
049{
050    /**
051     *  Property name for setting the image for the note.  Value is <tt>{@value}</tt>.
052     */
053    public static final String PROP_NOTE_IMAGE    = "notePlugin.imageName";
054    
055    /**
056     *  The default name for the note.  Value is <tt>{@value}</tt>.
057     */
058    public static final String DEFAULT_NOTE_IMAGE = "note.png";
059
060    /**
061     *  {@inheritDoc}
062     */
063    public String execute(WikiContext context, Map<String, String> params) throws PluginException
064    {
065        String commandline = params.get(DefaultPluginManager.PARAM_CMDLINE);
066        if (commandline == null || commandline.length() == 0)
067        {
068            return "Unable to obtain plugin command line from parameter'" + DefaultPluginManager.PARAM_CMDLINE + "'"; // I18N
069        }
070
071        String commentImage = imageUrl(context);
072
073        String commentText = clean(commandline);
074
075        return "<img src='" + commentImage + "' alt=\"Comment: " + 
076               commentText + "\" title=\"" + commentText + "\"/>";
077    }
078
079    private String imageUrl( WikiContext ctx )
080    {
081        WikiEngine engine = ctx.getEngine();
082        String commentImage = engine.getWikiProperties().getProperty(PROP_NOTE_IMAGE,
083                                                                     DEFAULT_NOTE_IMAGE);
084
085        commentImage = "images/"+commentImage;
086        
087        String resource = engine.getTemplateManager().findResource( ctx, 
088                                                                    engine.getTemplateDir(), 
089                                                                    commentImage );
090        
091        // JSPWIKI-876 Fixed error with Note Plugin. Only one preceding "/" is needed.
092        if (resource != null && resource.startsWith("/")) {
093            resource = resource.substring(1);
094        }
095        return ctx.getURL( WikiContext.NONE, resource );
096    }
097
098
099    /**
100     *  Cleans the side.
101     * 
102     * @param commandline
103     */
104    private String clean(String commandline)
105    {
106        return TextUtil.replaceEntities( commandline );
107    }
108
109}
110