001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.footnote;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.plugin.Plugin;
037: import org.blojsom.plugin.PluginException;
038: import org.blojsom.util.BlojsomUtils;
039:
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.text.MessageFormat;
043: import java.util.Iterator;
044: import java.util.Map;
045: import java.util.TreeMap;
046: import java.util.regex.Matcher;
047: import java.util.regex.Pattern;
048:
049: /**
050: * Footnote Expansion Plugin
051: *
052: * @author Mark Lussier
053: * @author David Czarnecki
054: * @since blojsom 3.0
055: * @version $Id: FootnotePlugin.java,v 1.3 2007/01/17 02:35:10 czarneckid Exp $
056: */
057: public class FootnotePlugin implements Plugin {
058:
059: private static final String FOOTNOTE_METADATA = "footnote";
060: private static final String REGEX_FOOTNOTE = "\\[(\\d+)\\]";
061: private static final String FOOTNOTE_LINKAGE_FORMAT = "[{0}] {1}";
062: private static final String FOOTNOTES_PROCESSED_METADATA = "footnotes-processed";
063:
064: private Log _logger = LogFactory.getLog(FootnotePlugin.class);
065:
066: /**
067: * Initialize this plugin. This method only called when the plugin is instantiated.
068: *
069: * @throws org.blojsom.plugin.PluginException
070: * If there is an error initializing the plugin
071: */
072: public void init() throws PluginException {
073: }
074:
075: /**
076: * Process the blog entries
077: *
078: * @param httpServletRequest Request
079: * @param httpServletResponse Response
080: * @param blog {@link Blog} instance
081: * @param context Context
082: * @param entries Blog entries retrieved for the particular request
083: * @return Modified set of blog entries
084: * @throws PluginException If there is an error processing the blog entries
085: */
086: public Entry[] process(HttpServletRequest httpServletRequest,
087: HttpServletResponse httpServletResponse, Blog blog,
088: Map context, Entry[] entries) throws PluginException {
089: Pattern footnotePattern = Pattern.compile(REGEX_FOOTNOTE);
090:
091: for (int i = 0; i < entries.length; i++) {
092: Entry entry = entries[i];
093: String content = entry.getDescription();
094: Matcher matcher = footnotePattern.matcher(content);
095: StringBuffer modifiedContent = new StringBuffer();
096: Map footnotes = new TreeMap();
097:
098: while (matcher.find()) {
099: int footnoteIndex;
100:
101: try {
102: footnoteIndex = Integer.parseInt(matcher.group(1));
103: if (BlojsomUtils.checkMapForKey(
104: entry.getMetaData(), FOOTNOTE_METADATA
105: + "-" + footnoteIndex)) {
106: footnotes.put(Integer.toString(footnoteIndex),
107: entry.getMetaData().get(
108: FOOTNOTE_METADATA + "-"
109: + footnoteIndex));
110: }
111:
112: matcher.appendReplacement(modifiedContent,
113: "<sup id=\"footnoteref-" + footnoteIndex
114: + "\"><a href=\"#footnote-"
115: + footnoteIndex + "\">"
116: + footnoteIndex + "</a></sup>");
117: } catch (NumberFormatException e) {
118: if (_logger.isErrorEnabled()) {
119: _logger
120: .error("Footnote index in post is not a valid integer ["
121: + matcher.group(1) + "]");
122: }
123: }
124: }
125:
126: matcher.appendTail(modifiedContent);
127:
128: if (!footnotes.isEmpty()
129: && !BlojsomUtils.checkMapForKey(
130: entry.getMetaData(),
131: FOOTNOTES_PROCESSED_METADATA)) {
132: modifiedContent.append("<br/><br/>");
133: modifiedContent.append("<div class=\"footnote\">");
134: modifiedContent.append("<hr/>");
135: modifiedContent.append("<ol>");
136: Iterator footnotesIterator = footnotes.keySet()
137: .iterator();
138:
139: while (footnotesIterator.hasNext()) {
140: String footnoteIndex = (String) footnotesIterator
141: .next();
142: modifiedContent.append("<li id=\"footnote-")
143: .append(footnoteIndex).append("\"><p>");
144: modifiedContent
145: .append(MessageFormat
146: .format(
147: FOOTNOTE_LINKAGE_FORMAT,
148: new Object[] {
149: footnoteIndex,
150: entry
151: .getMetaData()
152: .get(
153: FOOTNOTE_METADATA
154: + "-"
155: + footnoteIndex) }));
156: modifiedContent
157: .append("<a href=\"#footnoteref-")
158: .append(footnoteIndex)
159: .append(
160: "\" class=\"footnoteBackLink\" title=\"Jump back to footnote ")
161: .append(footnoteIndex).append(
162: " in the text.\">");
163: modifiedContent.append("↩</a></p></li>");
164: }
165:
166: modifiedContent.append("</ol>");
167: modifiedContent.append("</div>");
168:
169: entry.getMetaData().put(FOOTNOTES_PROCESSED_METADATA,
170: "true");
171: }
172:
173: entry.setDescription(modifiedContent.toString());
174: }
175:
176: return entries;
177: }
178:
179: /**
180: * Perform any cleanup for the plugin. Called after {@link #process}.
181: *
182: * @throws org.blojsom.plugin.PluginException
183: * If there is an error performing cleanup for this plugin
184: */
185: public void cleanup() throws PluginException {
186: }
187:
188: /**
189: * Called when BlojsomServlet is taken out of service
190: *
191: * @throws org.blojsom.plugin.PluginException
192: * If there is an error in finalizing this plugin
193: */
194: public void destroy() throws PluginException {
195: }
196:
197: }
|