*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPObfuscatorAction.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. 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     Klaus Hartlage - www.eclipseproject.de
9 **********************************************************************/
10 package net.sourceforge.phpeclipse.actions;
11
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.Iterator;
15
16 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.mover.DefaultFilter;
19 import net.sourceforge.phpeclipse.mover.DirectoryWalker;
20 import net.sourceforge.phpeclipse.mover.IFilter;
21 import net.sourceforge.phpeclipse.mover.IMover;
22 import net.sourceforge.phpeclipse.mover.obfuscator.PHPAnalyzer;
23 import net.sourceforge.phpeclipse.mover.obfuscator.PHPIdentifier;
24 import net.sourceforge.phpeclipse.mover.obfuscator.PHPObfuscatorMover;
25 import net.sourceforge.phpeclipse.preferences.IObfuscatorPreferences;
26 import net.sourceforge.phpeclipse.views.PHPConsole;
27
28 import org.eclipse.core.resources.IFile;
29 import org.eclipse.core.resources.IFolder;
30 import org.eclipse.core.resources.IProject;
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.runtime.CoreException;
33 import org.eclipse.jface.action.IAction;
34 import org.eclipse.jface.preference.IPreferenceStore;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.jface.viewers.ISelectionProvider;
37 import org.eclipse.jface.viewers.StructuredSelection;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.ui.IObjectActionDelegate;
40 import org.eclipse.ui.IWorkbenchPart;
41
42 public class PHPObfuscatorAction implements IObjectActionDelegate {
43   private static String[] PREDEFINED_PHP_VARIABLES =
44   {
45     "$this",
46     "$AUTH_TYPE",
47     "$CONTENT_LENGTH",
48     "$CONTENT_TYPE",
49     "$GATEWAY_INTERFACE",
50     "$GLOBALS",
51     "$HTTP_ACCEPT",
52     "$HTTP_COOKIE",
53     "$HTTP_COOKIE_VARS",
54     "$HTTP_POST_VARS",
55     "$HTTP_REFERER",
56     "$HTTP_USER_AGENT",
57     "$PATH_INFO",
58     "$PATH_TRANSLATED",
59     "$PHP_AUTH_PW",
60     "$PHP_AUTH_USER",
61     "$PHP_ERRORMSG",
62     "$PHP_SELF",
63     "$QUERY_STRING",
64     "$REMOTE_ADDR",
65     "$REMOTE_HOST",
66     "$REMOTE_IDENT",
67     "$REMOTE_USER",
68     "$REQUEST_METHOD",
69     "$SCRIPT_NAME",
70     "$SERVER_NAME",
71     "$SERVER_PORT",
72     "$SERVER_PROTOCOL",
73     "$SERVER_SOFTWARE"
74   };
75   private IWorkbenchPart workbenchPart;
76   /**
77    * Constructor for Action1.
78    */
79   public PHPObfuscatorAction() {
80     super();
81   }
82
83   /**
84    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
85    */
86   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
87     workbenchPart = targetPart;
88   }
89
90   public void run(IAction action) {
91     ISelectionProvider selectionProvider = null;
92     selectionProvider = workbenchPart.getSite().getSelectionProvider();
93
94     StructuredSelection selection = null;
95     selection = (StructuredSelection) selectionProvider.getSelection();
96     PHPConsole console = PHPConsole.getInstance();
97
98     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
99
100     Shell shell = null;
101     Iterator iterator = null;
102     iterator = selection.iterator();
103     while (iterator.hasNext()) {
104       //  obj => selected object in the view
105       Object obj = iterator.next();
106
107       // is it a resource
108       if (obj instanceof IResource) {
109         IResource resource = (IResource) obj;
110         IProject proj = resource.getProject();
111         String sourcePath;
112       //  String publishPath = "c:\\temp";
113         String publishPath;
114         try {
115           publishPath = proj.getPersistentProperty(IObfuscatorPreferences.PUBLISH_PROPERTY_NAME);
116         } catch (CoreException e) {
117           return;
118         }
119         HashMap identifierMap = new HashMap(8096);
120         for (int i=0;i<PREDEFINED_PHP_VARIABLES.length;i++) {
121           identifierMap.put(PREDEFINED_PHP_VARIABLES[i], new PHPIdentifier(PREDEFINED_PHP_VARIABLES[i],PHPIdentifier.VARIABLE) );
122         }
123         DefaultFilter[] filter = {
124           IFilter.PHP_FILTER,
125           IFilter.DEFAULT_FILTER,
126         };
127         IMover[] mover =
128           {
129             new PHPAnalyzer(PHPConsole.getInstance(), new Scanner(false, false), identifierMap),
130             new PHPObfuscatorMover(PHPConsole.getInstance(), new Scanner(true, true), identifierMap)};
131         DirectoryWalker walker = new DirectoryWalker(mover, filter);
132         switch (resource.getType()) {
133           case IResource.PROJECT :
134             IProject project = (IProject) resource;
135             sourcePath = project.getLocation().toOSString();
136             try {
137               walker.walk(sourcePath, publishPath);
138             } catch (IOException e) {
139             }
140             break;
141           case IResource.FOLDER :
142             IFolder folder = (IFolder) resource;
143             sourcePath = folder.getLocation().toOSString();
144             try {
145               walker.walk(sourcePath, publishPath);
146             } catch (IOException e) {
147             }
148             break;
149           case IResource.FILE :
150             // single file:
151             IFile file = (IFile) resource;
152             sourcePath = file.getLocation().toOSString();
153             try {
154               walker.walk(sourcePath, publishPath);
155             } catch (IOException e) {
156             }
157             break;
158         }
159       }
160     }
161   }
162   /**
163      * @see IActionDelegate#selectionChanged(IAction, ISelection)
164      */
165   public void selectionChanged(IAction action, ISelection selection) {
166   }
167
168 }