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-2007 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.modules.ruby.rhtml.lexer.api;
043:
044: import org.netbeans.modules.ruby.rhtml.lexer.RhtmlLexer;
045: import java.util.Collection;
046: import java.util.EnumSet;
047: import java.util.Map;
048: import org.netbeans.modules.ruby.lexer.RubyTokenId;
049: import org.netbeans.api.html.lexer.HTMLTokenId;
050: import org.netbeans.api.lexer.InputAttributes;
051: import org.netbeans.api.lexer.Language;
052: import org.netbeans.api.lexer.LanguagePath;
053: import org.netbeans.api.lexer.Token;
054: import org.netbeans.api.lexer.TokenId;
055: import org.netbeans.spi.lexer.LanguageEmbedding;
056: import org.netbeans.spi.lexer.LanguageHierarchy;
057: import org.netbeans.spi.lexer.Lexer;
058: import org.netbeans.spi.lexer.LexerRestartInfo;
059:
060: /**
061: * Token Ids for Embedded Ruby (RHTML)
062: *
063: * @todo Worry about trim mode - See section 22.1 of Agile Web Development With Rails
064: *
065: * @author Marek Fukala
066: * @author Tor Norbye
067: */
068:
069: public enum RhtmlTokenId implements TokenId {
070:
071: HTML("html"),
072: /** Contents inside <%# %> */
073: RUBYCOMMENT("comment"),
074: /** Contents inside <%= %> */
075: RUBY_EXPR("ruby"),
076: /** Contents inside <% %> */
077: RUBY("ruby"),
078: /** <% or %> */
079: DELIMITER("ruby-delimiter"); // Note - referenced in LexUtilities
080:
081: public static final String MIME_TYPE = "application/x-httpd-eruby"; // NOI18N
082:
083: private final String primaryCategory;
084:
085: public static boolean isRuby(TokenId id) {
086: return id == RUBY || id == RUBY_EXPR || id == RUBYCOMMENT;
087: }
088:
089: RhtmlTokenId(String primaryCategory) {
090: this .primaryCategory = primaryCategory;
091: }
092:
093: public String primaryCategory() {
094: return primaryCategory;
095: }
096:
097: // Token ids declaration
098: private static final Language<RhtmlTokenId> language = new LanguageHierarchy<RhtmlTokenId>() {
099: protected Collection<RhtmlTokenId> createTokenIds() {
100: return EnumSet.allOf(RhtmlTokenId.class);
101: }
102:
103: @Override
104: protected Map<String, Collection<RhtmlTokenId>> createTokenCategories() {
105: return null;
106: }
107:
108: public Lexer<RhtmlTokenId> createLexer(
109: LexerRestartInfo<RhtmlTokenId> info) {
110: return new RhtmlLexer(info);
111: }
112:
113: @Override
114: protected LanguageEmbedding<? extends TokenId> embedding(
115: Token<RhtmlTokenId> token, LanguagePath languagePath,
116: InputAttributes inputAttributes) {
117: switch (token.id()) {
118: case HTML:
119: return LanguageEmbedding.create(HTMLTokenId.language(),
120: 0, 0, true);
121: case RUBY_EXPR:
122: case RUBY:
123: return LanguageEmbedding.create(RubyTokenId.language(),
124: 0, 0, false);
125: default:
126: return null;
127: }
128: }
129:
130: public String mimeType() {
131: return RhtmlTokenId.MIME_TYPE;
132: }
133: }.language();
134:
135: public static Language<RhtmlTokenId> language() {
136: return language;
137: }
138: }
|