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.markdown;
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.BlojsomConstants;
039: import org.blojsom.util.BlojsomUtils;
040:
041: import javax.servlet.ServletConfig;
042: import javax.servlet.http.HttpServletRequest;
043: import javax.servlet.http.HttpServletResponse;
044: import java.io.*;
045: import java.util.Map;
046:
047: /**
048: * MarkdownPlugin
049: * <p/>
050: * To use the Markdown plugin, you will need to download the Markdown tool from
051: * <a href="http://daringfireball.net/projects/markdown/">John Gruber's Markdown site</a>.
052: *
053: * @author David Czarnecki
054: * @version $Id: MarkdownPlugin.java,v 1.3 2007/01/17 02:35:11 czarneckid Exp $
055: * @since blojsom 3.0
056: */
057: public class MarkdownPlugin implements Plugin {
058:
059: private Log _logger = LogFactory.getLog(MarkdownPlugin.class);
060:
061: /**
062: * Metadata key to identify a Markdown post
063: */
064: private static final String METADATA_RUN_MARKDOWN = "run-markdown";
065:
066: /**
067: * Extension of Markdown post
068: */
069: private static final String MARKDOWN_EXTENSION = ".markdown";
070:
071: /**
072: * Initialization parameter for the command to start a Markdown session
073: */
074: private static final String PLUGIN_MARKDOWN_EXECUTION_IP = "plugin-markdown-execution";
075:
076: private String _markdownExecution;
077: private ServletConfig _servletConfig;
078:
079: /**
080: * Initialize this plugin. This method only called when the plugin is instantiated.
081: *
082: * @throws PluginException If there is an error initializing the plugin
083: */
084: public void init() throws PluginException {
085: _markdownExecution = _servletConfig
086: .getInitParameter(PLUGIN_MARKDOWN_EXECUTION_IP);
087:
088: if (BlojsomUtils.checkNullOrBlank(_markdownExecution)) {
089: if (_logger.isErrorEnabled()) {
090: _logger
091: .error("No Markdown execution string provided. Use initialization parameter: "
092: + PLUGIN_MARKDOWN_EXECUTION_IP);
093: }
094: }
095: }
096:
097: /**
098: * Set the {@link ServletConfig}
099: *
100: * @param servletConfig {@link ServletConfig}
101: */
102: public void setServletConfig(ServletConfig servletConfig) {
103: _servletConfig = servletConfig;
104: }
105:
106: /**
107: * Process the blog entries
108: *
109: * @param httpServletRequest Request
110: * @param httpServletResponse Response
111: * @param blog {@link Blog} instance
112: * @param context Context
113: * @param entries Blog entries retrieved for the particular request
114: * @return Modified set of blog entries
115: * @throws PluginException If there is an error processing the blog entries
116: */
117: public Entry[] process(HttpServletRequest httpServletRequest,
118: HttpServletResponse httpServletResponse, Blog blog,
119: Map context, Entry[] entries) throws PluginException {
120: if (!BlojsomUtils.checkNullOrBlank(_markdownExecution)) {
121: for (int i = 0; i < entries.length; i++) {
122: Entry entry = entries[i];
123:
124: if ((entry.getPostSlug().endsWith(MARKDOWN_EXTENSION) || BlojsomUtils
125: .checkMapForKey(entry.getMetaData(),
126: METADATA_RUN_MARKDOWN))) {
127: try {
128: Process process = Runtime.getRuntime().exec(
129: _markdownExecution);
130: BufferedWriter bw = new BufferedWriter(
131: new OutputStreamWriter(process
132: .getOutputStream(),
133: BlojsomConstants.UTF8));
134: BufferedReader br = new BufferedReader(
135: new InputStreamReader(process
136: .getInputStream(),
137: BlojsomConstants.UTF8));
138: bw.write(entry.getDescription());
139: bw.close();
140: String input;
141: StringBuffer collectedDescription = new StringBuffer();
142:
143: while ((input = br.readLine()) != null) {
144: collectedDescription.append(input).append(
145: BlojsomConstants.LINE_SEPARATOR);
146: }
147:
148: entry.setDescription(collectedDescription
149: .toString());
150: br.close();
151: } catch (IOException e) {
152: if (_logger.isErrorEnabled()) {
153: _logger.error(e);
154: }
155: }
156: }
157: }
158: }
159:
160: return entries;
161: }
162:
163: /**
164: * Perform any cleanup for the plugin. Called after {@link #process}.
165: *
166: * @throws PluginException If there is an error performing cleanup for this plugin
167: */
168: public void cleanup() throws PluginException {
169: }
170:
171: /**
172: * Called when BlojsomServlet is taken out of service
173: *
174: * @throws PluginException If there is an error in finalizing this plugin
175: */
176: public void destroy() throws PluginException {
177: }
178: }
|