File Source: radeoxdecorator.java
/*
P/P * Method: net.sourceforge.pebble.decorator.RadeoxWikiRenderEngine__static_init
*/
1 package net.sourceforge.pebble.decorator;
2
3 import net.sourceforge.pebble.domain.BlogEntry;
4 import net.sourceforge.pebble.domain.Blog;
5 import net.sourceforge.pebble.domain.StaticPage;
6 import net.sourceforge.pebble.util.UrlRewriter;
7 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
/*
P/P * Method: void net.sourceforge.pebble.decorator.RadeoxDecorator()
*/
8 import org.radeox.api.engine.RenderEngine;
9 import org.radeox.api.engine.WikiRenderEngine;
10 import org.radeox.api.engine.context.InitialRenderContext;
11 import org.radeox.api.engine.context.RenderContext;
12 import org.radeox.engine.BaseRenderEngine;
13 import org.radeox.engine.context.BaseInitialRenderContext;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 /**
19 * Decorates blog entries and comments by rendering them with Radeox, internal
20 * links pointing to static pages within the blog.
21 *
22 * @author Simon Brown
23 */
24 public class RadeoxDecorator extends ContentDecoratorSupport {
25
26 private static final String WIKI_START_TAG = "<wiki>";
27 private static final String WIKI_END_TAG = "</wiki>";
28
29 /**
30 * Decorates the specified blog entry.
31 *
32 * @param context the context in which the decoration is running
33 * @param blogEntry the blog entry to be decorated
34 */
35 public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {
/*
P/P * Method: void decorate(ContentDecoratorContext, BlogEntry)
*
* Preconditions:
* blogEntry != null
* blogEntry.propertyChangeSupport != null
* init'ed(blogEntry.body)
* init'ed(blogEntry.excerpt)
* this.blog != null
* this.blog.properties != null
*
* Postconditions:
* blogEntry.body != null
* init'ed(blogEntry.excerpt)
*/
36 InitialRenderContext initialContext = new BaseInitialRenderContext();
37 initialContext.set(RenderContext.INPUT_LOCALE, getBlog().getLocale());
38 RenderEngine engineWithContext = new RadeoxWikiRenderEngine(initialContext, getBlog());
39
40 blogEntry.setExcerpt(wikify(blogEntry.getExcerpt(), engineWithContext, initialContext));
41 blogEntry.setBody(wikify(blogEntry.getBody(), engineWithContext, initialContext));
42 }
43
44 /**
45 * Decorates the specified static page.
46 *
47 * @param context the context in which the decoration is running
48 * @param staticPage the static page to be decorated
49 */
50 public void decorate(ContentDecoratorContext context, StaticPage staticPage) {
/*
P/P * Method: void decorate(ContentDecoratorContext, StaticPage)
*
* Preconditions:
* init'ed(staticPage.body)
* staticPage != null
* staticPage.propertyChangeSupport != null
* this.blog != null
* this.blog.properties != null
*
* Postconditions:
* staticPage.body != null
*/
51 InitialRenderContext initialContext = new BaseInitialRenderContext();
52 initialContext.set(RenderContext.INPUT_LOCALE, getBlog().getLocale());
53 RenderEngine engineWithContext = new RadeoxWikiRenderEngine(initialContext, getBlog());
54
55 staticPage.setBody(wikify(staticPage.getBody(), engineWithContext, initialContext));
56 }
57
58 private String wikify(String content, RenderEngine renderEngine, InitialRenderContext renderContext) {
59 // is there work to do?
/*
P/P * Method: String wikify(String, RenderEngine, InitialRenderContext)
*
* Preconditions:
* (soft) renderEngine != null
*
* Presumptions:
* java.lang.String:length(...)@79 - java.lang.String:length(...)@79 in -232+1..231
* java.util.regex.Pattern:compile(...)@68 != null
* java.util.regex.Pattern:matcher(...)@70 != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* content: Addr_Set{null}, Inverse{null}
* java.lang.String:length(...)@60: {1..232-1}, {0}
* java.util.regex.Matcher:find(...)@73: {1}, {0}
*/
60 if (content == null || content.length() == 0) {
61 return "";
62 }
63
64 // this pattern says "take the shortest match you can find where there are
65 // one or more characters between wiki tags"
66 // - the match is case insensitive and DOTALL means that newlines are
67 // - considered as a character match
68 Pattern p = Pattern.compile(WIKI_START_TAG + ".+?" + WIKI_END_TAG,
69 Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
70 Matcher m = p.matcher(content);
71
72 // while there are blocks to be escaped
73 while (m.find()) {
74 int start = m.start();
75 int end = m.end();
76
77 // grab the text, strip off the escape tags and transform it
78 String textToWikify = content.substring(start, end);
79 textToWikify = textToWikify.substring(WIKI_START_TAG.length(), textToWikify.length() - WIKI_END_TAG.length());
80 textToWikify = renderEngine.render(textToWikify, renderContext);
81
82 // now add it back into the original text
83 content = content.substring(0, start) + textToWikify + content.substring(end, content.length());
84 m = p.matcher(content);
85 }
86
87 return content;
88 }
89
90 }
91
92 class RadeoxWikiRenderEngine extends BaseRenderEngine implements WikiRenderEngine {
93
94 private Blog blog;
95
96 public RadeoxWikiRenderEngine(InitialRenderContext context, Blog blog) {
/*
P/P * Method: void net.sourceforge.pebble.decorator.RadeoxWikiRenderEngine(InitialRenderContext, Blog)
*
* Preconditions:
* context != null
*
* Postconditions:
* this.blog == blog
* init'ed(this.blog)
*/
97 super(context);
98 context.setRenderEngine(this);
99 this.blog = blog;
100 }
101
/*
P/P * Method: bool exists(String)
*
* Preconditions:
* this.blog != null
* this.blog.staticPageIndex != null
* this.blog.staticPageIndex.index != null
*
* Postconditions:
* init'ed(return_value)
*/
102 public boolean exists(String name) {
103 return blog.getStaticPageIndex().contains(name);
104 }
105
/*
P/P * Method: bool showCreate()
*
* Postconditions:
* return_value == 1
*/
106 public boolean showCreate() {
107 return true;
108 }
109
/*
P/P * Method: void appendLink(StringBuffer, String, String)
*
* Preconditions:
* buffer != null
* this.blog != null
* (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
* (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
* (soft) init'ed(this.blog.id)
*/
110 public void appendLink(StringBuffer buffer, String name, String view) {
111 appendLink(buffer, name, view, null);
112 }
113
/*
P/P * Method: void appendLink(StringBuffer, String, String, String)
*
* Preconditions:
* buffer != null
* this.blog != null
* (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
* (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
* (soft) init'ed(this.blog.id)
*
* Test Vectors:
* anchor: Addr_Set{null}, Inverse{null}
* java.lang.String:length(...)@118: {0}, {1..232-1}
*/
114 public void appendLink(StringBuffer buffer, String name, String view, String anchor) {
115 buffer.append("<a href=\"");
116 StringBuffer url = new StringBuffer();
117 url.append(blog.getUrl()).append("pages/").append(name).append(".html");
118 if (anchor != null && anchor.trim().length() > 0) {
119 url.append("#");
120 url.append(anchor);
121 }
122 buffer.append(UrlRewriter.doRewrite(url.toString()));
123 buffer.append("\">");
124 buffer.append(view);
125 buffer.append("</a>");
126 }
127
/*
P/P * Method: void appendCreateLink(StringBuffer, String, String)
*
* Preconditions:
* buffer != null
*/
128 public void appendCreateLink(StringBuffer buffer, String name, String view) {
129 buffer.append("<a href=\"addStaticPage.secureaction?name=");
130 buffer.append(name);
131 buffer.append("\">");
132 buffer.append(view);
133 buffer.append("</a><sup>?</sup>");
134 }
135 }
SofCheck Inspector Build Version : 2.22510
| radeoxdecorator.java |
2010-Jun-25 19:40:32 |
| radeoxdecorator.class |
2010-Jul-19 20:23:40 |
| radeoxwikirenderengine.class |
2010-Jul-19 20:23:40 |