261673fd33a8321978940f0a1bc93ef08d7fb9e4
[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 import org.eclipse.ui.IEditorDescriptor;
22 import org.eclipse.ui.IEditorRegistry;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.PlatformUI;
25
26 public class PHPFileUtil {
27 //  private static String[] PHP_EXTENSIONS = null;
28
29   public final static String[] SMARTY_EXTENSIONS = { "tpl" };
30
31   public static boolean isPHPFile(IFile file) {
32 //    String extension = file.getFileExtension();
33     return isPHPFileName(file.getLocation().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 creating extra strings.
47    */
48   public final static boolean isPHPFileName(String name) {
49
50         IWorkbench workbench = PlatformUI.getWorkbench();
51         IEditorRegistry registry = workbench.getEditorRegistry();
52         IEditorDescriptor[] descriptors = registry.getEditors(name);
53
54         for (int i = 0; i < descriptors.length; i++) {
55                         if (descriptors[i].getId().equals(PHPeclipsePlugin.EDITOR_ID)) {
56         return true;
57                         }
58                 }
59 //    String extension = getFileExtension(name);
60 //    if (extension == null) {
61 //      return false;
62 //    }
63 //    extension = extension.toLowerCase();
64 //    PHP_EXTENSIONS = getExtensions();
65 //    if (PHP_EXTENSIONS == null) {
66 //      return false;
67 //    }
68 //    for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
69 //      if (extension.equals(PHP_EXTENSIONS[i])) {
70 //        return true;
71 //      }
72 //    }
73     return false;
74   }
75
76   /**
77    * Returns true iff the file extension is a valid PHP Unit name implementation is not creating extra strings.
78    */
79   public final static boolean isValidPHPUnitName(String filename) {
80     return PHPFileUtil.isPHPFileName(filename);
81   }
82
83   /**
84    * @return Returns the PHP extensions.
85    */
86 //  public static String[] getExtensions() {
87 //    if (PHP_EXTENSIONS == null) {
88 //      ArrayList list = new ArrayList();
89 //      final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
90 //      String extensions = store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
91 //      extensions = extensions.trim();
92 //      if (extensions.length() != 0) {
93 //        StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
94 //        String token;
95 //        while (tokenizer.hasMoreTokens()) {
96 //          token = tokenizer.nextToken();
97 //          if (token != null && token.length() >= 1) {
98 //            list.add(token);
99 //          }
100 //        }
101 //        if (list.size() != 0) {
102 //          PHP_EXTENSIONS = new String[list.size()];
103 //          for (int i = 0; i < list.size(); i++) {
104 //            PHP_EXTENSIONS[i] = (String) list.get(i);
105 //          }
106 //        }
107 //      }
108 //    }
109 //    return PHP_EXTENSIONS;
110 //  }
111
112   /**
113    * @param php_extensions
114    *          The PHP extensions to set.
115    */
116 //  public static void setExtensions(String[] php_extensions) {
117 //    PHP_EXTENSIONS = php_extensions;
118 //  }
119
120   /**
121    * Creata the file for the given absolute file path
122    *
123    * @param absoluteFilePath
124    * @param project
125    * @return the file for the given absolute file path or <code>null</code> if no existing file can be found
126    */
127   public static IFile createFile(IPath absoluteFilePath, IProject project) {
128     if (absoluteFilePath == null || project == null) {
129       return null;
130     }
131
132     String projectPath = project.getLocation().toString();
133     String filePath = absoluteFilePath.toString().substring(projectPath.length() + 1);
134     return project.getFile(filePath);
135
136   }
137
138   /**
139    * Determine the path of an include name string
140    *
141    * @param includeNameString
142    * @param resource
143    * @param project
144    * @return the path for the given include filename or <code>null</code> if no existing file can be found
145    */
146   public static IPath determineFilePath(String includeNameString, IResource resource, IProject project) {
147     IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
148     IPath resourcePath = resource.getProjectRelativePath();
149
150     File file = null;
151     IPath path = null;
152     path = documentRootPath.append(includeNameString);
153     file = path.toFile();
154     if (file.exists()) {
155       return path;
156     }
157
158     if (includeNameString.startsWith("../")) {
159       path = project.getLocation().append(resourcePath.removeLastSegments(1));
160       path = path.append(includeNameString);
161       file = path.toFile();
162       if (file.exists()) {
163         return path;
164       }
165     }
166
167     // includeNameString contains no path separator
168     path = project.getLocation().append(resourcePath.removeLastSegments(1));
169     path = path.append(includeNameString);
170     file = path.toFile();
171     if (file.exists()) {
172       return path;
173     }
174     //    }
175
176     List includePaths = ProjectPrefUtil.getIncludePaths(project);
177     if (includePaths.size() > 0) {
178       for (int i = 0; i < includePaths.size(); i++) {
179         path = new Path(includePaths.get(i).toString()).append(includeNameString);
180         file = path.toFile();
181         if (file.exists()) {
182           return path;
183         }
184       }
185     }
186     return null;
187   }
188 }