KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > SnipRenderEngine


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.render;
27
28 import org.radeox.api.engine.ImageRenderEngine;
29 import org.radeox.api.engine.IncludeRenderEngine;
30 import org.radeox.api.engine.WikiRenderEngine;
31 import org.radeox.api.engine.context.RenderContext;
32 import org.radeox.engine.BaseRenderEngine;
33 import org.radeox.filter.context.FilterContext;
34 import org.radeox.util.Encoder;
35 import org.snipsnap.app.Application;
36 import org.snipsnap.config.Configuration;
37 import org.snipsnap.render.context.SnipRenderContext;
38 import org.snipsnap.render.filter.context.SnipFilterContext;
39 import org.snipsnap.serialization.StringBufferWriter;
40 import org.snipsnap.snip.Snip;
41 import org.snipsnap.snip.SnipLink;
42 import org.snipsnap.snip.SnipSpaceFactory;
43 import org.snipsnap.user.AuthenticationService;
44
45 import java.io.IOException JavaDoc;
46 import java.io.Writer JavaDoc;
47
48 /**
49  * renderEngine implementation for SnipSnap which understoods e.g.
50  * howto include other snips.
51  *
52  * @author Stephan J. Schmidt
53  * @version $Id: SnipRenderEngine.java 1831 2005-08-08 13:06:31Z leo $
54  */

55
56 public class SnipRenderEngine extends BaseRenderEngine
57         implements WikiRenderEngine, IncludeRenderEngine, ImageRenderEngine {
58
59 // private SnipSpace space;
60
private AuthenticationService authService;
61
62   public SnipRenderEngine(AuthenticationService authService) {
63     this.authService = authService;
64     //MacroRepository.getInstance().addLoader(new GroovyMacroLoader());
65
// DOES NOT WORK BECAUSE OF ASPECTS
66
// this.space = space;
67
}
68
69   public String JavaDoc getName() {
70     return "snipsnap";
71   }
72
73   public boolean exists(String JavaDoc name) {
74     return SnipSpaceFactory.getInstance().exists(name);
75   }
76
77   public boolean showCreate() {
78     return authService.isAuthenticated(Application.get().getUser());
79   }
80
81   public void appendLink(StringBuffer JavaDoc buffer, String JavaDoc name, String JavaDoc view, String JavaDoc anchor) {
82     SnipLink.appendLink(buffer, name, view, anchor);
83   }
84
85   public void appendLink(StringBuffer JavaDoc buffer, String JavaDoc name, String JavaDoc view) {
86     SnipLink.appendLink(buffer, name, view);
87   }
88
89   public void appendCreateLink(StringBuffer JavaDoc buffer, String JavaDoc name, String JavaDoc view) {
90     Configuration config = Application.get().getConfiguration();
91     String JavaDoc encodedSpace = config.getEncodedSpace();
92
93     if (name.indexOf(encodedSpace.charAt(0)) == -1) {
94       SnipLink.appendCreateLink(buffer, name);
95     } else {
96       buffer.append("&#91;<span class=\"error\">illegal '" + encodedSpace + "' in " + Encoder.escape(name) + "</span>&#93;");
97     }
98   }
99
100   public String JavaDoc include(String JavaDoc name) {
101     Snip includeSnip = SnipSpaceFactory.getInstance().load(name);
102     if (null != includeSnip) {
103       return includeSnip.getContent();
104     } else {
105       return null;
106     }
107   }
108
109   public String JavaDoc getExternalImageLink() {
110     Writer writer = new StringBufferWriter();
111     try {
112       SnipLink.appendImage(writer, "Icon-Extlink", "&gt;&gt;");
113     } catch (IOException JavaDoc e) {
114       // ignore
115
}
116     return writer.toString();
117   }
118
119   public String JavaDoc render(String JavaDoc content, RenderContext context) {
120     init();
121     FilterContext filterContext = new SnipFilterContext(((SnipRenderContext) context).getSnip());
122     filterContext.setRenderContext(context);
123     context.setRenderEngine(this);
124     return fp.filter(content, filterContext);
125   }
126 }
127
Popular Tags