1) Reintroduced some out commented methods. (Did break some preference settings)
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / util / PHPFileUtil.java
1 /*
2  * Created on 09.08.2003
3  *
4  */
5
6 /*duplicated incastrix*/
7 package net.sourceforge.phpdt.internal.ui.util;
8
9 import java.io.File;
10 import java.util.List;
11
12 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13 import net.sourceforge.phpeclipse.ui.WebUI;
14 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
15
16 import org.eclipse.core.filebuffers.FileBuffers;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.ui.IEditorDescriptor;
23 import org.eclipse.ui.IEditorRegistry;
24 import org.eclipse.ui.IWorkbench;
25 import org.eclipse.ui.PlatformUI;
26
27 public class PHPFileUtil {
28         // private static String[] PHP_EXTENSIONS = null;
29
30         public final static String[] SMARTY_EXTENSIONS = { "tpl" };
31
32         public static boolean isPHPFile(IFile file) {
33                 return isPHPFileName(file.getFullPath().toString());
34         }
35
36         // public final static String getFileExtension(String name) {
37         // int index = name.lastIndexOf('.');
38         // if (index == -1)
39         // return null;
40         // if (index == (name.length() - 1))
41         // return null; //$NON-NLS-1$
42         // return name.substring(index + 1);
43         // }
44
45         /**
46          * Returns true iff str.toLowerCase().endsWith(".php") implementation is not
47          * creating extra strings.
48          */
49         public final static boolean isPHPFileName(String name) {
50
51                 // avoid handling a file without base name, e.g. ".php", which is a
52                 // valid
53                 // Eclipse resource name
54                 File file = new File(name);
55                 if (file.getName().startsWith(".")) {
56                         return false;
57                 }
58                 IWorkbench workbench = PlatformUI.getWorkbench();
59                 IEditorRegistry registry = workbench.getEditorRegistry();
60                 IEditorDescriptor[] descriptors = registry.getEditors(name);
61
62                 for (int i = 0; i < descriptors.length; i++) {
63                         if (descriptors[i].getId().equals(WebUI.EDITOR_ID)) {
64                                 return true;
65                         }
66                 }
67                 // String extension = getFileExtension(name);
68                 // if (extension == null) {
69                 // return false;
70                 // }
71                 // extension = extension.toLowerCase();
72                 // PHP_EXTENSIONS = getExtensions();
73                 // if (PHP_EXTENSIONS == null) {
74                 // return false;
75                 // }
76                 // for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
77                 // if (extension.equals(PHP_EXTENSIONS[i])) {
78                 // return true;
79                 // }
80                 // }
81                 return false;
82         }
83
84         /**
85          * Returns true iff the file extension is a valid PHP Unit name
86          * implementation is not creating extra strings.
87          */
88 //      public final static boolean isValidPHPUnitName(String filename) {
89 //              return PHPFileUtil.isPHPFileName(filename);
90 //      }
91
92         /**
93          * @return Returns the PHP extensions.
94          */
95         // public static String[] getExtensions() {
96         // if (PHP_EXTENSIONS == null) {
97         // ArrayList list = new ArrayList();
98         // final IPreferenceStore store =
99         // PHPeclipsePlugin.getDefault().getPreferenceStore();
100         // String extensions =
101         // store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
102         // extensions = extensions.trim();
103         // if (extensions.length() != 0) {
104         // StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
105         // String token;
106         // while (tokenizer.hasMoreTokens()) {
107         // token = tokenizer.nextToken();
108         // if (token != null && token.length() >= 1) {
109         // list.add(token);
110         // }
111         // }
112         // if (list.size() != 0) {
113         // PHP_EXTENSIONS = new String[list.size()];
114         // for (int i = 0; i < list.size(); i++) {
115         // PHP_EXTENSIONS[i] = (String) list.get(i);
116         // }
117         // }
118         // }
119         // }
120         // return PHP_EXTENSIONS;
121         // }
122         /**
123          * @param php_extensions
124          *            The PHP extensions to set.
125          */
126         // public static void setExtensions(String[] php_extensions) {
127         // PHP_EXTENSIONS = php_extensions;
128         // }
129         /**
130          * Creata the file for the given absolute file path
131          * 
132          * @param absoluteFilePath
133          * @param project
134          * @return the file for the given absolute file path or <code>null</code>
135          *         if no existing file can be found
136          */
137 //      public static IFile createFile(IPath absoluteFilePath, IProject project) {
138 //              if (absoluteFilePath == null || project == null) {
139 //                      return null;
140 //              }
141 //
142 //              String projectPath = project.getFullPath().toString();
143 //              String filePath = absoluteFilePath.toString().substring(
144 //                              projectPath.length() + 1);
145 //              return project.getFile(filePath);
146 //
147 //      }
148
149         /**
150          * Determine the path of an include name string
151          * 
152          * @param includeNameString
153          * @param resource
154          * @param project
155          * @return the path for the given include filename or <code>null</code> if
156          *         no existing file can be found
157          */
158         public static IPath determineFilePath(String includeNameString,
159                         IResource resource, IProject project) {
160                 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
161                 IPath resourcePath = resource.getProjectRelativePath();
162
163                 IPath path = null;
164                 
165                 // script location based
166                 path = project.getFullPath().append(resourcePath.removeLastSegments(1))
167                                 .append(includeNameString);
168                 //path = 
169                 if (fileExists(path, false)) {
170                         return path;
171                 }
172                 // project root based
173                 path = project.getFullPath().append(includeNameString);
174                 if (fileExists(path, false)) {
175                         return path;
176                 }
177                 
178                 // DocumentRoot (absolute path) based
179                 path = documentRootPath.append(includeNameString);
180                 if (fileExists(path, true)) {
181                         return path;
182                 }
183
184                 // IncludePaths settings (absolute path) based
185                 List includePaths = ProjectPrefUtil.getIncludePaths(project);
186                 if (includePaths.size() > 0) {
187                         for (int i = 0; i < includePaths.size(); i++) {
188                                 path = new Path(includePaths.get(i).toString())
189                                                 .append(includeNameString);
190                                 if (fileExists(path, true)) {
191                                         return path;
192                                 }
193                         }
194                 }
195                 return null;
196         }
197
198         private static boolean fileExists(IPath path, boolean absolute) {
199                 File file = path.toFile();
200                 if (file.exists()) {
201                         return true;
202                 }
203                 if (!absolute) {
204                         IFile ifile = FileBuffers.getWorkspaceFileAtLocation(path);
205                         if (ifile != null) {
206                             IResource resource = ifile;
207                 if (resource.exists()) {
208                     return true;
209                 }
210                         }
211                 }
212                 return false;
213         }
214 }