Coverage Report - net.sourceforge.pebble.decorator.RadeoxDecorator
 
Classes in this File Line Coverage Branch Coverage Complexity
RadeoxDecorator
50%
13/26
66%
4/6
1.667
RadeoxWikiRenderEngine
16%
4/25
0%
0/4
1.667
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 
 33  
 package net.sourceforge.pebble.decorator;
 34  
 
 35  
 import net.sourceforge.pebble.domain.BlogEntry;
 36  
 import net.sourceforge.pebble.domain.Blog;
 37  
 import net.sourceforge.pebble.domain.StaticPage;
 38  
 import net.sourceforge.pebble.util.UrlRewriter;
 39  
 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 40  
 import org.radeox.api.engine.RenderEngine;
 41  
 import org.radeox.api.engine.WikiRenderEngine;
 42  
 import org.radeox.api.engine.context.InitialRenderContext;
 43  
 import org.radeox.api.engine.context.RenderContext;
 44  
 import org.radeox.engine.BaseRenderEngine;
 45  
 import org.radeox.engine.context.BaseInitialRenderContext;
 46  
 
 47  
 import java.util.regex.Matcher;
 48  
 import java.util.regex.Pattern;
 49  
 
 50  
 /**
 51  
  * Decorates blog entries and comments by rendering them with Radeox, internal
 52  
  * links pointing to static pages within the blog.
 53  
  *
 54  
  * @author Simon Brown
 55  
  */
 56  2720
 public class RadeoxDecorator extends ContentDecoratorSupport {
 57  
 
 58  
   private static final String WIKI_START_TAG = "<wiki>";
 59  
   private static final String WIKI_END_TAG = "</wiki>";
 60  
 
 61  
   /**
 62  
    * Decorates the specified blog entry.
 63  
    *
 64  
    * @param context   the context in which the decoration is running
 65  
    * @param blogEntry the blog entry to be decorated
 66  
    */
 67  
   public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) {
 68  28
     InitialRenderContext initialContext = new BaseInitialRenderContext();
 69  28
     initialContext.set(RenderContext.INPUT_LOCALE, getBlog().getLocale());
 70  28
     RenderEngine engineWithContext = new RadeoxWikiRenderEngine(initialContext, getBlog());
 71  
 
 72  28
     blogEntry.setExcerpt(wikify(blogEntry.getExcerpt(), engineWithContext, initialContext));
 73  28
     blogEntry.setBody(wikify(blogEntry.getBody(), engineWithContext, initialContext));
 74  28
   }
 75  
 
 76  
   /**
 77  
    * Decorates the specified static page.
 78  
    *
 79  
    * @param context    the context in which the decoration is running
 80  
    * @param staticPage the static page to be decorated
 81  
    */
 82  
   public void decorate(ContentDecoratorContext context, StaticPage staticPage) {
 83  0
     InitialRenderContext initialContext = new BaseInitialRenderContext();
 84  0
     initialContext.set(RenderContext.INPUT_LOCALE, getBlog().getLocale());
 85  0
     RenderEngine engineWithContext = new RadeoxWikiRenderEngine(initialContext, getBlog());
 86  
 
 87  0
     staticPage.setBody(wikify(staticPage.getBody(), engineWithContext, initialContext));
 88  0
   }
 89  
 
 90  
   private String wikify(String content, RenderEngine renderEngine, InitialRenderContext renderContext) {
 91  
     // is there work to do?
 92  56
     if (content == null || content.length() == 0) {
 93  36
       return "";
 94  
     }
 95  
 
 96  
     // this pattern says "take the shortest match you can find where there are
 97  
     // one or more characters between wiki tags"
 98  
     //  - the match is case insensitive and DOTALL means that newlines are
 99  
     //  - considered as a character match
 100  20
     Pattern p = Pattern.compile(WIKI_START_TAG + ".+?" + WIKI_END_TAG,
 101  
         Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
 102  20
     Matcher m = p.matcher(content);
 103  
 
 104  
     // while there are blocks to be escaped
 105  20
     while (m.find()) {
 106  0
       int start = m.start();
 107  0
       int end = m.end();
 108  
 
 109  
       // grab the text, strip off the escape tags and transform it
 110  0
       String textToWikify = content.substring(start, end);
 111  0
       textToWikify = textToWikify.substring(WIKI_START_TAG.length(), textToWikify.length() - WIKI_END_TAG.length());
 112  0
       textToWikify = renderEngine.render(textToWikify, renderContext);
 113  
 
 114  
       // now add it back into the original text
 115  0
       content = content.substring(0, start) + textToWikify + content.substring(end, content.length());
 116  0
       m = p.matcher(content);
 117  0
     }
 118  
 
 119  20
     return content;
 120  
   }
 121  
 
 122  
 }
 123  
 
 124  
 class RadeoxWikiRenderEngine extends BaseRenderEngine implements WikiRenderEngine {
 125  
 
 126  
   private Blog blog;
 127  
 
 128  
   public RadeoxWikiRenderEngine(InitialRenderContext context, Blog blog) {
 129  28
     super(context);
 130  28
     context.setRenderEngine(this);
 131  28
     this.blog = blog;
 132  28
   }
 133  
 
 134  
   public boolean exists(String name) {
 135  0
     return blog.getStaticPageIndex().contains(name);
 136  
   }
 137  
 
 138  
   public boolean showCreate() {
 139  0
     return true;
 140  
   }
 141  
 
 142  
   public void appendLink(StringBuffer buffer, String name, String view) {
 143  0
     appendLink(buffer, name, view, null);
 144  0
   }
 145  
 
 146  
   public void appendLink(StringBuffer buffer, String name, String view, String anchor) {
 147  0
     buffer.append("<a href=\"");
 148  0
     StringBuffer url = new StringBuffer();
 149  0
     url.append(blog.getUrl()).append("pages/").append(name).append(".html");
 150  0
     if (anchor != null && anchor.trim().length() > 0) {
 151  0
       url.append("#");
 152  0
       url.append(anchor);
 153  
     }
 154  0
     buffer.append(UrlRewriter.doRewrite(url.toString()));
 155  0
     buffer.append("\">");
 156  0
     buffer.append(view);
 157  0
     buffer.append("</a>");
 158  0
   }
 159  
 
 160  
   public void appendCreateLink(StringBuffer buffer, String name, String view) {
 161  0
     buffer.append("<a href=\"addStaticPage.secureaction?name=");
 162  0
     buffer.append(name);
 163  0
     buffer.append("\">");
 164  0
     buffer.append(view);
 165  0
     buffer.append("</a><sup>?</sup>");
 166  0
   }
 167  
 }