public String execute()

in jspwiki-main/src/main/java/org/apache/wiki/plugin/Image.java [97:212]


    public String execute( final Context context, final Map<String, String> params ) throws PluginException {
        final Engine engine  = context.getEngine();
        String src           = getCleanParameter( params, PARAM_SRC );
        final String align   = getCleanParameter( params, PARAM_ALIGN );
        final String ht      = getCleanParameter( params, PARAM_HEIGHT );
        final String wt      = getCleanParameter( params, PARAM_WIDTH );
        final String alt     = getCleanParameter( params, PARAM_ALT );
        final String caption = getCleanParameter( params, PARAM_CAPTION );
        final String link    = getCleanParameter( params, PARAM_LINK );
        String target        = getCleanParameter( params, PARAM_TARGET );
        final String style   = getCleanParameter( params, PARAM_STYLE );
        final String cssclass= getCleanParameter( params, PARAM_CLASS );
        final String border  = getCleanParameter( params, PARAM_BORDER );
        final String title   = getCleanParameter( params, PARAM_TITLE );

        if( src == null ) {
            throw new PluginException("Parameter 'src' is required for Image plugin");
        }

        //if( cssclass == null ) cssclass = "imageplugin";

        if( target != null && !validTargetValue(target) ) {
            target = null; // not a valid value so ignore
        }

        try {
            final AttachmentManager mgr = engine.getManager( AttachmentManager.class );
            final Attachment att = mgr.getAttachmentInfo( context, src );

            if( att != null ) {
                src = context.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), att.getName() );
            }
        } catch( final ProviderException e ) {
            throw new PluginException( "Attachment info failed: " + e.getMessage() );
        }

        final StringBuilder result = new StringBuilder();

        result.append( "<table border=\"0\" class=\"imageplugin\"" );

        if( title != null ) {
            result.append( " title=\"" ).append( title ).append( "\"" );
        }

        if( align != null ) {
            if( align.equals( "center" ) ) {
                result.append( " style=\"margin-left: auto; margin-right: auto; text-align:center; vertical-align:middle;\"" );
            } else {
                result.append( " style=\"float:" ).append( align ).append( ";\"" );
            }
        }

        result.append( ">\n" );

        if( caption != null ) {
            result.append( "<caption>" ).append( caption ).append( "</caption>\n" );
        }

        // move css class and style to the container of the image, so it doesn't affect the caption
        result.append( "<tr><td" );

        if( cssclass != null ) {
            result.append( " class=\"" ).append( cssclass ).append( "\"" );
        }

        if( style != null ) {
            result.append( " style=\"" ).append( style );

            // Make sure that we add a ";" to the end of the style string
            if( result.charAt( result.length()-1 ) != ';' ) {
                result.append( ";" );
            }

            result.append("\"");
        }

        result.append( ">" );

        if( link != null ) {
            result.append( "<a href=\"" ).append( link ).append( "\"" );
            if( target != null ) {
                result.append( " target=\"" ).append( target ).append( "\"" );
            }
            result.append(">");
        }

        if( !context.getBooleanWikiProperty( MarkupParser.PROP_ALLOWHTML, false ) ) {
            if( src.startsWith( "data:" ) || src.startsWith( "javascript:" ) ) {
                src = "http://invalid_url" + src;
            }
        }
        result.append( "<img src=\"" ).append( src ).append( "\"" );

        if( ht != null ) {
            result.append( " height=\"" ).append( ht ).append( "\"" );
        }
        if( wt != null ) {
            result.append( " width=\"" ).append( wt ).append( "\"" );
        }
        if( alt != null ) {
            result.append( " alt=\"" ).append( alt ).append( "\"" );
        }
        if( border != null ) {
            result.append( " border=\"" ).append( border ).append( "\"" );
        }
        // if( map != null )    result.append(" map=\""+map+"\"");

        result.append(" />");
        if( link != null ) {
            result.append("</a>");
        }
        result.append("</td></tr>\n");
        result.append("</table>\n");

        return result.toString();
    }