e99172aa31284b46223f291fb282dd4a8ef3b619
[phpeclipse.git] /
1 /*
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
7  *
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  *
11  * $Id: XMLCDATAScanner.java,v 1.1 2004-09-02 18:28:03 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.internal.text;
15
16 import java.util.Map;
17
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.rules.IToken;
21 import org.eclipse.jface.text.rules.ITokenScanner;
22 import org.eclipse.jface.text.rules.Token;
23
24 import net.sourceforge.phpeclipse.xml.ui.text.IXMLSyntaxConstants;
25
26 /**
27  * @author Igor Malinin
28  */
29 public class XMLCDATAScanner implements ITokenScanner {
30
31         private Map tokens;
32
33         private IDocument document;
34
35         private int begin;
36
37         private int end;
38
39         private int offset;
40
41         private int length;
42
43         private int position;
44
45         public XMLCDATAScanner(Map tokens) {
46                 this.tokens = tokens;
47         }
48
49         /*
50          * @see ITokenScanner#setRange(IDocument, int, int)
51          */
52         public void setRange(IDocument document, int offset, int length) {
53                 this.document = document;
54
55                 this.begin = offset;
56                 this.end = offset + length;
57
58                 this.offset = offset;
59                 this.position = offset;
60                 this.length = 0;
61         }
62
63         /*
64          * @see ITokenScanner#nextToken()
65          */
66         public IToken nextToken() {
67                 offset += length;
68
69                 if (position == begin) {
70                         position += 3; // <![
71
72                         try {
73                                 if (document.get(position, 6).equals("CDATA[")) {
74                                         position += 6;
75                                 }
76                         } catch (BadLocationException e) {
77                         }
78
79                         return getToken(IXMLSyntaxConstants.XML_CDATA);
80                 }
81
82                 if (position == end) {
83                         return getToken(null);
84                 }
85
86                 try {
87                         int p = end - 3;
88                         if (document.get(p, 3).equals("]]>")) {
89                                 if (position == p) {
90                                         position = end;
91                                         return getToken(IXMLSyntaxConstants.XML_CDATA);
92                                 }
93
94                                 position = p;
95                         } else {
96                                 position = end;
97                         }
98                 } catch (BadLocationException e) {
99                 }
100
101                 return getToken(IXMLSyntaxConstants.XML_DEFAULT);
102         }
103
104         private IToken getToken(String type) {
105                 length = position - offset;
106
107                 if (length == 0) {
108                         return Token.EOF;
109                 }
110
111                 if (type == null) {
112                         return Token.UNDEFINED;
113                 }
114
115                 IToken token = (IToken) tokens.get(type);
116                 if (token == null) {
117                         return Token.UNDEFINED;
118                 }
119
120                 return token;
121         }
122
123         /*
124          * @see ITokenScanner#getTokenOffset()
125          */
126         public int getTokenOffset() {
127                 return offset;
128         }
129
130         /*
131          * @see ITokenScanner#getTokenLength()
132          */
133         public int getTokenLength() {
134                 return length;
135         }
136 }