KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > markdown > MarkdownPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.markdown;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.blog.Blog;
36 import org.blojsom.blog.Entry;
37 import org.blojsom.plugin.Plugin;
38 import org.blojsom.plugin.PluginException;
39 import org.blojsom.util.BlojsomConstants;
40 import org.blojsom.util.BlojsomUtils;
41
42 import javax.servlet.ServletConfig JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.io.*;
46 import java.util.Map JavaDoc;
47
48 /**
49  * MarkdownPlugin
50  * <p/>
51  * To use the Markdown plugin, you will need to download the Markdown tool from
52  * <a HREF="http://daringfireball.net/projects/markdown/">John Gruber's Markdown site</a>.
53  *
54  * @author David Czarnecki
55  * @version $Id: MarkdownPlugin.java,v 1.2 2006/03/20 22:50:45 czarneckid Exp $
56  * @since blojsom 3.0
57  */

58 public class MarkdownPlugin implements Plugin {
59
60     private Log _logger = LogFactory.getLog(MarkdownPlugin.class);
61
62     /**
63      * Metadata key to identify a Markdown post
64      */

65     private static final String JavaDoc METADATA_RUN_MARKDOWN = "run-markdown";
66
67     /**
68      * Extension of Markdown post
69      */

70     private static final String JavaDoc MARKDOWN_EXTENSION = ".markdown";
71
72     /**
73      * Initialization parameter for the command to start a Markdown session
74      */

75     private static final String JavaDoc PLUGIN_MARKDOWN_EXECUTION_IP = "plugin-markdown-execution";
76
77     private String JavaDoc _markdownExecution;
78     private ServletConfig JavaDoc _servletConfig;
79
80     /**
81      * Initialize this plugin. This method only called when the plugin is instantiated.
82      *
83      * @throws PluginException If there is an error initializing the plugin
84      */

85     public void init() throws PluginException {
86         _markdownExecution = _servletConfig.getInitParameter(PLUGIN_MARKDOWN_EXECUTION_IP);
87
88         if (BlojsomUtils.checkNullOrBlank(_markdownExecution)) {
89             if (_logger.isErrorEnabled()) {
90                 _logger.error("No Markdown execution string provided. Use initialization parameter: " + PLUGIN_MARKDOWN_EXECUTION_IP);
91             }
92         }
93     }
94
95     /**
96      * Set the {@link ServletConfig}
97      *
98      * @param servletConfig {@link ServletConfig}
99      */

100     public void setServletConfig(ServletConfig JavaDoc servletConfig) {
101         _servletConfig = servletConfig;
102     }
103
104     /**
105      * Process the blog entries
106      *
107      * @param httpServletRequest Request
108      * @param httpServletResponse Response
109      * @param blog {@link Blog} instance
110      * @param context Context
111      * @param entries Blog entries retrieved for the particular request
112      * @return Modified set of blog entries
113      * @throws PluginException If there is an error processing the blog entries
114      */

115     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
116         if (!BlojsomUtils.checkNullOrBlank(_markdownExecution)) {
117             for (int i = 0; i < entries.length; i++) {
118                 Entry entry = entries[i];
119
120                 if ((entry.getPostSlug().endsWith(MARKDOWN_EXTENSION) || BlojsomUtils.checkMapForKey(entry.getMetaData(), METADATA_RUN_MARKDOWN)))
121                 {
122                     try {
123                         Process JavaDoc process = Runtime.getRuntime().exec(_markdownExecution);
124                         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), BlojsomConstants.UTF8));
125                         BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), BlojsomConstants.UTF8));
126                         bw.write(entry.getDescription());
127                         bw.close();
128                         String JavaDoc input;
129                         StringBuffer JavaDoc collectedDescription = new StringBuffer JavaDoc();
130
131                         while ((input = br.readLine()) != null) {
132                             collectedDescription.append(input).append(BlojsomConstants.LINE_SEPARATOR);
133                         }
134
135                         entry.setDescription(collectedDescription.toString());
136                         br.close();
137                     } catch (IOException e) {
138                         if (_logger.isErrorEnabled()) {
139                             _logger.error(e);
140                         }
141                     }
142                 }
143             }
144         }
145
146         return entries;
147     }
148
149     /**
150      * Perform any cleanup for the plugin. Called after {@link #process}.
151      *
152      * @throws PluginException If there is an error performing cleanup for this plugin
153      */

154     public void cleanup() throws PluginException {
155     }
156
157     /**
158      * Called when BlojsomServlet is taken out of service
159      *
160      * @throws PluginException If there is an error in finalizing this plugin
161      */

162     public void destroy() throws PluginException {
163     }
164 }
Popular Tags