1 31 package org.blojsom.plugin.meta; 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.BlojsomUtils; 40 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import java.io.BufferedReader ; 44 import java.io.IOException ; 45 import java.io.StringReader ; 46 import java.util.HashMap ; 47 import java.util.Map ; 48 49 56 public class MetaPlugin implements Plugin { 57 58 private Log _logger = LogFactory.getLog(MetaPlugin.class); 59 60 private String _metaPrefix = "meta-"; 61 62 65 public MetaPlugin() { 66 } 67 68 73 public void init() throws PluginException { 74 } 75 76 87 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 88 String description; 89 Entry entry; 90 BufferedReader br; 91 StringReader sr; 92 String line; 93 StringBuffer noMetaDescription; 94 95 for (int i = 0; i < entries.length; i++) { 96 entry = entries[i]; 97 description = entry.getDescription(); 98 sr = new StringReader (description); 99 br = new BufferedReader (sr); 100 101 try { 102 int equalIndex; 103 String key; 104 String value; 105 Map metadata; 106 noMetaDescription = new StringBuffer (); 107 108 while ((line = br.readLine()) != null) { 109 if (line.trim().startsWith(_metaPrefix)) { 110 equalIndex = line.indexOf("=", _metaPrefix.length()); 111 if (equalIndex != -1) { 112 key = line.substring(_metaPrefix.length(), equalIndex); 113 value = line.substring(equalIndex + 1); 114 metadata = entry.getMetaData(); 115 if (metadata != null) { 116 metadata.put(key, value); 117 entry.setMetaData(metadata); 118 } else { 119 metadata = new HashMap (); 120 metadata.put(key, value); 121 entry.setMetaData(metadata); 122 } 123 } else { 124 noMetaDescription.append(line).append(BlojsomUtils.LINE_SEPARATOR); 125 } 126 } else { 127 noMetaDescription.append(line).append(BlojsomUtils.LINE_SEPARATOR); 128 } 129 } 130 131 entry.setDescription(noMetaDescription.toString()); 132 } catch (IOException e) { 133 if (_logger.isErrorEnabled()) { 134 _logger.error(e); 135 } 136 } 137 } 138 139 return entries; 140 } 141 142 147 public void cleanup() throws PluginException { 148 } 149 150 155 public void destroy() throws PluginException { 156 } 157 } 158 | Popular Tags |