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
8 Klaus Hartlage - www.eclipseproject.de
9 **********************************************************************/
10 package net.sourceforge.phpeclipse.actions;
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.Iterator;
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;
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;
42 public class PHPObfuscatorAction implements IObjectActionDelegate {
43 private static String[] PREDEFINED_PHP_VARIABLES =
75 private IWorkbenchPart workbenchPart;
77 * Constructor for Action1.
79 public PHPObfuscatorAction() {
84 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
86 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
87 workbenchPart = targetPart;
90 public void run(IAction action) {
91 ISelectionProvider selectionProvider = null;
92 selectionProvider = workbenchPart.getSite().getSelectionProvider();
94 StructuredSelection selection = null;
95 selection = (StructuredSelection) selectionProvider.getSelection();
96 PHPConsole console = PHPConsole.getInstance();
98 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
101 Iterator iterator = null;
102 iterator = selection.iterator();
103 while (iterator.hasNext()) {
104 // obj => selected object in the view
105 Object obj = iterator.next();
108 if (obj instanceof IResource) {
109 IResource resource = (IResource) obj;
110 IProject proj = resource.getProject();
112 // String publishPath = "c:\\temp";
115 publishPath = proj.getPersistentProperty(IObfuscatorPreferences.PUBLISH_PROPERTY_NAME);
116 } catch (CoreException e) {
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) );
123 DefaultFilter[] filter = {
125 IFilter.DEFAULT_FILTER,
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();
137 walker.walk(sourcePath, publishPath);
138 } catch (IOException e) {
141 case IResource.FOLDER :
142 IFolder folder = (IFolder) resource;
143 sourcePath = folder.getLocation().toOSString();
145 walker.walk(sourcePath, publishPath);
146 } catch (IOException e) {
149 case IResource.FILE :
151 IFile file = (IFile) resource;
152 sourcePath = file.getLocation().toOSString();
154 walker.walk(sourcePath, publishPath);
155 } catch (IOException e) {
163 * @see IActionDelegate#selectionChanged(IAction, ISelection)
165 public void selectionChanged(IAction action, ISelection selection) {