2 * Created on 28.04.2003
5 package net.sourceforge.phpeclipse.phpeditor.php;
7 import org.eclipse.jface.text.*;
8 import org.eclipse.jface.text.rules.*;
12 * Defines a partition in a phpdocument. This class keeps tracks of
13 * partitions contained within other partitions.
15 * @author Stefan Langer
16 * @version $Revision: 1.1 $
18 public abstract class Partition
20 private IDocument fDocument;
21 private IToken fContentToken;
22 private String fParentPartition;
23 private char[] fTextBuffer;
24 private int fOffset = 0;
27 private boolean inDoubleString = false;
28 private boolean inSingleString = false;
30 public Partition(IDocument document, char[] delim, String contentType, String parentPartition)
33 fContentToken = new Token(contentType);
34 fParentPartition = parentPartition;
39 public Partition(IDocument document, char[] delim, String contentType)
41 this(document, delim, contentType, IPHPPartitionScannerConstants.HTML);
45 * Checks wether the specified type is allowed within this
48 * @param type The type of the partition to check.
50 * @return <code>true</code> if the partition is allowed within this
51 * partition otherwise <code>false</code>.
53 abstract protected boolean allowedPartition(String type);
55 abstract protected boolean scan();
57 protected boolean isEnd()
59 return fOffset >= fTextBuffer.length;
64 if(fOffset > fTextBuffer.length)
65 return ICharacterScanner.EOF;
67 char ret = fTextBuffer[fOffset++];
72 inSingleString = !inSingleString;
76 inDoubleString = !inDoubleString;
82 protected void unread(int i)
84 for (int j = 0; j < i && fOffset > 0; j++)
86 char read = fTextBuffer[--fOffset];
92 inSingleString = !inSingleString;
96 inDoubleString = !inDoubleString;
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)
107 // read the full range into the internal buffer
109 inSingleString = false;
110 inDoubleString = false;
111 fTextBuffer = fDocument.get(offset, length).toCharArray();
115 protected boolean checkPattern(String pattern, boolean ignoreCase)
117 char[] checkPattern = pattern.toCharArray();
118 int offset = fOffset;
119 for(int i=0; i<checkPattern.length; i++)
121 if(isEnd() || !letterEquals(read(), checkPattern[i], ignoreCase))
130 private boolean letterEquals(int test, char letter, boolean ignoreCase)
136 && Character.isLowerCase(letter)
137 && test == Character.toUpperCase(letter))
141 && Character.isUpperCase(letter)
142 && test == Character.toLowerCase(letter))
150 protected boolean inString()
152 return inDoubleString || inSingleString;
158 public IToken getToken()
160 return fContentToken;
163 public int getLength()
171 public IDocument getDocument()
179 public String getParentPartition()
181 return fParentPartition;
187 public void setDocument(IDocument document)
189 fDocument = document;
195 public void setParentPartition(String string)
197 fParentPartition = string;
204 public char getEntry()
212 public char getExit()
220 protected int getOffset()
228 protected void setOffset(int i)