public String execute()

in jspwiki-main/src/main/java/org/apache/wiki/plugin/TableOfContents.java [151:235]


    public String execute( final Context context, final Map< String, String > params ) throws PluginException {
        final Engine engine = context.getEngine();
        final Page page = context.getPage();
        final ResourceBundle rb = Preferences.getBundle( context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE );

        if( context.getVariable( VAR_ALREADY_PROCESSING ) != null ) {
            //return rb.getString("tableofcontents.title");
            return "<a href=\"#section-TOC\" class=\"toc\">"+rb.getString("tableofcontents.title")+"</a>";
        }

        final StringBuilder sb = new StringBuilder();

        sb.append("<div class=\"toc\">\n");
        sb.append("<div class=\"collapsebox\">\n");

        final String title = params.get(PARAM_TITLE);
        sb.append("<h4 id=\"section-TOC\">");
        if( title != null ) {
            sb.append( TextUtil.replaceEntities( title ) );
        } else {
            sb.append( rb.getString( "tableofcontents.title" ) );
        }
        sb.append( "</h4>\n" );

        // should we use an ordered list?
        m_usingNumberedList = false;
        if( params.containsKey( PARAM_NUMBERED ) ) {
            final String numbered = params.get( PARAM_NUMBERED );
            if( numbered.equalsIgnoreCase( "true" ) ) {
                m_usingNumberedList = true;
            } else if( numbered.equalsIgnoreCase( "yes" ) ) {
                m_usingNumberedList = true;
            }
        }

        // if we are using a numbered list, get the rest of the parameters (if any) ...
        if (m_usingNumberedList) {
            int start = 0;
            final String startStr = params.get(PARAM_START);
            if( ( startStr != null ) && ( startStr.matches( "^\\d+$" ) ) ) {
                start = Integer.parseInt(startStr);
            }
            if (start < 0) start = 0;

            m_starting = start;
            m_level1Index = start - 1;
            if (m_level1Index < 0) m_level1Index = 0;
            m_level2Index = 0;
            m_level3Index = 0;
            m_prefix = TextUtil.replaceEntities( params.get(PARAM_PREFIX) );
            if (m_prefix == null) m_prefix = "";
            m_lastLevel = Heading.HEADING_LARGE;
        }

        try {
            String wikiText = engine.getManager( PageManager.class ).getPureText( page );
            final boolean runFilters = "true".equals( engine.getManager( VariableManager.class ).getValue( context, VariableManager.VAR_RUNFILTERS, "true" ) );

            if( runFilters ) {
				try {
					final FilterManager fm = engine.getManager( FilterManager.class );
					wikiText = fm.doPreTranslateFiltering(context, wikiText);

				} catch( final Exception e ) {
					LOG.error("Could not construct table of contents: Filter Error", e);
					throw new PluginException("Unable to construct table of contents (see logs)");
				}
            }

            context.setVariable( VAR_ALREADY_PROCESSING, "x" );

            final MarkupParser parser = engine.getManager( RenderingManager.class ).getParser( context, wikiText );
            parser.addHeadingListener( this );
            parser.parse();

            sb.append( "<ul>\n" ).append( m_buf ).append( "</ul>\n" );
        } catch( final IOException e ) {
            LOG.error("Could not construct table of contents", e);
            throw new PluginException("Unable to construct table of contents (see logs)");
        }

        sb.append("</div>\n</div>\n");

        return sb.toString();
    }