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 org.apache.wiki.api.core.Context;
024import org.apache.wiki.api.core.ContextEnum;
025import org.apache.wiki.api.core.Engine;
026import org.apache.wiki.api.exceptions.PluginException;
027import org.apache.wiki.api.plugin.Plugin;
028import org.apache.wiki.ui.TemplateManager;
029import org.apache.wiki.util.TextUtil;
030
031import java.util.Map;
032
033/**
034 * Outputs an image with the supplied text as the <tt>title</tt> which is shown as a tooltip by
035 * most browsers. This is intended for short one line comments.
036 * <p>
037 * See http://www.456bereastreet.com/archive/200412/the_alt_and_title_attributes/ for discussion on
038 * alt and title attributes.
039 * <p>
040 * Adaption of the CommentPlugin written by Scott Hulbert, cleaned up and generalized, but basically
041 * his concept.
042 * <p>
043 * 
044 *  <p>Parameters : </p>
045 *  <ul>
046 *  <li><b>_cmdline</b> - the commentText</li>
047 *  </ul>
048 *  
049 */
050public class Note implements Plugin {
051
052    /** Property name for setting the image for the note.  Value is <tt>{@value}</tt>. */
053    public static final String PROP_NOTE_IMAGE    = "notePlugin.imageName";
054    
055    /** The default name for the note.  Value is <tt>{@value}</tt>. */
056    public static final String DEFAULT_NOTE_IMAGE = "note.png";
057
058    /**
059     *  {@inheritDoc}
060     */
061    @Override
062    public String execute( final Context context, final Map<String, String> params) throws PluginException {
063        final String commandline = params.get(DefaultPluginManager.PARAM_CMDLINE);
064        if (commandline == null || commandline.isEmpty()) {
065            return "Unable to obtain plugin command line from parameter'" + DefaultPluginManager.PARAM_CMDLINE + "'"; // I18N
066        }
067
068        final String commentImage = imageUrl(context);
069
070        final String commentText = clean(commandline);
071
072        return "<img src='" + commentImage + "' alt=\"Comment: " + 
073               commentText + "\" title=\"" + commentText + "\"/>";
074    }
075
076    private String imageUrl( final Context ctx ) {
077        final Engine engine = ctx.getEngine();
078        String commentImage = engine.getWikiProperties().getProperty( PROP_NOTE_IMAGE, DEFAULT_NOTE_IMAGE );
079        commentImage = "images/" + commentImage;
080        
081        String resource = engine.getManager( TemplateManager.class ).findResource( ctx, engine.getTemplateDir(), commentImage );
082        
083        // JSPWIKI-876 Fixed error with Note Plugin. Only one preceding "/" is needed.
084        if( resource != null && resource.startsWith( "/" ) ) {
085            resource = resource.substring(1);
086        }
087        return ctx.getURL( ContextEnum.PAGE_NONE.getRequestContext(), resource );
088    }
089
090
091    /**
092     *  Cleans the side.
093     * 
094     * @param commandline
095     */
096    private String clean( final String commandline)
097    {
098        return TextUtil.replaceEntities( commandline );
099    }
100
101}
102