package net.sourceforge.phpdt.core.tests.lucene; import java.io.IOException; import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration; import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration; import net.sourceforge.phpeclipse.internal.compiler.ast.MethodDeclaration; import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.eclipse.core.resources.IFile; public class PHPWriter { private IndexWriter fWriter = null; public PHPWriter(String indexPath, boolean create) throws IOException { // Make a lucene writer and create new Lucene index with arg3 = true fWriter = new IndexWriter(indexPath, new StandardAnalyzer(), create); } public boolean addDocument(CompilationUnitDeclaration computedUnit, IFile file) { Document doc = new Document(); try { if (file != null) { doc.add(Field.Keyword("filename", file.getName())); doc.add(Field.Keyword("path", file.getProjectRelativePath().toString())); } if (computedUnit.types != null) { Object obj; MethodDeclaration m; TypeDeclaration c; for (int i = computedUnit.types.size(); --i >= 0;) { obj = computedUnit.types.get(i); if (obj instanceof MethodDeclaration) { m = (MethodDeclaration) obj; // add the php function name String function = new String(m.selector); doc.add(Field.Keyword("f", function)); doc.add(Field.UnIndexed(function, "class meta-info")); } else if (obj instanceof TypeDeclaration) { c = (TypeDeclaration) obj; // add the class-name String clazz = new String(c.name); // new Field("c", clazz, true, false, true, false); doc.add(Field.Keyword("c", clazz)); doc.add(Field.UnIndexed(clazz, "class meta-info")); if (c.fields!=null) { for (int j = 0; j < c.fields.length; j++) { String attribute = new String(c.fields[j].name); doc.add(Field.Keyword("a", attribute)); doc.add(Field.UnIndexed(attribute, "field meta-data")); } } if (c.methods!=null) { for (int j = 0; j < c.methods.length; j++) { String function = new String(c.methods[j].selector); doc.add(Field.Keyword("m", function)); doc.add(Field.UnIndexed(function, "method meta-data")); } } } } } fWriter.addDocument(doc); return true; } catch (IOException e) { e.printStackTrace(); } return false; } public void close() { try { fWriter.optimize(); fWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }