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.engine;
025:
026: import java.io.BufferedReader;
027: import java.io.IOException;
028: import java.io.Reader;
029: import java.io.Writer;
030: import java.util.Iterator;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.radeox.EngineManager;
035: import org.radeox.api.engine.RenderEngine;
036: import org.radeox.api.engine.context.InitialRenderContext;
037: import org.radeox.api.engine.context.RenderContext;
038: import org.radeox.api.macro.Macro;
039: import org.radeox.engine.context.BaseInitialRenderContext;
040: import org.radeox.filter.Filter;
041: import org.radeox.filter.FilterPipe;
042: import org.radeox.filter.context.BaseFilterContext;
043: import org.radeox.filter.context.FilterContext;
044: import org.radeox.macro.MacroRepository;
045: import org.radeox.util.Service;
046:
047: /**
048: * Base implementation of RenderEngine
049: *
050: * @author Stephan J. Schmidt
051: * @version $Id: BaseRenderEngine.java 7707 2006-04-12 17:30:19Z
052: * ian@caret.cam.ac.uk $
053: */
054:
055: public class BaseRenderEngine implements RenderEngine {
056: private static Log log = LogFactory.getLog(BaseRenderEngine.class);
057:
058: protected InitialRenderContext initialContext;
059:
060: protected FilterPipe fp;
061:
062: public BaseRenderEngine(InitialRenderContext context) {
063: this .initialContext = context;
064: }
065:
066: public BaseRenderEngine() {
067: this (new BaseInitialRenderContext());
068: }
069:
070: protected void init() {
071: if (null == fp) {
072: fp = new FilterPipe(initialContext);
073:
074: Iterator iterator = Service.providers(Filter.class);
075: while (iterator.hasNext()) {
076: try {
077: Filter filter = (Filter) iterator.next();
078: fp.addFilter(filter);
079: log.debug("Loaded filter: "
080: + filter.getClass().getName());
081: } catch (Exception e) {
082: log.warn("BaseRenderEngine: unable to load filter",
083: e);
084: }
085: }
086:
087: fp.init();
088: // Logger.debug("FilterPipe = "+fp.toString());
089: }
090: }
091:
092: /**
093: * Name of the RenderEngine. This is used to get a RenderEngine instance
094: * with EngineManager.getInstance(name);
095: *
096: * @return name Name of the engine
097: */
098: public String getName() {
099: return EngineManager.DEFAULT;
100: }
101:
102: /**
103: * Render an input with text markup and return a String with e.g. HTML
104: *
105: * @param content
106: * String with the input to render
107: * @param context
108: * Special context for the filter engine, e.g. with configuration
109: * information
110: * @return result Output with rendered content
111: */
112: public String render(String content, RenderContext context) {
113: init();
114: FilterContext filterContext = new BaseFilterContext();
115: filterContext.setRenderContext(context);
116: return fp.filter(content, filterContext);
117: }
118:
119: /**
120: * Render an input with text markup from a Reader and write the result to a
121: * writer
122: *
123: * @param in
124: * Reader to read the input from
125: * @param context
126: * Special context for the render engine, e.g. with configuration
127: * information
128: */
129: public String render(Reader in, RenderContext context)
130: throws IOException {
131: StringBuffer buffer = new StringBuffer();
132: BufferedReader inputReader = new BufferedReader(in);
133: String line;
134: while ((line = inputReader.readLine()) != null) {
135: buffer.append(line);
136: }
137: return render(buffer.toString(), context);
138: }
139:
140: public void render(Writer out, String content, RenderContext context)
141: throws IOException {
142: out.write(render(content, context));
143: }
144:
145: public void addMacro(Macro macro) {
146: MacroRepository mr = MacroRepository.getInstance();
147: mr.put(macro.getName(), macro);
148: }
149:
150: }
|