BrightSide Workbench Full Report + Source Code
SearchFormatter.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.search;
19 
20 import java.util.Stack;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23 
28 public class SearchFormatter {
29  protected Stack lines;
30  protected String query, reQuery;
31  protected Pattern pat;
32  protected int count, found = 0;
33  protected boolean ignoreCase;
34 
36  public SearchFormatter(String query, boolean ignoreCase) {
37  this.query = query;
38  this.ignoreCase = ignoreCase;
39  lines = new Stack();
40  reQuery = queryToRegExp(query);
41  pat = Pattern.compile(reQuery);
42  }
43 
44  public void processLine(String line) {
45  if(line == null) return;
46  line = removeHTMLTag(line, "script");
47  line = removeHTMLTag(line, "style");
48  line = removeHTML(line);
49  line = removeMacros(line);
50  Matcher mat = pat.matcher(line);
51  int lfound = 0;
52  while(mat.find()) {
53  lfound++;
54  }
55  if(lfound > 0) {
56  line = line.replaceAll("(" + reQuery + ")", "<span class='found'>$1</span>");
57  lines.push(line);
58  found += lfound;
59  }
60  }
61 
62  public boolean wasFound() {
63  return lines.size() > 0;
64  }
65 
66  public String getResult() {
67  String result = "";
68  for(int i = 0; i < lines.size(); i++) {
69  result += lines.get(i) + "<br/>";
70  }
71  return result;
72  }
73 
74  public int getFound() {
75  return found;
76  }
77 
78  public String queryToRegExp(String query) {
79  if(query == null) return "";
80  String[] values = query.split("\\s+");
81  count = values.length;
82  String result = "(", sep = "";
83  for(int i = 0; i < count; i++) {
84  result += sep + "(" + values[i] + ")";
85  sep = "|";
86  }
87  return (ignoreCase ? "(?i)" : "") + result + ")";
88  }
89 
90  public static String queryToSQL(String field, String query, boolean ignoreCase) {
91  String[] values = query.split("\\s+");
92  String result = "(", sep = "";
93  for(int i = 0; i < values.length; i++) {
94  if(ignoreCase) {
95  result += sep + "UCASE(" + field + ") LIKE '%" + values[i].toUpperCase() + "%' ";
96  }
97  else {
98  result += sep + field + " LIKE '%" + values[i] + "%' ";
99  }
100  sep = "OR ";
101  }
102  return result + ")";
103  }
104 
105  public static String removeHTML(String line) {
106  return line.replaceAll("\\<\\/?[\\?a-zA-Z\\-\\:\\_0-9]+\\ ?.*?\\>", "");
107  }
108 
109  public static String removeHTMLTag(String line, String string) {
110  return line.replaceAll("\\<" + string + " .*?\\<\\/" + string + "\\>", "");
111  }
112 
113  private static String removeMacros(String line) {
114  return line.replaceAll("\\{\\@[\\?\\&a-zA-Z\\-\\:\\_0-9\\ \\.]+\\}", "");
115  }
116 
117 }
static String removeHTMLTag(String line, String string)
static String queryToSQL(String field, String query, boolean ignoreCase)
SearchFormatter(String query, boolean ignoreCase)