1 13 package info.magnolia.cms.taglibs.util; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ItemType; 17 import info.magnolia.cms.core.NodeData; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 import javax.jcr.PropertyType; 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.JspWriter ; 27 import javax.servlet.jsp.tagext.TagSupport ; 28 29 import org.apache.commons.lang.ArrayUtils; 30 import org.apache.commons.lang.StringUtils; 31 import org.apache.commons.lang.exception.NestableRuntimeException; 32 import org.slf4j.Logger; 33 import org.slf4j.LoggerFactory; 34 35 36 40 public class SearchResultSnippetTag extends TagSupport { 41 42 45 private static final long serialVersionUID = 222L; 46 47 50 private static Logger log = LoggerFactory.getLogger(SearchResultSnippetTag.class); 51 52 55 private Content page; 56 57 60 private String query; 61 62 65 private int chars = 100; 66 67 70 private int maxSnippets = 3; 71 72 76 public void setQuery(String query) { 77 this.query = query; 78 } 79 80 84 public void setChars(int chars) { 85 this.chars = chars; 86 } 87 88 92 public void setMaxSnippets(int maxSnippets) { 93 this.maxSnippets = maxSnippets; 94 } 95 96 100 public void setPage(Content page) { 101 this.page = page; 102 } 103 104 107 public int doStartTag() throws JspException { 108 109 JspWriter out = this.pageContext.getOut(); 110 try { 111 Iterator iterator = getSnippets().iterator(); 112 while (iterator.hasNext()) { 113 out.println(iterator.next()); 114 } 115 } 116 catch (IOException e) { 117 throw new NestableRuntimeException(e); 119 } 120 return EVAL_PAGE; 121 } 122 123 132 public Collection getSnippets() { 133 134 log.debug("collecting snippets"); 136 Collection snippets = new ArrayList (); 137 String [] searchTerms = StringUtils.split(this.query); 138 139 Collection paragraphCollections = this.page.getChildren(ItemType.CONTENTNODE); 140 141 Iterator iterator = paragraphCollections.iterator(); 142 outer : while (iterator.hasNext()) { 143 Content paragraphCollection = (Content) iterator.next(); 144 145 Collection paragraphs = paragraphCollection.getChildren(); 146 147 Iterator parIterator = paragraphs.iterator(); 148 while (parIterator.hasNext()) { 149 Content paragraph = (Content) parIterator.next(); 150 151 log.debug("Iterating on paragraph {}", paragraph); 153 Collection properties = paragraph.getNodeDataCollection(); 154 155 Iterator dataIterator = properties.iterator(); 156 while (dataIterator.hasNext()) { 157 NodeData property = (NodeData) dataIterator.next(); 158 if (property.getType() != PropertyType.BINARY) { 159 160 String resultString = property.getString(); 161 162 log.debug("Iterating on property {}", property.getName()); log.debug("Property value is {}", resultString); 165 if (resultString.length() < 20) { 168 continue; 169 } 170 171 for (int j = 0; j < searchTerms.length; j++) { 172 String searchTerm = StringUtils.lowerCase(searchTerms[j]); 173 174 if (!ArrayUtils.contains(SimpleSearchTag.KEYWORDS, searchTerm) && searchTerm.length() > 2) { 176 177 log.debug("Looking for search term [{}] in [{}]", searchTerm, resultString); 179 if (!StringUtils.contains(resultString.toLowerCase(), searchTerm)) { 182 continue; 183 } 184 185 resultString = stripHtmlTags(resultString); 187 188 int pos = resultString.toLowerCase().indexOf(searchTerm); 190 if (pos > -1) { 191 192 int posEnd = pos + searchTerm.length(); 193 int from = (pos - chars / 2); 194 if (from < 0) { 195 from = 0; 196 } 197 198 int to = from + chars; 199 if (to > resultString.length()) { 200 to = resultString.length(); 201 } 202 203 StringBuffer snippet = new StringBuffer (); 204 205 snippet.append(StringUtils.substring(resultString, from, pos)); 206 snippet.append("<strong>"); snippet.append(StringUtils.substring(resultString, pos, posEnd)); 208 snippet.append("</strong>"); snippet.append(StringUtils.substring(resultString, posEnd, to)); 210 211 if (from > 0) { 212 snippet.insert(0, "... "); } 214 if (to < resultString.length()) { 215 snippet.append("... "); } 217 218 log.debug("Search term found, adding snippet {}", snippet); 220 snippets.add(snippet); 221 if (snippets.size() >= this.maxSnippets) { 222 223 log.debug("Maximum number of snippets ({}) reached, exiting", Integer.toString(this.maxSnippets)); 225 226 break outer; 227 } 228 } 229 } 230 } 231 } 232 } 233 } 234 } 235 236 return snippets; 237 } 238 239 243 protected String stripHtmlTags(String resultString) { 244 return resultString.replaceAll("\\<(.*?\\s*)*\\>", StringUtils.EMPTY); } 246 247 250 public void release() { 251 this.query = null; 252 this.page = null; 253 this.chars = 100; 254 this.maxSnippets = 3; 255 super.release(); 256 } 257 258 } 259 | Popular Tags |