2 * This file is part of "SnipSnap Radeox Rendering Engine".
4 * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel All Rights Reserved.
6 * Please visit http://radeox.org/ for updates and contact.
8 * --LICENSE NOTICE-- This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
9 * General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any
12 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --LICENSE NOTICE--
19 package org.plog4u.wiki.macro;
21 import java.io.IOException;
22 import java.io.Writer;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Locale;
27 import java.util.ResourceBundle;
30 //import org.apache.commons.logging.Log;
31 //import org.apache.commons.logging.LogFactory;
32 import org.plog4u.wiki.filter.ICachableMacro;
33 import org.plog4u.wiki.filter.INoParserBodyFilterMacro;
34 import org.plog4u.wiki.macro.code.AbstractCPPBasedCodeFilter;
35 import org.radeox.api.engine.context.InitialRenderContext;
36 import org.radeox.api.engine.context.RenderContext;
37 import org.radeox.filter.context.BaseFilterContext;
38 import org.radeox.filter.context.FilterContext;
39 import org.radeox.macro.LocalePreserved;
40 import org.radeox.macro.code.SourceCodeFormatter;
41 import org.radeox.macro.parameter.MacroParameter;
42 import org.radeox.util.Encoder;
43 import org.radeox.util.Service;
46 * Macro for displaying programming language source code. CodeMacro knows about different source code formatters which can be
47 * plugged into radeox to display more languages. CodeMacro displays Java, Ruby or SQL code.
49 * @author stephan @team sonicteam
53 public class CodeMacro extends LocalePreserved implements INoParserBodyFilterMacro, ICachableMacro {
54 // private static Log log = LogFactory.getLog(CodeMacro.class);
56 private Map formatters;
57 private FilterContext nullContext = new BaseFilterContext();
62 private String[] paramDescription = { "?1: syntax highlighter to use, defaults to java" };
64 public String[] getParamDescription() {
65 return paramDescription;
68 public String getLocaleKey() {
72 public void setInitialContext(InitialRenderContext context) {
73 super.setInitialContext(context);
74 Locale outputLocale = (Locale) context.get(RenderContext.OUTPUT_LOCALE);
75 String outputName = (String) context.get(RenderContext.OUTPUT_BUNDLE_NAME);
76 ResourceBundle outputMessages = ResourceBundle.getBundle(outputName, outputLocale);
78 start = outputMessages.getString(getLocaleKey() + ".start");
79 end = outputMessages.getString(getLocaleKey() + ".end");
83 formatters = new HashMap();
85 Iterator formatterIt = Service.providers(SourceCodeFormatter.class);
86 while (formatterIt.hasNext()) {
88 SourceCodeFormatter formatter = (SourceCodeFormatter) formatterIt.next();
89 String name = formatter.getName();
90 if (formatters.containsKey(name)) {
91 SourceCodeFormatter existing = (SourceCodeFormatter) formatters.get(name);
92 if (existing.getPriority() < formatter.getPriority()) {
93 formatters.put(name, formatter);
94 // log.debug("Replacing formatter: " + formatter.getClass() + " (" + name + ")");
97 formatters.put(name, formatter);
98 // log.debug("Loaded formatter: " + formatter.getClass() + " (" + name + ")");
100 } catch (Exception e) {
101 // log.warn("CodeMacro: unable to load code formatter", e);
114 public void execute(Writer writer, MacroParameter params) throws IllegalArgumentException, IOException {
116 SourceCodeFormatter formatter = null;
118 if (params.getLength() == 0 || !formatters.containsKey(params.get("0"))) {
119 formatter = (SourceCodeFormatter) formatters.get(initialContext.get(RenderContext.DEFAULT_FORMATTER));
120 if (null == formatter) {
121 System.err.println("Formatter not found.");
122 formatter = (SourceCodeFormatter) formatters.get("java");
123 result = formatter.filter(params.getContent(), nullContext);
124 } else if (formatter instanceof AbstractCPPBasedCodeFilter) {
125 result = formatter.filter(params.getContent(), nullContext);
127 result = formatter.filter(Encoder.escape(params.getContent()), nullContext);
130 formatter = (SourceCodeFormatter) formatters.get(params.get("0"));
131 if (formatter instanceof AbstractCPPBasedCodeFilter) {
132 result = formatter.filter(params.getContent(), nullContext);
134 result = formatter.filter(Encoder.escape(params.getContent()), nullContext);
139 writer.write(replace(result.trim()));