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;
26 import net.sourceforge.phpeclipse.ui.WebUI;
28 import org.eclipse.core.resources.IContainer;
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IPath;
33 import org.eclipse.jface.preference.IPreferenceStore;
36 * Analyzing php files in a first pass over all resources !
38 public class ObfuscatorPass1Exporter implements ITerminalSymbols {
40 protected Scanner fScanner;
44 protected HashMap fIdentifierMap;
46 public ObfuscatorPass1Exporter(Scanner scanner, HashMap identifierMap) {
48 fIdentifierMap = identifierMap;
52 * Get the next token from input
54 private void getNextToken() {
57 fToken = fScanner.getNextToken();
59 int currentEndPosition = fScanner.getCurrentTokenEndPosition();
60 int currentStartPosition = fScanner
61 .getCurrentTokenStartPosition();
63 System.out.print(currentStartPosition + ","
64 + currentEndPosition + ": ");
65 System.out.println(fScanner.toStringAction(fToken));
68 } catch (InvalidInputException e) {
71 fToken = TokenNameERROR;
74 private void parseIdentifiers(boolean goBack) {
80 IPreferenceStore store = WebUI.getDefault()
81 .getPreferenceStore();
83 while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
84 if (fToken == TokenNameVariable) {
85 identifier = new String(fScanner
86 .getCurrentIdentifierSource());
87 value = (PHPIdentifier) fIdentifierMap.get(identifier);
89 fIdentifierMap.put(identifier, new PHPIdentifier(null,
90 PHPIdentifier.VARIABLE));
93 // } else if (fToken == TokenNamefunction) {
95 // if (fToken == TokenNameAND) {
98 // if (fToken == TokenNameIdentifier) {
99 // ident = fScanner.getCurrentIdentifierSource();
100 // outlineInfo.addVariable(new String(ident));
101 // temp = new PHPFunctionDeclaration(current, new
102 // String(ident), fScanner.getCurrentTokenStartPosition());
103 // current.add(temp);
105 // parseDeclarations(outlineInfo, temp, true);
107 // } else if (fToken == TokenNameclass) {
109 // if (fToken == TokenNameIdentifier) {
110 // ident = fScanner.getCurrentIdentifierSource();
111 // outlineInfo.addVariable(new String(ident));
112 // temp = new PHPClassDeclaration(current, new
113 // String(ident), fScanner.getCurrentTokenStartPosition());
114 // current.add(temp);
117 // //skip fTokens for classname, extends and others until we
118 // have the opening '{'
119 // while (fToken != TokenNameLBRACE && fToken !=
120 // TokenNameEOF && fToken != TokenNameERROR) {
123 // parseDeclarations(outlineInfo, temp, true);
126 } else if (fToken == TokenNameStringDoubleQuote) {
127 char currentCharacter;
128 int i = fScanner.startPosition;
129 ArrayList varList = new ArrayList();
131 while (i < fScanner.currentPosition) {
132 currentCharacter = fScanner.source[i++];
133 if (currentCharacter == '$'
134 && fScanner.source[i - 2] != '\\') {
135 StringBuffer varName = new StringBuffer();
137 while (i < fScanner.currentPosition) {
138 currentCharacter = fScanner.source[i++];
140 .isPHPIdentifierPart(currentCharacter)) {
143 varName.append(currentCharacter);
145 varList.add(varName.toString());
149 for (i = 0; i < varList.size(); i++) {
150 identifier = (String) varList.get(i);
151 value = (PHPIdentifier) fIdentifierMap.get(identifier);
153 fIdentifierMap.put(identifier, new PHPIdentifier(
154 null, PHPIdentifier.VARIABLE));
159 } else if (fToken == TokenNameLBRACE) {
162 } else if (fToken == TokenNameRBRACE) {
165 if (counter == 0 && goBack) {
172 } catch (SyntaxError sytaxErr) {
178 * Do nothing in first pass
180 public void createFolder(IPath destinationPath) {
182 // new File(destinationPath.toOSString()).mkdir();
186 * Writes the passed resource to the specified location recursively
188 public void write(IResource resource, IPath destinationPath)
189 throws CoreException, IOException {
190 if (resource.getType() == IResource.FILE)
191 writeFile((IFile) resource, destinationPath);
193 writeChildren((IContainer) resource, destinationPath);
197 * Exports the passed container's children
199 protected void writeChildren(IContainer folder, IPath destinationPath)
200 throws CoreException, IOException {
201 if (folder.isAccessible()) {
202 IResource[] children = folder.members();
203 for (int i = 0; i < children.length; i++) {
204 IResource child = children[i];
205 writeResource(child, destinationPath.append(child.getName()));
211 * Analyzes the passed file resource for the PHP obfuscator
213 protected void writeFile(IFile file, IPath destinationPath)
214 throws IOException, CoreException {
215 if (!PHPFileUtil.isPHPFile(file)) {
218 InputStream stream = null;
219 char[] charArray = null;
221 stream = new BufferedInputStream(file.getContents());
222 charArray = Util.getInputStreamAsCharArray(stream, -1, null);
223 } catch (IOException e) {
227 if (stream != null) {
230 } catch (IOException e) {
234 if (charArray == null) {
235 // TODO show error message
238 /* fScanner initialization */
239 fScanner.setSource(charArray);
240 fScanner.setPHPMode(false);
241 fToken = TokenNameEOF;
243 parseIdentifiers(false);
247 * Writes the passed resource to the specified location recursively
249 protected void writeResource(IResource resource, IPath destinationPath)
250 throws CoreException, IOException {
251 if (resource.getType() == IResource.FILE)
252 writeFile((IFile) resource, destinationPath);
254 createFolder(destinationPath);
255 writeChildren((IContainer) resource, destinationPath);