4ae432586adf5a69ad02f03c88dcb2c951c559e1
[phpeclipse.git] / net.sourceforge.phpeclipse.xml.ui / src / net / sourceforge / phpeclipse / xml / ui / internal / text / XMLCDATAScanner.java
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.3 2007-04-05 07:50:15 toshihiro Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.internal.text;
15
16 import java.util.Map;
17
18 import net.sourceforge.phpeclipse.xml.ui.text.IXMLSyntaxConstants;
19
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.rules.IToken;
23 import org.eclipse.jface.text.rules.ITokenScanner;
24 import org.eclipse.jface.text.rules.Token;
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 //#1622108
71 //                      position += 3; // <![
72 //                      try {
73 //                              if (document.get(position, 6).equals("CDATA[")) {
74 //                                      position += 6;
75 //                              }
76 //                      } catch (BadLocationException e) {
77 //                      }
78                         try {
79                                 if (document.get(position, 3).equals("<![")) {
80                                         position += 3;
81                                         if (document.get(position, 6).equals("CDATA[")) {
82                                                 position += 6;
83                                         }
84                                 } // else: xml cdata part is divided by php script block
85                         } catch (BadLocationException e) {
86                         }
87
88                         return getToken(IXMLSyntaxConstants.XML_CDATA);
89                 }
90
91                 if (position == end) {
92                         return getToken(null);
93                 }
94
95                 try {
96                         int p = end - 3;
97                         if (document.get(p, 3).equals("]]>")) {
98                                 if (position == p) {
99                                         position = end;
100                                         return getToken(IXMLSyntaxConstants.XML_CDATA);
101                                 }
102
103                                 position = p;
104                         } else {
105                                 position = end;
106                         }
107                 } catch (BadLocationException e) {
108                 }
109
110                 return getToken(IXMLSyntaxConstants.XML_DEFAULT);
111         }
112
113         private IToken getToken(String type) {
114                 length = position - offset;
115
116                 if (length == 0) {
117                         return Token.EOF;
118                 }
119
120                 if (type == null) {
121                         return Token.UNDEFINED;
122                 }
123
124                 IToken token = (IToken) tokens.get(type);
125                 if (token == null) {
126                         return Token.UNDEFINED;
127                 }
128
129                 return token;
130         }
131
132         /*
133          * @see ITokenScanner#getTokenOffset()
134          */
135         public int getTokenOffset() {
136                 return offset;
137         }
138
139         /*
140          * @see ITokenScanner#getTokenLength()
141          */
142         public int getTokenLength() {
143                 return length;
144         }
145 }