From: musk <musk>
Date: Tue, 6 May 2003 13:03:45 +0000 (+0000)
Subject: Partitionscanner Framework
X-Git-Url: http://secure.phpeclipse.com

Partitionscanner Framework
---

diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPPartition.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPPartition.java
new file mode 100644
index 0000000..522df96
--- /dev/null
+++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPPartition.java
@@ -0,0 +1,80 @@
+/*
+ * Created on 28.04.2003
+ *
+ */
+package net.sourceforge.phpeclipse.phpeditor.php;
+
+import org.eclipse.jface.text.*;
+
+/**
+ * @author slanger
+ * @version $Revision: 1.1 $
+ */
+public class PHPPartition extends Partition
+{
+	private boolean fShortTagsEnabled = true;
+	
+    /**
+     * @param document
+     * @param delim
+     * @param contentType
+     * @param parentPartition
+     */
+    public PHPPartition(IDocument document, String parentPartition)
+    {
+        super(
+            document,
+            new char[] { '<', '>' },
+            IPHPPartitionScannerConstants.PHP,
+            parentPartition);
+    }
+
+    
+
+    /* (non-Javadoc)
+     * @see net.sourceforge.phpeclipse.phpeditor.php.Partition#allowedPartition(java.lang.String)
+     */
+    protected boolean allowedPartition(String type)
+    {
+        if(type.equals(IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT))
+        	return true;
+        	
+        return false;
+    }
+
+    /* (non-Javadoc)
+     * @see net.sourceforge.phpeclipse.phpeditor.php.Partition#scan()
+     */
+    protected boolean scan()
+    {
+    	int ch;
+    	if(fShortTagsEnabled && checkPattern("<?", true))
+    	{
+    		scanToEnd();
+    	}
+    	else if(checkPattern("<?php", false))
+    	{
+    		scanToEnd();
+    	}    
+    	else
+    		return false;
+    	
+    	return true;	
+    }
+	
+	private void scanToEnd()
+	{
+		if(isEnd())
+			return;
+				
+		while(!checkPattern("?>", true))
+		{
+			int offset = getOffset();
+			if(checkPattern("/*", true))
+			{	
+				unread(2);
+				return;
+			}
+		}		
+	}
+}
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/Partition.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/Partition.java
new file mode 100644
index 0000000..bf83815
--- /dev/null
+++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/Partition.java
@@ -0,0 +1,233 @@
+/*
+ * Created on 28.04.2003
+ *
+ */
+package net.sourceforge.phpeclipse.phpeditor.php;
+
+import org.eclipse.jface.text.*;
+import org.eclipse.jface.text.rules.*;
+
+
+/**
+ * Defines a partition in a phpdocument. This class keeps tracks of
+ * partitions contained within other partitions.
+ * 
+ * @author Stefan Langer
+ * @version $Revision: 1.1 $
+ */
+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, IPHPPartitionScannerConstants.HTML);
+	}
+	
+	/**
+	 * Checks wether the specified type is allowed within this 
+	 * partition type.
+	 * 
+     * @param type The type of the partition to check.
+     * 
+     * @return <code>true</code> if the partition is allowed within this
+     * 			partition otherwise <code>false</code>.
+     */
+    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<checkPattern.length; i++)
+    	{
+    		if(isEnd() || !letterEquals(read(), checkPattern[i], ignoreCase))
+    		{
+    			fOffset = offset;
+    			return false;
+    		}
+    	}
+    	return true;
+    }
+	
+	private boolean letterEquals(int test, char letter, boolean ignoreCase)
+	{
+		if (test == letter)
+			return true;
+		else if (
+			ignoreCase
+				&& Character.isLowerCase(letter)
+				&& test == Character.toUpperCase(letter))
+			return true;
+		else if (
+			ignoreCase
+				&& Character.isUpperCase(letter)
+				&& test == Character.toLowerCase(letter))
+			return true;
+
+		return false;
+	}
+    
+
+    
+    protected boolean inString()
+    {
+    	return inDoubleString || inSingleString;
+    }
+    
+    
+    
+    
+    public IToken getToken()
+    {
+		return fContentToken;
+    }
+    
+    public int getLength()
+    {
+    	return fOffset;
+    }
+    
+    /**
+     * @return
+     */
+    public IDocument getDocument()
+    {
+        return fDocument;
+    }
+
+    /**
+     * @return
+     */
+    public String getParentPartition()
+    {
+        return fParentPartition;
+    }
+
+    /**
+     * @param document
+     */
+    public void setDocument(IDocument document)
+    {
+        fDocument = document;
+    }
+
+    /**
+     * @param string
+     */
+    public void setParentPartition(String string)
+    {
+        fParentPartition = string;
+    }
+    
+    
+    /**
+     * @return
+     */
+    public char getEntry()
+    {
+        return entry;
+    }
+
+    /**
+     * @return
+     */
+    public char getExit()
+    {
+        return exit;
+    }
+
+    /**
+     * @return
+     */
+    protected int getOffset()
+    {
+        return fOffset;
+    }
+
+    /**
+     * @param i
+     */
+    protected void setOffset(int i)
+    {
+        fOffset = i;
+    }
+
+}
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PartitionStack.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PartitionStack.java
new file mode 100644
index 0000000..380b4cf
--- /dev/null
+++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PartitionStack.java
@@ -0,0 +1,67 @@
+/*
+ * Created on 28.04.2003
+ *
+ */
+package net.sourceforge.phpeclipse.phpeditor.php;
+
+import java.util.*;
+
+import org.eclipse.jface.text.*;
+
+/**
+ * A stack for keeping track of the contenttypes for partitions that 
+ * contain other partitions.
+ * 
+ * @author Stefan Langer
+ * @version $Revision: 1.1 $
+ */
+public class PartitionStack
+{
+    private ArrayList fPartitionStack = new ArrayList(5);
+    private int fStackTop = -1;
+    
+    /**
+     * Pushes the specified contenttype onto the partitionstack. 
+     * This will keep track of the last partitions read.
+     * @param contentType The contenttype to push onto the stack.
+     */
+    public void pushStack(String contentType)
+    {
+    	if(fStackTop < fPartitionStack.size())
+    	{
+    		fPartitionStack.add(++fStackTop, contentType);
+    	}
+    }
+    /**
+     * Returns the contentype of the last partition on the partition stack.
+     * If no partition is currently on the stack this function simply returns
+     * the HTML contenttype as default.
+     * @return The contenttype of the last partition on stack.
+     */
+    public String popStack()
+    {
+    	if(fStackTop >= 0)
+    	{
+    		return (String)fPartitionStack.get(fStackTop--);
+    	}
+    	
+    	return IPHPPartitionScannerConstants.HTML;
+    }
+    
+    public boolean isEmpty()
+    {
+    	return (fStackTop < 0);
+    }
+    
+    /**
+     * Initializes this stack from the specified document for the
+     * specified offset.
+     * @param offset 		The offset to initialize from
+     * @param fDocument	The document to initialize from
+     */
+    public void initializeStack(int offset, IDocument fDocument)
+    {
+    	
+    }
+
+}