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;
43 protected HashMap fIdentifierMap;
45 public ObfuscatorPass1Exporter(Scanner scanner, HashMap identifierMap) {
47 fIdentifierMap = identifierMap;
51 * Get the next token from input
53 private void getNextToken() {
56 fToken = fScanner.getNextToken();
58 int currentEndPosition = fScanner.getCurrentTokenEndPosition();
59 int currentStartPosition = fScanner
60 .getCurrentTokenStartPosition();
62 System.out.print(currentStartPosition + ","
63 + currentEndPosition + ": ");
64 System.out.println(fScanner.toStringAction(fToken));
67 } catch (InvalidInputException e) {
70 fToken = TokenNameERROR;
73 private void parseIdentifiers(boolean goBack) {
79 IPreferenceStore store = PHPeclipsePlugin.getDefault()
80 .getPreferenceStore();
82 while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
83 if (fToken == TokenNameVariable) {
84 identifier = new String(fScanner
85 .getCurrentIdentifierSource());
86 value = (PHPIdentifier) fIdentifierMap.get(identifier);
88 fIdentifierMap.put(identifier, new PHPIdentifier(null,
89 PHPIdentifier.VARIABLE));
92 // } else if (fToken == TokenNamefunction) {
94 // if (fToken == TokenNameAND) {
97 // if (fToken == TokenNameIdentifier) {
98 // ident = fScanner.getCurrentIdentifierSource();
99 // outlineInfo.addVariable(new String(ident));
100 // temp = new PHPFunctionDeclaration(current, new
101 // String(ident), fScanner.getCurrentTokenStartPosition());
102 // current.add(temp);
104 // parseDeclarations(outlineInfo, temp, true);
106 // } else if (fToken == TokenNameclass) {
108 // if (fToken == TokenNameIdentifier) {
109 // ident = fScanner.getCurrentIdentifierSource();
110 // outlineInfo.addVariable(new String(ident));
111 // temp = new PHPClassDeclaration(current, new
112 // String(ident), fScanner.getCurrentTokenStartPosition());
113 // current.add(temp);
116 // //skip fTokens for classname, extends and others until we
117 // have the opening '{'
118 // while (fToken != TokenNameLBRACE && fToken !=
119 // TokenNameEOF && fToken != TokenNameERROR) {
122 // parseDeclarations(outlineInfo, temp, true);
125 } else if (fToken == TokenNameStringDoubleQuote) {
126 char currentCharacter;
127 int i = fScanner.startPosition;
128 ArrayList varList = new ArrayList();
130 while (i < fScanner.currentPosition) {
131 currentCharacter = fScanner.source[i++];
132 if (currentCharacter == '$'
133 && fScanner.source[i - 2] != '\\') {
134 StringBuffer varName = new StringBuffer();
136 while (i < fScanner.currentPosition) {
137 currentCharacter = fScanner.source[i++];
139 .isPHPIdentifierPart(currentCharacter)) {
142 varName.append(currentCharacter);
144 varList.add(varName.toString());
148 for (i = 0; i < varList.size(); i++) {
149 identifier = (String) varList.get(i);
150 value = (PHPIdentifier) fIdentifierMap.get(identifier);
152 fIdentifierMap.put(identifier, new PHPIdentifier(
153 null, PHPIdentifier.VARIABLE));
158 } else if (fToken == TokenNameLBRACE) {
161 } else if (fToken == TokenNameRBRACE) {
164 if (counter == 0 && goBack) {
171 } catch (SyntaxError sytaxErr) {
177 * Do nothing in first pass
179 public void createFolder(IPath destinationPath) {
181 // new File(destinationPath.toOSString()).mkdir();
185 * Writes the passed resource to the specified location recursively
187 public void write(IResource resource, IPath destinationPath)
188 throws CoreException, IOException {
189 if (resource.getType() == IResource.FILE)
190 writeFile((IFile) resource, destinationPath);
192 writeChildren((IContainer) resource, destinationPath);
196 * Exports the passed container's children
198 protected void writeChildren(IContainer folder, IPath destinationPath)
199 throws CoreException, IOException {
200 if (folder.isAccessible()) {
201 IResource[] children = folder.members();
202 for (int i = 0; i < children.length; i++) {
203 IResource child = children[i];
204 writeResource(child, destinationPath.append(child.getName()));
210 * Analyzes the passed file resource for the PHP obfuscator
212 protected void writeFile(IFile file, IPath destinationPath)
213 throws IOException, CoreException {
214 if (!PHPFileUtil.isPHPFile(file)) {
217 InputStream stream = null;
218 char[] charArray = null;
220 stream = new BufferedInputStream(file.getContents());
221 charArray = Util.getInputStreamAsCharArray(stream, -1, null);
222 } catch (IOException e) {
226 if (stream != null) {
229 } catch (IOException e) {
233 if (charArray == null) {
234 // TODO show error message
237 /* fScanner initialization */
238 fScanner.setSource(charArray);
239 fScanner.setPHPMode(false);
240 fToken = TokenNameEOF;
242 parseIdentifiers(false);
246 * Writes the passed resource to the specified location recursively
248 protected void writeResource(IResource resource, IPath destinationPath)
249 throws CoreException, IOException {
250 if (resource.getType() == IResource.FILE)
251 writeFile((IFile) resource, destinationPath);
253 createFolder(destinationPath);
254 writeChildren((IContainer) resource, destinationPath);