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