Partitionscanner Framework
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / Partition.java
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 (file)
index 0000000..bf83815
--- /dev/null
@@ -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;
+    }
+
+}