d370d3bcd8c75562b20da5f34072168409286289
[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.ImportReference;
8 import net.sourceforge.phpeclipse.internal.compiler.ast.MethodDeclaration;
9 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
10
11 import org.apache.lucene.analysis.standard.StandardAnalyzer;
12 import org.apache.lucene.document.Document;
13 import org.apache.lucene.document.Field;
14 import org.apache.lucene.index.IndexWriter;
15 import org.eclipse.core.resources.IFile;
16
17 public class PHPWriter {
18         private IndexWriter fWriter = null;
19
20 public PHPWriter(String indexPath, boolean create) throws IOException {
21                 // Make a lucene writer and create new Lucene index with arg3 = true
22                 fWriter = new IndexWriter(indexPath, new StandardAnalyzer(), create);
23         }
24
25         public boolean addDocument(CompilationUnitDeclaration computedUnit, IFile file) {
26                 Document doc = new Document();
27                 try {
28                         if (file != null) {
29                                 doc.add(Field.Keyword("filename", file.getName()));
30                                 doc.add(Field.Keyword("path", file.getProjectRelativePath().toString()));
31                         }
32                         if (computedUnit.imports != null) {
33                                 ImportReference imp;
34                                 for (int i=0; i<computedUnit.imports.length; i++) {
35                                         // add the php include
36                                         imp = computedUnit.imports[i];
37                                         String incl = new String(imp.includeSource);
38                                         doc.add(Field.Keyword("i", incl));
39                                         doc.add(Field.UnIndexed(incl, "include meta-info"));
40                                 }
41                         }
42                         if (computedUnit.types != null) {
43                                 Object obj;
44                                 MethodDeclaration m;
45                                 TypeDeclaration c;
46                                 for (int i=0; i<computedUnit.types.size(); i++) {
47                                         obj = computedUnit.types.get(i);
48                                         if (obj instanceof MethodDeclaration) {
49                                                 m = (MethodDeclaration) obj;
50                                                 // add the php function name
51                                                 String function = new String(m.selector);
52                                                 doc.add(Field.Keyword("f", function));
53                                                 doc.add(Field.UnIndexed(function, "class meta-info"));
54                                         } else if (obj instanceof TypeDeclaration) {
55                                                 c = (TypeDeclaration) obj;
56                                                 // add the class-name
57                                                 String clazz = new String(c.name);
58 //                                              new Field("c", clazz, true, false, true, false);
59                                                 doc.add(Field.Keyword("c", clazz));
60                                                 doc.add(Field.UnIndexed(clazz, "class meta-info"));
61                                                 if (c.fields!=null) {
62                                                         for (int j = 0; j < c.fields.length; j++) {
63                                                                 String attribute = new String(c.fields[j].name);
64                                                                 doc.add(Field.Keyword("a", attribute));
65                                                                 doc.add(Field.UnIndexed(attribute, "field meta-data"));
66                                                         }
67                                                 }
68                                                 if (c.methods!=null) {
69                                                         for (int j = 0; j < c.methods.length; j++) {
70                                                                 String function = new String(c.methods[j].selector);
71                                                                 doc.add(Field.Keyword("m", function));
72                                                                 doc.add(Field.UnIndexed(function, "method meta-data"));
73                                                         }
74                                                 }
75                                         }
76                                 }
77                         }
78                         fWriter.addDocument(doc);
79                         return true;
80                 } catch (IOException e) {
81                         e.printStackTrace();
82                 }
83                 return false;
84         }
85
86         public void close() {
87
88                 try {
89                         fWriter.optimize();
90                         fWriter.close();
91                 } catch (IOException e) {
92                         e.printStackTrace();
93                 }
94
95         }
96
97 }