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