KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > syntax > XMLSyntaxTokenMapper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.text.syntax;
20
21 import org.netbeans.editor.*;
22 import org.netbeans.modules.xml.text.syntax.javacc.lib.*;
23 import org.netbeans.modules.xml.text.syntax.javacc.XMLSyntaxConstants;
24
25 /**
26  * Factory mapping jjID => TokenID.
27  *
28  * @author Petr Kuzel
29  * @version 1.0
30  */

31 public class XMLSyntaxTokenMapper implements JJMapperInterface, JJConstants, XMLSyntaxConstants {
32
33
34     /** Create token for particular ID. */
35     public JJTokenID createToken(int id) {
36
37         switch(id) {
38
39             case JJ_EOL:
40                 return XMLTokenContext.EOL;
41                 
42             case JJ_EOF:
43                 throw new Error JavaDoc("guessToken() must be called for such case."); // NOI18N
44

45             case JJ_ERR:
46                 return XMLTokenContext.ERROR;
47                 
48             // case RCB:
49
case RSB:
50             case TAG_START:
51             case DECL_START:
52             case PI_START:
53             case CDATA_START:
54             case COND_END_IN_DEFAULT:
55             case EQ_IN_TAG_ATTLIST:
56             case TAG_END:
57             case PI_CONTENT_START:
58             case PI_END:
59             case PI_CONTENT_END:
60             case CDATA_END:
61             case COND:
62             case DECL_END:
63             case ENTITY_END:
64             case ELEMENT_END:
65             case NOTATION_END:
66             case COND_END:
67             case ATTLIST_END:
68             case DOCTYPE_END:
69                 
70                 return XMLTokenContext.SYMBOL;
71                 
72             case TAG_NAME:
73                 
74                 return XMLTokenContext.TAG;
75
76             case ERR_IN_GREF:
77             case ERR_IN_TAG:
78             case ERR_IN_TAG_ATTLIST:
79             case ERR_IN_PI:
80             case ERR_IN_PI_CONTENT:
81             case ERR_IN_DECL:
82             case ERR_IN_NOTATION:
83             case ERR_IN_COND:
84             case ERR_IN_ATTLIST:
85             case ERR_IN_DOCTYPE:
86             case ERR_IN_DEFAULT:
87             case ERR_IN_COMMENT:
88                 
89                 return XMLTokenContext.ERROR;
90                 
91             case ATT_NAME:
92                 
93                 return XMLTokenContext.ATT;
94                 
95             case PI_TARGET:
96             case XML_TARGET:
97                 
98                 return XMLTokenContext.TARGET;
99                 
100 // case B_IN_CDATA:
101
case TEXT_IN_CDATA:
102                 
103                 return XMLTokenContext.CDATA;
104
105             case MARKUP_IN_CDATA:
106
107                 return XMLTokenContext.CDATA_MARKUP;
108                 
109             case KW_IN_XML_DECL:
110             case ENTITY:
111             case ATTLIST:
112             case DOCTYPE:
113             case ELEMENT:
114             case NOTATION:
115             case KW_IN_ENTITY:
116             case EMPTY:
117             case PCDATA:
118             case ANY:
119             case SYSTEM_IN_NOTATION:
120             case INCLUDE:
121             case IGNORE:
122             case REQUIRED:
123             case IMPLIED:
124             case FIXED:
125             case ID_IN_ATTLIST:
126             case CDATA:
127             case IDREF:
128             case IDREFS:
129             case ENTITY_IN_ATTLIST:
130             case ENTITIES:
131             case NMTOKEN:
132             case NMTOKENS:
133             case NOTATION_IN_ATTLIST:
134             case PUBLIC:
135             case SYSTEM:
136                 
137                 return XMLTokenContext.KW;
138                 
139                 
140             case PREF_START:
141             case TEXT_IN_PREF:
142             case GREF_END:
143             case PREF_END:
144             case GREF_START:
145             case TEXT_IN_GREF:
146                 
147                 return XMLTokenContext.REF;
148                                 
149             case CHARS_START:
150             case TEXT_IN_CHARS:
151             case CHARS_END:
152             case GREF_CHARS_START:
153             case TEXT_IN_GREF_CHARS:
154             case GREF_CHARS_END:
155                 
156             case STRING_START:
157             case TEXT_IN_STRING:
158             case STRING_END:
159             case GREF_STRING_START:
160             case TEXT_IN_GREF_STRING:
161             case GREF_STRING_END:
162                 
163                 return XMLTokenContext.STRING;
164                 
165             case COMMENT_START:
166             case TEXT_IN_COMMENT:
167             case COMMENT_END:
168                 
169                 return XMLTokenContext.COMMENT;
170                 
171                 default:
172                     return XMLTokenContext.PLAIN;
173         }
174         
175     }
176     /** @return token guessed for particular state. */
177     public final JJTokenID guessToken(String JavaDoc token,int state,boolean lastBuffer) {
178         
179         switch (state) {
180             case XMLSyntaxConstants.IN_COMMENT:
181                 if (!("--".equals(token) || "-".equals(token))) { // NOI18N
182
return XMLTokenContext.COMMENT;
183                 } else {
184                     return cannotGuess(lastBuffer);
185                 }
186                 
187             case XMLSyntaxConstants.IN_GREF:
188             case XMLSyntaxConstants.IN_PREF:
189                 return XMLTokenContext.REF;
190                 
191             case XMLSyntaxConstants.IN_STRING:
192             case XMLSyntaxConstants.IN_CHARS:
193             case XMLSyntaxConstants.IN_GREF_STRING:
194             case XMLSyntaxConstants.IN_GREF_CHARS:
195                 return XMLTokenContext.STRING;
196                 
197             default:
198                 return cannotGuess(lastBuffer);
199         }
200     }
201     
202     private JJTokenID cannotGuess(boolean lastBuffer) {
203         return cannotGuess(lastBuffer, XMLTokenContext.PLAIN);
204     }
205     
206     private JJTokenID cannotGuess(boolean lastBuffer, JJTokenID supposed) {
207         if (lastBuffer) {
208             return supposed;
209         } else {
210             //ask for next buffer
211
return null;
212         }
213         
214     }
215     /** @return supposed token for particular id and state. */
216     public JJTokenID supposedToken(String JavaDoc token,int id,int state) {
217         switch (state) {
218             case IN_TAG:
219                 return XMLTokenContext.TAG;
220                 
221             case IN_COMMENT:
222                 return XMLTokenContext.COMMENT;
223                 
224             default:
225                 return null;
226         }
227     }
228     
229     
230 }
231
Popular Tags