Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / Partition.java
1 /*
2  * Created on 28.04.2003
3  *
4  */
5 package net.sourceforge.phpeclipse.phpeditor.php;
6
7 import org.eclipse.jface.text.BadLocationException;
8 import org.eclipse.jface.text.IDocument;
9 import org.eclipse.jface.text.rules.ICharacterScanner;
10 import org.eclipse.jface.text.rules.IToken;
11 import org.eclipse.jface.text.rules.Token;
12
13
14 /**
15  * Defines a partition in a phpdocument. This class keeps tracks of
16  * partitions contained within other partitions.
17  * 
18  * @author Stefan Langer
19  * @version $Revision: 1.2 $
20  */
21 public abstract class Partition
22 {
23         private IDocument fDocument;
24         private IToken fContentToken;
25         private String fParentPartition;
26         private char[] fTextBuffer;
27         private int fOffset = 0;
28         private char entry;
29         private char exit;
30         private boolean inDoubleString = false;
31         private boolean inSingleString = false;
32         
33         public Partition(IDocument document, char[] delim, String contentType, String parentPartition)
34         {
35                 fDocument = document;
36                 fContentToken = new Token(contentType);
37                 fParentPartition = parentPartition;
38                 entry = delim[0];
39                 exit = delim[1];
40         }
41         
42         public Partition(IDocument document, char[] delim, String contentType)
43         {
44                 this(document, delim, contentType, IPHPPartitionScannerConstants.HTML);
45         }
46         
47         /**
48          * Checks wether the specified type is allowed within this 
49          * partition type.
50          * 
51      * @param type The type of the partition to check.
52      * 
53      * @return <code>true</code> if the partition is allowed within this
54      *                  partition otherwise <code>false</code>.
55      */
56     abstract protected boolean allowedPartition(String type);
57     
58     abstract protected boolean scan();
59     
60     protected boolean isEnd()
61     {
62         return fOffset >= fTextBuffer.length;           
63     }
64     
65     protected int read()
66     {
67         if(fOffset > fTextBuffer.length)
68                 return ICharacterScanner.EOF;
69         
70         char ret = fTextBuffer[fOffset++];
71         switch(ret)
72         {
73                 case '\'':
74                         if(!inDoubleString)
75                                 inSingleString = !inSingleString;
76                         break;
77                 case '"':
78                         if(!inSingleString)
79                                 inDoubleString = !inDoubleString;
80                         break;
81         }
82         return ret;
83     }
84     
85     protected void unread(int i)
86     {
87         for (int j = 0; j < i && fOffset > 0; j++)
88         {
89             char read = fTextBuffer[--fOffset];
90
91             switch (read)
92             {
93                 case '\'' :
94                     if (!inDoubleString)
95                         inSingleString = !inSingleString;
96                     break;
97                 case '"' :
98                     if (!inSingleString)
99                         inDoubleString = !inDoubleString;
100                     break;
101             }
102         } // END FOR
103     }
104     
105     public boolean scanRange(int offset, int length)
106         throws BadLocationException
107     {   // short circuit scanning if entry is not correct
108         if (fDocument.getChar(offset) != entry)
109             return false;
110                 // read the full range into the internal buffer
111                 fOffset = 0;
112                 inSingleString = false;
113                 inDoubleString = false;
114         fTextBuffer = fDocument.get(offset, length).toCharArray();
115         return scan();
116     }
117     
118     protected boolean checkPattern(String pattern, boolean ignoreCase)
119     {
120         char[] checkPattern = pattern.toCharArray();
121         int offset = fOffset;
122         for(int i=0; i<checkPattern.length; i++)
123         {
124                 if(isEnd() || !letterEquals(read(), checkPattern[i], ignoreCase))
125                 {
126                         fOffset = offset;
127                         return false;
128                 }
129         }
130         return true;
131     }
132         
133         private boolean letterEquals(int test, char letter, boolean ignoreCase)
134         {
135                 if (test == letter)
136                         return true;
137                 else if (
138                         ignoreCase
139                                 && Character.isLowerCase(letter)
140                                 && test == Character.toUpperCase(letter))
141                         return true;
142                 else if (
143                         ignoreCase
144                                 && Character.isUpperCase(letter)
145                                 && test == Character.toLowerCase(letter))
146                         return true;
147
148                 return false;
149         }
150     
151
152     
153     protected boolean inString()
154     {
155         return inDoubleString || inSingleString;
156     }
157     
158     
159     
160     
161     public IToken getToken()
162     {
163                 return fContentToken;
164     }
165     
166     public int getLength()
167     {
168         return fOffset;
169     }
170     
171     /**
172      * @return
173      */
174     public IDocument getDocument()
175     {
176         return fDocument;
177     }
178
179     /**
180      * @return
181      */
182     public String getParentPartition()
183     {
184         return fParentPartition;
185     }
186
187     /**
188      * @param document
189      */
190     public void setDocument(IDocument document)
191     {
192         fDocument = document;
193     }
194
195     /**
196      * @param string
197      */
198     public void setParentPartition(String string)
199     {
200         fParentPartition = string;
201     }
202     
203     
204     /**
205      * @return
206      */
207     public char getEntry()
208     {
209         return entry;
210     }
211
212     /**
213      * @return
214      */
215     public char getExit()
216     {
217         return exit;
218     }
219
220     /**
221      * @return
222      */
223     protected int getOffset()
224     {
225         return fOffset;
226     }
227
228     /**
229      * @param i
230      */
231     protected void setOffset(int i)
232     {
233         fOffset = i;
234     }
235
236 }