Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PhotoDecorator |
|
| 4.0;4 |
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 | package net.sourceforge.pebble.decorator; | |
33 | ||
34 | import net.sourceforge.pebble.api.decorator.ContentDecoratorContext; | |
35 | import net.sourceforge.pebble.domain.BlogEntry; | |
36 | import net.sourceforge.pebble.domain.StaticPage; | |
37 | import net.sourceforge.pebble.util.StringUtils; | |
38 | ||
39 | import java.util.regex.Pattern; | |
40 | import java.util.regex.Matcher; | |
41 | import java.io.BufferedReader; | |
42 | import java.io.StringReader; | |
43 | import java.io.IOException; | |
44 | ||
45 | import org.apache.commons.logging.Log; | |
46 | import org.apache.commons.logging.LogFactory; | |
47 | ||
48 | /** | |
49 | * Takes a simple description of photos and generates boilerplate markup. | |
50 | * | |
51 | * @author Simon Brown | |
52 | */ | |
53 | 12 | public class PhotoDecorator extends ContentDecoratorSupport { |
54 | ||
55 | 4 | private static final Log log = LogFactory.getLog(PhotoDecorator.class); |
56 | ||
57 | private static final String PHOTOS_START_TAG = "<photos>"; | |
58 | private static final String PHOTOS_END_TAG = "</photos>"; | |
59 | ||
60 | /** | |
61 | * Decorates the specified blog entry. | |
62 | * | |
63 | * @param context the context in which the decoration is running | |
64 | * @param blogEntry the blog entry to be decorated | |
65 | */ | |
66 | public void decorate(ContentDecoratorContext context, BlogEntry blogEntry) { | |
67 | 40 | blogEntry.setBody(markup(blogEntry.getBody())); |
68 | 40 | blogEntry.setExcerpt(markup(blogEntry.getExcerpt())); |
69 | 40 | } |
70 | ||
71 | /** | |
72 | * Decorates the specified static page. | |
73 | * | |
74 | * @param context the context in which the decoration is running | |
75 | * @param staticPage the static page to be decorated | |
76 | */ | |
77 | public void decorate(ContentDecoratorContext context, StaticPage staticPage) { | |
78 | 20 | staticPage.setBody(markup(staticPage.getBody())); |
79 | 20 | } |
80 | ||
81 | private String markup(String content) { | |
82 | // is there work to do? | |
83 | 100 | if (content == null || content.length() == 0) { |
84 | 64 | return ""; |
85 | } | |
86 | ||
87 | // this pattern says "take the shortest match you can find where there are | |
88 | // one or more characters between escape tags" | |
89 | // - the match is case insensitive and DOTALL means that newlines are | |
90 | // - considered as a character match | |
91 | 36 | Pattern p = Pattern.compile(PHOTOS_START_TAG + ".+?" + PHOTOS_END_TAG, |
92 | Pattern.CASE_INSENSITIVE | Pattern.DOTALL); | |
93 | 36 | Matcher m = p.matcher(content); |
94 | ||
95 | // while there are blocks to be escaped | |
96 | 60 | while (m.find()) { |
97 | 24 | int start = m.start(); |
98 | 24 | int end = m.end(); |
99 | ||
100 | // grab the text, strip off the "photos" tags and transform it | |
101 | 24 | String textToMarkup = content.substring(start, end); |
102 | 24 | textToMarkup = textToMarkup.substring(PHOTOS_START_TAG.length(), textToMarkup.length() - PHOTOS_END_TAG.length()); |
103 | ||
104 | 24 | StringBuffer buf = new StringBuffer(); |
105 | 24 | buf.append("<div class=\"photos\">\n"); |
106 | ||
107 | try { | |
108 | 24 | BufferedReader reader = new BufferedReader(new StringReader(textToMarkup)); |
109 | 24 | String line = reader.readLine(); |
110 | 24 | buf.append("<div>\n"); |
111 | 24 | boolean foundPhotos = false; |
112 | 108 | while (line != null) { |
113 | 84 | if (line.trim().equals("")) { |
114 | 36 | if (foundPhotos) { |
115 | 12 | buf.append("</div>\n"); |
116 | 12 | buf.append("<div>\n"); |
117 | } | |
118 | } else { | |
119 | 48 | String[] tokens = line.split("\\|"); |
120 | 48 | buf.append("<img src=\""); |
121 | 48 | buf.append(tokens[0]); |
122 | 48 | buf.append("\" class=\"photo\" alt=\""); |
123 | 48 | if (tokens.length == 2) { |
124 | 36 | buf.append(tokens[1]); |
125 | } | |
126 | 48 | buf.append("\" />\n"); |
127 | 48 | foundPhotos = true; |
128 | } | |
129 | ||
130 | 84 | line = reader.readLine(); |
131 | } | |
132 | 24 | buf.append("</div>\n"); |
133 | 0 | } catch (IOException ioe) { |
134 | 0 | log.warn(ioe); |
135 | 24 | } |
136 | ||
137 | 24 | buf.append("</div>"); |
138 | ||
139 | // now add it back into the original text | |
140 | 24 | content = content.substring(0, start) + buf.toString() + content.substring(end, content.length()); |
141 | 24 | m = p.matcher(content); |
142 | 24 | } |
143 | ||
144 | 36 | return content; |
145 | } | |
146 | ||
147 | } |