1 package net.sourceforge.phpeclipse.builder;
 
   3 import java.io.BufferedInputStream;
 
   4 import java.io.BufferedReader;
 
   5 import java.io.FileNotFoundException;
 
   6 import java.io.FileReader;
 
   7 import java.io.FileWriter;
 
   8 import java.io.IOException;
 
   9 import java.io.InputStream;
 
  10 import java.util.ArrayList;
 
  11 import java.util.Collection;
 
  12 import java.util.Comparator;
 
  13 import java.util.HashMap;
 
  14 import java.util.Iterator;
 
  15 import java.util.List;
 
  17 import java.util.SortedMap;
 
  18 import java.util.StringTokenizer;
 
  19 import java.util.TreeMap;
 
  21 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
 
  22 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
 
  23 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
 
  24 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
 
  25 import net.sourceforge.phpdt.internal.compiler.util.Util;
 
  26 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  27 import net.sourceforge.phpeclipse.obfuscator.PHPIdentifier;
 
  29 import org.eclipse.core.resources.IFile;
 
  30 import org.eclipse.core.runtime.CoreException;
 
  31 import org.eclipse.core.runtime.IStatus;
 
  34  * Manages the identifer index information for a specific project
 
  37 public class IdentifierIndexManager {
 
  38   public class LineCreator implements ITerminalSymbols {
 
  39     private Scanner fScanner;
 
  43     public LineCreator() {
 
  44       fScanner = new Scanner(true, false, false, false, true, null, null);
 
  48      * Add the information of the current identifier to the line
 
  50      * @param typeOfIdentifier
 
  51      *          the type of the identifier ('c'lass, 'd'efine, 'f'unction, 'm'ethod(class), 'v'ariable(class) 'g'lobal variable)
 
  55      *          Buffer for the current index line
 
  57      *          the offset of the PHPdoc comment if available
 
  59      *          the length of the PHPdoc comment if available
 
  61     private void addIdentifierInformation(char typeOfIdentifier, char[] identifier, StringBuffer line, int phpdocOffset,
 
  64       line.append(typeOfIdentifier);
 
  65       line.append(identifier);
 
  66       line.append("\to"); // Offset
 
  67       line.append(fScanner.getCurrentTokenStartPosition());
 
  68       if (phpdocOffset >= 0) {
 
  69         line.append("\tp"); // phpdoc offset
 
  70         line.append(phpdocOffset);
 
  71         line.append("\tl"); // phpdoc length
 
  72         line.append(phpdocLength);
 
  76     private void addClassVariableInformation(char typeOfIdentifier, char[] identifier, StringBuffer line, int phpdocOffset,
 
  79       line.append(typeOfIdentifier);
 
  80       line.append(identifier);
 
  81       line.append("\to"); // Offset
 
  82       // we don't store the '$' in the index for class variables:
 
  83       line.append(fScanner.getCurrentTokenStartPosition() + 1);
 
  84       if (phpdocOffset >= 0) {
 
  85         line.append("\tp"); // phpdoc offset
 
  86         line.append(phpdocOffset);
 
  87         line.append("\tl"); // phpdoc length
 
  88         line.append(phpdocLength);
 
  93      * Get the next token from input
 
  95     private void getNextToken() {
 
  97         fToken = fScanner.getNextToken();
 
  99           int currentEndPosition = fScanner.getCurrentTokenEndPosition();
 
 100           int currentStartPosition = fScanner.getCurrentTokenStartPosition();
 
 101           System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
 
 102           System.out.println(fScanner.toStringAction(fToken));
 
 105       } catch (InvalidInputException e) {
 
 107         //        e.printStackTrace();
 
 109       fToken = TokenNameERROR;
 
 112     private void parseDeclarations(char[] parent, StringBuffer buf, boolean goBack) {
 
 114       char[] classVariable;
 
 116       boolean hasModifiers = false;
 
 117       int phpdocOffset = -1;
 
 118       int phpdocLength = -1;
 
 120         while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
 
 122           hasModifiers = false;
 
 123           if (fToken == TokenNameCOMMENT_PHPDOC) {
 
 124             phpdocOffset = fScanner.getCurrentTokenStartPosition();
 
 125             phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1;
 
 127             while (fToken == TokenNamestatic || fToken == TokenNamefinal || fToken == TokenNamepublic
 
 128                 || fToken == TokenNameprotected || fToken == TokenNameprivate || fToken == TokenNameabstract) {
 
 132             if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
 
 136           if (fToken == TokenNamefunction) {
 
 138             if (fToken == TokenNameAND) {
 
 141             if (fToken == TokenNameIdentifier) {
 
 142               ident = fScanner.getCurrentIdentifierSource();
 
 143               if (parent != null && equalCharArrays(parent, ident)) {
 
 144                 // constructor function
 
 145                 addIdentifierInformation('k', ident, buf, phpdocOffset, phpdocLength);
 
 147                 if (parent != null) {
 
 148                   // class method function
 
 149                   addIdentifierInformation('m', ident, buf, phpdocOffset, phpdocLength);
 
 151                   // nested function ?!
 
 152                   addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength);
 
 156               parseDeclarations(null, buf, true);
 
 158           } else if (fToken == TokenNameclass || fToken == TokenNameinterface) {
 
 160             if (fToken == TokenNameIdentifier) {
 
 161               ident = fScanner.getCurrentIdentifierSource();
 
 162               addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
 
 164               //skip tokens for classname, extends and others until we have
 
 166               while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
 
 169               parseDeclarations(ident, buf, true);
 
 171           } else if (fToken == TokenNamevar || hasModifiers || fToken == TokenNamestatic || fToken == TokenNamefinal
 
 172               || fToken == TokenNamepublic || fToken == TokenNameprotected || fToken == TokenNameprivate) {
 
 173             while (fToken == TokenNamevar || fToken == TokenNamestatic || fToken == TokenNamefinal || fToken == TokenNamepublic
 
 174                 || fToken == TokenNameprotected || fToken == TokenNameprivate) {
 
 177             if (fToken == TokenNameVariable) {
 
 178               ident = fScanner.getCurrentIdentifierSource();
 
 179               classVariable = new char[ident.length - 1];
 
 180               System.arraycopy(ident, 1, classVariable, 0, ident.length - 1);
 
 181               addClassVariableInformation('v', classVariable, buf, phpdocOffset, phpdocLength);
 
 184           } else if (!hasModifiers && fToken == TokenNameIdentifier) {
 
 185             ident = fScanner.getCurrentIdentifierSource();
 
 187             if (ident.length == 6 && ident[0] == 'd' && ident[1] == 'e' && ident[2] == 'f' && ident[3] == 'i' && ident[4] == 'n'
 
 188                 && ident[5] == 'e') {
 
 189               if (fToken == TokenNameLPAREN) {
 
 191                 if (fToken == TokenNameStringDoubleQuote) {
 
 192                   ident = fScanner.getCurrentStringLiteralSource();
 
 193                   addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
 
 198           } else if (fToken == TokenNameglobal) {
 
 200             while (fToken != TokenNameEOF && fToken != TokenNameERROR && fToken != TokenNameSEMICOLON && fToken != TokenNameLBRACE
 
 201                 && fToken != TokenNameRBRACE) {
 
 203               if (fToken == TokenNameVariable) {
 
 204                 ident = fScanner.getCurrentIdentifierSource();
 
 205                 addIdentifierInformation('g', ident, buf, phpdocOffset, phpdocLength);
 
 208           } else if (fToken == TokenNameLBRACE) {
 
 211           } else if (fToken == TokenNameRBRACE) {
 
 214             if (counter == 0 && goBack) {
 
 221       } catch (SyntaxError e) {
 
 222         // TODO Auto-generated catch block
 
 227     synchronized public void parseIdentifiers(char[] charArray, StringBuffer buf) {
 
 231       boolean hasModifiers = false;
 
 232       int phpdocOffset = -1;
 
 233       int phpdocLength = -1;
 
 234       fScanner.setSource(charArray);
 
 235       fScanner.setPHPMode(false);
 
 236       fToken = TokenNameEOF;
 
 239         while (fToken != TokenNameEOF) { // && fToken != TokenNameERROR) {
 
 241           hasModifiers = false;
 
 242           if (fToken == TokenNameCOMMENT_PHPDOC) {
 
 243             phpdocOffset = fScanner.getCurrentTokenStartPosition();
 
 244             phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1;
 
 246             while (fToken == TokenNamestatic || fToken == TokenNamefinal || fToken == TokenNamepublic
 
 247                 || fToken == TokenNameprotected || fToken == TokenNameprivate || fToken == TokenNameabstract) {
 
 251             if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
 
 255           if (fToken == TokenNamefunction) {
 
 257             if (fToken == TokenNameAND) {
 
 260             if (fToken == TokenNameIdentifier) {
 
 261               ident = fScanner.getCurrentIdentifierSource();
 
 262               addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength);
 
 264               parseDeclarations(null, buf, true);
 
 266           } else if (fToken == TokenNameclass || fToken == TokenNameinterface) {
 
 268             if (fToken == TokenNameIdentifier) {
 
 269               ident = fScanner.getCurrentIdentifierSource();
 
 270               addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
 
 272               //skip fTokens for classname, extends and others until we have
 
 274               while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
 
 277               parseDeclarations(ident, buf, true);
 
 279           } else if (fToken == TokenNameVariable) {
 
 281             ident = fScanner.getCurrentIdentifierSource();
 
 282             addIdentifierInformation('g', ident, buf, phpdocOffset, phpdocLength);
 
 284           } else if (!hasModifiers && fToken == TokenNameIdentifier) {
 
 285             ident = fScanner.getCurrentIdentifierSource();
 
 287             if (ident.length == 6 && ident[0] == 'd' && ident[1] == 'e' && ident[2] == 'f' && ident[3] == 'i' && ident[4] == 'n'
 
 288                 && ident[5] == 'e') {
 
 289               if (fToken == TokenNameLPAREN) {
 
 291                 if (fToken == TokenNameStringDoubleQuote) {
 
 292                   ident = fScanner.getCurrentStringLiteralSource();
 
 293                   addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
 
 302       } catch (SyntaxError e) {
 
 303         // TODO Auto-generated catch block
 
 309   class StringComparator implements Comparator {
 
 310     public int compare(Object o1, Object o2) {
 
 311       String s1 = (String) o1;
 
 312       String s2 = (String) o2;
 
 313       return s1.compareTo(s2);
 
 314       //        return s1.toUpperCase().compareTo(s2.toUpperCase());
 
 317     public boolean equals(Object o) {
 
 318       String s = (String) o;
 
 319       return compare(this, o) == 0;
 
 323   private HashMap fFileMap;
 
 325   private String fFilename;
 
 327   private TreeMap fIndentifierMap;
 
 329   public IdentifierIndexManager(String filename) {
 
 330     fFilename = filename;
 
 336    * Check if 2 char arrays are equal
 
 342   private static boolean equalCharArrays(char[] a, char[] b) {
 
 343     if (a.length != b.length) {
 
 346     for (int i = 0; i < b.length; i++) {
 
 354   public LineCreator createLineCreator() {
 
 355     return new LineCreator();
 
 359    * Add the information for a given IFile resource
 
 362   public void addFile(IFile fileToParse) {
 
 363     //    InputStream iStream;
 
 364     LineCreator lineCreator = createLineCreator();
 
 366       addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getProjectRelativePath().toString(),
 
 368     } catch (CoreException e1) {
 
 369       // TODO Auto-generated catch block
 
 370       e1.printStackTrace();
 
 377    * @throws CoreException
 
 379   public void addInputStream(InputStream stream, String filePath, LineCreator lineCreator) throws CoreException {
 
 381       StringBuffer lineBuffer = new StringBuffer();
 
 382       lineBuffer.append(filePath);
 
 383       int lineLength = lineBuffer.length();
 
 384       lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1, null), lineBuffer);
 
 385       //      if (lineLength != lineBuffer.length()) {
 
 386       // always add the file for Open Include Action
 
 387       addLine(lineBuffer.toString());
 
 389     } catch (IOException e) {
 
 393         if (stream != null) {
 
 396       } catch (IOException e) {
 
 402    * Adds a line of the index file for function, class, class-method and class-variable names
 
 406   private void addLine(String line) {
 
 407     StringTokenizer tokenizer;
 
 408     String phpFileName = null;
 
 410     String identifier = null;
 
 411     String classname = null;
 
 412     String offset = null;
 
 413     PHPIdentifierLocation phpIdentifier = null;
 
 414     boolean tokenExists = false;
 
 415     tokenizer = new StringTokenizer(line, "\t");
 
 416     // first token contains the filename:
 
 417     if (tokenizer.hasMoreTokens()) {
 
 418       phpFileName = tokenizer.nextToken();
 
 419       //System.out.println(token);
 
 423     // all the other tokens are identifiers:
 
 424     while (tokenizer.hasMoreTokens()) {
 
 425       token = tokenizer.nextToken();
 
 426       //System.out.println(token);
 
 427       switch (token.charAt(0)) {
 
 430         identifier = token.substring(1);
 
 431         classname = identifier;
 
 432         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName);
 
 436         identifier = token.substring(1);
 
 437         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
 
 441         identifier = token.substring(1);
 
 442         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
 
 446         identifier = token.substring(1);
 
 447         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
 
 450         // constructor function name
 
 451         identifier = token.substring(1);
 
 452         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CONSTRUCTOR, phpFileName);
 
 455         //method inside a class
 
 456         identifier = token.substring(1);
 
 457         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
 
 460         // variable inside a class
 
 461         identifier = token.substring(1);
 
 462         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname);
 
 465         // offset information
 
 467         if (phpIdentifier != null) {
 
 468           offset = token.substring(1);
 
 469           phpIdentifier.setOffset(Integer.parseInt(offset));
 
 473         // PHPdoc offset information
 
 475         if (phpIdentifier != null) {
 
 476           offset = token.substring(1);
 
 477           phpIdentifier.setPHPDocOffset(Integer.parseInt(offset));
 
 481         // PHPdoc length information
 
 483         if (phpIdentifier != null) {
 
 484           offset = token.substring(1);
 
 485           phpIdentifier.setPHPDocLength(Integer.parseInt(offset));
 
 489         PHPeclipsePlugin.log(IStatus.ERROR, "Unknown token character in IdentifierIndexManager: " + token.charAt(0));
 
 491         phpIdentifier = null;
 
 494       if (identifier != null && phpIdentifier != null) {
 
 496         ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
 
 498           list = new ArrayList();
 
 499           list.add(phpIdentifier);
 
 500           fIndentifierMap.put(identifier, list);
 
 502           boolean flag = false;
 
 503           for (int i = 0; i < list.size(); i++) {
 
 504             if (list.get(i).equals(phpIdentifier)) {
 
 510             list.add(phpIdentifier);
 
 515     //    if (tokenExists) {
 
 516     fFileMap.put(phpFileName, line);
 
 521    * Change the information for a given IFile resource
 
 524   public void changeFile(IFile fileToParse) {
 
 525     removeFile(fileToParse);
 
 526     addFile(fileToParse);
 
 530    * Get a list of all PHPIdentifierLocation object's associated with an identifier
 
 535   public List getLocations(String identifier) {
 
 536     return (List) fIndentifierMap.get(identifier);
 
 540    * Initialize (i.e. clear) the current index information
 
 543   public void initialize() {
 
 544     fIndentifierMap = new TreeMap(new StringComparator());
 
 545     fFileMap = new HashMap();
 
 548   private void readFile() {
 
 549     FileReader fileReader;
 
 551       fileReader = new FileReader(fFilename);
 
 552       BufferedReader bufferedReader = new BufferedReader(fileReader);
 
 554       while (bufferedReader.ready()) {
 
 555         // all entries for one file are in a line
 
 556         // separated by tabs !
 
 557         line = bufferedReader.readLine();
 
 561     } catch (FileNotFoundException e) {
 
 563       // TODO DialogBox which asks the user if she/he likes to build new index?
 
 564     } catch (IOException e) {
 
 565       // TODO Auto-generated catch block
 
 571    * Remove the information for a given IFile resource
 
 574   public void removeFile(IFile fileToParse) {
 
 575     //    String line = (String)
 
 576     // fFileMap.get(fileToParse.getLocation().toString());
 
 577     String line = (String) fFileMap.get(fileToParse.getProjectRelativePath().toString());
 
 584    * Removes a line of the index file for function, class, class-method and class-variable names
 
 588   private void removeLine(String line) {
 
 589     StringTokenizer tokenizer;
 
 590     String phpFileName = null;
 
 592     String identifier = null;
 
 593     String classname = null;
 
 594     PHPIdentifier phpIdentifier = null;
 
 595     boolean tokenExists = false;
 
 596     tokenizer = new StringTokenizer(line, "\t");
 
 597     // first token contains the filename:
 
 598     if (tokenizer.hasMoreTokens()) {
 
 599       phpFileName = tokenizer.nextToken();
 
 600       //System.out.println(token);
 
 605     // all the other tokens are identifiers:
 
 606     while (tokenizer.hasMoreTokens()) {
 
 607       token = tokenizer.nextToken();
 
 608       //System.out.println(token);
 
 609       switch (token.charAt(0)) {
 
 612         identifier = token.substring(1);
 
 613         classname = identifier;
 
 614         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName);
 
 618         identifier = token.substring(1);
 
 619         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
 
 623         identifier = token.substring(1);
 
 624         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
 
 628         identifier = token.substring(1);
 
 629         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
 
 632         // constructor function name
 
 633         identifier = token.substring(1);
 
 634         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CONSTRUCTOR, phpFileName);
 
 637         //method inside a class
 
 638         identifier = token.substring(1);
 
 639         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
 
 642         // offset information
 
 646         // PHPdoc offset information
 
 650         // PHPdoc length information
 
 654         // variable inside a class
 
 655         identifier = token.substring(1);
 
 656         phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname);
 
 659         PHPeclipsePlugin.log(IStatus.ERROR, "Unknown token character in IdentifierIndexManager: " + token.charAt(0));
 
 661         phpIdentifier = null;
 
 664       if (identifier != null && phpIdentifier != null) {
 
 665         ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
 
 668           for (int i = 0; i < list.size(); i++) {
 
 669             if (list.get(i).equals(phpIdentifier)) {
 
 674           if (list.size() == 0) {
 
 675             fIndentifierMap.remove(identifier);
 
 680     fFileMap.remove(phpFileName);
 
 684    * Save the current index information in the projects index file
 
 687   public void writeFile() {
 
 688     FileWriter fileWriter;
 
 690       fileWriter = new FileWriter(fFilename);
 
 692       Collection collection = fFileMap.values();
 
 693       Iterator iterator = collection.iterator();
 
 694       while (iterator.hasNext()) {
 
 695         line = (String) iterator.next();
 
 696         fileWriter.write(line + '\n');
 
 699     } catch (FileNotFoundException e) {
 
 700       // ignore exception; project is deleted by user
 
 701     } catch (IOException e) {
 
 702       // TODO Auto-generated catch block
 
 712   public SortedMap getIdentifierMap() {
 
 713     return fIndentifierMap;
 
 716   synchronized public List getFileList(String filePattern) {
 
 717     Set set = fFileMap.keySet();
 
 721     Iterator iter = set.iterator();
 
 722     ArrayList list = new ArrayList();
 
 725     while (iter.hasNext()) {
 
 726       fileName = (String) iter.next();
 
 727       if ((index = fileName.indexOf(filePattern)) != -1 && fileName.length() == (index + filePattern.length())) {