File Source: UtilitiesModel.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.ui.rendering.model;
20
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.Map;
24 import java.util.TimeZone;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import org.apache.commons.lang.StringEscapeUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.roller.weblogger.WebloggerException;
32 import org.apache.roller.weblogger.pojos.WeblogPermission;
33 import org.apache.roller.weblogger.pojos.wrapper.WeblogWrapper;
34 import org.apache.roller.weblogger.ui.core.RollerSession;
35 import org.apache.roller.weblogger.ui.rendering.util.WeblogRequest;
36 import org.apache.roller.util.DateUtil;
37 import org.apache.roller.util.RegexUtil;
38 import org.apache.roller.weblogger.pojos.Weblog;
39 import org.apache.roller.weblogger.ui.rendering.util.ParsedRequest;
40 import org.apache.roller.weblogger.util.URLUtilities;
41 import org.apache.roller.weblogger.util.Utilities;
42
43 /**
44 * Model which provides access to a set of general utilities.
45 */
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.model.UtilitiesModel()
*
* Postconditions:
* this.parsedRequest == null
* this.weblog == null
*/
46 public class UtilitiesModel implements Model {
47
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.model.UtilitiesModel__static_init
*
* Postconditions:
* init'ed(BR_TAG_PATTERN)
* init'ed(CLOSING_A_TAG_PATTERN)
* init'ed(CLOSING_BLOCKQUOTE_TAG_PATTERN)
* init'ed(CLOSING_B_TAG_PATTERN)
* init'ed(CLOSING_I_TAG_PATTERN)
* init'ed(CLOSING_LI_TAG_PATTERN)
* init'ed(CLOSING_OL_TAG_PATTERN)
* init'ed(CLOSING_PRE_TAG_PATTERN)
* init'ed(CLOSING_P_TAG_PATTERN)
* init'ed(CLOSING_UL_TAG_PATTERN)
* ...
*/
48 private static Log log = LogFactory.getLog(UtilitiesModel.class);
49
50 private static Pattern mLinkPattern =
51 Pattern.compile("<a href=.*?>", Pattern.CASE_INSENSITIVE);
52 private static final Pattern OPENING_B_TAG_PATTERN =
53 Pattern.compile("<b>", Pattern.CASE_INSENSITIVE);
54 private static final Pattern CLOSING_B_TAG_PATTERN =
55 Pattern.compile("</b>", Pattern.CASE_INSENSITIVE);
56 private static final Pattern OPENING_I_TAG_PATTERN =
57 Pattern.compile("<i>", Pattern.CASE_INSENSITIVE);
58 private static final Pattern CLOSING_I_TAG_PATTERN =
59 Pattern.compile("</i>", Pattern.CASE_INSENSITIVE);
60 private static final Pattern OPENING_BLOCKQUOTE_TAG_PATTERN =
61 Pattern.compile("<blockquote>", Pattern.CASE_INSENSITIVE);
62 private static final Pattern CLOSING_BLOCKQUOTE_TAG_PATTERN =
63 Pattern.compile("</blockquote>", Pattern.CASE_INSENSITIVE);
64 private static final Pattern BR_TAG_PATTERN =
65 Pattern.compile("<br */*>", Pattern.CASE_INSENSITIVE);
66 private static final Pattern OPENING_P_TAG_PATTERN =
67 Pattern.compile("<p>", Pattern.CASE_INSENSITIVE);
68 private static final Pattern CLOSING_P_TAG_PATTERN =
69 Pattern.compile("</p>", Pattern.CASE_INSENSITIVE);
70 private static final Pattern OPENING_PRE_TAG_PATTERN =
71 Pattern.compile("<pre>", Pattern.CASE_INSENSITIVE);
72 private static final Pattern CLOSING_PRE_TAG_PATTERN =
73 Pattern.compile("</pre>", Pattern.CASE_INSENSITIVE);
74 private static final Pattern OPENING_UL_TAG_PATTERN =
75 Pattern.compile("<ul>", Pattern.CASE_INSENSITIVE);
76 private static final Pattern CLOSING_UL_TAG_PATTERN =
77 Pattern.compile("</ul>", Pattern.CASE_INSENSITIVE);
78 private static final Pattern OPENING_OL_TAG_PATTERN =
79 Pattern.compile("<ol>", Pattern.CASE_INSENSITIVE);
80 private static final Pattern CLOSING_OL_TAG_PATTERN =
81 Pattern.compile("</ol>", Pattern.CASE_INSENSITIVE);
82 private static final Pattern OPENING_LI_TAG_PATTERN =
83 Pattern.compile("<li>", Pattern.CASE_INSENSITIVE);
84 private static final Pattern CLOSING_LI_TAG_PATTERN =
85 Pattern.compile("</li>", Pattern.CASE_INSENSITIVE);
86 private static final Pattern CLOSING_A_TAG_PATTERN =
87 Pattern.compile("</a>", Pattern.CASE_INSENSITIVE);
88 private static final Pattern OPENING_A_TAG_PATTERN =
89 Pattern.compile("<a href=.*?>", Pattern.CASE_INSENSITIVE);
90 private static final Pattern QUOTE_PATTERN =
91 Pattern.compile(""", Pattern.CASE_INSENSITIVE);
92
93 private ParsedRequest parsedRequest = null;
94 private Weblog weblog = null;
95
96
97 /** Template context name to be used for model */
98 public String getModelName() {
/*
P/P * Method: String getModelName()
*
* Postconditions:
* return_value == &"utils"
*/
99 return "utils";
100 }
101
102
103 /** Init page model based on request */
104 public void init(Map initData) throws WebloggerException {
105
106 // we expect the init data to contain a parsedRequest object
/*
P/P * Method: void init(Map)
*
* Preconditions:
* initData != null
*
* Presumptions:
* java.util.Map:get(...)@107 != null
*
* Postconditions:
* (soft) this.parsedRequest != null
* possibly_updated(this.weblog)
*
* Test Vectors:
* org.apache.roller.weblogger.ui.rendering.util.WeblogRequest:instanceof(...)@113: {0}, {1}
*/
107 parsedRequest = (ParsedRequest) initData.get("parsedRequest");
108 if(parsedRequest == null) {
109 throw new WebloggerException("expected parsedRequest from init data");
110 }
111
112 // extract weblog object if possible
113 if(parsedRequest instanceof WeblogRequest) {
114 WeblogRequest weblogRequest = (WeblogRequest) parsedRequest;
115 weblog = weblogRequest.getWeblog();
116 }
117 }
118
119
120 //---------------------------------------------------- Authentication utils
121
122 public boolean isUserAuthorizedToAuthor(WeblogWrapper weblog) {
123 try {
/*
P/P * Method: bool isUserAuthorizedToAuthor(WeblogWrapper)
*
* Preconditions:
* (soft) log != null
* (soft) this.parsedRequest != null
* (soft) weblog != null
* (soft) weblog.pojo != null
*
* Presumptions:
* init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR)
*
* Postconditions:
* init'ed(return_value)
*/
124 if (parsedRequest.getAuthenticUser() != null) {
125 return weblog.getPojo().hasUserPermissions(
126 parsedRequest.getUser(), WeblogPermission.AUTHOR);
127 }
128 } catch (Exception e) {
129 log.warn("ERROR: checking user authorization", e);
130 }
131 return false;
132 }
133
134 public boolean isUserAuthorizedToAdmin(WeblogWrapper weblog) {
135 try {
/*
P/P * Method: bool isUserAuthorizedToAdmin(WeblogWrapper)
*
* Preconditions:
* (soft) log != null
* (soft) this.parsedRequest != null
* (soft) weblog != null
* (soft) weblog.pojo != null
*
* Presumptions:
* init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN)
*
* Postconditions:
* init'ed(return_value)
*/
136 if (parsedRequest.getAuthenticUser() != null) {
137 return weblog.getPojo().hasUserPermissions(
138 parsedRequest.getUser(), WeblogPermission.ADMIN);
139 }
140 } catch (Exception e) {
141 log.warn("ERROR: checking user authorization", e);
142 }
143 return false;
144 }
145
146 public boolean isUserAuthenticated() {
/*
P/P * Method: bool isUserAuthenticated()
*
* Preconditions:
* this.parsedRequest != null
*
* Postconditions:
* init'ed(return_value)
*/
147 return (parsedRequest.getAuthenticUser() != null);
148 }
149
150 //-------------------------------------------------------------- Date utils
151 /**
152 * Return date for current time.
153 */
154 public static Date getNow() {
/*
P/P * Method: Date getNow()
*
* Postconditions:
* return_value == &new Date(getNow#1)
* new Date(getNow#1) num objects == 1
*/
155 return new Date();
156 }
157
158 /**
159 * Format date using SimpleDateFormat format string.
160 */
161 public String formatDate(Date d, String fmt) {
/*
P/P * Method: String formatDate(Date, String)
*
* Preconditions:
* this.weblog != null
*
* Postconditions:
* init'ed(return_value)
*/
162 return formatDate(d, fmt, weblog.getTimeZoneInstance());
163 }
164
165 /**
166 * Format date using SimpleDateFormat format string.
167 */
168 public String formatDate(Date d, String fmt, TimeZone tzOverride) {
169
/*
P/P * Method: String formatDate(Date, String, TimeZone)
*
* Preconditions:
* (soft) this.weblog != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* d: Addr_Set{null}, Inverse{null}
* fmt: Inverse{null}, Addr_Set{null}
* tzOverride: Addr_Set{null}, Inverse{null}
*/
170 if(d == null || fmt == null)
171 return fmt;
172
173 SimpleDateFormat format = new SimpleDateFormat(fmt, weblog.getLocaleInstance());
174 if(tzOverride != null) {
175 format.setTimeZone(tzOverride);
176 }
177
178 return format.format(d);
179 }
180
181 /**
182 * Format date in ISO-8601 format.
183 */
184 public String formatIso8601Date(Date d) {
/*
P/P * Method: String formatIso8601Date(Date)
*
* Postconditions:
* init'ed(return_value)
*/
185 return DateUtil.formatIso8601(d);
186 }
187
188 /**
189 * Format date in ISO-8601 format.
190 */
191 public String formatIso8601Day(Date d) {
/*
P/P * Method: String formatIso8601Day(Date)
*
* Postconditions:
* init'ed(return_value)
*/
192 return DateUtil.formatIso8601Day(d);
193 }
194
195 /**
196 * Return a date in RFC-822 format.
197 */
198 public String formatRfc822Date(Date date) {
/*
P/P * Method: String formatRfc822Date(Date)
*
* Postconditions:
* init'ed(return_value)
*/
199 return DateUtil.formatRfc822(date);
200 }
201
202 /**
203 * Return a date in 8 character format YYYYMMDD.
204 */
205 public String format8charsDate(Date date) {
/*
P/P * Method: String format8charsDate(Date)
*
* Postconditions:
* init'ed(return_value)
*/
206 return DateUtil.format8chars(date);
207 }
208
209
210 //------------------------------------------------------------ String utils
211
212 public boolean isEmpty(String str) {
/*
P/P * Method: bool isEmpty(String)
*
* Postconditions:
* init'ed(return_value)
*/
213 return StringUtils.isEmpty(str);
214 }
215
216 public boolean isNotEmpty(String str) {
/*
P/P * Method: bool isNotEmpty(String)
*
* Postconditions:
* init'ed(return_value)
*/
217 return StringUtils.isNotEmpty(str);
218 }
219
220 public String[] split(String str1, String str2) {
/*
P/P * Method: String[] split(String, String)
*
* Postconditions:
* init'ed(return_value)
*/
221 return StringUtils.split(str1, str2);
222 }
223
224 public boolean equals(String str1, String str2) {
/*
P/P * Method: bool equals(String, String)
*
* Postconditions:
* init'ed(return_value)
*/
225 return StringUtils.equals(str1, str2);
226 }
227
228 public boolean isAlphanumeric(String str) {
/*
P/P * Method: bool isAlphanumeric(String)
*
* Postconditions:
* init'ed(return_value)
*/
229 return StringUtils.isAlphanumeric(str);
230 }
231
232 public String[] stripAll(String[] strs) {
/*
P/P * Method: String[] stripAll(String[])
*
* Postconditions:
* init'ed(return_value)
*/
233 return StringUtils.stripAll(strs);
234 }
235
236 public String left(String str, int length) {
/*
P/P * Method: String left(String, int)
*
* Postconditions:
* init'ed(return_value)
*/
237 return StringUtils.left(str, length);
238 }
239
240 public String escapeHTML(String str) {
/*
P/P * Method: String escapeHTML(String)
*
* Postconditions:
* init'ed(return_value)
*/
241 return StringEscapeUtils.escapeHtml(str);
242 }
243
244 public String unescapeHTML(String str) {
/*
P/P * Method: String unescapeHTML(String)
*
* Postconditions:
* init'ed(return_value)
*/
245 return StringEscapeUtils.unescapeHtml(str);
246 }
247
248 public String escapeXML(String str) {
/*
P/P * Method: String escapeXML(String)
*
* Postconditions:
* init'ed(return_value)
*/
249 return StringEscapeUtils.escapeXml(str);
250 }
251
252 public String unescapeXML(String str) {
/*
P/P * Method: String unescapeXML(String)
*
* Postconditions:
* init'ed(return_value)
*/
253 return StringEscapeUtils.unescapeXml(str);
254 }
255
256 public String escapeJavaScript(String str) {
/*
P/P * Method: String escapeJavaScript(String)
*
* Postconditions:
* init'ed(return_value)
*/
257 return StringEscapeUtils.escapeJavaScript(str);
258 }
259
260 public String unescapeJavaScript(String str) {
/*
P/P * Method: String unescapeJavaScript(String)
*
* Postconditions:
* init'ed(return_value)
*/
261 return StringEscapeUtils.unescapeJavaScript(str);
262 }
263
264 public String replace(String src, String target, String rWith) {
/*
P/P * Method: String replace(String, String, String)
*
* Postconditions:
* init'ed(return_value)
*/
265 return StringUtils.replace(src, target, rWith);
266 }
267
268 public String replace(String src, String target, String rWith, int maxCount) {
/*
P/P * Method: String replace(String, String, String, int)
*
* Postconditions:
* init'ed(return_value)
*/
269 return StringUtils.replace(src, target, rWith, maxCount);
270 }
271
272 private String replace(String string, Pattern pattern, String replacement) {
/*
P/P * Method: String replace(String, Pattern, String)
*
* Preconditions:
* pattern != null
*
* Presumptions:
* java.util.regex.Pattern:matcher(...)@273 != null
*
* Postconditions:
* init'ed(return_value)
*/
273 Matcher m = pattern.matcher(string);
274 return m.replaceAll(replacement);
275 }
276
277 /**
278 * Remove occurences of html, defined as any text
279 * between the characters "<" and ">". Replace
280 * any HTML tags with a space.
281 */
282 public String removeHTML(String str) {
/*
P/P * Method: String removeHTML(String)
*
* Postconditions:
* return_value != null
*/
283 return removeHTML(str, true);
284 }
285
286 /**
287 * Remove occurences of html, defined as any text
288 * between the characters "<" and ">".
289 * Optionally replace HTML tags with a space.
290 */
291 public String removeHTML(String str, boolean addSpace) {
/*
P/P * Method: String removeHTML(String, bool)
*
* Postconditions:
* return_value != null
*/
292 return Utilities.removeHTML(str, addSpace);
293 }
294
295 /**
296 * Autoformat.
297 */
298 public String autoformat(String s) {
/*
P/P * Method: String autoformat(String)
*
* Postconditions:
* init'ed(return_value)
*/
299 return Utilities.autoformat(s);
300 }
301
302 /**
303 * Strips HTML and truncates.
304 */
305 public String truncate(String str, int lower, int upper, String appendToEnd) {
306 // this method is a dupe of truncateText() method
/*
P/P * Method: String truncate(String, int, int, String)
*
* Preconditions:
* str != null
*
* Postconditions:
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* return_value == One-of{str, &java.lang.StringBuilder:toString(...)}
* return_value != null
*/
307 return truncateText(str, lower, upper, appendToEnd);
308 }
309
310 public String truncateNicely(String str, int lower, int upper, String appendToEnd) {
/*
P/P * Method: String truncateNicely(String, int, int, String)
*
* Preconditions:
* str != null
*
* Postconditions:
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* return_value == One-of{str, &java.lang.StringBuilder:toString(...)}
* return_value != null
*/
311 return Utilities.truncateNicely(str, lower, upper, appendToEnd);
312 }
313
314 public String truncateText(String str, int lower, int upper, String appendToEnd) {
/*
P/P * Method: String truncateText(String, int, int, String)
*
* Preconditions:
* str != null
*
* Postconditions:
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* return_value == One-of{str, &java.lang.StringBuilder:toString(...)}
* return_value != null
*/
315 return Utilities.truncateText(str, lower, upper, appendToEnd);
316 }
317
318 public String hexEncode(String str) {
/*
P/P * Method: String hexEncode(String)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* org.apache.commons.lang.StringUtils:isEmpty(...)@319: {0}, {1}
*/
319 if (StringUtils.isEmpty(str)) return str;
320
321 return RegexUtil.encode(str);
322 }
323
324 public String encodeEmail(String str) {
/*
P/P * Method: String encodeEmail(String)
*
* Postconditions:
* init'ed(return_value)
*/
325 return str!=null ? RegexUtil.encodeEmail(str) : null;
326 }
327
328 /**
329 * URL encoding.
330 * @param s a string to be URL-encoded
331 * @return URL encoding of s using character encoding UTF-8; null if s is null.
332 */
333 public final String encode(String s) {
/*
P/P * Method: String encode(String)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* s: Addr_Set{null}, Inverse{null}
*/
334 if(s != null) {
335 return URLUtilities.encode(s);
336 } else {
337 return s;
338 }
339 }
340
341 /**
342 * URL decoding.
343 * @param s a URL-encoded string to be URL-decoded
344 * @return URL decoded value of s using character encoding UTF-8; null if s is null.
345 */
346 public final String decode(String s) {
/*
P/P * Method: String decode(String)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* s: Addr_Set{null}, Inverse{null}
*/
347 if(s != null) {
348 return URLUtilities.decode(s);
349 } else {
350 return s;
351 }
352 }
353
354 /**
355 * Code (stolen from Pebble) to add rel="nofollow" string to all links in HTML.
356 */
357 public String addNofollow(String html) {
/*
P/P * Method: String addNofollow(String)
*
* Preconditions:
* (soft) org/apache/roller/weblogger/util/Utilities.mLinkPattern != null
*
* Postconditions:
* init'ed(java.lang.StringBuffer:toString(...)._tainted)
* return_value == One-of{html, &java.lang.StringBuffer:toString(...)}
* init'ed(return_value)
*/
358 return Utilities.addNofollow(html);
359 }
360
361 /**
362 * Transforms the given String into a subset of HTML displayable on a web
363 * page. The subset includes <b>, <i>, <p>, <br>,
364 * <pre> and <a href> (and their corresponding end tags).
365 *
366 * @param s the String to transform
367 * @return the transformed String
368 */
369 public String transformToHTMLSubset(String s) {
/*
P/P * Method: String transformToHTMLSubset(String)
*
* Postconditions:
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* init'ed(return_value)
*/
370 return Utilities.transformToHTMLSubset(s);
371 }
372
373 /**
374 * Convert a byte array into a Base64 string (as used in mime formats)
375 */
376 public String toBase64(byte[] aValue) {
/*
P/P * Method: String toBase64(byte[])
*
* Preconditions:
* aValue != null
* (soft) aValue.length in {0, 3..232-1}
* (soft) init'ed(aValue[...])
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* return_value == &java.lang.StringBuffer:toString(...)
*/
377 return Utilities.toBase64(aValue);
378 }
379
380 }
SofCheck Inspector Build Version : 2.18479
| UtilitiesModel.java |
2009-Jan-02 14:25:38 |
| UtilitiesModel.class |
2009-Sep-04 03:12:44 |