bdd54d9770962964906271fde6b1c5e7347be8a4
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpdt / core / tests / lucene / PHPWriter.java
1 package net.sourceforge.phpdt.core.tests.lucene;
2
3 import java.io.IOException;
4
5 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
6 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
7 import net.sourceforge.phpeclipse.internal.compiler.ast.MethodDeclaration;
8 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
9
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;
15
16 public class PHPWriter {
17         private IndexWriter fWriter = null;
18
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);
22         }
23
24         public boolean addDocument(CompilationUnitDeclaration computedUnit, IFile file) {
25                 Document doc = new Document();
26                 try {
27                         if (file != null) {
28                                 doc.add(Field.Keyword("filename", file.getName()));
29                                 doc.add(Field.Keyword("path", file.getProjectRelativePath().toString()));
30                         }
31                         if (computedUnit.types != null) {
32                                 Object obj;
33                                 MethodDeclaration m;
34                                 TypeDeclaration c;
35                                 for (int i = computedUnit.types.size(); --i >= 0;) {
36                                         obj = computedUnit.types.get(i);
37                                         if (obj instanceof MethodDeclaration) {
38                                                 m = (MethodDeclaration) obj;
39                                                 // add the php function name
40                                                 String function = new String(m.selector);
41                                                 doc.add(Field.Keyword("f", function));
42                                                 doc.add(Field.UnIndexed(function, "class meta-info"));
43                                         } else if (obj instanceof TypeDeclaration) {
44                                                 c = (TypeDeclaration) obj;
45                                                 // add the class-name
46                                                 String clazz = new String(c.name);
47 //                                              new Field("c", clazz, true, false, true, false);
48                                                 doc.add(Field.Keyword("c", clazz));
49                                                 doc.add(Field.UnIndexed(clazz, "class meta-info"));
50                                                 if (c.fields!=null) {
51                                                         for (int j = 0; j < c.fields.length; j++) {
52                                                                 String attribute = new String(c.fields[j].name);
53                                                                 doc.add(Field.Keyword("a", attribute));
54                                                                 doc.add(Field.UnIndexed(attribute, "field meta-data"));
55                                                         }
56                                                 }
57                                                 if (c.methods!=null) {
58                                                         for (int j = 0; j < c.methods.length; j++) {
59                                                                 String function = new String(c.methods[j].selector);
60                                                                 doc.add(Field.Keyword("m", function));
61                                                                 doc.add(Field.UnIndexed(function, "method meta-data"));
62                                                         }
63                                                 }
64                                         }
65                                 }
66                         }
67                         fWriter.addDocument(doc);
68                         return true;
69                 } catch (IOException e) {
70                         e.printStackTrace();
71                 }
72                 return false;
73         }
74
75         public void close() {
76
77                 try {
78                         fWriter.optimize();
79                         fWriter.close();
80                 } catch (IOException e) {
81                         e.printStackTrace();
82                 }
83
84         }
85
86 }