package com.lognet.wiki.plugin.link; import java.util.Map; import com.ecyrd.jspwiki.plugin.*; import com.ecyrd.jspwiki.*; import com.ecyrd.jspwiki.attachment.AttachmentManager; import com.ecyrd.jspwiki.attachment.Attachment; import com.ecyrd.jspwiki.providers.ProviderException; /** * * * @author Lou Masters * based on code from Arnaud Lacour */ public class Link implements WikiPlugin { public static final String PARAM_SRC = "src"; public static final String PARAM_LABEL = "label"; public static final String PARAM_TARGET = "target"; /** * This method is used to clean away things like quotation marks which * a malicious user could use to stop processing and insert javascript. */ private static final String getCleanParameter( Map params, String paramId ) { return TextUtil.replaceEntities( (String) params.get( paramId ) ); } public String execute( WikiContext context, Map params ) throws PluginException { WikiEngine engine = context.getEngine(); String src = getCleanParameter( params, PARAM_SRC ); String label = getCleanParameter( params, PARAM_LABEL ); String target = getCleanParameter( params, PARAM_TARGET ); if( src == null ) { throw new PluginException("Parameter 'src' is required for Link plugin"); } if ( target == null ) { target = "_self"; } if ( label == null ) { label = ""; } try { AttachmentManager mgr = engine.getAttachmentManager(); Attachment att = mgr.getAttachmentInfo( context, src ); if(att==null) { System.out.println("Attachment null with Src= " + src); } if( att != null ) { src = context.getURL( WikiContext.ATTACH, att.getName() ); } } catch( ProviderException e ) { throw new PluginException( "Attachment info failed: "+e.getMessage() ); } catch(Exception ee) { ee.printStackTrace(); throw new PluginException( "General error getting attachment: "+ee.getMessage() ); } StringBuffer result = new StringBuffer(); result.append(""); result.append(label); result.append(""); return result.toString(); } }