Source Code Cross Referenced for AcronymsPlugin.java in  » Blogger-System » apache-roller-3.1 » org » apache » roller » ui » rendering » plugins » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Blogger System » apache roller 3.1 » org.apache.roller.ui.rendering.plugins 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 &lt;acronym title='Hyper Text Markup Language'&gt;HTML&lt;/acronym&gt;. "
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.