Commiting more changes to fix RSE issues with PHP projects.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / obfuscator / ObfuscatorIgnores.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpeclipse.obfuscator;
6
7 import java.io.File;
8 import java.io.InputStream;
9
10 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
11
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.jface.dialogs.ErrorDialog;
16
17 /**
18  * <code>ObfuscatorIgnores</code> gives access to the available templates.
19  */
20 public class ObfuscatorIgnores extends ObfuscatorIgnoreSet {
21
22         private static final String DEFAULT_FILE = "default-obfuscator.xml"; //$NON-NLS-1$
23
24         private static final String TEMPLATE_FILE = "obfuscator.xml"; //$NON-NLS-1$
25
26         /** Singleton. */
27         private static ObfuscatorIgnores fgIgnores;
28
29         private IProject fProject;
30
31         public ObfuscatorIgnores(IProject project) {
32                 fProject = project;
33                 try {
34                         File templateFile = getTemplateFile();
35                         if (templateFile.exists()) {
36                                 addFromFile(templateFile);
37                         } else {
38                                 addFromStream(getDefaultsAsStream());
39                                 saveToFile(templateFile);
40                         }
41
42                 } catch (CoreException e) {
43                         PHPeclipsePlugin.log(e);
44                         ErrorDialog.openError(null, ObfuscatorMessages
45                                         .getString("Obfuscator.error.title"), //$NON-NLS-1$
46                                         e.getMessage(), e.getStatus());
47
48                         clear();
49                 }
50         }
51
52         /**
53          * Returns an instance of templates.
54          */
55         // public static ObfuscatorIgnores getInstance() {
56         // if (fgIgnores == null)
57         // fgIgnores = create();
58         //
59         // return fgIgnores;
60         // }
61         //
62         // private static ObfuscatorIgnores create() {
63         // ObfuscatorIgnores templates = new ObfuscatorIgnores();
64         //
65         // try {
66         // File templateFile = getTemplateFile();
67         // if (templateFile.exists()) {
68         // templates.addFromFile(templateFile);
69         // } else {
70         // templates.addFromStream(getDefaultsAsStream());
71         // templates.saveToFile(templateFile);
72         // }
73         //
74         // } catch (CoreException e) {
75         // PHPeclipsePlugin.log(e);
76         // ErrorDialog.openError(null,
77         // ObfuscatorMessages.getString("Templates.error.title"), //$NON-NLS-1$
78         // e.getMessage(), e.getStatus());
79         //
80         // templates.clear();
81         // }
82         //
83         // return templates;
84         // }
85         /**
86          * Resets the template set.
87          */
88         public void reset() throws CoreException {
89                 clear();
90                 addFromFile(getTemplateFile());
91         }
92
93         /**
94          * Resets the template set with the default templates.
95          */
96         public void restoreDefaults() throws CoreException {
97                 clear();
98                 addFromStream(getDefaultsAsStream());
99         }
100
101         /**
102          * Saves the template set.
103          */
104         public void save() throws CoreException {
105                 saveToFile(getTemplateFile());
106         }
107
108         private InputStream getDefaultsAsStream() {
109                 return ObfuscatorIgnores.class.getResourceAsStream(DEFAULT_FILE);
110         }
111
112         private File getTemplateFile() {
113                 IPath path = fProject.getFullPath();
114                 // PHPeclipsePlugin.getDefault().getStateLocation();
115                 path = path.append(TEMPLATE_FILE);
116
117                 return path.toFile();
118         }
119 }