Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart4.png 41% of files have more coverage
43   135   14   4,78
6   85   0,33   4,5
9     1,56  
2    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  RadeoxDecorator       Line # 24 22 0% 6 13 55,2% 0.55172414
  RadeoxWikiRenderEngine       Line # 92 21 0% 8 25 13,8% 0.13793103
 
  (14)
 
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;
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  14 toggle public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {
36  14 InitialRenderContext initialContext = new BaseInitialRenderContext();
37  14 initialContext.set(RenderContext.INPUT_LOCALE, getBlog().getLocale());
38  14 RenderEngine engineWithContext = new RadeoxWikiRenderEngine(initialContext, getBlog());
39   
40  14 blogEntry.setExcerpt(wikify(blogEntry.getExcerpt(), engineWithContext, initialContext));
41  14 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  0 toggle public void decorate(ContentDecoratorContext context, StaticPage staticPage) {
51  0 InitialRenderContext initialContext = new BaseInitialRenderContext();
52  0 initialContext.set(RenderContext.INPUT_LOCALE, getBlog().getLocale());
53  0 RenderEngine engineWithContext = new RadeoxWikiRenderEngine(initialContext, getBlog());
54   
55  0 staticPage.setBody(wikify(staticPage.getBody(), engineWithContext, initialContext));
56    }
57   
 
58  28 toggle private String wikify(String content, RenderEngine renderEngine, InitialRenderContext renderContext) {
59    // is there work to do?
60  28 if (content == null || content.length() == 0) {
61  18 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  10 Pattern p = Pattern.compile(WIKI_START_TAG + ".+?" + WIKI_END_TAG,
69    Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
70  10 Matcher m = p.matcher(content);
71   
72    // while there are blocks to be escaped
73  10 while (m.find()) {
74  0 int start = m.start();
75  0 int end = m.end();
76   
77    // grab the text, strip off the escape tags and transform it
78  0 String textToWikify = content.substring(start, end);
79  0 textToWikify = textToWikify.substring(WIKI_START_TAG.length(), textToWikify.length() - WIKI_END_TAG.length());
80  0 textToWikify = renderEngine.render(textToWikify, renderContext);
81   
82    // now add it back into the original text
83  0 content = content.substring(0, start) + textToWikify + content.substring(end, content.length());
84  0 m = p.matcher(content);
85    }
86   
87  10 return content;
88    }
89   
90    }
91   
 
92    class RadeoxWikiRenderEngine extends BaseRenderEngine implements WikiRenderEngine {
93   
94    private Blog blog;
95   
 
96  14 toggle public RadeoxWikiRenderEngine(InitialRenderContext context, Blog blog) {
97  14 super(context);
98  14 context.setRenderEngine(this);
99  14 this.blog = blog;
100    }
101   
 
102  0 toggle public boolean exists(String name) {
103  0 return blog.getStaticPageIndex().contains(name);
104    }
105   
 
106  0 toggle public boolean showCreate() {
107  0 return true;
108    }
109   
 
110  0 toggle public void appendLink(StringBuffer buffer, String name, String view) {
111  0 appendLink(buffer, name, view, null);
112    }
113   
 
114  0 toggle public void appendLink(StringBuffer buffer, String name, String view, String anchor) {
115  0 buffer.append("<a href=\"");
116  0 StringBuffer url = new StringBuffer();
117  0 url.append(blog.getUrl()).append("pages/").append(name).append(".html");
118  0 if (anchor != null && anchor.trim().length() > 0) {
119  0 url.append("#");
120  0 url.append(anchor);
121    }
122  0 buffer.append(UrlRewriter.doRewrite(url.toString()));
123  0 buffer.append("\">");
124  0 buffer.append(view);
125  0 buffer.append("</a>");
126    }
127   
 
128  0 toggle public void appendCreateLink(StringBuffer buffer, String name, String view) {
129  0 buffer.append("<a href=\"addStaticPage.secureaction?name=");
130  0 buffer.append(name);
131  0 buffer.append("\">");
132  0 buffer.append(view);
133  0 buffer.append("</a><sup>?</sup>");
134    }
135    }