RC2 compatibility
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / builder / IdentifierIndexManager.java
index fbabccb..982d0ef 100644 (file)
@@ -12,17 +12,22 @@ import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 import java.util.SortedMap;
 import java.util.StringTokenizer;
 import java.util.TreeMap;
+
 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
 import net.sourceforge.phpdt.internal.compiler.util.Util;
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.obfuscator.PHPIdentifier;
+
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
 /**
  * Manages the identifer index information for a specific project
  *  
@@ -32,14 +37,14 @@ public class IdentifierIndexManager {
     private Scanner fScanner;
     private int fToken;
     public LineCreator() {
-      fScanner = new Scanner(true, false);
+      fScanner = new Scanner(true, false, false, false, true, null, null);
     }
     /**
      * Add the information of the current identifier to the line
      * 
      * @param typeOfIdentifier
      *            the type of the identifier ('c'lass, 'd'efine, 'f'unction,
-     *            'm'ethod, 'v'ariable)
+     *            'm'ethod(class), 'v'ariable(class) 'g'lobal variable)
      * @param identifier
      *            current identifier
      * @param line
@@ -79,6 +84,7 @@ public class IdentifierIndexManager {
         return;
       } catch (InvalidInputException e) {
         // ignore errors
+//        e.printStackTrace();
       }
       fToken = TokenNameERROR;
     }
@@ -164,7 +170,7 @@ public class IdentifierIndexManager {
                 ident[5]=='e') {
               if (fToken == TokenNameLPAREN) {
                 getNextToken();
-                if (fToken == TokenNameStringLiteral) {
+                if (fToken == TokenNameStringDoubleQuote) {
                   ident = fScanner.getCurrentStringLiteralSource();
                   addIdentifierInformation('d', ident, buf, phpdocOffset,
                       phpdocLength);
@@ -172,6 +178,19 @@ public class IdentifierIndexManager {
                 }
               }
             }
+          } else if (fToken == TokenNameglobal) {
+            // global variable
+            while (fToken != TokenNameEOF && fToken != TokenNameERROR && 
+                   fToken != TokenNameSEMICOLON &&
+                   fToken != TokenNameLBRACE &&
+                   fToken != TokenNameRBRACE ) {
+              getNextToken(); 
+              if (fToken == TokenNameVariable) {
+                ident = fScanner.getCurrentIdentifierSource();
+                addIdentifierInformation('g', ident, buf, phpdocOffset,
+                    phpdocLength);
+              }
+            }
           } else if (fToken == TokenNameLBRACE) {
             getNextToken();
             counter++;
@@ -190,7 +209,7 @@ public class IdentifierIndexManager {
         e.printStackTrace();
       }
     }
-    public void parseIdentifiers(char[] charArray, StringBuffer buf) {
+    synchronized public void parseIdentifiers(char[] charArray, StringBuffer buf) {
       char[] ident;
       String identifier;
       int counter = 0;
@@ -201,7 +220,7 @@ public class IdentifierIndexManager {
       fToken = TokenNameEOF;
       getNextToken();
       try {
-        while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
+        while (fToken != TokenNameEOF) { // && fToken != TokenNameERROR) {
           phpdocOffset = -1;
           if (fToken == TokenNameCOMMENT_PHPDOC) {
             phpdocOffset = fScanner.getCurrentTokenStartPosition();
@@ -239,6 +258,12 @@ public class IdentifierIndexManager {
               }
               parseDeclarations(ident, buf, true);
             }
+          } else if (fToken == TokenNameVariable) {
+            // global variable
+            ident = fScanner.getCurrentIdentifierSource();
+            addIdentifierInformation('g', ident, buf, phpdocOffset,
+                phpdocLength);
+            getNextToken(); 
           } else if (fToken == TokenNameIdentifier) {
             ident = fScanner.getCurrentIdentifierSource();
             getNextToken();
@@ -251,7 +276,7 @@ public class IdentifierIndexManager {
                 ident[5]=='e') {
               if (fToken == TokenNameLPAREN) {
                 getNextToken();
-                if (fToken == TokenNameStringLiteral) {
+                if (fToken == TokenNameStringDoubleQuote) {
                   ident = fScanner.getCurrentStringLiteralSource();
                   addIdentifierInformation('d', ident, buf, phpdocOffset,
                       phpdocLength);
@@ -307,51 +332,48 @@ public class IdentifierIndexManager {
     }
     return true;
   }
+  public LineCreator createLineCreator() {
+    return new LineCreator();
+  }
   /**
    * Add the information for a given IFile resource
    *  
    */
   public void addFile(IFile fileToParse) {
     //    InputStream iStream;
-    LineCreator lineCreator = new LineCreator();
+    LineCreator lineCreator = createLineCreator();
     try {
-      //      iStream = fileToParse.getContents();
-      //
-      //      StringBuffer buf = new StringBuffer();
-      //      int c0;
-      //      try {
-      //        while ((c0 = iStream.read()) != (-1)) {
-      //          buf.append((char) c0);
-      //        }
-      //      } catch (IOException e) {
-      //        return;
-      //      }
-      InputStream stream = null;
+      addInputStream(new BufferedInputStream(fileToParse.getContents()), fileToParse.getFullPath().toString(), lineCreator);
+    } catch (CoreException e1) {
+      // TODO Auto-generated catch block
+      e1.printStackTrace();
+    }
+  }
+  /**
+   * @param fileToParse
+   * @param lineCreator
+   * @throws CoreException
+   */
+  public void addInputStream(InputStream stream, String filePath, LineCreator lineCreator) throws CoreException {
+    try {
+      StringBuffer lineBuffer = new StringBuffer();
+      lineBuffer.append(filePath);
+      int lineLength = lineBuffer.length();
+      lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
+          null), lineBuffer);
+//      if (lineLength != lineBuffer.length()) {
+      // always add the file for Open Include Action  
+      addLine(lineBuffer.toString());
+//      }
+    }  catch (IOException e) {
+        e.printStackTrace();
+    } finally {
       try {
-        stream = new BufferedInputStream(fileToParse.getContents());
-        StringBuffer lineBuffer = new StringBuffer();
-        lineBuffer.append(fileToParse.getFullPath().toString());
-        int lineLength = lineBuffer.length();
-        // lineCreator.parseIdentifiers(buf.toString().toCharArray(),
-        // lineBuffer);
-        lineCreator.parseIdentifiers(Util.getInputStreamAsCharArray(stream, -1,
-            null), lineBuffer);
-        if (lineLength != lineBuffer.length()) {
-          addLine(lineBuffer.toString());
+        if (stream != null) {
+          stream.close();
         }
       } catch (IOException e) {
-        return;
-      } finally {
-        try {
-          if (stream != null) {
-            stream.close();
-          }
-        } catch (IOException e) {
-        }
       }
-    } catch (CoreException e1) {
-      // TODO Auto-generated catch block
-      e1.printStackTrace();
     }
   }
   /**
@@ -401,6 +423,12 @@ public class IdentifierIndexManager {
           phpIdentifier = new PHPIdentifierLocation(identifier,
               PHPIdentifier.FUNCTION, phpFileName);
           break;
+        case 'g' :
+          // global variable 
+          identifier = token.substring(1);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
+          break;
         case 'k' :
           // constructor function name
           identifier = token.substring(1);
@@ -444,6 +472,7 @@ public class IdentifierIndexManager {
           }
           break;
         default :
+          PHPeclipsePlugin.log(IStatus.ERROR, "Unknown token character in IdentifierIndexManager: "+token.charAt(0));
           identifier = null;
           phpIdentifier = null;
           classname = null;
@@ -469,9 +498,9 @@ public class IdentifierIndexManager {
         }
       }
     }
-    if (tokenExists) {
+//    if (tokenExists) {
       fFileMap.put(phpFileName, line);
-    }
+//    } 
   }
   /**
    * Change the information for a given IFile resource
@@ -553,7 +582,8 @@ public class IdentifierIndexManager {
       //System.out.println(token);
     } else {
       return;
-    }
+    } 
+    int offset = -1;
     // all the other tokens are identifiers:
     while (tokenizer.hasMoreTokens()) {
       token = tokenizer.nextToken();
@@ -578,6 +608,12 @@ public class IdentifierIndexManager {
           phpIdentifier = new PHPIdentifierLocation(identifier,
               PHPIdentifier.FUNCTION, phpFileName);
           break;
+        case 'g' :
+          // global variable  
+          identifier = token.substring(1);
+          phpIdentifier = new PHPIdentifierLocation(identifier,
+              PHPIdentifier.GLOBAL_VARIABLE, phpFileName);
+          break;
         case 'k' :
           // constructor function name
           identifier = token.substring(1);
@@ -590,6 +626,10 @@ public class IdentifierIndexManager {
           phpIdentifier = new PHPIdentifierLocation(identifier,
               PHPIdentifier.METHOD, phpFileName, classname);
           break;
+        case 'o' :
+          // offset information
+          identifier = null;
+          break;
         case 'v' :
           // variable inside a class
           identifier = token.substring(1);
@@ -597,6 +637,7 @@ public class IdentifierIndexManager {
               PHPIdentifier.VARIABLE, phpFileName, classname);
           break;
         default :
+          PHPeclipsePlugin.log(IStatus.ERROR, "Unknown token character in IdentifierIndexManager: "+token.charAt(0));
           identifier = null;
           phpIdentifier = null;
           classname = null;
@@ -650,4 +691,22 @@ public class IdentifierIndexManager {
   public SortedMap getIdentifierMap() {
     return fIndentifierMap;
   }
+
+  synchronized public List getFileList(String filePattern) {
+    Set set = fFileMap.keySet();
+    if (set.isEmpty()) {
+      return null;
+    }
+    Iterator iter = set.iterator();
+    ArrayList list = new ArrayList();
+    String fileName;
+    int index;
+    while(iter.hasNext()) {
+      fileName = (String) iter.next();
+      if ((index=fileName.indexOf(filePattern))!=-1 && fileName.length()==(index+filePattern.length())) {
+        list.add(fileName);
+      }
+    }
+    return list;
+  }
 }
\ No newline at end of file