1 31 package org.blojsom.plugin.xpath; 32 33 import org.apache.commons.jxpath.JXPathContext; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.blojsom.blog.Blog; 37 import org.blojsom.blog.Entry; 38 import org.blojsom.plugin.Plugin; 39 import org.blojsom.plugin.PluginException; 40 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import java.util.ArrayList ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 56 public class XPathPlugin implements Plugin { 57 58 private static final String XPATH_PARAM = "xpath"; 59 60 private Log _logger = LogFactory.getLog(XPathPlugin.class); 61 62 68 public void init() throws PluginException { 69 JXPathContext.newContext(Entry.class); 70 } 71 72 83 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 84 Entry[] results; 85 String xpath = httpServletRequest.getParameter(XPATH_PARAM); 86 87 if (xpath != null && !"".equals(xpath)) { 88 xpath = xpath.trim(); 89 90 if (_logger.isDebugEnabled()) { 91 _logger.debug("Attempting xpath query with: " + xpath); 92 } 93 94 EntryWrapper entryWrapper = new EntryWrapper(entries); 95 List foundEntries = new ArrayList (); 96 JXPathContext xpathcontext = JXPathContext.newContext(entryWrapper); 97 98 try { 99 Iterator entryIterator = xpathcontext.iterate(xpath); 100 101 while (entryIterator.hasNext()) { 102 Object object = entryIterator.next(); 103 Entry entry = (Entry) object; 104 foundEntries.add(entry); 105 } 106 } catch (Exception e) { 107 if (_logger.isErrorEnabled()) { 108 _logger.error(e); 109 } 110 } 111 112 if (foundEntries.size() == 0) { 113 results = new Entry[0]; 114 } else { 115 results = new Entry[foundEntries.size()]; 116 for (int x = 0; x < foundEntries.size(); x++) { 117 results[x] = (Entry) foundEntries.get(x); 118 } 119 } 120 } else { 121 results = entries; 122 } 123 124 return results; 125 } 126 127 132 public void cleanup() throws PluginException { 133 } 134 135 140 public void destroy() throws PluginException { 141 } 142 } 143 144 145 | Popular Tags |