001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.plugins;
020:
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Properties;
024: import java.util.regex.Matcher;
025: import java.util.regex.Pattern;
026: import org.apache.commons.lang.StringEscapeUtils;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.roller.RollerException;
030: import org.apache.roller.business.RollerFactory;
031: import org.apache.roller.business.UserManager;
032: import org.apache.roller.business.WeblogEntryPlugin;
033: import org.apache.roller.pojos.WeblogEntryData;
034: import org.apache.roller.pojos.WeblogTemplate;
035: import org.apache.roller.pojos.WebsiteData;
036:
037: /**
038: * Adds full text to pre-defined acronyms.
039: *
040: * Example: HTML would become <acronym title="Hyper Text Markup Language">HTML</acronym>
041: *
042: * @author <a href="mailto:molen@mail.com">Jaap van der Molen</a>
043: * @version $Revision: 1.3 $
044: */
045: public class AcronymsPlugin implements WeblogEntryPlugin {
046:
047: private static final Log mLogger = LogFactory
048: .getLog(AcronymsPlugin.class);
049:
050: protected String name = "Acronyms";
051: protected String description = "Expands acronyms defined in _acronym page. "
052: + "Example: definition 'HTML=Hyper Text Markup Language' "
053: + "becomes <acronym title='Hyper Text Markup Language'>HTML</acronym>. "
054: + "You must create an "
055: + "<a href='page.do?method=editPages&rmik=tabbedmenu.website.pages'>"
056: + "_acronym page</a> to use Acronyms.";
057:
058: public AcronymsPlugin() {
059: super ();
060: mLogger.debug("AcronymsPlugin instantiated.");
061: }
062:
063: public String getName() {
064: return name;
065: }
066:
067: public String getDescription() {
068: return StringEscapeUtils.escapeJavaScript(description);
069: }
070:
071: public void init(WebsiteData website) throws RollerException {
072: }
073:
074: public String render(WeblogEntryData entry, String str) {
075: String text = str;
076:
077: if (mLogger.isDebugEnabled()) {
078: mLogger.debug("render(entry = " + entry.getId() + ")");
079: }
080:
081: /*
082: * Get acronyms Properties.
083: */
084: Properties acronyms = loadAcronyms(entry.getWebsite());
085: mLogger.debug("acronyms.size()=" + acronyms.size());
086: if (acronyms.size() == 0) {
087: return text;
088: }
089:
090: /*
091: * Compile the user's acronyms into RegEx patterns.
092: */
093: Pattern[] acronymPatterns = new Pattern[acronyms.size()];
094: String[] acronymTags = new String[acronyms.size()];
095: int count = 0;
096: for (Iterator iter = acronyms.keySet().iterator(); iter
097: .hasNext();) {
098: String acronym = (String) iter.next();
099: acronymPatterns[count] = Pattern.compile("\\b" + acronym
100: + "\\b");
101: mLogger.debug("match '" + acronym + "'");
102: acronymTags[count] = "<acronym title=\""
103: + acronyms.getProperty(acronym) + "\">" + acronym
104: + "</acronym>";
105: count++;
106: }
107:
108: // if there are none, no work to do
109: if (acronymPatterns == null || acronymPatterns.length == 0) {
110: return text;
111: }
112:
113: return matchAcronyms(text, acronymPatterns, acronymTags);
114: }
115:
116: /**
117: * Look for any _acronyms Page and parse it into Properties.
118: * @param website
119: * @return
120: * @throws RollerException
121: */
122: private Properties loadAcronyms(WebsiteData website) {
123: Properties acronyms = new Properties();
124: try {
125: UserManager userMgr = RollerFactory.getRoller()
126: .getUserManager();
127: WeblogTemplate acronymsPage = userMgr.getPageByName(
128: website, "_acronyms");
129: if (acronymsPage != null) {
130: acronyms = parseAcronymPage(acronymsPage, acronyms);
131: }
132: } catch (RollerException e) {
133: // not much we can do about it
134: mLogger.warn(e);
135: }
136: return acronyms;
137: }
138:
139: /**
140: * Iterates through the acronym properties and replaces matching
141: * acronyms in the entry text with acronym html-tags.
142: *
143: * @param text entry text
144: * @param acronyms user provided set of acronyms
145: * @return entry text with acronym explanations
146: */
147: private String matchAcronyms(String text,
148: Pattern[] acronymPatterns, String[] acronymTags) {
149: if (mLogger.isDebugEnabled()) {
150: mLogger.debug("matchAcronyms(" + text + ")");
151: }
152:
153: Matcher matcher = null;
154: for (int i = 0; i < acronymPatterns.length; i++) {
155: matcher = acronymPatterns[i].matcher(text);
156: text = matcher.replaceAll(acronymTags[i]);
157: }
158: return text;
159: }
160:
161: /**
162: * Parse the Template of the provided WeblogTemplate and turns it
163: * into a <code>Properties</code> collection.
164: *
165: * @param acronymPage
166: * @return acronym properties (key = acronym, value= full text), empty if Template is empty
167: */
168: private Properties parseAcronymPage(WeblogTemplate acronymPage,
169: Properties acronyms) {
170: String rawAcronyms = acronymPage.getContents();
171:
172: if (mLogger.isDebugEnabled()) {
173: mLogger.debug("parsing _acronyms template: \n'"
174: + rawAcronyms + "'");
175: }
176:
177: String regex = "\n"; // end of line
178: String[] lines = rawAcronyms.split(regex);
179:
180: if (lines != null) {
181: for (int i = 0; i < lines.length; i++) {
182: int index = lines[i].indexOf('=');
183: if (index > 0) {
184: String key = lines[i].substring(0, index).trim();
185: String value = lines[i].substring(index + 1,
186: lines[i].length()).trim();
187: acronyms.setProperty(key, value);
188: }
189: }
190: }
191:
192: return acronyms;
193: }
194:
195: }
|