added class fields to outline
[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_inc = ".inc".toCharArray(); //$NON-NLS-1$
21   public final static char[] SUFFIX_INC = ".INC".toCharArray(); //$NON-NLS-1$
22   public final static char[] SUFFIX_html = ".html".toCharArray(); //$NON-NLS-1$
23   public final static char[] SUFFIX_HTML = ".HTML".toCharArray(); //$NON-NLS-1$
24   public final static char[] SUFFIX_tpl = ".tpl".toCharArray(); //$NON-NLS-1$
25   public final static char[] SUFFIX_TPL = ".TPL".toCharArray(); //$NON-NLS-1$
26
27   public  static boolean isPHPFile(IFile file) {
28     String extension = file.getFileExtension();
29     return isPHPFileName(file.getLocation().toString());
30   }
31
32   /**
33    * Returns true iff str.toLowerCase().endsWith(".php")
34    * implementation is not creating extra strings.
35    */
36   public final static boolean isPHPFileName(String name) {
37     return isPHP_FileName(name) || isPHP3_FileName(name) || isPHP4_FileName(name) || isINC_FileName(name);
38   }
39   //  static public boolean isPHPFile(String extension) {
40   //    if ("php".equalsIgnoreCase(extension)
41   //      || "php3".equalsIgnoreCase(extension)
42   //      || "php4".equalsIgnoreCase(extension)
43   //      || "inc".equalsIgnoreCase(extension)) {
44   //      return true;
45   //    }
46   //    return false;
47   //  }
48
49   /**
50    * Returns true iff str.toLowerCase().endsWith(".php")
51    * implementation is not creating extra strings.
52    */
53   private final static boolean isPHP_FileName(String name) {
54     int nameLength = name == null ? 0 : name.length();
55     int suffixLength = SUFFIX_PHP.length;
56     if (nameLength < suffixLength)
57       return false;
58
59     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
60       char c = name.charAt(offset + i);
61       if (c != SUFFIX_php[i] && c != SUFFIX_PHP[i])
62         return false;
63     }
64     return true;
65   }
66
67   /**
68    * Returns true iff str.toLowerCase().endsWith(".php3")
69    * implementation is not creating extra strings.
70    */
71   private final static boolean isPHP3_FileName(String name) {
72     int nameLength = name == null ? 0 : name.length();
73     int suffixLength = SUFFIX_PHP3.length;
74     if (nameLength < suffixLength)
75       return false;
76
77     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
78       char c = name.charAt(offset + i);
79       if (c != SUFFIX_php3[i] && c != SUFFIX_PHP3[i])
80         return false;
81     }
82     return true;
83   }
84
85   /**
86    * Returns true iff str.toLowerCase().endsWith(".php4")
87    * implementation is not creating extra strings.
88    */
89   private final static boolean isPHP4_FileName(String name) {
90     int nameLength = name == null ? 0 : name.length();
91     int suffixLength = SUFFIX_PHP4.length;
92     if (nameLength < suffixLength)
93       return false;
94
95     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
96       char c = name.charAt(offset + i);
97       if (c != SUFFIX_php4[i] && c != SUFFIX_PHP4[i])
98         return false;
99     }
100     return true;
101   }
102
103   /**
104    * Returns true iff str.toLowerCase().endsWith(".inc")
105    * implementation is not creating extra strings.
106    */
107   private final static boolean isINC_FileName(String name) {
108     int nameLength = name == null ? 0 : name.length();
109     int suffixLength = SUFFIX_INC.length;
110     if (nameLength < suffixLength)
111       return false;
112
113     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
114       char c = name.charAt(offset + i);
115       if (c != SUFFIX_inc[i] && c != SUFFIX_INC[i])
116         return false;
117     }
118     return true;
119   }
120   
121   /**
122    * Returns true iff str.toLowerCase().endsWith(".html")
123    * implementation is not creating extra strings.
124    */
125   public final static boolean isHTML_FileName(String name) {
126     int nameLength = name == null ? 0 : name.length();
127     int suffixLength = SUFFIX_HTML.length;
128     if (nameLength < suffixLength)
129       return false;
130
131     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
132       char c = name.charAt(offset + i);
133       if (c != SUFFIX_html[i] && c != SUFFIX_HTML[i])
134         return false;
135     }
136     return true;
137   }
138   /**
139    * Returns true iff str.toLowerCase().endsWith(".tpl")
140    * implementation is not creating extra strings.
141    */
142   public final static boolean isTPL_FileName(String name) {
143     int nameLength = name == null ? 0 : name.length();
144     int suffixLength = SUFFIX_TPL.length;
145     if (nameLength < suffixLength)
146       return false;
147
148     for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
149       char c = name.charAt(offset + i);
150       if (c != SUFFIX_tpl[i] && c != SUFFIX_TPL[i])
151         return false;
152     }
153     return true;
154   }
155   
156   /**
157          * Returns true iff str.toLowerCase().endsWith(".java")
158          * implementation is not creating extra strings.
159          */
160         public final static boolean isValidPHPUnitName(String name) {
161                 return PHPFileUtil.isPHPFileName(name) ||
162                        PHPFileUtil.isHTML_FileName(name) ||
163                            PHPFileUtil.isTPL_FileName(name);
164         }
165 }