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];
36 * Creates a PHPAnalyzer
37 * @param console reports error to the PHPConsole
39 public PHPAnalyzer(PHPConsole console, Scanner scanner, HashMap identifierMap) {
41 this.fScanner = scanner;
42 this.fIdentifierMap = identifierMap;
46 * gets the next token from input
48 private void getNextToken() {
51 fToken = fScanner.getNextToken();
53 int currentEndPosition = fScanner.getCurrentTokenEndPosition();
54 int currentStartPosition = fScanner.getCurrentTokenStartPosition();
56 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
57 System.out.println(fScanner.toStringAction(fToken));
60 } catch (InvalidInputException e) {
63 fToken = TokenNameERROR;
66 private void parseIdentifiers(boolean goBack) {
72 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
74 while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
75 if (fToken == TokenNameVariable) {
76 identifier = new String(fScanner.getCurrentIdentifierSource());
77 value = (PHPIdentifier) fIdentifierMap.get(identifier);
79 fIdentifierMap.put(identifier, new PHPIdentifier(null, PHPIdentifier.VARIABLE));
82 // } else if (fToken == TokenNamefunction) {
84 // if (fToken == TokenNameAND) {
87 // if (fToken == TokenNameIdentifier) {
88 // ident = fScanner.getCurrentIdentifierSource();
89 // outlineInfo.addVariable(new String(ident));
90 // temp = new PHPFunctionDeclaration(current, new String(ident), fScanner.getCurrentTokenStartPosition());
93 // parseDeclarations(outlineInfo, temp, true);
95 // } else if (fToken == TokenNameclass) {
97 // if (fToken == TokenNameIdentifier) {
98 // ident = fScanner.getCurrentIdentifierSource();
99 // outlineInfo.addVariable(new String(ident));
100 // temp = new PHPClassDeclaration(current, new String(ident), fScanner.getCurrentTokenStartPosition());
101 // current.add(temp);
104 // //skip fTokens for classname, extends and others until we have the opening '{'
105 // while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
108 // parseDeclarations(outlineInfo, temp, true);
111 } else if (fToken == TokenNameStringLiteral) {
112 char currentCharacter;
113 int i = fScanner.startPosition;
114 ArrayList varList = new ArrayList();
116 while (i < fScanner.currentPosition) {
117 currentCharacter = fScanner.source[i++];
118 if (currentCharacter == '$' && fScanner.source[i - 2] != '\\') {
119 StringBuffer varName = new StringBuffer();
121 while (i < fScanner.currentPosition) {
122 currentCharacter = fScanner.source[i++];
123 if (!Scanner.isPHPIdentifierPart(currentCharacter)) {
126 varName.append(currentCharacter);
128 varList.add(varName.toString());
132 for (i = 0; i < varList.size(); i++) {
133 identifier = (String) varList.get(i);
134 value = (PHPIdentifier) fIdentifierMap.get(identifier);
136 fIdentifierMap.put(identifier, new PHPIdentifier(null, PHPIdentifier.VARIABLE));
141 } else if ((fToken == TokenNameLBRACE) || (fToken == TokenNameDOLLAR_LBRACE)) {
144 } else if (fToken == TokenNameRBRACE) {
147 if (counter == 0 && goBack) {
154 } catch (SyntaxError sytaxErr) {
160 * @param sourceFile the file to move
161 * @param targetDir the directory to copy the result to
162 * @return file or null if the file was ignored
164 public File move(File sourceFile, File targetDir) {
166 StringBuffer buf = new StringBuffer();
169 long filelen = sourceFile.length();
170 char[] charArray = new char[(int) filelen];
172 BufferedReader br = new BufferedReader(new FileReader(sourceFile.getAbsolutePath()));
173 br.read(charArray, 0, (int) filelen);
176 // String input = buf.toString();
177 /* fScanner initialization */
178 fScanner.setSource(charArray);
179 fScanner.setPHPMode(false);
180 fToken = TokenNameEOF;
182 parseIdentifiers(false);
184 } catch (IOException e) {
185 fConsole.write(e.toString());