1 package net.sourceforge.phpdt.core.tests.lucene;
3 import java.io.IOException;
5 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
6 import net.sourceforge.phpeclipse.internal.compiler.ast.ImportReference;
7 import net.sourceforge.phpeclipse.internal.compiler.ast.MethodDeclaration;
8 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
10 import org.apache.lucene.analysis.standard.StandardAnalyzer;
11 import org.apache.lucene.document.Document;
12 import org.apache.lucene.document.Field;
13 import org.apache.lucene.index.IndexWriter;
14 import org.eclipse.core.resources.IFile;
16 public class PHPWriter {
17 private IndexWriter fWriter = null;
19 public PHPWriter(String indexPath, boolean create) throws IOException {
20 // Make a lucene writer and create new Lucene index with arg3 = true
21 fWriter = new IndexWriter(indexPath, new StandardAnalyzer(), create);
24 public boolean addDocument(CompilationUnitDeclaration computedUnit, IFile file) {
25 Document doc = new Document();
28 doc.add(Field.Keyword("filename", file.getName()));
29 doc.add(Field.Keyword("path", file.getProjectRelativePath().toString()));
31 if (computedUnit.imports != null) {
33 for (int i=0; i<computedUnit.imports.length; i++) {
34 // add the php include
35 imp = computedUnit.imports[i];
36 String incl = new String(imp.includeSource);
37 doc.add(Field.Keyword("i", incl));
38 doc.add(Field.UnIndexed(incl, "include meta-info"));
41 if (computedUnit.types != null) {
45 for (int i=0; i<computedUnit.types.size(); i++) {
46 obj = computedUnit.types.get(i);
47 if (obj instanceof MethodDeclaration) {
48 m = (MethodDeclaration) obj;
49 // add the php function name
50 String function = new String(m.selector);
51 doc.add(Field.Keyword("f", function));
52 doc.add(Field.UnIndexed(function, "function meta-info"));
53 } else if (obj instanceof TypeDeclaration) {
54 c = (TypeDeclaration) obj;
56 String clazz = new String(c.name);
57 // new Field("c", clazz, true, false, true, false);
58 doc.add(Field.Keyword("c", clazz));
59 doc.add(Field.UnIndexed(clazz, "class meta-info"));
61 for (int j = 0; j < c.fields.length; j++) {
62 String attribute = new String(c.fields[j].name);
63 doc.add(Field.Keyword("a", attribute));
64 doc.add(Field.UnIndexed(attribute, "field meta-data"));
67 if (c.methods!=null) {
68 for (int j = 0; j < c.methods.length; j++) {
69 String function = new String(c.methods[j].selector);
70 doc.add(Field.Keyword("m", function));
71 doc.add(Field.UnIndexed(function, "method meta-data"));
77 fWriter.addDocument(doc);
79 } catch (IOException e) {
90 } catch (IOException e) {