2 * This program and the accompanying materials
3 * are made available under the terms of the Common Public License v1.0
4 * which accompanies this distribution, and is available at
5 * http://www.eclipse.org/legal/cpl-v10.html
6 * Created on 05.03.2003
8 * @author Stefan Langer (musk)
9 * @version $Revision: 1.2 $
11 package net.sourceforge.phpeclipse.phpeditor.php;
13 import java.util.ArrayList;
14 import java.util.HashMap;
17 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
19 import org.eclipse.jface.text.Assert;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.ITypedRegion;
23 import org.eclipse.jface.text.rules.ICharacterScanner;
24 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
25 import org.eclipse.jface.text.rules.IToken;
26 import org.eclipse.jface.text.rules.Token;
31 public class HTMLPartitionScanner implements IPartitionTokenScanner {
32 private static final boolean DEBUG = false;
34 private boolean fInString = false;
35 private boolean fInDoubString = false;
36 private IDocument fDocument = null;
37 private int fOffset = -1;
38 private String fContentType = IPHPPartitions.HTML;
39 private String fPrevContentType = IPHPPartitions.HTML;
40 private boolean partitionBorder = false;
41 private int fTokenOffset;
42 private int fEnd = -1;
44 private int fCurrentLength;
45 private int fFileType;
46 private Map tokens = new HashMap();
48 public HTMLPartitionScanner() {
49 this(IPHPPartitions.PHP_FILE);
52 public HTMLPartitionScanner(int fileType) {
53 this.tokens.put(IPHPPartitions.HTML, new Token(IPHPPartitions.HTML));
55 IPHPPartitions.HTML_MULTILINE_COMMENT,
56 new Token(IPHPPartitions.HTML_MULTILINE_COMMENT));
58 this.tokens.put(IPHPPartitions.SMARTY, new Token(IPHPPartitions.SMARTY));
60 IPHPPartitions.SMARTY_MULTILINE_COMMENT,
61 new Token(IPHPPartitions.SMARTY_MULTILINE_COMMENT));
63 this.tokens.put(IDocument.DEFAULT_CONTENT_TYPE, new Token(IDocument.DEFAULT_CONTENT_TYPE));
67 private IToken getToken(String type) {
68 fLength = fCurrentLength;
73 int line = fDocument.getLineOfOffset(fOffset);
74 System.err.println("Error at " + line + " offset:" + String.valueOf(fOffset - fDocument.getLineOffset(line)));
76 } catch (BadLocationException e) { // should never happen
77 // TODO Write stacktrace to log
81 Assert.isTrue(fLength > 0, "Partition length <= 0!");
83 // String can never cross partition borders so reset string detection
85 fInDoubString = false;
86 IToken token = (IToken) this.tokens.get(type);
87 Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
89 System.out.println("Partition: fTokenOffset=" + fTokenOffset + " fContentType=" + type + " fLength=" + fLength);
95 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument, int, int, java.lang.String, int)
97 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
99 System.out.println("*****");
100 System.out.println("PartialRange: contentType=" + contentType + " partitionOffset=" + partitionOffset);
104 if (partitionOffset > -1) {
105 partitionBorder = false;
106 // because of strings we have to parse the whole partition
107 this.setRange(document, partitionOffset, offset - partitionOffset + length);
108 // sometimes we get a wrong partition so we retrieve the partition
109 // directly from the document
110 fContentType = fDocument.getContentType(partitionOffset);
112 this.setRange(document, offset, length);
114 } catch (BadLocationException e) {
115 // should never happen
116 // TODO print stack trace to log
117 // fall back just scan the whole document again
118 this.setRange(document, 0, fDocument.getLength());
124 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
126 public int getTokenLength() {
131 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
133 public int getTokenOffset() {
138 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
140 public IToken nextToken() {
143 // check if we are not allready at the end of the
145 if ((c = read()) == ICharacterScanner.EOF) {
146 partitionBorder = false;
151 if (partitionBorder) {
152 fTokenOffset = fOffset;
153 partitionBorder = false;
156 while ((c = read()) != ICharacterScanner.EOF) {
159 if ( checkPattern(new char[] { '!', '-', '-' })) { // return previouse partition
160 if (fContentType != IPHPPartitions.HTML_MULTILINE_COMMENT && fCurrentLength > 4) {
162 IToken token = getToken(fContentType);
163 fContentType = IPHPPartitions.HTML_MULTILINE_COMMENT;
166 fContentType = IPHPPartitions.HTML_MULTILINE_COMMENT;
168 fTokenOffset = fOffset - 4;
173 if (fContentType == IPHPPartitions.HTML_MULTILINE_COMMENT
174 && checkPattern(new char[] { '-', '>' })) {
175 fContentType = IPHPPartitions.HTML;
176 partitionBorder = true;
177 return getToken(IPHPPartitions.HTML_MULTILINE_COMMENT);
180 case '{' : // SMARTY code starts here ?
181 if (fFileType == IPHPPartitions.SMARTY_FILE) {
182 if ((c = read()) == '*') {
185 "SMARTYDOC_TOKEN start "
196 if (fContentType != IPHPPartitions.SMARTY_MULTILINE_COMMENT && fCurrentLength > 2) {
197 // SMARTY doc code starts here
199 IToken token = getToken(fContentType);
200 fContentType = IPHPPartitions.SMARTY_MULTILINE_COMMENT;
202 // } else if (fContentType == IPHPPartitionScannerConstants.HTML && fOffset == 2) {
203 // fContentType = IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT;
204 } else { // if (fContentType == IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT) {
205 fContentType = IPHPPartitions.SMARTY_MULTILINE_COMMENT;
206 fTokenOffset = fOffset - 2;
213 "SMARTY_TOKEN start "
222 if (c != ICharacterScanner.EOF) {
225 if (fContentType != IPHPPartitions.SMARTY && fCurrentLength > 1) {
227 IToken token = getToken(fContentType);
228 fContentType = IPHPPartitions.SMARTY;
230 // } else if (fContentType == IPHPPartitionScannerConstants.HTML && fOffset==1) {
231 // fContentType = IPHPPartitionScannerConstants.SMARTY;
233 fContentType = IPHPPartitions.SMARTY;
234 fTokenOffset = fOffset - 1;
239 case '}' : // SMARTY code ends here ?
240 if (fFileType == IPHPPartitions.SMARTY_FILE && fContentType == IPHPPartitions.SMARTY) {
252 fContentType = IPHPPartitions.HTML;
253 partitionBorder = true;
254 return getToken(IPHPPartitions.SMARTY);
258 // if (!isInString(IPHPPartitions.PHP_PARTITIONING) && (c = read()) == '*') { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
259 // if (fContentType == IPHPPartitions.PHP_PARTITIONING && fCurrentLength > 2) {
261 // IToken token = getToken(fContentType);
262 // fContentType = IPHPPartitions.PHP_PHPDOC_COMMENT;
264 // } else if (fContentType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
265 // fTokenOffset = fOffset - 2;
266 // fCurrentLength = 2;
269 // } else if (!isInString(IPHPPartitions.PHP_PARTITIONING) && c != ICharacterScanner.EOF)
273 if (fFileType == IPHPPartitions.SMARTY_FILE && (c = read()) == '}') {
276 "SMARTYDOC_TOKEN end "
285 if (fContentType == IPHPPartitions.SMARTY_MULTILINE_COMMENT) {
286 fContentType = IPHPPartitions.HTML;
287 partitionBorder = true;
288 return getToken(IPHPPartitions.SMARTY_MULTILINE_COMMENT);
294 fInString = !fInString;
297 // toggle String mode
299 fInDoubString = !fInDoubString;
302 } // end of file reached but we have to return the
304 return getToken(fContentType);
307 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)
309 public void setRange(IDocument document, int offset, int length) {
311 System.out.println("SET RANGE: offset=" + offset + " length=" + length);
314 fDocument = document;
316 fTokenOffset = offset;
319 fEnd = fOffset + length;
321 fInDoubString = false;
322 fContentType = IPHPPartitions.HTML;
323 // String[] prev = getPartitionStack(offset);
328 if (fOffset < fEnd) {
330 return fDocument.getChar(fOffset++);
332 return ICharacterScanner.EOF;
333 } catch (BadLocationException e) {
334 // should never happen
335 // TODO write stacktrace to log
337 return ICharacterScanner.EOF;
341 private void unread() {
346 private void unread(int num) {
348 fCurrentLength -= num;
351 private boolean checkPattern(char[] pattern) {
352 return checkPattern(pattern, false);
356 * Check if next character sequence read from document is equals to
357 * the provided pattern. Pattern is read from left to right until the
358 * first character read doesn't match. If this happens all read characters are
360 * @param pattern The pattern to check.
361 * @return <code>true</code> if pattern is equals else returns <code>false</code>.
363 private boolean checkPattern(char[] pattern, boolean ignoreCase) {
364 int prevOffset = fOffset;
365 int prevLength = fCurrentLength;
366 for (int i = 0; i < pattern.length; i++) {
369 if (c == ICharacterScanner.EOF || !letterEquals(c, pattern[i], ignoreCase)) {
370 fOffset = prevOffset;
371 fCurrentLength = prevLength;
379 private boolean letterEquals(int test, char letter, boolean ignoreCase) {
382 else if (ignoreCase && Character.isLowerCase(letter) && test == Character.toUpperCase(letter))
384 else if (ignoreCase && Character.isUpperCase(letter) && test == Character.toLowerCase(letter))
391 * Checks wether the offset is in a <code>String</code> and the specified
392 * contenttype is the current content type.
393 * Strings are delimited, mutual exclusive, by a " or by a '.
395 * @param contentType The contenttype to check.
396 * @return <code>true</code> if the current offset is in a string else
399 private boolean isInString(String contentType) {
400 if (fContentType == contentType)
401 return (fInString || fInDoubString);
407 * Returns the previouse partition stack for the given offset.
409 * @param offset The offset to return the previouse partitionstack for.
411 * @return The stack as a string array.
413 private String[] getPartitionStack(int offset) {
414 ArrayList types = new ArrayList();
417 ITypedRegion region = fDocument.getPartition(offset);
418 tmpOffset = region.getOffset();
419 while (tmpOffset - 1 > 0) {
420 region = fDocument.getPartition(tmpOffset - 1);
421 tmpOffset = region.getOffset();
422 types.add(0, region.getType());
424 } catch (BadLocationException e) {
430 String[] retVal = new String[types.size()];
432 retVal = (String[]) types.toArray(retVal);