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.xpath;
031:
032: import org.apache.commons.jxpath.JXPathContext;
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.blog.Blog;
037: import org.blojsom.plugin.PluginException;
038: import org.blojsom.plugin.search.SimpleSearchPlugin;
039:
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.util.ArrayList;
043: import java.util.Iterator;
044: import java.util.List;
045: import java.util.Map;
046:
047: /**
048: * XPathSearchPlugin
049: *
050: * @author David Czarnecki
051: * @author Mark Lussier
052: * @since blojsom 3.0
053: * @version $Id: XPathSearchPlugin.java,v 1.2 2007/01/17 02:35:07 czarneckid Exp $
054: */
055: public class XPathSearchPlugin extends SimpleSearchPlugin {
056:
057: private Log _logger = LogFactory.getLog(XPathSearchPlugin.class);
058:
059: /**
060: * Process the blog entries
061: *
062: * @param httpServletRequest Request
063: * @param httpServletResponse Response
064: * @param blog {@link Blog} instance
065: * @param context Context
066: * @param entries Blog entries retrieved for the particular request
067: * @return Modified set of blog entries
068: * @throws PluginException If there is an error processing the blog entries
069: */
070: public Entry[] process(HttpServletRequest httpServletRequest,
071: HttpServletResponse httpServletResponse, Blog blog,
072: Map context, Entry[] entries) throws PluginException {
073: Entry[] results;
074:
075: String query = httpServletRequest.getParameter(QUERY_PARAM);
076:
077: if (query != null) {
078: query = query.trim();
079: if (query.startsWith("/")) {
080: if (_logger.isDebugEnabled()) {
081: _logger.debug("Attempting xpath query with: "
082: + query);
083: }
084:
085: EntryWrapper entryWrapper = new EntryWrapper(entries);
086: List foundEntries = new ArrayList();
087: JXPathContext xpathcontext = JXPathContext
088: .newContext(entryWrapper);
089: try {
090: Iterator entryIterator = xpathcontext
091: .iterate(query);
092: while (entryIterator.hasNext()) {
093: Object object = entryIterator.next();
094: Entry entry = (Entry) object;
095: foundEntries.add(entry);
096: }
097: } catch (Exception e) {
098: if (_logger.isErrorEnabled()) {
099: _logger.error(e);
100: }
101: }
102:
103: if (foundEntries.size() == 0) {
104: results = new Entry[0];
105: } else {
106: results = new Entry[foundEntries.size()];
107: for (int x = 0; x < foundEntries.size(); x++) {
108: results[x] = (Entry) foundEntries.get(x);
109: }
110: }
111: } else {
112: results = super.process(httpServletRequest,
113: httpServletResponse, blog, context, entries);
114: }
115: } else {
116: results = entries;
117: }
118:
119: return results;
120: }
121: }
|