1 package net.sourceforge.phpeclipse.mover.obfuscator;
 
   4  * Analyze the file with the PHP Scanner
 
   6 import java.io.BufferedReader;
 
   8 import java.io.FileReader;
 
   9 import java.io.IOException;
 
  10 import java.util.ArrayList;
 
  11 import java.util.HashMap;
 
  13 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
 
  14 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
 
  15 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
 
  16 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
 
  17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  18 import net.sourceforge.phpeclipse.mover.DefaultMover;
 
  19 import net.sourceforge.phpeclipse.views.PHPConsole;
 
  21 import org.eclipse.jface.preference.IPreferenceStore;
 
  23 public class PHPAnalyzer extends DefaultMover implements ITerminalSymbols {
 
  25   protected Scanner fScanner;
 
  28   protected HashMap fIdentifierMap;
 
  31    * buffer, for obvious reasons access to this buffer must
 
  34   protected byte[] bytes = new byte[1024];
 
  37    * Creates a PHPAnalyzer
 
  38    * @param console reports error to the PHPConsole
 
  40   public PHPAnalyzer(PHPConsole console, Scanner scanner, HashMap identifierMap) {
 
  42     this.fScanner = scanner;
 
  43     this.fIdentifierMap = identifierMap;
 
  47    * gets the next token from input
 
  49   private void getNextToken() {
 
  52       fToken = fScanner.getNextToken();
 
  54         int currentEndPosition = fScanner.getCurrentTokenEndPosition();
 
  55         int currentStartPosition = fScanner.getCurrentTokenStartPosition();
 
  57         System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
 
  58         System.out.println(fScanner.toStringAction(fToken));
 
  61     } catch (InvalidInputException e) {
 
  64     fToken = TokenNameERROR;
 
  67   private void parseIdentifiers(boolean goBack) {
 
  73     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
 
  75       while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
 
  76         if (fToken == TokenNameVariable) {
 
  77           identifier = new String(fScanner.getCurrentIdentifierSource());
 
  78           value = (PHPIdentifier) fIdentifierMap.get(identifier);
 
  80             fIdentifierMap.put(identifier, new PHPIdentifier(null, PHPIdentifier.VARIABLE));
 
  83           //        } else if (fToken == TokenNamefunction) {
 
  85           //          if (fToken == TokenNameAND) {
 
  88           //          if (fToken == TokenNameIdentifier) {
 
  89           //            ident = fScanner.getCurrentIdentifierSource();
 
  90           //            outlineInfo.addVariable(new String(ident));
 
  91           //            temp = new PHPFunctionDeclaration(current, new String(ident), fScanner.getCurrentTokenStartPosition());
 
  94           //            parseDeclarations(outlineInfo, temp, true);
 
  96           //        } else if (fToken == TokenNameclass) {
 
  98           //          if (fToken == TokenNameIdentifier) {
 
  99           //            ident = fScanner.getCurrentIdentifierSource();
 
 100           //            outlineInfo.addVariable(new String(ident));
 
 101           //            temp = new PHPClassDeclaration(current, new String(ident), fScanner.getCurrentTokenStartPosition());
 
 102           //            current.add(temp);
 
 105           //            //skip fTokens for classname, extends and others until we have the opening '{'
 
 106           //            while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
 
 109           //            parseDeclarations(outlineInfo, temp, true);
 
 112         } else if (fToken == TokenNameStringLiteral) {
 
 113           char currentCharacter;
 
 114           int i = fScanner.startPosition;
 
 115           ArrayList varList = new ArrayList();
 
 117           while (i < fScanner.currentPosition) {
 
 118             currentCharacter = fScanner.source[i++];
 
 119             if (currentCharacter == '$' && fScanner.source[i - 2] != '\\') {
 
 120               StringBuffer varName = new StringBuffer();
 
 122               while (i < fScanner.currentPosition) {
 
 123                 currentCharacter = fScanner.source[i++];
 
 124                 if (!Scanner.isPHPIdentifierPart(currentCharacter)) {
 
 127                 varName.append(currentCharacter);
 
 129               varList.add(varName.toString());
 
 133           for (i = 0; i < varList.size(); i++) {
 
 134             identifier = (String) varList.get(i);
 
 135             value = (PHPIdentifier) fIdentifierMap.get(identifier);
 
 137               fIdentifierMap.put(identifier, new PHPIdentifier(null, PHPIdentifier.VARIABLE));
 
 142         } else if ((fToken == TokenNameLBRACE) || (fToken == TokenNameDOLLAR_LBRACE)) {
 
 145         } else if (fToken == TokenNameRBRACE) {
 
 148           if (counter == 0 && goBack) {
 
 155     } catch (SyntaxError sytaxErr) {
 
 161    * @param sourceFile the file to move
 
 162    * @param targetDir the directory to copy the result to
 
 163    * @return file or null if the file was ignored
 
 165   public File move(File sourceFile, File targetDir) {
 
 167     StringBuffer buf = new StringBuffer();
 
 170       long filelen = sourceFile.length();
 
 171       char[] charArray = new char[(int) filelen];
 
 173       BufferedReader br = new BufferedReader(new FileReader(sourceFile.getAbsolutePath()));
 
 174       br.read(charArray, 0, (int) filelen);
 
 177       //  String input = buf.toString();
 
 178       /* fScanner initialization */
 
 179       fScanner.setSource(charArray);
 
 180       fScanner.setPHPMode(false);
 
 181       fToken = TokenNameEOF;
 
 183       parseIdentifiers(false);
 
 185     } catch (IOException e) {
 
 186       fConsole.write(e.toString());