added *.module (Drupal) as a valid extension
[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 org.eclipse.core.resources.IFile;
8
9 /**
10  * @author khartlage
11  *
12  */
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$
24   public final static char[] SUFFIX_module = ".module".toCharArray(); //$NON-NLS-1$
25   public final static char[] SUFFIX_MODULE = ".MODULE".toCharArray(); //$NON-NLS-1$
26   
27   public final static char[] SUFFIX_inc = ".inc".toCharArray(); //$NON-NLS-1$
28   public final static char[] SUFFIX_INC = ".INC".toCharArray(); //$NON-NLS-1$
29   public final static char[] SUFFIX_html = ".html".toCharArray(); //$NON-NLS-1$
30   public final static char[] SUFFIX_HTML = ".HTML".toCharArray(); //$NON-NLS-1$
31   public final static char[] SUFFIX_tpl = ".tpl".toCharArray(); //$NON-NLS-1$
32   public final static char[] SUFFIX_TPL = ".TPL".toCharArray(); //$NON-NLS-1$
33
34   public  static boolean isPHPFile(IFile file) {
35     String extension = file.getFileExtension();
36     return isPHPFileName(file.getLocation().toString());
37   }
38
39   /**
40    * Returns true iff str.toLowerCase().endsWith(".php")
41    * implementation is not creating extra strings.
42    */
43   public final static boolean isPHPFileName(String name) {
44     return isPHP_FileName(name) || 
45            isPHP3_FileName(name) || 
46            isPHP4_FileName(name) ||  
47            isPHP5_FileName(name) ||
48            isModule_FileName(name) || 
49            isPHTML_FileName(name) || 
50            isINC_FileName(name);
51   }
52   //  static public boolean isPHPFile(String extension) {
53   //    if ("php".equalsIgnoreCase(extension)
54   //      || "php3".equalsIgnoreCase(extension)
55   //      || "php4".equalsIgnoreCase(extension)
56   //      || "inc".equalsIgnoreCase(extension)) {
57   //      return true;
58   //    }
59   //    return false;
60   //  }
61
62   /**
63    * Returns true iff str.toLowerCase().endsWith(".php")
64    * implementation is not creating extra strings.
65    */
66   private final static boolean isPHP_FileName(String name) {
67     int nameLength = name == null ? 0 : name.length();
68     int suffixLength = SUFFIX_PHP.length;
69     if (nameLength < suffixLength)
70       return false;
71
72     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
73       char c = name.charAt(offset + i);
74       if (c != SUFFIX_php[i] && c != SUFFIX_PHP[i])
75         return false;
76     }
77     return true;
78   }
79
80   /**
81    * Returns true iff str.toLowerCase().endsWith(".php3")
82    * implementation is not creating extra strings.
83    */
84   private final static boolean isPHP3_FileName(String name) {
85     int nameLength = name == null ? 0 : name.length();
86     int suffixLength = SUFFIX_PHP3.length;
87     if (nameLength < suffixLength)
88       return false;
89
90     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
91       char c = name.charAt(offset + i);
92       if (c != SUFFIX_php3[i] && c != SUFFIX_PHP3[i])
93         return false;
94     }
95     return true;
96   }
97
98   /**
99    * Returns true iff str.toLowerCase().endsWith(".php4")
100    * implementation is not creating extra strings.
101    */
102   private final static boolean isPHP4_FileName(String name) {
103     int nameLength = name == null ? 0 : name.length();
104     int suffixLength = SUFFIX_PHP4.length;
105     if (nameLength < suffixLength)
106       return false;
107
108     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
109       char c = name.charAt(offset + i);
110       if (c != SUFFIX_php4[i] && c != SUFFIX_PHP4[i])
111         return false;
112     }
113     return true;
114   }
115
116   /**
117    * Returns true iff str.toLowerCase().endsWith(".php5")
118    * implementation is not creating extra strings.
119    */
120   private final static boolean isPHP5_FileName(String name) {
121     int nameLength = name == null ? 0 : name.length();
122     int suffixLength = SUFFIX_PHP5.length;
123     if (nameLength < suffixLength)
124       return false;
125
126     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
127       char c = name.charAt(offset + i);
128       if (c != SUFFIX_php5[i] && c != SUFFIX_PHP5[i])
129         return false;
130     }
131     return true;
132   }
133   /**
134    * Returns true iff str.toLowerCase().endsWith(".module")
135    * implementation is not creating extra strings.
136    */
137   private final static boolean isModule_FileName(String name) {
138     int nameLength = name == null ? 0 : name.length();
139     int suffixLength = SUFFIX_MODULE.length;
140     if (nameLength < suffixLength)
141       return false;
142
143     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
144       char c = name.charAt(offset + i);
145       if (c != SUFFIX_module[i] && c != SUFFIX_MODULE[i])
146         return false;
147     }
148     return true;
149   }
150  /**
151   * Returns true iff str.toLowerCase().endsWith(".phtml")
152   * implementation is not creating extra strings.
153   */
154  private final static boolean isPHTML_FileName(String name) {
155    int nameLength = name == null ? 0 : name.length();
156    int suffixLength = SUFFIX_PHTML.length; 
157    if (nameLength < suffixLength)
158      return false;
159
160    for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
161      char c = name.charAt(offset + i);
162      if (c != SUFFIX_phtml[i] && c != SUFFIX_PHTML[i])
163        return false;
164    }
165    return true;
166  }
167   
168   /**
169    * Returns true iff str.toLowerCase().endsWith(".inc")
170    * implementation is not creating extra strings.
171    */
172   private final static boolean isINC_FileName(String name) {
173     int nameLength = name == null ? 0 : name.length();
174     int suffixLength = SUFFIX_INC.length;
175     if (nameLength < suffixLength)
176       return false;
177
178     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
179       char c = name.charAt(offset + i);
180       if (c != SUFFIX_inc[i] && c != SUFFIX_INC[i])
181         return false;
182     }
183     return true;
184   }
185   
186   /**
187    * Returns true iff str.toLowerCase().endsWith(".html")
188    * implementation is not creating extra strings.
189    */
190   public final static boolean isHTML_FileName(String name) {
191     int nameLength = name == null ? 0 : name.length();
192     int suffixLength = SUFFIX_HTML.length;
193     if (nameLength < suffixLength)
194       return false;
195
196     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
197       char c = name.charAt(offset + i);
198       if (c != SUFFIX_html[i] && c != SUFFIX_HTML[i])
199         return false;
200     }
201     return true;
202   }
203   /**
204    * Returns true iff str.toLowerCase().endsWith(".tpl")
205    * implementation is not creating extra strings.
206    */
207   public final static boolean isTPL_FileName(String name) {
208     int nameLength = name == null ? 0 : name.length();
209     int suffixLength = SUFFIX_TPL.length;
210     if (nameLength < suffixLength)
211       return false;
212
213     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
214       char c = name.charAt(offset + i);
215       if (c != SUFFIX_tpl[i] && c != SUFFIX_TPL[i])
216         return false;
217     }
218     return true;
219   }
220   
221   /**
222          * Returns true iff the file extension is a valid PHP Unit name
223          * implementation is not creating extra strings.
224          */
225         public final static boolean isValidPHPUnitName(String filename) {
226                 return PHPFileUtil.isPHPFileName(filename) ||
227                        PHPFileUtil.isHTML_FileName(filename) ||
228                            PHPFileUtil.isTPL_FileName(filename);
229         }
230 }