/*
* Created on 28.04.2003
*
*/
package net.sourceforge.phpeclipse.phpeditor.php;
import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;
/**
* Defines a partition in a phpdocument. This class keeps tracks of
* partitions contained within other partitions.
*
* @author Stefan Langer
* @version $Revision: 1.4 $
*/
public abstract class Partition
{
private IDocument fDocument;
private IToken fContentToken;
private String fParentPartition;
private char[] fTextBuffer;
private int fOffset = 0;
private char entry;
private char exit;
private boolean inDoubleString = false;
private boolean inSingleString = false;
public Partition(IDocument document, char[] delim, String contentType, String parentPartition)
{
fDocument = document;
fContentToken = new Token(contentType);
fParentPartition = parentPartition;
entry = delim[0];
exit = delim[1];
}
public Partition(IDocument document, char[] delim, String contentType)
{
this(document, delim, contentType, IPHPPartitions.HTML);
}
/**
* Checks wether the specified type is allowed within this
* partition type.
*
* @param type The type of the partition to check.
*
* @return true
if the partition is allowed within this
* partition otherwise false
.
*/
abstract protected boolean allowedPartition(String type);
abstract protected boolean scan();
protected boolean isEnd()
{
return fOffset >= fTextBuffer.length;
}
protected int read()
{
if(fOffset > fTextBuffer.length)
return ICharacterScanner.EOF;
char ret = fTextBuffer[fOffset++];
switch(ret)
{
case '\'':
if(!inDoubleString)
inSingleString = !inSingleString;
break;
case '"':
if(!inSingleString)
inDoubleString = !inDoubleString;
break;
}
return ret;
}
protected void unread(int i)
{
for (int j = 0; j < i && fOffset > 0; j++)
{
char read = fTextBuffer[--fOffset];
switch (read)
{
case '\'' :
if (!inDoubleString)
inSingleString = !inSingleString;
break;
case '"' :
if (!inSingleString)
inDoubleString = !inDoubleString;
break;
}
} // END FOR
}
public boolean scanRange(int offset, int length)
throws BadLocationException
{ // short circuit scanning if entry is not correct
if (fDocument.getChar(offset) != entry)
return false;
// read the full range into the internal buffer
fOffset = 0;
inSingleString = false;
inDoubleString = false;
fTextBuffer = fDocument.get(offset, length).toCharArray();
return scan();
}
protected boolean checkPattern(String pattern, boolean ignoreCase)
{
char[] checkPattern = pattern.toCharArray();
int offset = fOffset;
for(int i=0; i