Delete association of htmledit.gif for the PHPUnitEditor
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / util / PHPFileUtil.java
1 /*
2  * Created on 09.08.2003
3  *  
4  */
5 package net.sourceforge.phpdt.internal.ui.util;
6
7 import java.io.File;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.StringTokenizer;
11
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jface.preference.IPreferenceStore;
21
22 public class PHPFileUtil {
23   private static String[] PHP_EXTENSIONS = null;
24
25   public final static String[] SMARTY_EXTENSIONS = { "tpl" };
26
27   public static boolean isPHPFile(IFile file) {
28     String extension = file.getFileExtension();
29     return isPHPFileName(file.getLocation().toString());
30   }
31
32   public final static String getFileExtension(String name) {
33     int index = name.lastIndexOf('.');
34     if (index == -1)
35       return null;
36     if (index == (name.length() - 1))
37       return null; //$NON-NLS-1$
38     return name.substring(index + 1);
39   }
40
41   /**
42    * Returns true iff str.toLowerCase().endsWith(".php") implementation is not creating extra strings.
43    */
44   public final static boolean isPHPFileName(String name) {
45     String extension = getFileExtension(name);
46     if (extension == null) {
47       return false;
48     }
49     extension = extension.toLowerCase();
50     PHP_EXTENSIONS = getExtensions();
51     if (PHP_EXTENSIONS == null) {
52       return false;
53     }
54     for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
55       if (extension.equals(PHP_EXTENSIONS[i])) {
56         return true;
57       }
58     }
59     return false;
60   }
61
62   /**
63    * Returns true iff the file extension is a valid PHP Unit name implementation is not creating extra strings.
64    */
65   public final static boolean isValidPHPUnitName(String filename) {
66     return PHPFileUtil.isPHPFileName(filename);
67   }
68
69   /**
70    * @return Returns the PHP extensions.
71    */
72   public static String[] getExtensions() {
73     if (PHP_EXTENSIONS == null) {
74       ArrayList list = new ArrayList();
75       final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
76       String extensions = store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
77       extensions = extensions.trim();
78       if (extensions.length() != 0) {
79         StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
80         String token;
81         while (tokenizer.hasMoreTokens()) {
82           token = tokenizer.nextToken();
83           if (token != null && token.length() >= 1) {
84             list.add(token);
85           }
86         }
87         if (list.size() != 0) {
88           PHP_EXTENSIONS = new String[list.size()];
89           for (int i = 0; i < list.size(); i++) {
90             PHP_EXTENSIONS[i] = (String) list.get(i);
91           }
92         }
93       }
94     }
95     return PHP_EXTENSIONS;
96   }
97
98   /**
99    * @param php_extensions
100    *          The PHP extensions to set.
101    */
102   public static void setExtensions(String[] php_extensions) {
103     PHP_EXTENSIONS = php_extensions;
104   }
105
106   /**
107    * Creata the file for the given absolute file path
108    * 
109    * @param absoluteFilePath
110    * @param project
111    * @return the file for the given absolute file path or <code>null</code> if no existing file can be found
112    */
113   public static IFile createFile(IPath absoluteFilePath, IProject project) {
114     if (absoluteFilePath == null || project == null) {
115       return null;
116     }
117
118     String projectPath = project.getLocation().toString();
119     String filePath = absoluteFilePath.toString().substring(projectPath.length() + 1);
120     return project.getFile(filePath);
121
122   }
123
124   /**
125    * Determine the path of an include name string
126    * 
127    * @param includeNameString
128    * @param resource
129    * @param project
130    * @return the path for the given include filename or <code>null</code> if no existing file can be found
131    */
132   public static IPath determineFilePath(String includeNameString, IResource resource, IProject project) {
133     IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
134     IPath resourcePath = resource.getProjectRelativePath();
135
136     File file = null;
137     IPath path = null;
138     path = documentRootPath.append(includeNameString);
139     file = path.toFile();
140     if (file.exists()) {
141       return path;
142     }
143
144     if (includeNameString.startsWith("../")) {
145       path = project.getLocation().append(resourcePath.removeLastSegments(1));
146       path = path.append(includeNameString);
147       file = path.toFile();
148       if (file.exists()) {
149         return path;
150       }
151     }
152
153     //    int index = includeNameString.indexOf('/');
154     //    if (index < 0) {
155     // includeNameString contains no path separator
156     path = project.getLocation().append(resourcePath.removeLastSegments(1));
157     path = path.append(includeNameString);
158     file = path.toFile();
159     if (file.exists()) {
160       return path;
161     }
162     //    }
163
164     List includePaths = ProjectPrefUtil.getIncludePaths(project);
165     if (includePaths.size() > 0) {
166       for (int i = 0; i < includePaths.size(); i++) {
167         path = new Path(includePaths.get(i).toString()).append(includeNameString);
168         file = path.toFile();
169         if (file.exists()) {
170           return path;
171         }
172       }
173     }
174     return null;
175   }
176 }