KickJava   Java API By Example, From Geeks To Geeks.

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


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.DTDSyntaxConstants;
24
25 /**
26  * Factory mappring jjID => TokenID.
27  *
28  * @author Petr Kuzel
29  * @version 1.0
30  */

31 public class DTDSyntaxTokenMapper implements JJMapperInterface, JJConstants, DTDSyntaxConstants {
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 DTDTokenContext.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 DTDTokenContext.ERROR;
47                 
48             case DECL_START:
49             case PI_START:
50             case COND_END_IN_DEFAULT:
51             case PI_CONTENT_START:
52             case PI_END:
53             case PI_CONTENT_END:
54             case XML_DECL_END:
55             case COND:
56             case DECL_END:
57             case ENTITY_END:
58             case ELEMENT_END:
59             case NOTATION_END:
60             case COND_END:
61             case ATTLIST_END:
62             case SYMBOL_IN_ELEMENT:
63                 
64                 return DTDTokenContext.SYMBOL;
65                 
66             case ERR_IN_PI:
67             case ERR_IN_PI_CONTENT:
68             case ERR_IN_DECL:
69             case ERR_IN_COND:
70             case ERR_IN_DEFAULT:
71             case ERR_IN_COMMENT:
72             case ERR_IN_CREF:
73             case ERR_IN_CHREF:
74                 
75                 return DTDTokenContext.ERROR;
76                 
77                 
78             case XML_TARGET:
79             case PI_TARGET:
80                 
81                 return DTDTokenContext.TARGET;
82                 
83             case ENTITY:
84             case ATTLIST:
85             case DOCTYPE:
86             case ELEMENT:
87             case NOTATION:
88             case KW_IN_ELEMENT:
89             case KW_IN_NOTATION:
90             case KW_IN_COND:
91             case KW_IN_ATTLIST:
92             case KW_IN_XML_DECL:
93             case KW_IN_ENTITY:
94                 
95                 return DTDTokenContext.KW;
96                 
97                 
98             case PREF_START:
99             case TEXT_IN_PREF:
100             case PREF_END:
101             case CREF_START:
102             case CREF_END:
103             case TEXT_IN_CREF:
104             case CHREF_START:
105             case CHREF_END:
106             case TEXT_IN_CHREF:
107                 
108                 return DTDTokenContext.REF;
109                 
110                 
111             case CHARS_START:
112             case TEXT_IN_CHARS:
113             case CHARS_END:
114             case STRING_START:
115             case TEXT_IN_STRING:
116             case STRING_END:
117                 
118                 return DTDTokenContext.STRING;
119                 
120             case COMMENT_START:
121             case TEXT_IN_COMMENT:
122             case COMMENT_END:
123                 
124                 return DTDTokenContext.COMMENT;
125                 
126             default:
127                 return DTDTokenContext.PLAIN;
128                                         
129         }
130         
131     }
132     
133     /** @return token guessed for particular state. */
134     public final JJTokenID guessToken(String JavaDoc token,int state,boolean lastBuffer) {
135         
136         switch (state) {
137             case IN_COMMENT:
138                 if (!("--".equals(token) || "-".equals(token))) { // NOI18N
139
return DTDTokenContext.COMMENT;
140                 } else {
141                     return cannotGuess(lastBuffer);
142                 }
143                 
144             case IN_CREF:
145             case IN_PREF:
146                 return DTDTokenContext.REF;
147                 
148             case IN_STRING:
149             case IN_CHARS:
150                 return DTDTokenContext.STRING;
151                 
152             default:
153                 return cannotGuess(lastBuffer);
154         }
155     }
156     
157     private JJTokenID cannotGuess(boolean lastBuffer) {
158         return cannotGuess(lastBuffer, DTDTokenContext.PLAIN);
159     }
160     
161     private JJTokenID cannotGuess(boolean lastBuffer, JJTokenID supposed) {
162         if (lastBuffer) {
163             return supposed;
164         } else {
165             //ask for next buffer
166
return null;
167         }
168         
169     }
170     /** Called if createToken(int id) return isError() token.
171     * @return supposed token for particular id and state.
172     */

173     public JJTokenID supposedToken(String JavaDoc token,int state,int id) {
174         
175         switch (state) {
176             case IN_COMMENT:
177                 return DTDTokenContext.COMMENT;
178                 
179             default:
180                 return null;
181         }
182     }
183     
184 }
185
Popular Tags