Child pages
  • Source code
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

XML

<test>
    <another tag="attribute"/>
</test>
package com.atlassian.confluence.renderer.radeox.macros;

import com.atlassian.confluence.renderer.radeox.ContentPreserver;
import com.atlassian.renderer.macro.macros.AbstractPanelMacro;
import org.apache.log4j.Category;
import org.radeox.filter.context.BaseFilterContext;
import org.radeox.filter.context.FilterContext;
import org.radeox.macro.code.SourceCodeFormatter;
import org.radeox.macro.parameter.MacroParameter;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class CodeMacro extends AbstractPanelMacro
{
    private Category log = Category.getInstance(getClass());
    private ContentPreserver contentPreserver = new ContentPreserver();
    private Map formatters;
    private FilterContext nullContext = new BaseFilterContext();

    public CodeMacro()
    {
        contentPreserver.addEmoticonsAsSpecial();
        contentPreserver.addDefaultSpecials();
        contentPreserver.addSpecial("\n");
        contentPreserver.addSpecial("\r", "");
    }

    public void setCodeFormatterList(List formattersList)
    {
        Iterator it = formattersList.iterator();

        formatters = new HashMap();
        while (it.hasNext())
        {
            try
            {
                SourceCodeFormatter formatter = (SourceCodeFormatter) it.next();
                String name = formatter.getName().toLowerCase();
                if (formatters.containsKey(name))
                {
                    SourceCodeFormatter existing = (SourceCodeFormatter) formatters.get(name);
                    if (existing.getPriority() < formatter.getPriority())
                    {
                        formatters.put(name, formatter);
                        log.debug("Replacing formatter: " + formatter.getClass() + " (" + name + ")");
                    }
                }
                else
                {
                    formatters.put(name, formatter);
                    log.debug("Loaded formatter: " + formatter.getClass() + " (" + name + ")");
                }
            }
            catch (Exception e)
            {
                log.warn("CodeMacro: unable to load code formatter", e);
            }
        }
    }

    public String getName()
    {
        return "code";
    }

    protected String getPanelCSSClass()
    {
        return "code";
    }

    protected String getPanelHeaderCSSClass()
    {
        return "codeHeader";
    }

    protected String getPanelContentCSSClass()
    {
        return "codeContent";
    }

    protected void writeContent(Writer writer, MacroParameter macroParameter, String content, String backgroundColor) throws IOException
    {
        SourceCodeFormatter formatter;

        if (macroParameter.getLength() == 0 || !formatters.containsKey(macroParameter.get("0").toLowerCase()))
            formatter = (SourceCodeFormatter) formatters.get("java");
        else
            formatter = (SourceCodeFormatter) formatters.get(macroParameter.get("0").toLowerCase());

        String formattedContent = formatter.filter(content.trim(), nullContext);
        String renderedContent = "<pre>" + contentPreserver.doPreserve(formattedContent) + "</pre>";

        super.writeContent(writer, macroParameter, renderedContent, backgroundColor);
    }
}

  • No labels