2 * Created on 09.08.2003
5 package net.sourceforge.phpdt.internal.ui.util;
10 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
11 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
13 import org.eclipse.core.filebuffers.FileBuffers;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.ui.IEditorDescriptor;
20 import org.eclipse.ui.IEditorRegistry;
21 import org.eclipse.ui.IWorkbench;
22 import org.eclipse.ui.PlatformUI;
24 public class PHPFileUtil {
25 // private static String[] PHP_EXTENSIONS = null;
27 public final static String[] SMARTY_EXTENSIONS = { "tpl" };
29 public static boolean isPHPFile(IFile file) {
30 return isPHPFileName(file.getFullPath().toString());
33 // public final static String getFileExtension(String name) {
34 // int index = name.lastIndexOf('.');
37 // if (index == (name.length() - 1))
38 // return null; //$NON-NLS-1$
39 // return name.substring(index + 1);
43 * Returns true iff str.toLowerCase().endsWith(".php") implementation is not
44 * creating extra strings.
46 public final static boolean isPHPFileName(String name) {
48 // avoid handling a file without base name, e.g. ".php", which is a
50 // Eclipse resource name
51 File file = new File(name);
52 if (file.getName().startsWith(".")) {
55 IWorkbench workbench = PlatformUI.getWorkbench();
56 IEditorRegistry registry = workbench.getEditorRegistry();
57 IEditorDescriptor[] descriptors = registry.getEditors(name);
59 for (int i = 0; i < descriptors.length; i++) {
60 if (descriptors[i].getId().equals(PHPeclipsePlugin.EDITOR_ID)) {
64 // String extension = getFileExtension(name);
65 // if (extension == null) {
68 // extension = extension.toLowerCase();
69 // PHP_EXTENSIONS = getExtensions();
70 // if (PHP_EXTENSIONS == null) {
73 // for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
74 // if (extension.equals(PHP_EXTENSIONS[i])) {
82 * Returns true iff the file extension is a valid PHP Unit name
83 * implementation is not creating extra strings.
85 public final static boolean isValidPHPUnitName(String filename) {
86 return PHPFileUtil.isPHPFileName(filename);
90 * @return Returns the PHP extensions.
92 // public static String[] getExtensions() {
93 // if (PHP_EXTENSIONS == null) {
94 // ArrayList list = new ArrayList();
95 // final IPreferenceStore store =
96 // PHPeclipsePlugin.getDefault().getPreferenceStore();
97 // String extensions =
98 // store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
99 // extensions = extensions.trim();
100 // if (extensions.length() != 0) {
101 // StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
103 // while (tokenizer.hasMoreTokens()) {
104 // token = tokenizer.nextToken();
105 // if (token != null && token.length() >= 1) {
109 // if (list.size() != 0) {
110 // PHP_EXTENSIONS = new String[list.size()];
111 // for (int i = 0; i < list.size(); i++) {
112 // PHP_EXTENSIONS[i] = (String) list.get(i);
117 // return PHP_EXTENSIONS;
120 * @param php_extensions
121 * The PHP extensions to set.
123 // public static void setExtensions(String[] php_extensions) {
124 // PHP_EXTENSIONS = php_extensions;
127 * Creata the file for the given absolute file path
129 * @param absoluteFilePath
131 * @return the file for the given absolute file path or <code>null</code>
132 * if no existing file can be found
134 public static IFile createFile(IPath absoluteFilePath, IProject project) {
135 if (absoluteFilePath == null || project == null) {
139 String projectPath = project.getLocation().toString();
140 String filePath = absoluteFilePath.toString().substring(
141 projectPath.length() + 1);
142 return project.getFile(filePath);
147 * Determine the path of an include name string
149 * @param includeNameString
152 * @return the path for the given include filename or <code>null</code> if
153 * no existing file can be found
155 public static IPath determineFilePath(String includeNameString,
156 IResource resource, IProject project) {
157 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
158 IPath resourcePath = resource.getProjectRelativePath();
163 // script location based
164 path = project.getLocation().append(resourcePath.removeLastSegments(1))
165 .append(includeNameString);
166 if (fileExists(path, false)) {
170 // project root based
171 path = project.getLocation().append(includeNameString);
172 if (fileExists(path, false)) {
176 // DocumentRoot (absolute path) based
177 path = documentRootPath.append(includeNameString);
178 if (fileExists(path, true)) {
182 // IncludePaths settings (absolute path) based
183 List includePaths = ProjectPrefUtil.getIncludePaths(project);
184 if (includePaths.size() > 0) {
185 for (int i = 0; i < includePaths.size(); i++) {
186 path = new Path(includePaths.get(i).toString())
187 .append(includeNameString);
188 if (fileExists(path, true)) {
196 private static boolean fileExists(IPath path, boolean absolute) {
197 File file = path.toFile();
202 IFile ifile = FileBuffers.getWorkspaceFileAtLocation(path);
204 file = ifile.getLocation().toFile();