1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.obfuscator;
13 import java.io.BufferedInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.ArrayList;
17 import java.util.HashMap;
19 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
20 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
21 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
22 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
23 import net.sourceforge.phpdt.internal.compiler.util.Util;
24 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
25 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
27 import org.eclipse.core.resources.IContainer;
28 import org.eclipse.core.resources.IFile;
29 import org.eclipse.core.resources.IResource;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IPath;
32 import org.eclipse.jface.preference.IPreferenceStore;
35 * Analyzing php files in a first pass over all resources !
37 public class ObfuscatorPass1Exporter implements ITerminalSymbols {
39 protected Scanner fScanner;
42 protected HashMap fIdentifierMap;
44 public ObfuscatorPass1Exporter(Scanner scanner, HashMap identifierMap) {
46 fIdentifierMap = identifierMap;
50 * Get the next token from input
52 private void getNextToken() {
55 fToken = fScanner.getNextToken();
57 int currentEndPosition = fScanner.getCurrentTokenEndPosition();
58 int currentStartPosition = fScanner.getCurrentTokenStartPosition();
60 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
61 System.out.println(fScanner.toStringAction(fToken));
64 } catch (InvalidInputException e) {
67 fToken = TokenNameERROR;
70 private void parseIdentifiers(boolean goBack) {
76 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
78 while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
79 if (fToken == TokenNameVariable) {
80 identifier = new String(fScanner.getCurrentIdentifierSource());
81 value = (PHPIdentifier) fIdentifierMap.get(identifier);
83 fIdentifierMap.put(identifier, new PHPIdentifier(null, PHPIdentifier.VARIABLE));
86 // } else if (fToken == TokenNamefunction) {
88 // if (fToken == TokenNameAND) {
91 // if (fToken == TokenNameIdentifier) {
92 // ident = fScanner.getCurrentIdentifierSource();
93 // outlineInfo.addVariable(new String(ident));
94 // temp = new PHPFunctionDeclaration(current, new String(ident), fScanner.getCurrentTokenStartPosition());
97 // parseDeclarations(outlineInfo, temp, true);
99 // } else if (fToken == TokenNameclass) {
101 // if (fToken == TokenNameIdentifier) {
102 // ident = fScanner.getCurrentIdentifierSource();
103 // outlineInfo.addVariable(new String(ident));
104 // temp = new PHPClassDeclaration(current, new String(ident), fScanner.getCurrentTokenStartPosition());
105 // current.add(temp);
108 // //skip fTokens for classname, extends and others until we have the opening '{'
109 // while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
112 // parseDeclarations(outlineInfo, temp, true);
115 } else if (fToken == TokenNameStringLiteral) {
116 char currentCharacter;
117 int i = fScanner.startPosition;
118 ArrayList varList = new ArrayList();
120 while (i < fScanner.currentPosition) {
121 currentCharacter = fScanner.source[i++];
122 if (currentCharacter == '$' && fScanner.source[i - 2] != '\\') {
123 StringBuffer varName = new StringBuffer();
125 while (i < fScanner.currentPosition) {
126 currentCharacter = fScanner.source[i++];
127 if (!Scanner.isPHPIdentifierPart(currentCharacter)) {
130 varName.append(currentCharacter);
132 varList.add(varName.toString());
136 for (i = 0; i < varList.size(); i++) {
137 identifier = (String) varList.get(i);
138 value = (PHPIdentifier) fIdentifierMap.get(identifier);
140 fIdentifierMap.put(identifier, new PHPIdentifier(null, PHPIdentifier.VARIABLE));
145 } else if (fToken == TokenNameLBRACE) {
148 } else if (fToken == TokenNameRBRACE) {
151 if (counter == 0 && goBack) {
158 } catch (SyntaxError sytaxErr) {
164 * Do nothing in first pass
166 public void createFolder(IPath destinationPath) {
168 // new File(destinationPath.toOSString()).mkdir();
171 * Writes the passed resource to the specified location recursively
173 public void write(IResource resource, IPath destinationPath) throws CoreException, IOException {
174 if (resource.getType() == IResource.FILE)
175 writeFile((IFile) resource, destinationPath);
177 writeChildren((IContainer) resource, destinationPath);
180 * Exports the passed container's children
182 protected void writeChildren(IContainer folder, IPath destinationPath) throws CoreException, IOException {
183 if (folder.isAccessible()) {
184 IResource[] children = folder.members();
185 for (int i = 0; i < children.length; i++) {
186 IResource child = children[i];
187 writeResource(child, destinationPath.append(child.getName()));
192 * Analyzes the passed file resource for the PHP obfuscator
194 protected void writeFile(IFile file, IPath destinationPath) throws IOException, CoreException {
195 if (! PHPFileUtil.isPHPFile(file) ){
198 InputStream stream = null;
199 char[] charArray = null;
201 stream = new BufferedInputStream(file.getContents());
202 charArray = Util.getInputStreamAsCharArray(stream, -1, null);
203 } catch (IOException e) {
207 if (stream != null) {
210 } catch (IOException e) {
214 if (charArray == null) {
215 // TODO show error message
218 /* fScanner initialization */
219 fScanner.setSource(charArray);
220 fScanner.setPHPMode(false);
221 fToken = TokenNameEOF;
223 parseIdentifiers(false);
226 * Writes the passed resource to the specified location recursively
228 protected void writeResource(IResource resource, IPath destinationPath) throws CoreException, IOException {
229 if (resource.getType() == IResource.FILE)
230 writeFile((IFile) resource, destinationPath);
232 createFolder(destinationPath);
233 writeChildren((IContainer) resource, destinationPath);