KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > jsp > lexer > JspTokenId


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
20 package org.netbeans.api.jsp.lexer;
21
22 import java.util.Collection JavaDoc;
23 import java.util.EnumSet JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.netbeans.api.html.lexer.HTMLTokenId;
26 import org.netbeans.api.java.lexer.JavaTokenId;
27 import org.netbeans.api.lexer.InputAttributes;
28 import org.netbeans.api.lexer.Language;
29 import org.netbeans.api.lexer.LanguagePath;
30 import org.netbeans.api.lexer.Token;
31 import org.netbeans.api.lexer.TokenId;
32 import org.netbeans.lib.jsp.lexer.JspLexer;
33 import org.netbeans.modules.el.lexer.api.ELTokenId;
34 import org.netbeans.spi.lexer.LanguageEmbedding;
35 import org.netbeans.spi.lexer.LanguageHierarchy;
36 import org.netbeans.spi.lexer.Lexer;
37 import org.netbeans.spi.lexer.LexerRestartInfo;
38
39 /**
40  * Token Ids for JSP language
41  *
42  * @author Marek Fukala
43  */

44
45 public enum JspTokenId implements TokenId {
46
47     TEXT("text"),
48     SCRIPTLET("scriptlet"),
49     ERROR("error"),
50     TAG("tag-directive"),
51     SYMBOL("symbol"),
52     SYMBOL2("scriptlet-delimiter"),
53     COMMENT("comment"),
54     ATTRIBUTE("attribute-name"),
55     ATTR_VALUE("attribute-value"),
56     EOL("EOL"),
57     WHITESPACE("jsp-whitespace"), //coloring workaround - prefix must be removed once the coloring is fully constructed based on language path
58
EL("expression-language");
59     
60    /** Java code in JSP types.*/
61     public static enum JavaCodeType {
62         SCRIPTLET("scriptlet"),
63         DECLARATION("declaration"),
64         EXPRESSION("expression");
65         
66         private final String JavaDoc type;
67         
68         JavaCodeType(String JavaDoc type) {
69             this.type = type;
70         }
71     }
72     
73     /** Use this property for jsp scriptlet token get the information about the type of the code. See {@JavaCodeType} */
74     public static final String JavaDoc SCRIPTLET_TOKEN_TYPE_PROPERTY = "JAVA_CODE_TYPE";
75
76     private final String JavaDoc primaryCategory;
77
78     JspTokenId() {
79         this(null);
80     }
81
82     JspTokenId(String JavaDoc primaryCategory) {
83         this.primaryCategory = primaryCategory;
84     }
85
86     public String JavaDoc primaryCategory() {
87         return primaryCategory;
88     }
89
90     // Token ids declaration
91
private static final Language<JspTokenId> language = new LanguageHierarchy<JspTokenId>() {
92         @Override JavaDoc
93         protected Collection JavaDoc<JspTokenId> createTokenIds() {
94             return EnumSet.allOf(JspTokenId.class);
95         }
96         
97         @Override JavaDoc
98         protected Map JavaDoc<String JavaDoc,Collection JavaDoc<JspTokenId>> createTokenCategories() {
99             //Map<String,Collection<JspTokenId>> cats = new HashMap<String,Collection<JspTokenId>>();
100
// Additional literals being a lexical error
101
//cats.put("error", EnumSet.of());
102
return null;
103         }
104         
105         @Override JavaDoc
106         protected Lexer<JspTokenId> createLexer(LexerRestartInfo<JspTokenId> info) {
107             return new JspLexer(info);
108         }
109         
110         @Override JavaDoc
111         protected LanguageEmbedding<? extends TokenId> embedding(
112         Token<JspTokenId> token, LanguagePath languagePath, InputAttributes inputAttributes) {
113             switch(token.id()) {
114                 case TEXT:
115                     return LanguageEmbedding.create(HTMLTokenId.language(), 0, 0);
116                 case EL:
117                     //lexer infrastructure workaround - need to adjust skiplenghts in case of short token
118
int startSkipLength = token.length() > 2 ? 2 : token.length();
119                     int endSkipLength = token.length() > 2 ? 1 : 0;
120                     return LanguageEmbedding.create(ELTokenId.language(), startSkipLength, endSkipLength);
121                     
122                 case SCRIPTLET:
123                     return LanguageEmbedding.create(JavaTokenId.language(), 0, 0);
124                     
125                 default:
126                     return null;
127             }
128         }
129         
130         @Override JavaDoc
131         protected String JavaDoc mimeType() {
132             return "text/x-jsp";
133         }
134     }.language();
135     
136     public static Language<JspTokenId> language() {
137         return language;
138     }
139     
140
141 }
142
143
Popular Tags