Prepared better HEREDOC support; see comment for bug #1319276
[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.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;
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.imports != null) {
32                                 ImportReference imp;
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"));
39                                 }
40                         }
41                         if (computedUnit.types != null) {
42                                 Object obj;
43                                 MethodDeclaration m;
44                                 TypeDeclaration c;
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;
55                                                 // add the class-name
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"));
60                                                 if (c.fields!=null) {
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"));
65                                                         }
66                                                 }
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"));
72                                                         }
73                                                 }
74                                         }
75                                 }
76                         }
77                         fWriter.addDocument(doc);
78                         return true;
79                 } catch (IOException e) {
80                         e.printStackTrace();
81                 }
82                 return false;
83         }
84
85         public void close() {
86
87                 try {
88                         fWriter.optimize();
89                         fWriter.close();
90                 } catch (IOException e) {
91                         e.printStackTrace();
92                 }
93
94         }
95
96 }