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.3 $
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;
36 private boolean fInDoubString = false;
38 private IDocument fDocument = null;
40 private int fOffset = -1;
42 private String fContentType = IPHPPartitions.HTML;
44 private String fPrevContentType = IPHPPartitions.HTML;
46 private boolean partitionBorder = false;
48 private int fTokenOffset;
50 private int fEnd = -1;
54 private int fCurrentLength;
56 private int fFileType;
58 private Map tokens = new HashMap();
60 public HTMLPartitionScanner() {
61 this(IPHPPartitions.PHP_FILE);
64 public HTMLPartitionScanner(int fileType) {
65 this.tokens.put(IPHPPartitions.HTML, new Token(IPHPPartitions.HTML));
66 this.tokens.put(IPHPPartitions.HTML_MULTILINE_COMMENT, new Token(
67 IPHPPartitions.HTML_MULTILINE_COMMENT));
70 .put(IPHPPartitions.SMARTY, new Token(IPHPPartitions.SMARTY));
71 this.tokens.put(IPHPPartitions.SMARTY_MULTILINE_COMMENT, new Token(
72 IPHPPartitions.SMARTY_MULTILINE_COMMENT));
74 this.tokens.put(IDocument.DEFAULT_CONTENT_TYPE, new Token(
75 IDocument.DEFAULT_CONTENT_TYPE));
79 private IToken getToken(String type) {
80 fLength = fCurrentLength;
85 int line = fDocument.getLineOfOffset(fOffset);
86 System.err.println("Error at "
89 + String.valueOf(fOffset
90 - fDocument.getLineOffset(line)));
92 } catch (BadLocationException e) { // should never happen
93 // TODO Write stacktrace to log
97 Assert.isTrue(fLength > 0, "Partition length <= 0!");
99 // String can never cross partition borders so reset string detection
101 fInDoubString = false;
102 IToken token = (IToken) this.tokens.get(type);
103 Assert.isNotNull(token, "Token for type \"" + type + "\" not found!");
105 System.out.println("Partition: fTokenOffset=" + fTokenOffset
106 + " fContentType=" + type + " fLength=" + fLength);
114 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner#setPartialRange(org.eclipse.jface.text.IDocument,
115 * int, int, java.lang.String, int)
117 public void setPartialRange(IDocument document, int offset, int length,
118 String contentType, int partitionOffset) {
120 System.out.println("*****");
121 System.out.println("PartialRange: contentType=" + contentType
122 + " partitionOffset=" + partitionOffset);
126 if (partitionOffset > -1) {
127 partitionBorder = false;
128 // because of strings we have to parse the whole partition
129 this.setRange(document, partitionOffset, offset
130 - partitionOffset + length);
131 // sometimes we get a wrong partition so we retrieve the
133 // directly from the document
134 fContentType = fDocument.getContentType(partitionOffset);
136 this.setRange(document, offset, length);
138 } catch (BadLocationException e) {
139 // should never happen
140 // TODO print stack trace to log
141 // fall back just scan the whole document again
142 this.setRange(document, 0, fDocument.getLength());
150 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
152 public int getTokenLength() {
159 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
161 public int getTokenOffset() {
168 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
170 public IToken nextToken() {
173 // check if we are not allready at the end of the
175 if ((c = read()) == ICharacterScanner.EOF) {
176 partitionBorder = false;
181 if (partitionBorder) {
182 fTokenOffset = fOffset;
183 partitionBorder = false;
186 while ((c = read()) != ICharacterScanner.EOF) {
189 if (checkPattern(new char[] { '!', '-', '-' })) { // return
192 if (fContentType != IPHPPartitions.HTML_MULTILINE_COMMENT
193 && fCurrentLength > 4) {
195 IToken token = getToken(fContentType);
196 fContentType = IPHPPartitions.HTML_MULTILINE_COMMENT;
199 fContentType = IPHPPartitions.HTML_MULTILINE_COMMENT;
201 fTokenOffset = fOffset - 4;
206 if (fContentType == IPHPPartitions.HTML_MULTILINE_COMMENT
207 && checkPattern(new char[] { '-', '>' })) {
208 fContentType = IPHPPartitions.HTML;
209 partitionBorder = true;
210 return getToken(IPHPPartitions.HTML_MULTILINE_COMMENT);
213 case '{': // SMARTY code starts here ?
214 if (fFileType == IPHPPartitions.SMARTY_FILE) {
215 if ((c = read()) == '*') {
217 System.out.println("SMARTYDOC_TOKEN start "
218 + fTokenOffset + " fContentType="
219 + fContentType + " fLength=" + fLength
220 + " fOffset=" + fOffset
221 + " fCurrentLength=" + fCurrentLength);
223 if (fContentType != IPHPPartitions.SMARTY_MULTILINE_COMMENT
224 && fCurrentLength > 2) {
225 // SMARTY doc code starts here
227 IToken token = getToken(fContentType);
228 fContentType = IPHPPartitions.SMARTY_MULTILINE_COMMENT;
230 // } else if (fContentType ==
231 // IPHPPartitionScannerConstants.HTML && fOffset ==
234 // IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT;
235 } else { // if (fContentType ==
236 // IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT)
238 fContentType = IPHPPartitions.SMARTY_MULTILINE_COMMENT;
239 fTokenOffset = fOffset - 2;
245 System.out.println("SMARTY_TOKEN start " + fTokenOffset
246 + " fContentType=" + fContentType + " fLength="
247 + fLength + " fOffset=" + fOffset);
249 if (c != ICharacterScanner.EOF) {
252 if (fContentType != IPHPPartitions.SMARTY
253 && fCurrentLength > 1) {
255 IToken token = getToken(fContentType);
256 fContentType = IPHPPartitions.SMARTY;
258 // } else if (fContentType ==
259 // IPHPPartitionScannerConstants.HTML && fOffset==1) {
260 // fContentType = IPHPPartitionScannerConstants.SMARTY;
262 fContentType = IPHPPartitions.SMARTY;
263 fTokenOffset = fOffset - 1;
268 case '}': // SMARTY code ends here ?
269 if (fFileType == IPHPPartitions.SMARTY_FILE
270 && fContentType == IPHPPartitions.SMARTY) {
272 System.out.println("SMARTY_TOKEN end " + fTokenOffset
273 + " fContentType=" + fContentType + " fLength="
274 + fLength + " fOffset=" + fOffset);
276 fContentType = IPHPPartitions.HTML;
277 partitionBorder = true;
278 return getToken(IPHPPartitions.SMARTY);
282 // if (!isInString(IPHPPartitions.PHP_PARTITIONING) && (c = read())
283 // == '*') { // MULTINE COMMENT JAVASCRIPT, CSS, PHP
284 // if (fContentType == IPHPPartitions.PHP_PARTITIONING &&
285 // fCurrentLength > 2) {
287 // IToken token = getToken(fContentType);
288 // fContentType = IPHPPartitions.PHP_PHPDOC_COMMENT;
290 // } else if (fContentType == IPHPPartitions.PHP_PHPDOC_COMMENT) {
291 // fTokenOffset = fOffset - 2;
292 // fCurrentLength = 2;
295 // } else if (!isInString(IPHPPartitions.PHP_PARTITIONING) && c !=
296 // ICharacterScanner.EOF)
300 if (fFileType == IPHPPartitions.SMARTY_FILE
301 && (c = read()) == '}') {
303 System.out.println("SMARTYDOC_TOKEN end "
304 + fTokenOffset + " fContentType="
305 + fContentType + " fLength=" + fLength
306 + " fOffset=" + fOffset);
308 if (fContentType == IPHPPartitions.SMARTY_MULTILINE_COMMENT) {
309 fContentType = IPHPPartitions.HTML;
310 partitionBorder = true;
311 return getToken(IPHPPartitions.SMARTY_MULTILINE_COMMENT);
317 fInString = !fInString;
320 // toggle String mode
322 fInDoubString = !fInDoubString;
325 } // end of file reached but we have to return the
327 return getToken(fContentType);
333 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument,
336 public void setRange(IDocument document, int offset, int length) {
338 System.out.println("SET RANGE: offset=" + offset + " length="
342 fDocument = document;
344 fTokenOffset = offset;
347 fEnd = fOffset + length;
349 fInDoubString = false;
350 fContentType = IPHPPartitions.HTML;
351 // String[] prev = getPartitionStack(offset);
356 if (fOffset < fEnd) {
358 return fDocument.getChar(fOffset++);
360 return ICharacterScanner.EOF;
361 } catch (BadLocationException e) {
362 // should never happen
363 // TODO write stacktrace to log
365 return ICharacterScanner.EOF;
369 private void unread() {
374 private void unread(int num) {
376 fCurrentLength -= num;
379 private boolean checkPattern(char[] pattern) {
380 return checkPattern(pattern, false);
384 * Check if next character sequence read from document is equals to the
385 * provided pattern. Pattern is read from left to right until the first
386 * character read doesn't match. If this happens all read characters are
390 * The pattern to check.
391 * @return <code>true</code> if pattern is equals else returns
392 * <code>false</code>.
394 private boolean checkPattern(char[] pattern, boolean ignoreCase) {
395 int prevOffset = fOffset;
396 int prevLength = fCurrentLength;
397 for (int i = 0; i < pattern.length; i++) {
400 if (c == ICharacterScanner.EOF
401 || !letterEquals(c, pattern[i], ignoreCase)) {
402 fOffset = prevOffset;
403 fCurrentLength = prevLength;
411 private boolean letterEquals(int test, char letter, boolean ignoreCase) {
414 else if (ignoreCase && Character.isLowerCase(letter)
415 && test == Character.toUpperCase(letter))
417 else if (ignoreCase && Character.isUpperCase(letter)
418 && test == Character.toLowerCase(letter))
425 * Checks wether the offset is in a <code>String</code> and the specified
426 * contenttype is the current content type. Strings are delimited, mutual
427 * exclusive, by a " or by a '.
430 * The contenttype to check.
431 * @return <code>true</code> if the current offset is in a string else
434 private boolean isInString(String contentType) {
435 if (fContentType == contentType)
436 return (fInString || fInDoubString);
442 * Returns the previouse partition stack for the given offset.
445 * The offset to return the previouse partitionstack for.
447 * @return The stack as a string array.
449 private String[] getPartitionStack(int offset) {
450 ArrayList types = new ArrayList();
453 ITypedRegion region = fDocument.getPartition(offset);
454 tmpOffset = region.getOffset();
455 while (tmpOffset - 1 > 0) {
456 region = fDocument.getPartition(tmpOffset - 1);
457 tmpOffset = region.getOffset();
458 types.add(0, region.getType());
460 } catch (BadLocationException e) {
466 String[] retVal = new String[types.size()];
468 retVal = (String[]) types.toArray(retVal);