2 * Created on 09.08.2003
5 package net.sourceforge.phpdt.internal.ui.util;
7 import org.eclipse.core.resources.IFile;
13 public class PHPFileUtil {
14 public final static char[] SUFFIX_php = ".php".toCharArray(); //$NON-NLS-1$
15 public final static char[] SUFFIX_PHP = ".PHP".toCharArray(); //$NON-NLS-1$
16 public final static char[] SUFFIX_php3 = ".php3".toCharArray(); //$NON-NLS-1$
17 public final static char[] SUFFIX_PHP3 = ".PHP3".toCharArray(); //$NON-NLS-1$
18 public final static char[] SUFFIX_php4 = ".php4".toCharArray(); //$NON-NLS-1$
19 public final static char[] SUFFIX_PHP4 = ".PHP4".toCharArray(); //$NON-NLS-1$
20 public final static char[] SUFFIX_php5 = ".php5".toCharArray(); //$NON-NLS-1$
21 public final static char[] SUFFIX_PHP5 = ".PHP5".toCharArray(); //$NON-NLS-1$
22 public final static char[] SUFFIX_phtml = ".phtml".toCharArray(); //$NON-NLS-1$
23 public final static char[] SUFFIX_PHTML = ".PHTML".toCharArray(); //$NON-NLS-1$
25 public final static char[] SUFFIX_inc = ".inc".toCharArray(); //$NON-NLS-1$
26 public final static char[] SUFFIX_INC = ".INC".toCharArray(); //$NON-NLS-1$
27 public final static char[] SUFFIX_html = ".html".toCharArray(); //$NON-NLS-1$
28 public final static char[] SUFFIX_HTML = ".HTML".toCharArray(); //$NON-NLS-1$
29 public final static char[] SUFFIX_tpl = ".tpl".toCharArray(); //$NON-NLS-1$
30 public final static char[] SUFFIX_TPL = ".TPL".toCharArray(); //$NON-NLS-1$
32 public static boolean isPHPFile(IFile file) {
33 String extension = file.getFileExtension();
34 return isPHPFileName(file.getLocation().toString());
38 * Returns true iff str.toLowerCase().endsWith(".php")
39 * implementation is not creating extra strings.
41 public final static boolean isPHPFileName(String name) {
42 return isPHP_FileName(name) ||
43 isPHP3_FileName(name) ||
44 isPHP4_FileName(name) ||
45 isPHP5_FileName(name) ||
46 isPHTML_FileName(name) ||
49 // static public boolean isPHPFile(String extension) {
50 // if ("php".equalsIgnoreCase(extension)
51 // || "php3".equalsIgnoreCase(extension)
52 // || "php4".equalsIgnoreCase(extension)
53 // || "inc".equalsIgnoreCase(extension)) {
60 * Returns true iff str.toLowerCase().endsWith(".php")
61 * implementation is not creating extra strings.
63 private final static boolean isPHP_FileName(String name) {
64 int nameLength = name == null ? 0 : name.length();
65 int suffixLength = SUFFIX_PHP.length;
66 if (nameLength < suffixLength)
69 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
70 char c = name.charAt(offset + i);
71 if (c != SUFFIX_php[i] && c != SUFFIX_PHP[i])
78 * Returns true iff str.toLowerCase().endsWith(".php3")
79 * implementation is not creating extra strings.
81 private final static boolean isPHP3_FileName(String name) {
82 int nameLength = name == null ? 0 : name.length();
83 int suffixLength = SUFFIX_PHP3.length;
84 if (nameLength < suffixLength)
87 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
88 char c = name.charAt(offset + i);
89 if (c != SUFFIX_php3[i] && c != SUFFIX_PHP3[i])
96 * Returns true iff str.toLowerCase().endsWith(".php4")
97 * implementation is not creating extra strings.
99 private final static boolean isPHP4_FileName(String name) {
100 int nameLength = name == null ? 0 : name.length();
101 int suffixLength = SUFFIX_PHP4.length;
102 if (nameLength < suffixLength)
105 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
106 char c = name.charAt(offset + i);
107 if (c != SUFFIX_php4[i] && c != SUFFIX_PHP4[i])
114 * Returns true iff str.toLowerCase().endsWith(".php4")
115 * implementation is not creating extra strings.
117 private final static boolean isPHP5_FileName(String name) {
118 int nameLength = name == null ? 0 : name.length();
119 int suffixLength = SUFFIX_PHP5.length;
120 if (nameLength < suffixLength)
123 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
124 char c = name.charAt(offset + i);
125 if (c != SUFFIX_php5[i] && c != SUFFIX_PHP5[i])
132 * Returns true iff str.toLowerCase().endsWith(".php4")
133 * implementation is not creating extra strings.
135 private final static boolean isPHTML_FileName(String name) {
136 int nameLength = name == null ? 0 : name.length();
137 int suffixLength = SUFFIX_PHTML.length;
138 if (nameLength < suffixLength)
141 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
142 char c = name.charAt(offset + i);
143 if (c != SUFFIX_phtml[i] && c != SUFFIX_PHTML[i])
150 * Returns true iff str.toLowerCase().endsWith(".inc")
151 * implementation is not creating extra strings.
153 private final static boolean isINC_FileName(String name) {
154 int nameLength = name == null ? 0 : name.length();
155 int suffixLength = SUFFIX_INC.length;
156 if (nameLength < suffixLength)
159 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
160 char c = name.charAt(offset + i);
161 if (c != SUFFIX_inc[i] && c != SUFFIX_INC[i])
168 * Returns true iff str.toLowerCase().endsWith(".html")
169 * implementation is not creating extra strings.
171 public final static boolean isHTML_FileName(String name) {
172 int nameLength = name == null ? 0 : name.length();
173 int suffixLength = SUFFIX_HTML.length;
174 if (nameLength < suffixLength)
177 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
178 char c = name.charAt(offset + i);
179 if (c != SUFFIX_html[i] && c != SUFFIX_HTML[i])
185 * Returns true iff str.toLowerCase().endsWith(".tpl")
186 * implementation is not creating extra strings.
188 public final static boolean isTPL_FileName(String name) {
189 int nameLength = name == null ? 0 : name.length();
190 int suffixLength = SUFFIX_TPL.length;
191 if (nameLength < suffixLength)
194 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
195 char c = name.charAt(offset + i);
196 if (c != SUFFIX_tpl[i] && c != SUFFIX_TPL[i])
203 * Returns true iff the file extension is a valid PHP Unit name
204 * implementation is not creating extra strings.
206 public final static boolean isValidPHPUnitName(String filename) {
207 return PHPFileUtil.isPHPFileName(filename) ||
208 PHPFileUtil.isHTML_FileName(filename) ||
209 PHPFileUtil.isTPL_FileName(filename);