File Source: AcronymsPlugin.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.business.plugins.entry;
20
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.Properties;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import org.apache.commons.lang.StringEscapeUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.roller.weblogger.WebloggerException;
30 import org.apache.roller.weblogger.business.WebloggerFactory;
31 import org.apache.roller.weblogger.business.UserManager;
32 import org.apache.roller.weblogger.business.plugins.entry.WeblogEntryPlugin;
33 import org.apache.roller.weblogger.pojos.WeblogEntry;
34 import org.apache.roller.weblogger.pojos.WeblogTemplate;
35 import org.apache.roller.weblogger.pojos.Weblog;
36
37
38 /**
39 * Adds full text to pre-defined acronyms.
40 *
41 * Example: HTML would become <acronym title="Hyper Text Markup Language">HTML</acronym>
42 *
43 * @author <a href="mailto:molen@mail.com">Jaap van der Molen</a>
44 * @version $Revision: 1.3 $
45 */
46 public class AcronymsPlugin implements WeblogEntryPlugin {
47
/*
P/P * Method: org.apache.roller.weblogger.business.plugins.entry.AcronymsPlugin__static_init
*
* Postconditions:
* init'ed(mLogger)
*/
48 private static final Log mLogger = LogFactory.getLog(AcronymsPlugin.class);
49
50 protected String name = "Acronyms";
51 protected String description = "Expands acronyms defined in _acronym page. " +
52 "Example: definition 'HTML=Hyper Text Markup Language' " +
53 "becomes <acronym title='Hyper Text Markup Language'>HTML</acronym>. " +
54 "You must create an " +
55 "<a href='page.do?method=editPages&rmik=tabbedmenu.website.pages'>" +
56 "_acronym page</a> to use Acronyms.";
57
58
59 public AcronymsPlugin() {
/*
P/P * Method: void org.apache.roller.weblogger.business.plugins.entry.AcronymsPlugin()
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@48 != null
*
* Postconditions:
* this.description == &"Expands acronyms defined in _acronym page. Example: definition 'HTML=H ... ages&rmik=tabbedmenu.website.pages'>_acronym page<.a> to use Acronyms."
* this.name == &"Acronyms"
*/
60 super();
61 mLogger.debug("AcronymsPlugin instantiated.");
62 }
63
64
65 public String getName() {
/*
P/P * Method: String getName()
*
* Preconditions:
* init'ed(this.name)
*
* Postconditions:
* return_value == this.name
* init'ed(return_value)
*/
66 return name;
67 }
68
69
70 public String getDescription() {
/*
P/P * Method: String getDescription()
*
* Preconditions:
* init'ed(this.description)
*
* Postconditions:
* init'ed(return_value)
*/
71 return StringEscapeUtils.escapeJavaScript(description);
72 }
73
74
/*
P/P * Method: void init(Weblog)
*/
75 public void init(Weblog website) throws WebloggerException {}
76
77
78 public String render(WeblogEntry entry, String str) {
/*
P/P * Method: String render(WeblogEntry, String)
*
* Preconditions:
* entry != null
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
*
* Presumptions:
* java.util.Properties:keySet(...)@100 != null
* java.util.Properties:size(...)@97 >= 0
* java.util.Properties:size(...)@98 >= 1
* java.util.Properties:size(...)@98 - java.util.Properties:size(...)@97 in 0..232-1
* java.util.regex.Pattern:compile(...)@102 init'ed
* ...
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@100: {0}, {1}
* java.util.Properties:size(...)@90: {-231..-1, 1..232-1}, {0}
* java.util.Properties:size(...)@97: {1..232-1}, {0}
* org.apache.commons.logging.Log:isDebugEnabled(...)@81: {0}, {1}
*/
79 String text = str;
80
81 if (mLogger.isDebugEnabled()) {
82 mLogger.debug("render(entry = "+entry.getId()+")");
83 }
84
85 /*
86 * Get acronyms Properties.
87 */
88 Properties acronyms = loadAcronyms(entry.getWebsite());
89 mLogger.debug("acronyms.size()=" + acronyms.size());
90 if (acronyms.size() == 0) {
91 return text;
92 }
93
94 /*
95 * Compile the user's acronyms into RegEx patterns.
96 */
97 Pattern[] acronymPatterns = new Pattern[acronyms.size()];
98 String[] acronymTags = new String[acronyms.size()];
99 int count = 0;
100 for (Iterator iter = acronyms.keySet().iterator(); iter.hasNext();) {
101 String acronym = (String) iter.next();
+ 102 acronymPatterns[count] = Pattern.compile("\\b" + acronym + "\\b");
103 mLogger.debug("match '" + acronym + "'");
104 acronymTags[count] =
105 "<acronym title=\""
106 + acronyms.getProperty(acronym)
107 + "\">"
108 + acronym
109 + "</acronym>";
110 count++;
111 }
112
113 // if there are none, no work to do
+ 114 if (acronymPatterns == null || acronymPatterns.length == 0) {
115 return text;
116 }
117
118 return matchAcronyms(text, acronymPatterns, acronymTags);
119 }
120
121
122 /**
123 * Look for any _acronyms Page and parse it into Properties.
124 * @param website
125 * @return
126 * @throws WebloggerException
127 */
128 private Properties loadAcronyms(Weblog website) {
/*
P/P * Method: Properties loadAcronyms(Weblog)
*
* Preconditions:
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
* (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
* (soft) website != null
*
* Presumptions:
* getWeblogger(...).userManager != null
* org.apache.commons.logging.LogFactory:getLog(...)@48 != null
* userMgr.strategy != null
* userMgr.strategy.emf != null
* userMgr.strategy.threadLocalEntityManager != null
*
* Postconditions:
* return_value == &new Properties(loadAcronyms#1)
* new Properties(loadAcronyms#1) num objects == 1
*/
129 Properties acronyms = new Properties();
130 try {
131 UserManager userMgr = WebloggerFactory.getWeblogger().getUserManager();
132 WeblogTemplate acronymsPage = userMgr.getPageByName(
133 website, "_acronyms");
134 if (acronymsPage != null) {
135 acronyms = parseAcronymPage(acronymsPage, acronyms);
136 }
137 } catch (WebloggerException e) {
138 // not much we can do about it
139 mLogger.warn(e);
140 }
141 return acronyms;
142 }
143
144
145 /**
146 * Iterates through the acronym properties and replaces matching
147 * acronyms in the entry text with acronym html-tags.
148 *
149 * @param text entry text
150 * @param acronyms user provided set of acronyms
151 * @return entry text with acronym explanations
152 */
153 private String matchAcronyms(String text, Pattern[] acronymPatterns, String[] acronymTags) {
/*
P/P * Method: String matchAcronyms(String, Pattern[], String[])
*
* Preconditions:
* acronymPatterns != null
* acronymPatterns.length <= 232-1
* (soft) acronymPatterns[...] != null
* (soft) acronymTags != null
* (soft) acronymPatterns.length <= acronymTags.length
* (soft) init'ed(acronymTags[...])
*
* Presumptions:
* java.util.regex.Pattern:matcher(...)@160 != null
* org.apache.commons.logging.LogFactory:getLog(...)@48 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* org.apache.commons.logging.Log:isDebugEnabled(...)@154: {0}, {1}
*/
154 if (mLogger.isDebugEnabled()) {
155 mLogger.debug("matchAcronyms("+text+")");
156 }
157
158 Matcher matcher = null;
159 for (int i=0; i<acronymPatterns.length; i++) {
160 matcher = acronymPatterns[i].matcher(text);
161 text = matcher.replaceAll(acronymTags[i]);
162 }
163 return text;
164 }
165
166 /**
167 * Parse the Template of the provided WeblogTemplate and turns it
168 * into a <code>Properties</code> collection.
169 *
170 * @param acronymPage
171 * @return acronym properties (key = acronym, value= full text), empty if Template is empty
172 */
173 private Properties parseAcronymPage(WeblogTemplate acronymPage, Properties acronyms) {
/*
P/P * Method: Properties parseAcronymPage(WeblogTemplate, Properties)
*
* Preconditions:
* acronymPage != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@48 != null
* org.apache.roller.weblogger.pojos.WeblogTemplate:getContents(...)@174 != null
*
* Postconditions:
* return_value == acronyms
* init'ed(return_value)
*
* Test Vectors:
* org.apache.commons.logging.Log:isDebugEnabled(...)@176: {0}, {1}
*/
174 String rawAcronyms = acronymPage.getContents();
175
176 if (mLogger.isDebugEnabled()) {
177 mLogger.debug("parsing _acronyms template: \n'"+rawAcronyms+"'");
178 }
179
180 String regex = "\n"; // end of line
181 String[] lines = rawAcronyms.split(regex);
182
+ 183 if (lines != null) {
+ 184 for (int i = 0; i < lines.length; i++) {
+ 185 int index = lines[i].indexOf('=');
186 if (index > 0) {
187 String key = lines[i].substring(0, index).trim();
188 String value =
189 lines[i].substring(index + 1, lines[i].length()).trim();
190 acronyms.setProperty(key, value);
191 }
192 }
193 }
194
195 return acronyms;
196 }
197
198 }
SofCheck Inspector Build Version : 2.18479
| AcronymsPlugin.java |
2009-Jan-02 14:25:28 |
| AcronymsPlugin.class |
2009-Sep-04 03:12:31 |