001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.jsp.lexer;
043:
044: import java.util.Collection;
045: import java.util.EnumSet;
046: import java.util.Map;
047: import org.netbeans.api.html.lexer.HTMLTokenId;
048: import org.netbeans.api.java.lexer.JavaTokenId;
049: import org.netbeans.api.lexer.InputAttributes;
050: import org.netbeans.api.lexer.Language;
051: import org.netbeans.api.lexer.LanguagePath;
052: import org.netbeans.api.lexer.Token;
053: import org.netbeans.api.lexer.TokenId;
054: import org.netbeans.lib.jsp.lexer.JspLexer;
055: import org.netbeans.modules.el.lexer.api.ELTokenId;
056: import org.netbeans.spi.lexer.LanguageEmbedding;
057: import org.netbeans.spi.lexer.LanguageHierarchy;
058: import org.netbeans.spi.lexer.Lexer;
059: import org.netbeans.spi.lexer.LexerRestartInfo;
060:
061: /**
062: * Token Ids for JSP language
063: *
064: * @author Marek Fukala
065: */
066:
067: public enum JspTokenId implements TokenId {
068:
069: TEXT("text"), SCRIPTLET("scriptlet"), ERROR("error"), TAG(
070: "tag-directive"), ENDTAG("endtag"), SYMBOL("symbol"), SYMBOL2(
071: "scriptlet-delimiter"), COMMENT("comment"), ATTRIBUTE(
072: "attribute-name"), ATTR_VALUE("attribute-value"), EOL("EOL"), WHITESPACE(
073: "jsp-whitespace"), //coloring workaround - prefix must be removed once the coloring is fully constructed based on language path
074: EL("expression-language");
075:
076: /** Java code in JSP types.*/
077: public static enum JavaCodeType {
078: SCRIPTLET("scriptlet"), DECLARATION("declaration"), EXPRESSION(
079: "expression");
080:
081: private final String type;
082:
083: JavaCodeType(String type) {
084: this .type = type;
085: }
086: }
087:
088: /** Use this property for jsp scriptlet token get the information about the type of the code. See {@JavaCodeType} */
089: public static final String SCRIPTLET_TOKEN_TYPE_PROPERTY = "JAVA_CODE_TYPE";
090:
091: private final String primaryCategory;
092:
093: JspTokenId() {
094: this (null);
095: }
096:
097: JspTokenId(String primaryCategory) {
098: this .primaryCategory = primaryCategory;
099: }
100:
101: public String primaryCategory() {
102: return primaryCategory;
103: }
104:
105: // Token ids declaration
106: private static final Language<JspTokenId> language = new LanguageHierarchy<JspTokenId>() {
107: @Override
108: protected Collection<JspTokenId> createTokenIds() {
109: return EnumSet.allOf(JspTokenId.class);
110: }
111:
112: @Override
113: protected Map<String, Collection<JspTokenId>> createTokenCategories() {
114: //Map<String,Collection<JspTokenId>> cats = new HashMap<String,Collection<JspTokenId>>();
115: // Additional literals being a lexical error
116: //cats.put("error", EnumSet.of());
117: return null;
118: }
119:
120: @Override
121: protected Lexer<JspTokenId> createLexer(
122: LexerRestartInfo<JspTokenId> info) {
123: return new JspLexer(info);
124: }
125:
126: @Override
127: protected LanguageEmbedding<?> embedding(
128: Token<JspTokenId> token, LanguagePath languagePath,
129: InputAttributes inputAttributes) {
130: switch (token.id()) {
131: case TEXT:
132: return LanguageEmbedding.create(HTMLTokenId.language(),
133: 0, 0, true);
134: case EL:
135: //lexer infrastructure workaround - need to adjust skiplenghts in case of short token
136: int startSkipLength = token.length() > 2 ? 2 : token
137: .length();
138: int endSkipLength = token.length() > 2 ? 1 : 0;
139: return LanguageEmbedding.create(ELTokenId.language(),
140: startSkipLength, endSkipLength);
141:
142: case SCRIPTLET:
143: return LanguageEmbedding.create(JavaTokenId.language(),
144: 0, 0, true);
145:
146: default:
147: return null;
148: }
149: }
150:
151: @Override
152: protected String mimeType() {
153: return "text/x-jsp";
154: }
155: }.language();
156:
157: public static Language<JspTokenId> language() {
158: return language;
159: }
160:
161: }
|