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    @Override
059    public String getSnipExample() {
060        return "Note " + PROP_NOTE_IMAGE + "=note.png This text shows as tooltip";
061    }
062    /**
063     *  {@inheritDoc}
064     */
065    @Override
066    public String execute( final Context context, final Map<String, String> params) throws PluginException {
067        final String commandline = params.get(DefaultPluginManager.PARAM_CMDLINE);
068        if (commandline == null || commandline.isEmpty()) {
069            return "Unable to obtain plugin command line from parameter'" + DefaultPluginManager.PARAM_CMDLINE + "'"; // I18N
070        }
071
072        final String commentImage = imageUrl(context);
073
074        final String commentText = clean(commandline);
075
076        return "<img src='" + commentImage + "' alt=\"Comment: " + 
077               commentText + "\" title=\"" + commentText + "\"/>";
078    }
079
080    private String imageUrl( final Context ctx ) {
081        final Engine engine = ctx.getEngine();
082        String commentImage = engine.getWikiProperties().getProperty( PROP_NOTE_IMAGE, DEFAULT_NOTE_IMAGE );
083        commentImage = "images/" + commentImage;
084        
085        String resource = engine.getManager( TemplateManager.class ).findResource( ctx, engine.getTemplateDir(), commentImage );
086        
087        // JSPWIKI-876 Fixed error with Note Plugin. Only one preceding "/" is needed.
088        if( resource != null && resource.startsWith( "/" ) ) {
089            resource = resource.substring(1);
090        }
091        return ctx.getURL( ContextEnum.PAGE_NONE.getRequestContext(), resource );
092    }
093
094
095    /**
096     *  Cleans the side.
097     * 
098     * @param commandline
099     */
100    private String clean( final String commandline)
101    {
102        return TextUtil.replaceEntities( commandline );
103    }
104
105}
106