2 * Created on 28.04.2003
5 package net.sourceforge.phpeclipse.phpeditor.php;
7 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
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;
17 * Defines a partition in a phpdocument. This class keeps tracks of
18 * partitions contained within other partitions.
20 * @author Stefan Langer
21 * @version $Revision: 1.4 $
23 public abstract class Partition
25 private IDocument fDocument;
26 private IToken fContentToken;
27 private String fParentPartition;
28 private char[] fTextBuffer;
29 private int fOffset = 0;
32 private boolean inDoubleString = false;
33 private boolean inSingleString = false;
35 public Partition(IDocument document, char[] delim, String contentType, String parentPartition)
38 fContentToken = new Token(contentType);
39 fParentPartition = parentPartition;
44 public Partition(IDocument document, char[] delim, String contentType)
46 this(document, delim, contentType, IPHPPartitions.HTML);
50 * Checks wether the specified type is allowed within this
53 * @param type The type of the partition to check.
55 * @return <code>true</code> if the partition is allowed within this
56 * partition otherwise <code>false</code>.
58 abstract protected boolean allowedPartition(String type);
60 abstract protected boolean scan();
62 protected boolean isEnd()
64 return fOffset >= fTextBuffer.length;
69 if(fOffset > fTextBuffer.length)
70 return ICharacterScanner.EOF;
72 char ret = fTextBuffer[fOffset++];
77 inSingleString = !inSingleString;
81 inDoubleString = !inDoubleString;
87 protected void unread(int i)
89 for (int j = 0; j < i && fOffset > 0; j++)
91 char read = fTextBuffer[--fOffset];
97 inSingleString = !inSingleString;
101 inDoubleString = !inDoubleString;
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)
112 // read the full range into the internal buffer
114 inSingleString = false;
115 inDoubleString = false;
116 fTextBuffer = fDocument.get(offset, length).toCharArray();
120 protected boolean checkPattern(String pattern, boolean ignoreCase)
122 char[] checkPattern = pattern.toCharArray();
123 int offset = fOffset;
124 for(int i=0; i<checkPattern.length; i++)
126 if(isEnd() || !letterEquals(read(), checkPattern[i], ignoreCase))
135 private boolean letterEquals(int test, char letter, boolean ignoreCase)
141 && Character.isLowerCase(letter)
142 && test == Character.toUpperCase(letter))
146 && Character.isUpperCase(letter)
147 && test == Character.toLowerCase(letter))
155 protected boolean inString()
157 return inDoubleString || inSingleString;
163 public IToken getToken()
165 return fContentToken;
168 public int getLength()
176 public IDocument getDocument()
184 public String getParentPartition()
186 return fParentPartition;
192 public void setDocument(IDocument document)
194 fDocument = document;
200 public void setParentPartition(String string)
202 fParentPartition = string;
209 public char getEntry()
217 public char getExit()
225 protected int getOffset()
233 protected void setOffset(int i)