KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > plugins > AcronymsPlugin


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.ui.rendering.plugins;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
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.RollerException;
30 import org.apache.roller.model.RollerFactory;
31 import org.apache.roller.model.UserManager;
32 import org.apache.roller.model.WeblogEntryPlugin;
33 import org.apache.roller.pojos.WeblogEntryData;
34 import org.apache.roller.pojos.WeblogTemplate;
35 import org.apache.roller.pojos.WebsiteData;
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     
48     private static final Log mLogger = LogFactory.getLog(AcronymsPlugin.class);
49     
50     protected String JavaDoc name = "Acronyms";
51     protected String JavaDoc description = "Expands acronyms defined in _acronym page. " +
52             "Example: definition 'HTML=Hyper Text Markup Language' " +
53             "becomes &lt;acronym title='Hyper Text Markup Language'&gt;HTML&lt;/acronym&gt;. " +
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() {
60         super();
61         mLogger.debug("AcronymsPlugin instantiated.");
62     }
63     
64     
65     public String JavaDoc getName() {
66         return name;
67     }
68     
69     
70     public String JavaDoc getDescription() {
71         return StringEscapeUtils.escapeJavaScript(description);
72     }
73     
74     
75     public void init(WebsiteData website) throws RollerException {}
76     
77     
78     public String JavaDoc render(WeblogEntryData entry, String JavaDoc str) {
79         String JavaDoc text = str;
80         
81         if (mLogger.isDebugEnabled()) {
82             mLogger.debug("render(entry = "+entry.getId()+")");
83         }
84         
85         /*
86          * Get acronyms Properties.
87          */

88         Properties JavaDoc 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 JavaDoc[] acronymPatterns = new Pattern JavaDoc[acronyms.size()];
98         String JavaDoc[] acronymTags = new String JavaDoc[acronyms.size()];
99         int count = 0;
100         for (Iterator JavaDoc iter = acronyms.keySet().iterator(); iter.hasNext();) {
101             String JavaDoc acronym = (String JavaDoc) 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 RollerException
127      */

128     private Properties JavaDoc loadAcronyms(WebsiteData website) {
129         Properties JavaDoc acronyms = new Properties JavaDoc();
130         try {
131             UserManager userMgr = RollerFactory.getRoller().getUserManager();
132             WeblogTemplate acronymsPage = userMgr.getPageByName(
133                     website, "_acronyms");
134             if (acronymsPage != null) {
135                 acronyms = parseAcronymPage(acronymsPage, acronyms);
136             }
137         } catch (RollerException 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 JavaDoc matchAcronyms(String JavaDoc text, Pattern JavaDoc[] acronymPatterns, String JavaDoc[] acronymTags) {
154         if (mLogger.isDebugEnabled()) {
155             mLogger.debug("matchAcronyms("+text+")");
156         }
157         
158         Matcher JavaDoc 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 JavaDoc parseAcronymPage(WeblogTemplate acronymPage, Properties JavaDoc acronyms) {
174         String JavaDoc rawAcronyms = acronymPage.getContents();
175         
176         if (mLogger.isDebugEnabled()) {
177             mLogger.debug("parsing _acronyms template: \n'"+rawAcronyms+"'");
178         }
179         
180         String JavaDoc regex = "\n"; // end of line
181
String JavaDoc[] 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 JavaDoc key = lines[i].substring(0, index).trim();
188                     String JavaDoc 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 }
199
Popular Tags