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