some bugfixes
[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.ObfuscatorIgnores;
23 import net.sourceforge.phpeclipse.mover.obfuscator.PHPAnalyzer;
24 import net.sourceforge.phpeclipse.mover.obfuscator.PHPObfuscatorMover;
25 import net.sourceforge.phpeclipse.preferences.ProjectProperties;
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 /**
43  * 
44  * @author khartlage
45  *
46  * Run the PHP Obfuscator 
47  */
48 public class PHPObfuscatorAction implements IObjectActionDelegate {
49
50   private IWorkbenchPart workbenchPart;
51   /**
52    * Constructor for PHPObfuscatorAction.
53    */
54   public PHPObfuscatorAction() {
55     super();
56   }
57
58   public void run(IAction action) {
59     ISelectionProvider selectionProvider = null;
60     selectionProvider = workbenchPart.getSite().getSelectionProvider();
61
62     StructuredSelection selection = null;
63     selection = (StructuredSelection) selectionProvider.getSelection();
64     PHPConsole console = PHPConsole.getInstance();
65
66     //    HashMap identifierMap = new HashMap(8096);
67     //    for (int i=0;i<PREDEFINED_PHP_VARIABLES.length;i++) {
68     //          identifierMap.put(PREDEFINED_PHP_VARIABLES[i], new PHPIdentifier(PREDEFINED_PHP_VARIABLES[i],PHPIdentifier.VARIABLE) );
69     //    }
70     HashMap identifierMap = null;
71
72     Shell shell = null;
73     Iterator iterator = null;
74     iterator = selection.iterator();
75     while (iterator.hasNext()) {
76       //  obj => selected object in the view
77       Object obj = iterator.next();
78
79       // is it a resource
80       if (obj instanceof IResource) {
81
82         IResource resource = (IResource) obj;
83         IProject proj = resource.getProject();
84         String sourcePath;
85
86         if (identifierMap == null) {
87           IPreferenceStore store =
88             PHPeclipsePlugin.getDefault().getPreferenceStore();
89           ObfuscatorIgnores ignore = new ObfuscatorIgnores(proj);
90           identifierMap = ignore.getIdentifierMap();
91         }
92
93         String publishPath;
94         try {
95           ProjectProperties properties = new ProjectProperties(proj);
96           publishPath = properties.getPublish();
97         } catch (CoreException e1) {
98           return;
99           //                                    e1.printStackTrace();
100         }
101         //        try {
102         //          publishPath = proj.getPersistentProperty(IObfuscatorPreferences.PUBLISH_PROPERTY_NAME);
103         //        } catch (CoreException e) {
104         //          return;
105         //        }
106
107         DefaultFilter[] filter =
108           { IFilter.PHP_FILTER, IFilter.DEFAULT_FILTER, };
109         IMover[] mover =
110           {
111             new PHPAnalyzer(
112               PHPConsole.getInstance(),
113               new Scanner(false, false),
114               identifierMap),
115             new PHPObfuscatorMover(
116               PHPConsole.getInstance(),
117               new Scanner(true, true),
118               identifierMap)};
119         DirectoryWalker walker = new DirectoryWalker(mover, filter);
120         switch (resource.getType()) {
121           case IResource.PROJECT :
122             IProject project = (IProject) resource;
123             sourcePath = project.getLocation().toOSString();
124             try {
125               walker.walk(sourcePath, publishPath);
126             } catch (IOException e) {
127             }
128             break;
129           case IResource.FOLDER :
130             IFolder folder = (IFolder) resource;
131             sourcePath = folder.getLocation().toOSString();
132             try {
133               walker.walk(sourcePath, publishPath);
134             } catch (IOException e) {
135             }
136             break;
137           case IResource.FILE :
138             // single file:
139             IFile file = (IFile) resource;
140             sourcePath = file.getLocation().toOSString();
141             try {
142               walker.walk(sourcePath, publishPath);
143             } catch (IOException e) {
144             }
145             break;
146         }
147       }
148     }
149   }
150   /**
151      * @see IActionDelegate#selectionChanged(IAction, ISelection)
152      */
153   public void selectionChanged(IAction action, ISelection selection) {
154   }
155
156   /**
157    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
158    */
159   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
160     workbenchPart = targetPart;
161   }
162
163 }