70844825a93ec0b348389760377ec3ceb68885c0
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / obfuscator / ObfuscatorPass1Exporter.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.obfuscator;
12
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;
18
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;
27
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;
34
35 /**
36  * Analyzing php files in a first pass over all resources !
37  */
38 public class ObfuscatorPass1Exporter implements ITerminalSymbols {
39
40         protected Scanner fScanner;
41
42         protected int fToken;
43
44         protected HashMap fIdentifierMap;
45
46         public ObfuscatorPass1Exporter(Scanner scanner, HashMap identifierMap) {
47                 fScanner = scanner;
48                 fIdentifierMap = identifierMap;
49         }
50
51         /**
52          * Get the next token from input
53          */
54         private void getNextToken() {
55
56                 try {
57                         fToken = fScanner.getNextToken();
58                         if (Scanner.DEBUG) {
59                                 int currentEndPosition = fScanner.getCurrentTokenEndPosition();
60                                 int currentStartPosition = fScanner
61                                                 .getCurrentTokenStartPosition();
62
63                                 System.out.print(currentStartPosition + ","
64                                                 + currentEndPosition + ": ");
65                                 System.out.println(fScanner.toStringAction(fToken));
66                         }
67                         return;
68                 } catch (InvalidInputException e) {
69
70                 }
71                 fToken = TokenNameERROR;
72         }
73
74         private void parseIdentifiers(boolean goBack) {
75                 char[] ident;
76                 String identifier;
77                 PHPIdentifier value;
78                 int counter = 0;
79
80                 IPreferenceStore store = WebUI.getDefault()
81                                 .getPreferenceStore();
82                 try {
83                         while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
84                                 if (fToken == TokenNameVariable) {
85                                         identifier = new String(fScanner
86                                                         .getCurrentIdentifierSource());
87                                         value = (PHPIdentifier) fIdentifierMap.get(identifier);
88                                         if (value == null) {
89                                                 fIdentifierMap.put(identifier, new PHPIdentifier(null,
90                                                                 PHPIdentifier.VARIABLE));
91                                         }
92                                         getNextToken();
93                                         // } else if (fToken == TokenNamefunction) {
94                                         // getNextToken();
95                                         // if (fToken == TokenNameAND) {
96                                         // getNextToken();
97                                         // }
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);
104                                         // getNextToken();
105                                         // parseDeclarations(outlineInfo, temp, true);
106                                         // }
107                                         // } else if (fToken == TokenNameclass) {
108                                         // getNextToken();
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);
115                                         // getNextToken();
116                                         //
117                                         // //skip fTokens for classname, extends and others until we
118                                         // have the opening '{'
119                                         // while (fToken != TokenNameLBRACE && fToken !=
120                                         // TokenNameEOF && fToken != TokenNameERROR) {
121                                         // getNextToken();
122                                         // }
123                                         // parseDeclarations(outlineInfo, temp, true);
124                                         // // stack.pop();
125                                         // }
126                                 } else if (fToken == TokenNameStringDoubleQuote) {
127                                         char currentCharacter;
128                                         int i = fScanner.startPosition;
129                                         ArrayList varList = new ArrayList();
130
131                                         while (i < fScanner.currentPosition) {
132                                                 currentCharacter = fScanner.source[i++];
133                                                 if (currentCharacter == '$'
134                                                                 && fScanner.source[i - 2] != '\\') {
135                                                         StringBuffer varName = new StringBuffer();
136                                                         varName.append("$");
137                                                         while (i < fScanner.currentPosition) {
138                                                                 currentCharacter = fScanner.source[i++];
139                                                                 if (!Scanner
140                                                                                 .isPHPIdentifierPart(currentCharacter)) {
141                                                                         break; // while loop
142                                                                 }
143                                                                 varName.append(currentCharacter);
144                                                         }
145                                                         varList.add(varName.toString());
146                                                 }
147                                         }
148
149                                         for (i = 0; i < varList.size(); i++) {
150                                                 identifier = (String) varList.get(i);
151                                                 value = (PHPIdentifier) fIdentifierMap.get(identifier);
152                                                 if (value == null) {
153                                                         fIdentifierMap.put(identifier, new PHPIdentifier(
154                                                                         null, PHPIdentifier.VARIABLE));
155                                                 }
156                                         }
157
158                                         getNextToken();
159                                 } else if (fToken == TokenNameLBRACE) {
160                                         getNextToken();
161                                         counter++;
162                                 } else if (fToken == TokenNameRBRACE) {
163                                         getNextToken();
164                                         --counter;
165                                         if (counter == 0 && goBack) {
166                                                 return;
167                                         }
168                                 } else {
169                                         getNextToken();
170                                 }
171                         }
172                 } catch (SyntaxError sytaxErr) {
173                         // do nothing
174                 }
175         }
176
177         /**
178          * Do nothing in first pass
179          */
180         public void createFolder(IPath destinationPath) {
181                 // do nothing here
182                 // new File(destinationPath.toOSString()).mkdir();
183         }
184
185         /**
186          * Writes the passed resource to the specified location recursively
187          */
188         public void write(IResource resource, IPath destinationPath)
189                         throws CoreException, IOException {
190                 if (resource.getType() == IResource.FILE)
191                         writeFile((IFile) resource, destinationPath);
192                 else
193                         writeChildren((IContainer) resource, destinationPath);
194         }
195
196         /**
197          * Exports the passed container's children
198          */
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()));
206                         }
207                 }
208         }
209
210         /**
211          * Analyzes the passed file resource for the PHP obfuscator
212          */
213         protected void writeFile(IFile file, IPath destinationPath)
214                         throws IOException, CoreException {
215                 if (!PHPFileUtil.isPHPFile(file)) {
216                         return;
217                 }
218                 InputStream stream = null;
219                 char[] charArray = null;
220                 try {
221                         stream = new BufferedInputStream(file.getContents());
222                         charArray = Util.getInputStreamAsCharArray(stream, -1, null);
223                 } catch (IOException e) {
224                         return;
225                 } finally {
226                         try {
227                                 if (stream != null) {
228                                         stream.close();
229                                 }
230                         } catch (IOException e) {
231                         }
232                 }
233
234                 if (charArray == null) {
235                         // TODO show error message
236                         return;
237                 }
238                 /* fScanner initialization */
239                 fScanner.setSource(charArray);
240                 fScanner.setPHPMode(false);
241                 fToken = TokenNameEOF;
242                 getNextToken();
243                 parseIdentifiers(false);
244         }
245
246         /**
247          * Writes the passed resource to the specified location recursively
248          */
249         protected void writeResource(IResource resource, IPath destinationPath)
250                         throws CoreException, IOException {
251                 if (resource.getType() == IResource.FILE)
252                         writeFile((IFile) resource, destinationPath);
253                 else {
254                         createFolder(destinationPath);
255                         writeChildren((IContainer) resource, destinationPath);
256                 }
257         }
258 }