*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / mover / obfuscator / PHPAnalyzer.java
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/mover/obfuscator/PHPAnalyzer.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/mover/obfuscator/PHPAnalyzer.java
new file mode 100644 (file)
index 0000000..bc7b049
--- /dev/null
@@ -0,0 +1,189 @@
+package net.sourceforge.phpeclipse.mover.obfuscator;
+
+/**
+ * Analyze the file with the PHP Scanner
+ */
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+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.phpeclipse.PHPeclipsePlugin;
+import net.sourceforge.phpeclipse.mover.DefaultMover;
+import net.sourceforge.phpeclipse.views.PHPConsole;
+
+import org.eclipse.jface.preference.IPreferenceStore;
+
+public class PHPAnalyzer extends DefaultMover implements ITerminalSymbols {
+
+  protected Scanner fScanner;
+  protected int fToken;
+
+  protected HashMap fIdentifierMap;
+
+  /**
+   * buffer, for obvious reasons access to this buffer must
+   * be synchronized
+   */
+  protected byte[] bytes = new byte[1024];
+  /**
+   * Creates a PHPAnalyzer
+   * @param console reports error to the PHPConsole
+   */
+  public PHPAnalyzer(PHPConsole console, Scanner scanner, HashMap identifierMap) {
+    super(console);
+    this.fScanner = scanner;
+    this.fIdentifierMap = identifierMap;
+  }
+
+  /**
+   * gets the next token from input
+   */
+  private void getNextToken() {
+
+    try {
+      fToken = fScanner.getNextToken();
+      if (Scanner.DEBUG) {
+        int currentEndPosition = fScanner.getCurrentTokenEndPosition();
+        int currentStartPosition = fScanner.getCurrentTokenStartPosition();
+
+        System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
+        System.out.println(fScanner.toStringAction(fToken));
+      }
+      return;
+    } catch (InvalidInputException e) {
+
+    }
+    fToken = TokenNameERROR;
+  }
+
+  private void parseIdentifiers(boolean goBack) {
+    char[] ident;
+    String identifier;
+    PHPIdentifier value;
+    int counter = 0;
+
+    IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
+    try {
+      while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
+        if (fToken == TokenNameVariable) {
+          identifier = new String(fScanner.getCurrentIdentifierSource());
+          value = (PHPIdentifier) fIdentifierMap.get(identifier);
+          if (value == null) {
+            fIdentifierMap.put(identifier, new PHPIdentifier(null, PHPIdentifier.VARIABLE));
+          }
+          getNextToken();
+          //        } else if (fToken == TokenNamefunction) {
+          //          getNextToken();
+          //          if (fToken == TokenNameAND) {
+          //            getNextToken();
+          //          }
+          //          if (fToken == TokenNameIdentifier) {
+          //            ident = fScanner.getCurrentIdentifierSource();
+          //            outlineInfo.addVariable(new String(ident));
+          //            temp = new PHPFunctionDeclaration(current, new String(ident), fScanner.getCurrentTokenStartPosition());
+          //            current.add(temp);
+          //            getNextToken();
+          //            parseDeclarations(outlineInfo, temp, true);
+          //          }
+          //        } else if (fToken == TokenNameclass) {
+          //          getNextToken();
+          //          if (fToken == TokenNameIdentifier) {
+          //            ident = fScanner.getCurrentIdentifierSource();
+          //            outlineInfo.addVariable(new String(ident));
+          //            temp = new PHPClassDeclaration(current, new String(ident), fScanner.getCurrentTokenStartPosition());
+          //            current.add(temp);
+          //            getNextToken();
+          //
+          //            //skip fTokens for classname, extends and others until we have the opening '{'
+          //            while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
+          //              getNextToken();
+          //            }
+          //            parseDeclarations(outlineInfo, temp, true);
+          //            //        stack.pop();
+          //          }
+        } else if (fToken == TokenNameStringLiteral) {
+          char currentCharacter;
+          int i = fScanner.startPosition;
+          ArrayList varList = new ArrayList();
+
+          while (i < fScanner.currentPosition) {
+            currentCharacter = fScanner.source[i++];
+            if (currentCharacter == '$' && fScanner.source[i - 2] != '\\') {
+              StringBuffer varName = new StringBuffer();
+              varName.append("$");
+              while (i < fScanner.currentPosition) {
+                currentCharacter = fScanner.source[i++];
+                if (!Scanner.isPHPIdentifierPart(currentCharacter)) {
+                  break; // while loop
+                }
+                varName.append(currentCharacter);
+              }
+              varList.add(varName.toString());
+            }
+          }
+
+          for (i = 0; i < varList.size(); i++) {
+            identifier = (String) varList.get(i);
+            value = (PHPIdentifier) fIdentifierMap.get(identifier);
+            if (value == null) {
+              fIdentifierMap.put(identifier, new PHPIdentifier(null, PHPIdentifier.VARIABLE));
+            }
+          }
+
+          getNextToken();
+        } else if ((fToken == TokenNameLBRACE) || (fToken == TokenNameDOLLAR_LBRACE)) {
+          getNextToken();
+          counter++;
+        } else if (fToken == TokenNameRBRACE) {
+          getNextToken();
+          --counter;
+          if (counter == 0 && goBack) {
+            return;
+          }
+        } else {
+          getNextToken();
+        }
+      }
+    } catch (SyntaxError sytaxErr) {
+      // do nothing 
+    }
+  }
+  /**
+   * Move one file.
+   * @param sourceFile the file to move
+   * @param targetDir the directory to copy the result to
+   * @return file or null if the file was ignored
+   */
+  public File move(File sourceFile, File targetDir) {
+
+    StringBuffer buf = new StringBuffer();
+
+    try {
+      long filelen = sourceFile.length();
+      char[] charArray = new char[(int) filelen];
+
+      BufferedReader br = new BufferedReader(new FileReader(sourceFile.getAbsolutePath()));
+      br.read(charArray, 0, (int) filelen);
+      br.close();
+
+      //  String input = buf.toString();
+      /* fScanner initialization */
+      fScanner.setSource(charArray);
+      fScanner.setPHPMode(false);
+      fToken = TokenNameEOF;
+      getNextToken();
+      parseIdentifiers(false);
+      return null;
+    } catch (IOException e) {
+      fConsole.write(e.toString());
+    }
+    return null;
+  }
+}
\ No newline at end of file