View Javadoc
1   /**
2    * This file Copyright (c) 2012-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.templating.jsp.taglib;
35  
36  import info.magnolia.jcr.util.NodeTypes;
37  import info.magnolia.jcr.util.NodeUtil;
38  
39  import java.io.IOException;
40  import java.util.ArrayList;
41  import java.util.Collection;
42  import java.util.Iterator;
43  import java.util.regex.Pattern;
44  
45  import javax.jcr.Node;
46  import javax.jcr.Property;
47  import javax.jcr.PropertyType;
48  import javax.servlet.jsp.JspException;
49  import javax.servlet.jsp.JspWriter;
50  import javax.servlet.jsp.tagext.TagSupport;
51  
52  import org.apache.commons.lang.ArrayUtils;
53  import org.apache.commons.lang.StringUtils;
54  import org.apache.commons.lang.exception.NestableRuntimeException;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  import org.tldgen.annotations.BodyContent;
59  import org.tldgen.annotations.Tag;
60  
61  
62  /**
63   * Output a set of snippets taken from any paragraph in the given page mathing any of the search term.
64   * @jsp.tag name="searchResultSnippet" body-content="empty"
65   * @jsp.tag-example
66   * <pre>
67   * &lt;cmsu:simplesearch query="${param.search}" var="results" /&gt;
68   * &lt;c:forEach items="${results}" var="page"&gt;
69   *   &lt;cmsu:searchResultSnippet query="${param.search}" page="${page}" /&gt;
70   * &lt;/c:forEach&gt;
71   *</pre>
72   */
73  @Tag(name="searchResultSnippet", bodyContent=BodyContent.EMPTY)
74  
75  public class SearchResultSnippetTag extends TagSupport {
76  
77      private static final Pattern HTML_STRIP = Pattern.compile("<.*?>", Pattern.DOTALL);
78  
79      private static final Logger log = LoggerFactory.getLogger(SearchResultSnippetTag.class);
80  
81      /**
82       * Start level.
83       */
84      private Node page;
85  
86      /**
87       * Query, natural language.
88       */
89      private String query;
90  
91      /**
92       * Number of chars to include in result.
93       */
94      private int chars = 100;
95  
96      /**
97       * Maximum number of snippets to include in result.
98       */
99      private int maxSnippets = 3;
100 
101     /**
102      * Search query.
103      * @jsp.attribute required="true" rtexprvalue="true"
104      */
105     public void setQuery(String query) {
106         this.query = query;
107     }
108 
109     /**
110      * Number of characters to include in search snippets. Default is 100.
111      * @jsp.attribute required="false" rtexprvalue="true" type="int"
112      */
113     public void setChars(int chars) {
114         this.chars = chars;
115     }
116 
117     /**
118      * Maximum number of snippets to print out.
119      * @jsp.attribute required="false" rtexprvalue="true" type="int"
120      */
121     public void setMaxSnippets(int maxSnippets) {
122         this.maxSnippets = maxSnippets;
123     }
124 
125     /**
126      * A Content node of type mgnl:content (a magnolia page), typically returned by the simpleSearch tag.
127      * @jsp.attribute required="true" rtexprvalue="true" type="info.magnolia.cms.core.Content"
128      */
129     public void setPage(Node page) {
130         this.page = page;
131     }
132 
133     /**
134      * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
135      */
136     @Override
137     public int doStartTag() throws JspException {
138 
139         JspWriter out = this.pageContext.getOut();
140         try {
141             Iterator iterator = getSnippets().iterator();
142             while (iterator.hasNext()) {
143                 out.println(iterator.next());
144             }
145         }
146         catch (IOException e) {
147             // should never happen
148             throw new NestableRuntimeException(e);
149         }
150         return EVAL_PAGE;
151     }
152 
153     /**
154      * Extract a collection of snippets from any paragraph in the given page.
155      * @return a collection of Strings.
156      * @todo avoid overlapping snippets (use regexp insted of simple indexOfs)
157      * @todo only extract snippets from user-configured properties
158      * @todo abbreviate on whitespace and puntuation, detect start of sentences
159      * @todo replace ampersand in regexp
160      * @todo break methods and write junits
161      */
162     public Collection getSnippets() {
163 
164         log.debug("collecting snippets");
165 
166         Collection snippets = new ArrayList();
167         String[] searchTerms = StringUtils.split(this.query);
168 
169         try{
170         Iterator<Node> iterator = NodeUtil.getNodes(this.page, NodeTypes.ContentNode.NAME).iterator();
171 
172         outer : while (iterator.hasNext()) {
173             Node paragraphCollection = iterator.next();
174 
175             Iterator<Node> parIterator = NodeUtil.getNodes(paragraphCollection, NodeTypes.ContentNode.NAME).iterator();
176             while (parIterator.hasNext()) {
177                 Node paragraph = parIterator.next();
178 
179                 log.debug("Iterating on paragraph {}", paragraph);
180 
181                 Iterator dataIterator = paragraph.getProperties();
182 
183                 while (dataIterator.hasNext()) {
184                     Property property = (Property) dataIterator.next();
185                     if (property.getType() != PropertyType.BINARY) {
186 
187                         String resultString = property.getString();
188 
189                         log.debug("Iterating on property {}", property.getName());
190                         log.debug("Property value is {}", resultString);
191 
192                         // a quick and buggy way to avoid configuration properties, we should allow the user to
193                         // configure a list of nodeData to search for...
194                         if (resultString.length() < 20) {
195                             continue;
196                         }
197 
198                         for (int j = 0; j < searchTerms.length; j++) {
199                             String searchTerm = StringUtils.lowerCase(searchTerms[j]);
200 
201                             // exclude keywords and words with less than 2 chars
202                             if (!ArrayUtils.contains(new String[]{"and", "or"}, searchTerm) && searchTerm.length() > 2) {
203 
204                                 log.debug("Looking for search term [{}] in [{}]", searchTerm, resultString);
205 
206                                 // first check, avoid using heavy string replaceAll operations if the search term is not
207                                 // there
208                                 if (!StringUtils.contains(resultString.toLowerCase(), searchTerm)) {
209                                     continue;
210                                 }
211 
212                                 // strips out html tags using a regexp
213                                 resultString = stripHtmlTags(resultString);
214 
215                                 // only get first matching keyword
216                                 int pos = resultString.toLowerCase().indexOf(searchTerm);
217                                 if (pos > -1) {
218 
219                                     int posEnd = pos + searchTerm.length();
220                                     int from = (pos - chars / 2);
221                                     if (from < 0) {
222                                         from = 0;
223                                     }
224 
225                                     int to = from + chars;
226                                     if (to > resultString.length()) {
227                                         to = resultString.length();
228                                     }
229 
230                                     StringBuffer snippet = new StringBuffer();
231 
232                                     snippet.append(StringUtils.substring(resultString, from, pos));
233                                     snippet.append("<strong>");
234                                     snippet.append(StringUtils.substring(resultString, pos, posEnd));
235                                     snippet.append("</strong>");
236                                     snippet.append(StringUtils.substring(resultString, posEnd, to));
237 
238                                     if (from > 0) {
239                                         snippet.insert(0, "... ");
240                                     }
241                                     if (to < resultString.length()) {
242                                         snippet.append("... ");
243                                     }
244 
245                                     log.debug("Search term found, adding snippet {}", snippet);
246 
247                                     snippets.add(snippet);
248                                     if (snippets.size() >= this.maxSnippets) {
249 
250                                         log.debug("Maximum number of snippets ({}) reached, exiting",
251                                             Integer.toString(this.maxSnippets));
252 
253                                         break outer;
254                                     }
255                                 }
256                             }
257                         }
258                     }
259                 }
260             }
261             
262         }
263         return snippets;
264         }catch(Exception e){
265             log.error(e.getMessage(), e);
266             return null;
267         }
268     }
269 
270     protected String stripHtmlTags(String input) {
271         return HTML_STRIP.matcher(input).replaceAll("");
272     }
273 
274     /**
275      * @see javax.servlet.jsp.tagext.TagSupport#release()
276      */
277     @Override
278     public void release() {
279         this.query = null;
280         this.page = null;
281         this.chars = 100;
282         this.maxSnippets = 3;
283         super.release();
284     }
285 
286 }