2 * Created on 28.04.2003
5 package net.sourceforge.phpeclipse.phpeditor.php;
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;
15 * Defines a partition in a phpdocument. This class keeps tracks of
16 * partitions contained within other partitions.
18 * @author Stefan Langer
19 * @version $Revision: 1.2 $
21 public abstract class Partition
23 private IDocument fDocument;
24 private IToken fContentToken;
25 private String fParentPartition;
26 private char[] fTextBuffer;
27 private int fOffset = 0;
30 private boolean inDoubleString = false;
31 private boolean inSingleString = false;
33 public Partition(IDocument document, char[] delim, String contentType, String parentPartition)
36 fContentToken = new Token(contentType);
37 fParentPartition = parentPartition;
42 public Partition(IDocument document, char[] delim, String contentType)
44 this(document, delim, contentType, IPHPPartitionScannerConstants.HTML);
48 * Checks wether the specified type is allowed within this
51 * @param type The type of the partition to check.
53 * @return <code>true</code> if the partition is allowed within this
54 * partition otherwise <code>false</code>.
56 abstract protected boolean allowedPartition(String type);
58 abstract protected boolean scan();
60 protected boolean isEnd()
62 return fOffset >= fTextBuffer.length;
67 if(fOffset > fTextBuffer.length)
68 return ICharacterScanner.EOF;
70 char ret = fTextBuffer[fOffset++];
75 inSingleString = !inSingleString;
79 inDoubleString = !inDoubleString;
85 protected void unread(int i)
87 for (int j = 0; j < i && fOffset > 0; j++)
89 char read = fTextBuffer[--fOffset];
95 inSingleString = !inSingleString;
99 inDoubleString = !inDoubleString;
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)
110 // read the full range into the internal buffer
112 inSingleString = false;
113 inDoubleString = false;
114 fTextBuffer = fDocument.get(offset, length).toCharArray();
118 protected boolean checkPattern(String pattern, boolean ignoreCase)
120 char[] checkPattern = pattern.toCharArray();
121 int offset = fOffset;
122 for(int i=0; i<checkPattern.length; i++)
124 if(isEnd() || !letterEquals(read(), checkPattern[i], ignoreCase))
133 private boolean letterEquals(int test, char letter, boolean ignoreCase)
139 && Character.isLowerCase(letter)
140 && test == Character.toUpperCase(letter))
144 && Character.isUpperCase(letter)
145 && test == Character.toLowerCase(letter))
153 protected boolean inString()
155 return inDoubleString || inSingleString;
161 public IToken getToken()
163 return fContentToken;
166 public int getLength()
174 public IDocument getDocument()
182 public String getParentPartition()
184 return fParentPartition;
190 public void setDocument(IDocument document)
192 fDocument = document;
198 public void setParentPartition(String string)
200 fParentPartition = string;
207 public char getEntry()
215 public char getExit()
223 protected int getOffset()
231 protected void setOffset(int i)