File Source: escapemarkupdecorator.java
/*
P/P * Method: net.sourceforge.pebble.decorator.EscapeMarkupDecorator__static_init
*/
1 package net.sourceforge.pebble.decorator;
2
3 import net.sourceforge.pebble.domain.BlogEntry;
4 import net.sourceforge.pebble.domain.StaticPage;
5 import net.sourceforge.pebble.util.StringUtils;
6 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
7
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10
11 /**
12 * Escapes < and > tags in the excerpt/body of blog entries and
13 * the body of static pages.
14 *
15 * @author Simon Brown
16 */
/*
P/P * Method: void net.sourceforge.pebble.decorator.EscapeMarkupDecorator()
*/
17 public class EscapeMarkupDecorator extends ContentDecoratorSupport {
18
19 private static final String ESCAPE_START_TAG = "<escape>";
20 private static final String ESCAPE_END_TAG = "</escape>";
21
22 /**
23 * Decorates the specified blog entry.
24 *
25 * @param context the context in which the decoration is running
26 * @param blogEntry the blog entry to be decorated
27 */
28 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)
*
* Postconditions:
* blogEntry.body != null
* init'ed(blogEntry.excerpt)
*/
29 String escapedBody = escape(blogEntry.getBody());
30 blogEntry.setBody(escapedBody);
31
32 String escapedExcerpt = escape(blogEntry.getExcerpt());
33 blogEntry.setExcerpt(escapedExcerpt);
34 }
35
36 /**
37 * Decorates the specified static page.
38 *
39 * @param context the context in which the decoration is running
40 * @param staticPage the static page to be decorated
41 */
42 public void decorate(ContentDecoratorContext context, StaticPage staticPage) {
/*
P/P * Method: void decorate(ContentDecoratorContext, StaticPage)
*
* Preconditions:
* staticPage != null
* init'ed(staticPage.body)
* staticPage.propertyChangeSupport != null
*
* Postconditions:
* staticPage.body != null
*/
43 String escapedBody = escape(staticPage.getBody());
44 staticPage.setBody(escapedBody);
45 }
46
47 private String escape(String content) {
48 // is there work to do?
/*
P/P * Method: String escape(String)
*
* Presumptions:
* java.lang.String:length(...)@68 - java.lang.String:length(...)@68 in -232+1..231
* java.util.regex.Pattern:compile(...)@57 != null
* java.util.regex.Pattern:matcher(...)@59 != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* content: Addr_Set{null}, Inverse{null}
* java.lang.String:length(...)@49: {1..232-1}, {0}
* java.util.regex.Matcher:find(...)@62: {1}, {0}
*/
49 if (content == null || content.length() == 0) {
50 return "";
51 }
52
53 // this pattern says "take the shortest match you can find where there are
54 // one or more characters between escape tags"
55 // - the match is case insensitive and DOTALL means that newlines are
56 // - considered as a character match
57 Pattern p = Pattern.compile(ESCAPE_START_TAG + ".+?" + ESCAPE_END_TAG,
58 Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
59 Matcher m = p.matcher(content);
60
61 // while there are blocks to be escaped
62 while (m.find()) {
63 int start = m.start();
64 int end = m.end();
65
66 // grab the text, strip off the escape tags and transform it
67 String textToEscape = content.substring(start, end);
68 textToEscape = textToEscape.substring(ESCAPE_START_TAG.length(), textToEscape.length() - ESCAPE_END_TAG.length());
69 textToEscape = StringUtils.transformHTML(textToEscape);
70
71 // now add it back into the original text
72 content = content.substring(0, start) + textToEscape + content.substring(end, content.length());
73 m = p.matcher(content);
74 }
75
76 return content;
77 }
78
79 }
SofCheck Inspector Build Version : 2.22510
| escapemarkupdecorator.java |
2010-Jun-25 19:40:32 |
| escapemarkupdecorator.class |
2010-Jul-19 20:23:40 |