KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > html > lexer > HTMLTokenId


1 /*
2  * Sun Public License Notice
3  *
4  * The contents of this file are subject to the Sun Public License
5  * Version 1.0 (the "License"). You may not use this file except in
6  * compliance with the License. A copy of the License is available at
7  * http://www.sun.com/
8  *
9  * The Original Code is NetBeans. The Initial Developer of the Original
10  * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
11  * Microsystems, Inc. All Rights Reserved.
12  */

13 package org.netbeans.api.html.lexer;
14
15 import java.util.Collection JavaDoc;
16 import java.util.EnumSet JavaDoc;
17 import java.util.Map JavaDoc;
18 import org.netbeans.api.lexer.InputAttributes;
19 import org.netbeans.api.lexer.Language;
20 import org.netbeans.api.lexer.Language;
21 import org.netbeans.api.lexer.LanguagePath;
22 import org.netbeans.api.lexer.Token;
23 import org.netbeans.api.lexer.TokenId;
24 import org.netbeans.lib.html.lexer.HTMLLexer;
25 import org.netbeans.spi.lexer.LanguageEmbedding;
26 import org.netbeans.spi.lexer.LanguageHierarchy;
27 import org.netbeans.spi.lexer.Lexer;
28 import org.netbeans.spi.lexer.LexerRestartInfo;
29
30 /**
31  * Token ids of HTML language
32  *
33  * @author Jan Lahoda, Miloslav Metelka, Marek Fukala
34  */

35 public enum HTMLTokenId implements TokenId {
36     
37     /** HTML text */
38     TEXT("text"),
39     /** HTML script e.g. javascript. */
40     SCRIPT("script"),
41     /** Whitespace in a tag: <code> &lt;BODY" "bgcolor=red&gt;</code>. */
42     WS("ws"),
43     /** Error token - returned in various erroneous situations. */
44     ERROR("error"),
45     /** HTML open tag name: <code>&lt;"BODY"/&gt;</code>.*/
46     TAG_OPEN("tag"),
47     /** HTML close tag name: <code>&lt;/"BODY"&gt;</code>.*/
48     TAG_CLOSE("tag"),
49     /** HTML tag attribute name: <code> &lt;BODY "bgcolor"=red&gt;</code>.*/
50     ARGUMENT("argument"),
51     /** Equals sign in HTML tag: <code> &lt;BODY bgcolor"="red&gt;</code>.*/
52     OPERATOR("operator"),
53     /** Attribute value in HTML tag: <code> &lt;BODY bgcolor="red"&gt;</code>.*/
54     VALUE("value"),
55     /** HTML block comment: <code> &lt;!-- xxx --&gt; </code>.*/
56     BLOCK_COMMENT("block-comment"),
57     /** HTML/SGML comment.*/
58     SGML_COMMENT("sgml-comment"),
59     /** HTML/SGML declaration: <code> &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt; </code>.*/
60     DECLARATION("sgml-declaration"),
61     /** Character reference: <code> &amp;amp; </code>.*/
62     CHARACTER("character"),
63     /** End of line.*/
64     EOL("text"),
65     /** HTML open tag symbol: <code> "&lt;"BODY&gt; </code>.*/
66     TAG_OPEN_SYMBOL("tag"),
67     /** HTML close tag symbol: <code> "&lt;/"BODY&gt; </code>.*/
68     TAG_CLOSE_SYMBOL("tag");
69     
70     private final String JavaDoc primaryCategory;
71
72     private static final String JavaDoc JAVASCRIPT_MIMETYPE = "text/javascript";//NOI18N
73

74     HTMLTokenId(String JavaDoc primaryCategory) {
75         this.primaryCategory = primaryCategory;
76     }
77
78     private static final Language<HTMLTokenId> language = new LanguageHierarchy<HTMLTokenId>() {
79         @Override JavaDoc
80         protected Collection JavaDoc<HTMLTokenId> createTokenIds() {
81             return EnumSet.allOf(HTMLTokenId.class);
82         }
83         
84         @Override JavaDoc
85         protected Map JavaDoc<String JavaDoc,Collection JavaDoc<HTMLTokenId>> createTokenCategories() {
86             //Map<String,Collection<HTMLTokenId>> cats = new HashMap<String,Collection<HTMLTokenId>>();
87
// Additional literals being a lexical error
88
//cats.put("error", EnumSet.of());
89
return null;
90         }
91         
92         @Override JavaDoc
93         protected Lexer<HTMLTokenId> createLexer(LexerRestartInfo<HTMLTokenId> info) {
94             return new HTMLLexer(info);
95         }
96         
97         @Override JavaDoc
98         protected LanguageEmbedding embedding(
99         Token<HTMLTokenId> token, LanguagePath languagePath, InputAttributes inputAttributes) {
100             if(token.id() == HTMLTokenId.SCRIPT) {
101                 Language lang = Language.find(JAVASCRIPT_MIMETYPE);
102                 if(lang == null) {
103                     return null; //no javascript language found
104
} else {
105                     return LanguageEmbedding.create(lang, 0, 0);
106                 }
107             } else {
108                 return null;
109             }
110         }
111         
112         @Override JavaDoc
113         protected String JavaDoc mimeType() {
114             return "text/html";
115         }
116     }.language();
117     
118     /** Gets a LanguageDescription describing a set of token ids
119      * that comprise the given language.
120      *
121      * @return non-null LanguageDescription
122      */

123     public static Language<HTMLTokenId> language() {
124         return language;
125     }
126     
127     /**
128      * Get name of primary token category into which this token belongs.
129      * <br/>
130      * Other token categories for this id can be defined in the language hierarchy.
131      *
132      * @return name of the primary token category into which this token belongs
133      * or null if there is no primary category for this token.
134      */

135     public String JavaDoc primaryCategory() {
136         return primaryCategory;
137     }
138     
139 }
140
Popular Tags