[hmath-commits] org.hmath.server/WEB-INF/src/org/hartmath/server/render HMathRenderEngine.java,NONE,
Status: Pre-Alpha
Brought to you by:
jsurfer
|
From: Klaus H. <js...@us...> - 2004-04-25 18:59:01
|
Update of /cvsroot/hmath/org.hmath.server/WEB-INF/src/org/hartmath/server/render In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25944/WEB-INF/src/org/hartmath/server/render Added Files: HMathRenderEngine.java BaseRenderEngine.java Log Message: misc changes --- NEW FILE: HMathRenderEngine.java --- /* * This file is part of "SnipSnap Wiki/Weblog". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel All Rights Reserved. * * Please visit http://snipsnap.org/ for updates and contact. * * --LICENSE NOTICE-- This program is free software; you can redistribute it and/or modify it under the terms of the GNU General * Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. --LICENSE NOTICE-- */ package org.hartmath.server.render; import java.io.IOException; import java.io.Writer; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.hartmath.server.cache.CachedPageCompiler; import org.hartmath.server.cache.CachedPage; import org.hartmath.server.filter.CachedWikipediaFilter; import org.hartmath.server.render.filter.context.HMathFilterContext; import org.radeox.api.engine.ImageRenderEngine; import org.radeox.api.engine.IncludeRenderEngine; import org.radeox.api.engine.WikiRenderEngine; import org.radeox.api.engine.context.RenderContext; import org.radeox.engine.context.BaseInitialRenderContext; import org.radeox.filter.context.FilterContext; import org.radeox.macro.MacroRepository; import org.radeox.util.Encoder; import org.snipsnap.app.Application; import org.snipsnap.config.Configuration; import org.snipsnap.render.context.SnipRenderContext; import org.snipsnap.render.macro.parameter.SnipMacroParameter; import org.snipsnap.serialization.StringBufferWriter; import org.snipsnap.snip.Snip; import org.snipsnap.snip.SnipLink; import org.snipsnap.snip.SnipSpaceFactory; import org.snipsnap.user.AuthenticationService; /** * renderEngine implementation for SnipSnap which understoods e.g. howto include other snips. * * @author Stephan J. Schmidt * @version $Id: HMathRenderEngine.java,v 1.1 2004/04/25 18:55:56 jsurfer Exp $ */ public class HMathRenderEngine extends BaseRenderEngine implements WikiRenderEngine, IncludeRenderEngine, ImageRenderEngine { // private SnipSpace space; private AuthenticationService authService; public HMathRenderEngine(AuthenticationService authService) { this.authService = authService; //MacroRepository.getInstance().addLoader(new GroovyMacroLoader()); // DOES NOT WORK BECAUSE OF ASPECTS // this.space = space; } public String getName() { return "snipsnap"; } public boolean exists(String name) { return SnipSpaceFactory.getInstance().exists(name); } public boolean showCreate() { return authService.isAuthenticated(Application.get().getUser()); } public void appendLink(StringBuffer buffer, String name, String view, String anchor) { SnipLink.appendLink(buffer, name, view, anchor); } public void appendLink(StringBuffer buffer, String name, String view) { SnipLink.appendLink(buffer, name, view); } public void appendCreateLink(StringBuffer buffer, String name, String view) { Configuration config = Application.get().getConfiguration(); String encodedSpace = config.getEncodedSpace(); if (name.indexOf(encodedSpace.charAt(0)) == -1) { SnipLink.appendCreateLink(buffer, name); } else { buffer.append("[<span class=\"error\">illegal '" + encodedSpace + "' in " + Encoder.escape(name) + "</span>]"); } } public String include(String name) { Snip includeSnip = SnipSpaceFactory.getInstance().load(name); if (null != includeSnip) { return includeSnip.getContent(); } else { return null; } } private String externalImageLink = null; public String getExternalImageLink() { if (null == externalImageLink) { Writer writer = new StringBufferWriter(); try { SnipLink.appendImage(writer, "Icon-Extlink", ">>"); } catch (IOException e) { // ignore } externalImageLink = writer.toString(); } return externalImageLink; } public String render(String content, RenderContext context) { init(); FilterContext filterContext = new HMathFilterContext(((SnipRenderContext) context).getSnip()); filterContext.setRenderContext(context); context.setRenderEngine(this); Map map = Application.get().getParameters(); HttpServletRequest request = (HttpServletRequest) map.get("request"); if (request.getAttribute("previewMode") != null) { CachedWikipediaFilter f = new CachedWikipediaFilter(); // f.setInitialContext(new BaseInitialRenderContext()); MacroRepository macros = MacroRepository.getInstance(); macros.setInitialContext(new BaseInitialRenderContext()); return f.filter(content, filterContext, macros, CachedWikipediaFilter.DUMMY_CACHED_PAGE, 1); } CachedWikipediaFilter f = new CachedWikipediaFilter(); MacroRepository macros = MacroRepository.getInstance(); macros.setInitialContext(new BaseInitialRenderContext()); SnipMacroParameter params = (SnipMacroParameter) filterContext.getMacroParameter(); Snip snip = params.getSnipRenderContext().getSnip(); String name = snip.getName(); String parent = snip.getParentName(); if (parent != null) { name = parent + name; } name = ((String) Application.get().getObject(Application.OID)) + name; String versionName = name+ snip.getVersion(); CachedPage cachedPage = (CachedPage) CachedPageCompiler.PAGE_MAP.get(versionName); if (cachedPage == null) { versionName = name+ (snip.getVersion()-1); cachedPage = (CachedPage) CachedPageCompiler.PAGE_MAP.get(versionName); if (cachedPage != null) { // delete previous version // System.out.println("remove:"+versionName); synchronized (CachedPageCompiler.PAGE_MAP){ CachedPageCompiler.PAGE_MAP.remove(versionName); } } } else { try { // System.out.println("found:"+versionName); return ((CachedPage)cachedPage.clone()).filter(filterContext, macros); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // create a new class: CachedPageCompiler compiler = new CachedPageCompiler("P" + Long.toString(CachedPageCompiler.CLASS_COUNTER)); String result = f.filter(content, filterContext, macros, compiler, 0); cachedPage = compiler.getCachedPage(); if (cachedPage != null && name != null) { versionName = name+ snip.getVersion(); // System.out.println("put:"+versionName); synchronized (CachedPageCompiler.PAGE_MAP){ CachedPageCompiler.PAGE_MAP.put(versionName, cachedPage); } return cachedPage.filter(filterContext, macros); } return result; } public String renderSnippet(String content, RenderContext context) { init(); FilterContext filterContext = new HMathFilterContext(((SnipRenderContext) context).getSnip()); filterContext.setRenderContext(context); context.setRenderEngine(this); CachedWikipediaFilter f = new CachedWikipediaFilter(); // f.setInitialContext(new BaseInitialRenderContext()); MacroRepository macros = MacroRepository.getInstance(); macros.setInitialContext(new BaseInitialRenderContext()); return f.filter(content, filterContext, macros, CachedWikipediaFilter.DUMMY_CACHED_PAGE, 1); } } --- NEW FILE: BaseRenderEngine.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel * All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * --LICENSE NOTICE-- */ package org.hartmath.server.render; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.radeox.EngineManager; import org.radeox.api.engine.RenderEngine; import org.radeox.engine.context.BaseInitialRenderContext; import org.radeox.api.engine.context.InitialRenderContext; import org.radeox.api.engine.context.RenderContext; import org.radeox.filter.Filter; import org.radeox.filter.FilterPipe; import org.radeox.filter.context.BaseFilterContext; import org.radeox.filter.context.FilterContext; import org.radeox.util.Service; import java.io.*; import java.util.Iterator; /** * Base implementation of RenderEngine * * @author Stephan J. Schmidt * @version $Id: BaseRenderEngine.java,v 1.1 2004/04/25 18:55:57 jsurfer Exp $ */ public abstract class BaseRenderEngine implements RenderEngine { private static Log log = LogFactory.getLog(BaseRenderEngine.class); protected InitialRenderContext initialContext; public BaseRenderEngine(InitialRenderContext context) { this.initialContext = context; } public BaseRenderEngine() { this(new BaseInitialRenderContext()); } protected void init() { } /** * Name of the RenderEngine. This is used to get a RenderEngine instance * with EngineManager.getInstance(name); * * @return name Name of the engine */ public String getName() { return EngineManager.DEFAULT; } /** * Render an input with text markup from a Reader and write the result to a writer * * @param in Reader to read the input from * @param context Special context for the render engine, e.g. with * configuration information */ public String render(Reader in, RenderContext context) throws IOException { StringBuffer buffer = new StringBuffer(); BufferedReader inputReader = new BufferedReader(in); String line; while ((line = inputReader.readLine()) != null) { buffer.append(line); } return render(buffer.toString(), context); } public void render(Writer out, String content, RenderContext context) throws IOException { out.write(render(content, context)); } } |