KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > velocity > plugins > acronyms > AcronymsPlugin


1 /*
2  * Filename: AcronymsPlugin.java
3  *
4  * Created on 22-Jun-04
5  */

6 package org.roller.presentation.velocity.plugins.acronyms;
7
8 import org.apache.commons.lang.StringEscapeUtils;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.apache.velocity.context.Context;
12 import org.roller.RollerException;
13 import org.roller.model.RollerFactory;
14 import org.roller.model.UserManager;
15 import org.roller.pojos.PageData;
16 import org.roller.pojos.WeblogEntryData;
17 import org.roller.pojos.WebsiteData;
18 import org.roller.presentation.RollerRequest;
19 import org.roller.presentation.velocity.PagePlugin;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 /**
27  * Adds full text to pre-defined acronyms.
28  *
29  * Example: HTML would become <acronym title="Hyper Text Markup Language">HTML</acronym>
30  *
31  * @author <a HREF="mailto:molen@mail.com">Jaap van der Molen</a>
32  * @version $Revision: 1.3 $
33  */

34 public class AcronymsPlugin implements PagePlugin
35 {
36     /**
37      * Logger
38      */

39     private static final Log mLogger = LogFactory.getLog(AcronymsPlugin.class);
40
41     /**
42      * Name of this Plugin.
43      */

44     protected String JavaDoc name = "Acronyms";
45     protected String JavaDoc description = "Expands acronyms defined in _acronym page. " +
46         "Example: definition 'HTML=Hyper Text Markup Language' " +
47         "becomes &lt;acronym title='Hyper Text Markup Language'&gt;HTML&lt;/acronym&gt;. " +
48         "You must create an " +
49         "<a HREF='page.do?method=editPages&rmik=tabbedmenu.website.pages'>" +
50         "_acronym page</a> to use Acronyms.";
51     
52     /**
53      * Constructor
54      */

55     public AcronymsPlugin()
56     {
57         super();
58         mLogger.debug("AcronymsPlugin instantiated.");
59     }
60
61     /**
62      * @see org.roller.presentation.velocity.PagePlugin#init(org.roller.presentation.RollerRequest, org.apache.velocity.context.Context)
63      */

64     public void init(RollerRequest rreq, Context ctx) throws RollerException
65     {
66         if (mLogger.isDebugEnabled()) {
67             mLogger.debug("init( rreq = "+rreq+", ctx = "+ctx+" )");
68         }
69     }
70     
71     /**
72      * Look for any _acronyms Page and parse it into Properties.
73      * @param website
74      * @return
75      * @throws RollerException
76      */

77     private Properties JavaDoc loadAcronyms(WebsiteData website)
78     {
79         Properties JavaDoc acronyms = new Properties JavaDoc();
80         try
81         {
82             UserManager userMgr = RollerFactory.getRoller().getUserManager();
83             PageData acronymsPage = userMgr.getPageByName(
84                                         website, "_acronyms");
85             if (acronymsPage != null)
86             {
87                 acronyms = parseAcronymPage(acronymsPage, acronyms);
88             }
89         }
90         catch (RollerException e)
91         {
92             // not much we can do about it
93
mLogger.warn(e);
94         }
95         return acronyms;
96     }
97
98     /**
99      * @see org.roller.presentation.velocity.PagePlugin#render(org.roller.pojos.WeblogEntryData, boolean)
100      */

101     public String JavaDoc render(WeblogEntryData entry, boolean skipFlag)
102     {
103         String JavaDoc text = entry.getText();
104         
105         if (mLogger.isDebugEnabled()) {
106             mLogger.debug("render( entry = "+entry.getId()+", skipFlag = "+skipFlag+" )");
107         }
108
109         /*
110          * Get acronyms Properties.
111          */

112         Properties JavaDoc acronyms = loadAcronyms(entry.getWebsite());
113         mLogger.debug("acronyms.size()=" + acronyms.size());
114         if (acronyms.size() == 0)
115         {
116             return text;
117         }
118
119         /*
120          * Compile the user's acronyms into RegEx patterns.
121          */

122         Pattern JavaDoc[] acronymPatterns = new Pattern JavaDoc[acronyms.size()];
123         String JavaDoc[] acronymTags = new String JavaDoc[acronyms.size()];
124         int count = 0;
125         for (Iterator JavaDoc iter = acronyms.keySet().iterator(); iter.hasNext();)
126         {
127             String JavaDoc acronym = (String JavaDoc) iter.next();
128             acronymPatterns[count] = Pattern.compile("\\b" + acronym + "\\b");
129             mLogger.debug("match '" + acronym + "'");
130             acronymTags[count] =
131                 "<acronym title=\""
132                 + acronyms.getProperty(acronym)
133                 + "\">"
134                 + acronym
135                 + "</acronym>";
136             count++;
137         }
138         
139         // check skipper
140
/* I don't think this Plugin should skip. -Lance
141         if (skipFlag)
142         {
143             return text;
144         }
145         */

146         
147         // if there are none, no work to do
148
if (acronymPatterns == null || acronymPatterns.length == 0) {
149             return text;
150         }
151
152         return matchAcronyms(text, acronymPatterns, acronymTags);
153     }
154
155     /**
156      * Without Website cannot lookup _acronyms page.
157      * @see org.roller.presentation.velocity.PagePlugin#render(java.lang.String)
158      */

159     public String JavaDoc render(String JavaDoc text)
160     {
161         return text;
162     }
163
164     /**
165      * @return this Page Plugin's name
166      */

167     public String JavaDoc toString()
168     {
169         return name;
170     }
171     
172     /**
173      * Iterates through the acronym properties and replaces matching
174      * acronyms in the entry text with acronym html-tags.
175      *
176      * @param text entry text
177      * @param acronyms user provided set of acronyms
178      * @return entry text with acronym explanations
179      */

180     private String JavaDoc matchAcronyms(String JavaDoc text, Pattern JavaDoc[] acronymPatterns, String JavaDoc[] acronymTags)
181     {
182         if (mLogger.isDebugEnabled()) {
183             mLogger.debug("matchAcronyms("+text+")");
184         }
185
186         Matcher JavaDoc matcher = null;
187         for (int i=0; i<acronymPatterns.length; i++)
188         {
189             matcher = acronymPatterns[i].matcher(text);
190             text = matcher.replaceAll(acronymTags[i]);
191         }
192         return text;
193     }
194
195     /**
196      * Parse the Template of the provided PageData and turns it
197      * into a <code>Properties</code> collection.
198      *
199      * @param acronymPage
200      * @return acronym properties (key = acronym, value= full text), empty if Template is empty
201      */

202     private Properties JavaDoc parseAcronymPage(PageData acronymPage, Properties JavaDoc acronyms)
203     {
204         String JavaDoc rawAcronyms = acronymPage.getTemplate();
205         
206         if (mLogger.isDebugEnabled())
207         {
208             mLogger.debug("parsing _acronyms template: \n'"+rawAcronyms+"'");
209         }
210         
211         String JavaDoc regex = "\n"; // end of line
212
String JavaDoc[] lines = rawAcronyms.split(regex);
213
214         if (lines != null)
215         {
216             for (int i = 0; i < lines.length; i++)
217             {
218                 int index = lines[i].indexOf('=');
219                 if (index > 0)
220                 {
221                     String JavaDoc key = lines[i].substring(0, index).trim();
222                     String JavaDoc value =
223                         lines[i].substring(index + 1, lines[i].length()).trim();
224                     acronyms.setProperty(key, value);
225                 }
226             }
227         }
228
229         return acronyms;
230     }
231
232     public String JavaDoc getName() { return name; }
233     public String JavaDoc getDescription() { return StringEscapeUtils.escapeJavaScript(description); }
234 }
235
Popular Tags