2 * Copyright (c) 2002-2004 Widespace, OU and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * Igor Malinin - initial contribution
11 * $Id: XMLPartitionScanner.java,v 1.1 2004-09-02 18:28:03 jsurfer Exp $
14 package net.sourceforge.phpeclipse.xml.ui.internal.text;
16 import java.util.HashMap;
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.rules.ICharacterScanner;
23 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
24 import org.eclipse.jface.text.rules.IToken;
25 import org.eclipse.jface.text.rules.Token;
31 * @author Igor Malinin
33 public class XMLPartitionScanner implements IPartitionTokenScanner {
34 public static final String XML_PI = "__xml_processing_instruction";
35 public static final String XML_COMMENT = "__xml_comment";
36 public static final String XML_DECL = "__xml_declaration";
37 public static final String XML_TAG = "__xml_tag";
38 public static final String XML_ATTRIBUTE = "__xml_attribute";
39 public static final String XML_CDATA = "__xml_cdata";
41 public static final String DTD_INTERNAL = "__dtd_internal";
42 public static final String DTD_INTERNAL_PI = "__dtd_internal_pi";
43 public static final String DTD_INTERNAL_COMMENT = "__dtd_internal_comment";
44 public static final String DTD_INTERNAL_DECL = "__dtd_internal_declaration";
45 public static final String DTD_CONDITIONAL = "__dtd_conditional";
47 public static final int STATE_DEFAULT = 0;
48 public static final int STATE_TAG = 1;
49 public static final int STATE_DECL = 2;
50 public static final int STATE_CDATA = 4;
52 public static final int STATE_INTERNAL = 8;
54 protected IDocument document;
60 protected int position;
63 protected boolean parsedtd;
65 protected Map tokens = new HashMap();
67 public XMLPartitionScanner(boolean parsedtd) {
68 this.parsedtd = parsedtd;
72 * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
74 public IToken nextToken() {
79 return nextTagToken();
82 return nextDeclToken();
85 return nextCDATAToken();
89 case ICharacterScanner.EOF:
90 state = STATE_DEFAULT;
91 return getToken(null);
95 case ICharacterScanner.EOF:
96 if (parsedtd || isInternal()) {
100 state = STATE_DEFAULT;
101 return getToken(XML_TAG);
104 return nextPIToken();
106 case '!': // <! <!DEFINITION or <![CDATA[ or <!--COMMENT
108 case ICharacterScanner.EOF:
109 state = STATE_DEFAULT;
110 return getToken(XML_TAG);
112 case '-': // <!- <!--COMMENT
114 case ICharacterScanner.EOF:
115 return nextDeclToken();
118 return nextCommentToken();
121 case '[': // <![ <![CDATA[ or <![%cond;[
123 return nextConditionalToken();
127 return nextCDATAToken();
131 return nextDeclToken();
134 if (parsedtd || isInternal()) {
140 return nextTagToken();
155 case ICharacterScanner.EOF:
156 state = STATE_DEFAULT;
157 return getToken(null);
160 if (parsedtd || isInternal()) {
162 case ICharacterScanner.EOF:
163 state = STATE_DEFAULT;
164 return getToken(null);
178 state &= STATE_INTERNAL;
179 return getToken(isInternal() ? DTD_INTERNAL : null);
186 if (position == offset) {
192 return getToken(DTD_INTERNAL);
198 private IToken nextTagToken() {
202 case ICharacterScanner.EOF:
204 state = STATE_DEFAULT;
205 return getToken(XML_TAG);
213 return getToken(XML_ATTRIBUTE);
220 case ICharacterScanner.EOF:
221 state = STATE_DEFAULT;
222 return getToken(XML_ATTRIBUTE);
232 case ICharacterScanner.EOF:
234 state = STATE_DEFAULT;
235 return getToken(XML_TAG);
241 return getToken(XML_TAG);
246 private IToken nextDeclToken() {
249 case ICharacterScanner.EOF:
250 state = STATE_DEFAULT;
251 return getToken(isInternal() ? DTD_INTERNAL_DECL : XML_DECL);
254 if (parsedtd || isInternal()) {
256 case ICharacterScanner.EOF:
257 state = STATE_DEFAULT;
258 return getToken(isInternal() ? DTD_INTERNAL : null);
273 state &= STATE_INTERNAL;
274 return getToken(isInternal() ? DTD_INTERNAL_DECL : XML_DECL);
276 case '[': // <!DOCTYPE xxx [dtd]>
278 state = STATE_INTERNAL;
279 return getToken(XML_DECL);
285 private IToken nextCommentToken() {
286 state &= STATE_INTERNAL;
290 case ICharacterScanner.EOF:
295 case ICharacterScanner.EOF:
300 case ICharacterScanner.EOF:
311 return getToken(isInternal() ? DTD_INTERNAL_COMMENT : XML_COMMENT);
314 private IToken nextPIToken() {
315 state &= STATE_INTERNAL;
319 case ICharacterScanner.EOF:
324 case ICharacterScanner.EOF:
333 return getToken(isInternal() ? DTD_INTERNAL_PI : XML_PI);
336 private IToken nextCDATAToken() {
337 state = STATE_DEFAULT;
342 case ICharacterScanner.EOF:
347 case ICharacterScanner.EOF:
352 case ICharacterScanner.EOF:
364 return getToken(XML_CDATA);
367 private IToken nextConditionalToken() {
368 state = STATE_DEFAULT;
375 case ICharacterScanner.EOF:
380 case ICharacterScanner.EOF:
385 case ICharacterScanner.EOF:
402 case ICharacterScanner.EOF:
407 case ICharacterScanner.EOF:
423 return getToken(DTD_CONDITIONAL);
426 private IToken getToken(String type) {
427 length = position - offset;
434 return Token.UNDEFINED;
437 IToken token = (IToken) tokens.get(type);
439 token = new Token(type);
440 tokens.put(type, token);
446 private boolean isInternal() {
447 return (state & STATE_INTERNAL) != 0;
451 if (position >= end) {
452 return ICharacterScanner.EOF;
456 return document.getChar(position++);
457 } catch (BadLocationException e) {
459 return ICharacterScanner.EOF;
463 private void unread() {
468 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
470 public int getTokenOffset() {
471 Assert.isTrue(offset>=0, Integer.toString(offset));
476 * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
478 public int getTokenLength() {
483 * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(IDocument, int, int)
485 public void setRange(IDocument document, int offset, int length) {
486 this.document = document;
487 this.end = offset + length;
489 this.offset = offset;
490 this.position = offset;
493 this.state = STATE_DEFAULT;
497 * @see org.eclipse.jface.text.rules.IPartitionTokenScanner
499 public void setPartialRange(
500 IDocument document, int offset, int length,
501 String contentType, int partitionOffset
503 this.document = document;
504 this.end = offset + length;
506 // NB! Undocumented value: -1
507 if (partitionOffset >= 0) {
508 offset = partitionOffset;
511 this.offset = offset;
512 this.position = offset;
515 if (contentType == XML_ATTRIBUTE) {
520 if (contentType == XML_TAG) {
521 state = isContinuationPartition() ? STATE_TAG : STATE_DEFAULT;
525 if (contentType == XML_DECL) {
526 state = isContinuationPartition() ? STATE_DECL : STATE_DEFAULT;
530 if (contentType == DTD_INTERNAL ||
531 contentType == DTD_INTERNAL_PI ||
532 contentType == DTD_INTERNAL_DECL ||
533 contentType == DTD_INTERNAL_COMMENT
535 state = STATE_INTERNAL;
539 state = STATE_DEFAULT;
542 private boolean isContinuationPartition() {
544 String type = document.getContentType(offset - 1);
546 if (type != IDocument.DEFAULT_CONTENT_TYPE) {
549 } catch (BadLocationException e) {}