1 package net.sourceforge.phpdt.core.tests.lucene;
3 import java.io.IOException;
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;
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;
17 public class PHPWriter {
18 private IndexWriter fWriter = null;
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);
25 public boolean addDocument(CompilationUnitDeclaration computedUnit, IFile file) {
26 Document doc = new Document();
29 doc.add(Field.Keyword("filename", file.getName()));
30 doc.add(Field.Keyword("path", file.getProjectRelativePath().toString()));
32 if (computedUnit.imports != null) {
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"));
42 if (computedUnit.types != null) {
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;
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"));
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"));
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"));
78 fWriter.addDocument(doc);
80 } catch (IOException e) {
91 } catch (IOException e) {