001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023:
024: package org.radeox.macro;
025:
026: import java.io.IOException;
027: import java.io.Writer;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Locale;
031: import java.util.Map;
032: import java.util.ResourceBundle;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.radeox.Messages;
037: import org.radeox.api.engine.context.InitialRenderContext;
038: import org.radeox.api.engine.context.RenderContext;
039: import org.radeox.api.macro.MacroParameter;
040: import org.radeox.filter.context.BaseFilterContext;
041: import org.radeox.filter.context.FilterContext;
042: import org.radeox.macro.code.SourceCodeFormatter;
043: import org.radeox.util.Service;
044:
045: /*
046: * Macro for displaying programming language source code. CodeMacro knows about
047: * different source code formatters which can be plugged into radeox to display
048: * more languages. CodeMacro displays Java, Ruby or SQL code. @author stephan
049: * @team sonicteam
050: *
051: * @version $Id: CodeMacro.java 29159 2007-04-19 01:46:15Z ajpoland@iupui.edu $
052: */
053:
054: public class CodeMacro extends LocalePreserved {
055: private static Log log = LogFactory.getLog(CodeMacro.class);
056:
057: private Map formatters;
058:
059: private FilterContext nullContext = new BaseFilterContext();
060:
061: private String start;
062:
063: private String end;
064:
065: private String[] paramDescription = { Messages
066: .getString("CodeMacro.0") }; //$NON-NLS-1$
067:
068: public String[] getParamDescription() {
069: return paramDescription;
070: }
071:
072: public String getLocaleKey() {
073: return "macro.code"; //$NON-NLS-1$
074: }
075:
076: public void setInitialContext(InitialRenderContext context) {
077: super .setInitialContext(context);
078: Locale outputLocale = (Locale) context
079: .get(RenderContext.OUTPUT_LOCALE);
080: String outputName = (String) context
081: .get(RenderContext.OUTPUT_BUNDLE_NAME);
082: ResourceBundle outputMessages = ResourceBundle.getBundle(
083: outputName, outputLocale);
084:
085: start = outputMessages.getString(getLocaleKey() + ".start"); //$NON-NLS-1$
086: end = outputMessages.getString(getLocaleKey() + ".end"); //$NON-NLS-1$
087: }
088:
089: public CodeMacro() {
090: formatters = new HashMap();
091:
092: Iterator formatterIt = Service
093: .providers(SourceCodeFormatter.class);
094: while (formatterIt.hasNext()) {
095: try {
096: SourceCodeFormatter formatter = (SourceCodeFormatter) formatterIt
097: .next();
098: String name = formatter.getName();
099: if (formatters.containsKey(name)) {
100: SourceCodeFormatter existing = (SourceCodeFormatter) formatters
101: .get(name);
102: if (existing.getPriority() < formatter
103: .getPriority()) {
104: formatters.put(name, formatter);
105: log.debug("Replacing formatter: " //$NON-NLS-1$
106: + formatter.getClass()
107: + " (" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$
108: }
109: } else {
110: formatters.put(name, formatter);
111: log
112: .debug("Loaded formatter: " + formatter.getClass() //$NON-NLS-1$
113: + " (" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$
114: }
115: } catch (Exception e) {
116: log.warn("CodeMacro: unable to load code formatter", e); //$NON-NLS-1$
117: }
118: }
119:
120: addSpecial('[');
121: addSpecial(']');
122: addSpecial('{');
123: addSpecial('}');
124: addSpecial('*');
125: addSpecial('-');
126: addSpecial('\\');
127: }
128:
129: public void execute(Writer writer, MacroParameter params)
130: throws IllegalArgumentException, IOException {
131:
132: SourceCodeFormatter formatter = null;
133:
134: if (params.getLength() == 0
135: || !formatters.containsKey(params.get("0"))) //$NON-NLS-1$
136: {
137: formatter = (SourceCodeFormatter) formatters
138: .get(initialContext
139: .get(RenderContext.DEFAULT_FORMATTER));
140: if (null == formatter) {
141: System.err.println("Formatter not found."); //$NON-NLS-1$
142: formatter = (SourceCodeFormatter) formatters
143: .get("java"); //$NON-NLS-1$
144: }
145: } else {
146: formatter = (SourceCodeFormatter) formatters.get(params
147: .get("0")); //$NON-NLS-1$
148: }
149:
150: String result = formatter.filter(params.getContent(),
151: nullContext);
152: result = replace(result.trim());
153:
154: writer.write(start);
155: writer.write(result.replaceAll("\n", "
")); //$NON-NLS-1$ //$NON-NLS-2$
156: writer.write(end);
157: return;
158: }
159: }
|