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