Prepared better HEREDOC support; see comment for bug #1319276
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpdt / core / tests / lucene / PHPSearcher.java
1 package net.sourceforge.phpdt.core.tests.lucene;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.apache.lucene.document.Document;
8 import org.apache.lucene.index.IndexReader;
9 import org.apache.lucene.index.Term;
10 import org.apache.lucene.index.TermEnum;
11 import org.apache.lucene.search.Hits;
12 import org.apache.lucene.search.IndexSearcher;
13 import org.apache.lucene.search.PrefixQuery;
14 import org.apache.lucene.search.Query;
15 import org.apache.lucene.search.TermQuery;
16
17 public class PHPSearcher {
18         private IndexSearcher fSearcher;
19
20         // private StandardAnalyzer analyzer;
21
22         public PHPSearcher(String indexPath) {
23                 try {
24                         fSearcher = new IndexSearcher(indexPath);
25                         // analyzer = new StandardAnalyzer();
26                 } catch (IOException e) {
27                         e.printStackTrace();
28                 }
29         }
30
31         public Hits getClassInfo(String ident) {
32                 Hits hits = null;
33                 try {
34                         Query query = new TermQuery(new Term("c", ident));
35                         hits = fSearcher.search(query);
36                         int hitCount = hits.length();
37                         Document doc;
38                         for (int i = 0; (i < hitCount && i < 10); i++) {
39                                 doc = hits.doc(i);
40                                 for (int j = 0; j < doc.getValues("c").length; j++) {
41                                         System.out.println(doc.getValues("c")[j]);
42                                 }
43                         }
44                 } catch (IOException e) {
45                         e.printStackTrace();
46                 }
47                 return hits;
48         }
49
50         public Hits getIncludeInfo(String ident) {
51                 Hits hits = null;
52                 try {
53                         // Query query = QueryParser.parse(ident, "f", new StandardAnalyzer());
54                         Query query = new TermQuery(new Term("i", ident));
55                         hits = fSearcher.search(query);
56                         int hitCount = hits.length();
57                         Document doc;
58                         for (int i = 0; (i < hitCount && i < 10); i++) {
59                                 doc = hits.doc(i);
60                                 for (int j = 0; j < doc.getValues("i").length; j++) {
61                                         System.out.println(doc.getValues("i")[j]);
62                                 }
63                         }
64                 } catch (IOException e) {
65                         e.printStackTrace();
66                 }
67                 return hits;
68         }
69
70         public Hits getFunctionInfo(String ident) {
71                 Hits hits = null;
72                 try {
73                         // Query query = QueryParser.parse(ident, "f", new StandardAnalyzer());
74                         Query query = new TermQuery(new Term("f", ident));
75                         hits = fSearcher.search(query);
76                         int hitCount = hits.length();
77                         Document doc;
78                         for (int i = 0; (i < hitCount && i < 10); i++) {
79                                 doc = hits.doc(i);
80                                 for (int j = 0; j < doc.getValues("f").length; j++) {
81                                         System.out.println(doc.getValues("f")[j]);
82                                 }
83                         }
84                 } catch (IOException e) {
85                         e.printStackTrace();
86                 }
87                 return hits;
88         }
89
90         public Hits getMethodInfo(String ident) {
91                 Hits hits = null;
92                 try {
93                         // Query query = QueryParser.parse(ident, "m", new StandardAnalyzer());
94                         Query query = new TermQuery(new Term("m", ident));
95                         hits = fSearcher.search(query);
96                         int hitCount = hits.length();
97                         Document doc;
98                         for (int i = 0; (i < hitCount && i < 10); i++) {
99                                 doc = hits.doc(i);
100                                 for (int j = 0; j < doc.getValues("m").length; j++) {
101                                         System.out.println(doc.getValues("m")[j]);
102                                 }
103                         }
104                 } catch (IOException e) {
105                         e.printStackTrace();
106                 }
107                 return hits;
108         }
109
110         public Hits getAttributeInfo(String ident) {
111                 Hits hits = null;
112                 try {
113                         // Query query = QueryParser.parse(ident, "m", new StandardAnalyzer());
114                         Query query = new TermQuery(new Term("a", ident));
115                         hits = fSearcher.search(query);
116                         int hitCount = hits.length();
117                         Document doc;
118                         for (int i = 0; (i < hitCount && i < 10); i++) {
119                                 doc = hits.doc(i);
120                                 for (int j = 0; j < doc.getValues("a").length; j++) {
121                                         System.out.println(doc.getValues("a")[j]);
122                                 }
123                         }
124                 } catch (IOException e) {
125                         e.printStackTrace();
126                 }
127                 return hits;
128         }
129
130         public List getAttributePrefix(String prefix) {
131                 Hits hits = null;
132                 ArrayList list = new ArrayList();
133                 try {
134                         Query query = new PrefixQuery(new Term("a", prefix));
135                         hits = fSearcher.search(query);
136                         int hitCount = hits.length();
137                         int len = prefix.length();
138                         Document doc;
139                         for (int i = 0; (i < hitCount && i < 10); i++) {
140                                 doc = hits.doc(i);
141                                 for (int j = 0; j < doc.getValues("a").length; j++) {
142                                         if (prefix.equals(doc.getValues("a")[j].substring(0, len))) {
143                                                 list.add(doc.getValues("a")[j]);
144                                         }
145                                 }
146                         }
147                 } catch (IOException e) {
148                         e.printStackTrace();
149                 }
150                 System.out.println(list.toString());
151                 return list;
152         }
153
154         public List getPrefixes(String path, String fld, String prefix) {
155                 ArrayList list = new ArrayList();
156                 try {
157                         IndexReader reader = IndexReader.open(path);
158                         Term target = new Term(fld, prefix);
159                         int len = prefix.length();
160                         TermEnum tenum = reader.terms(target);
161                         Term term;
162                         do {
163                                 term = tenum.term();
164                                 if (term == null || !term.field().equals(fld) || !prefix.equals(term.text().substring(0, len))) {
165                                         break;
166                                 }
167                                 list.add(term.text());
168                         } while (tenum.next());
169
170                 } catch (IOException e) {
171                         e.printStackTrace();
172                 }
173                 System.out.println(list.toString());
174                 return list;
175         }
176 }