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