?? acronymsplugin.java
字號:
/* * Filename: AcronymsPlugin.java * * Created on 22-Jun-04 */package org.roller.presentation.velocity.plugins.acronyms;import org.apache.commons.lang.StringEscapeUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.velocity.context.Context;import org.roller.RollerException;import org.roller.model.RollerFactory;import org.roller.model.UserManager;import org.roller.pojos.PageData;import org.roller.pojos.WeblogEntryData;import org.roller.pojos.WebsiteData;import org.roller.presentation.RollerRequest;import org.roller.presentation.velocity.PagePlugin;import java.util.Iterator;import java.util.Properties;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Adds full text to pre-defined acronyms. * * Example: HTML would become <acronym title="Hyper Text Markup Language">HTML</acronym> * * @author <a href="mailto:molen@mail.com">Jaap van der Molen</a> * @version $Revision: 1.3 $ */public class AcronymsPlugin implements PagePlugin{ /** * Logger */ private static final Log mLogger = LogFactory.getLog(AcronymsPlugin.class); /** * Name of this Plugin. */ protected String name = "Acronyms"; protected String description = "Expands acronyms defined in _acronym page. " + "Example: definition 'HTML=Hyper Text Markup Language' " + "becomes <acronym title='Hyper Text Markup Language'>HTML</acronym>. " + "You must create an " + "<a href='page.do?method=editPages&rmik=tabbedmenu.website.pages'>" + "_acronym page</a> to use Acronyms."; /** * Constructor */ public AcronymsPlugin() { super(); mLogger.debug("AcronymsPlugin instantiated."); } /** * @see org.roller.presentation.velocity.PagePlugin#init(org.roller.presentation.RollerRequest, org.apache.velocity.context.Context) */ public void init(RollerRequest rreq, Context ctx) throws RollerException { if (mLogger.isDebugEnabled()) { mLogger.debug("init( rreq = "+rreq+", ctx = "+ctx+" )"); } } /** * Look for any _acronyms Page and parse it into Properties. * @param website * @return * @throws RollerException */ private Properties loadAcronyms(WebsiteData website) { Properties acronyms = new Properties(); try { UserManager userMgr = RollerFactory.getRoller().getUserManager(); PageData acronymsPage = userMgr.getPageByName( website, "_acronyms"); if (acronymsPage != null) { acronyms = parseAcronymPage(acronymsPage, acronyms); } } catch (RollerException e) { // not much we can do about it mLogger.warn(e); } return acronyms; } /** * @see org.roller.presentation.velocity.PagePlugin#render(org.roller.pojos.WeblogEntryData, boolean) */ public String render(WeblogEntryData entry, boolean skipFlag) { String text = entry.getText(); if (mLogger.isDebugEnabled()) { mLogger.debug("render( entry = "+entry.getId()+", skipFlag = "+skipFlag+" )"); } /* * Get acronyms Properties. */ Properties acronyms = loadAcronyms(entry.getWebsite()); mLogger.debug("acronyms.size()=" + acronyms.size()); if (acronyms.size() == 0) { return text; } /* * Compile the user's acronyms into RegEx patterns. */ Pattern[] acronymPatterns = new Pattern[acronyms.size()]; String[] acronymTags = new String[acronyms.size()]; int count = 0; for (Iterator iter = acronyms.keySet().iterator(); iter.hasNext();) { String acronym = (String) iter.next(); acronymPatterns[count] = Pattern.compile("\\b" + acronym + "\\b"); mLogger.debug("match '" + acronym + "'"); acronymTags[count] = "<acronym title=\"" + acronyms.getProperty(acronym) + "\">" + acronym + "</acronym>"; count++; } // check skipper /* I don't think this Plugin should skip. -Lance if (skipFlag) { return text; } */ // if there are none, no work to do if (acronymPatterns == null || acronymPatterns.length == 0) { return text; } return matchAcronyms(text, acronymPatterns, acronymTags); } /** * Without Website cannot lookup _acronyms page. * @see org.roller.presentation.velocity.PagePlugin#render(java.lang.String) */ public String render(String text) { return text; } /** * @return this Page Plugin's name */ public String toString() { return name; } /** * Iterates through the acronym properties and replaces matching * acronyms in the entry text with acronym html-tags. * * @param text entry text * @param acronyms user provided set of acronyms * @return entry text with acronym explanations */ private String matchAcronyms(String text, Pattern[] acronymPatterns, String[] acronymTags) { if (mLogger.isDebugEnabled()) { mLogger.debug("matchAcronyms("+text+")"); } Matcher matcher = null; for (int i=0; i<acronymPatterns.length; i++) { matcher = acronymPatterns[i].matcher(text); text = matcher.replaceAll(acronymTags[i]); } return text; } /** * Parse the Template of the provided PageData and turns it * into a <code>Properties</code> collection. * * @param acronymPage * @return acronym properties (key = acronym, value= full text), empty if Template is empty */ private Properties parseAcronymPage(PageData acronymPage, Properties acronyms) { String rawAcronyms = acronymPage.getTemplate(); if (mLogger.isDebugEnabled()) { mLogger.debug("parsing _acronyms template: \n'"+rawAcronyms+"'"); } String regex = "\n"; // end of line String[] lines = rawAcronyms.split(regex); if (lines != null) { for (int i = 0; i < lines.length; i++) { int index = lines[i].indexOf('='); if (index > 0) { String key = lines[i].substring(0, index).trim(); String value = lines[i].substring(index + 1, lines[i].length()).trim(); acronyms.setProperty(key, value); } } } return acronyms; } public String getName() { return name; } public String getDescription() { return StringEscapeUtils.escapeJavaScript(description); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -