KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > lexer > SLanguageHierarchy


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.modules.languages.lexer;
21
22 import org.netbeans.api.languages.LanguagesManager;
23 import org.netbeans.api.languages.ParseException;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import org.netbeans.modules.languages.Language;
29 import org.netbeans.modules.languages.Language.TokenType;
30 import org.netbeans.api.languages.ParseException;
31 import org.netbeans.modules.languages.LanguagesManagerImpl;
32 import org.netbeans.spi.lexer.LanguageHierarchy;
33 import org.netbeans.spi.lexer.Lexer;
34 import org.netbeans.spi.lexer.LexerRestartInfo;
35
36
37 /**
38  *
39  * @author Jan Jancura
40  */

41 public class SLanguageHierarchy extends LanguageHierarchy<STokenId> {
42     
43     private String JavaDoc mimeType;
44     private Collection JavaDoc<STokenId> tokenIds;
45     private HashMap JavaDoc<String JavaDoc,STokenId> tokensMap;
46     
47     
48     SLanguageHierarchy (String JavaDoc mimeType) {
49         this.mimeType = mimeType;
50     }
51     
52     protected Collection JavaDoc<STokenId> createTokenIds () {
53         if (tokenIds == null) {
54             tokenIds = new ArrayList JavaDoc<STokenId> ();
55             tokensMap = new HashMap JavaDoc<String JavaDoc,STokenId> ();
56             Iterator JavaDoc<TokenType> it = getLanguage ().getTokenTypes ().iterator ();
57             while (it.hasNext ()) {
58                 TokenType t = it.next ();
59                 if (tokensMap.containsKey (t.getType ())) continue;
60                 STokenId tokenId = new STokenId (
61                     t.getType (),
62                     tokenIds.size (),
63                     t.getType ()
64                 );
65                 tokenIds.add (tokenId);
66                 tokensMap.put (t.getType (), tokenId);
67             }
68             STokenId errorTokenId = new STokenId (
69                 "error",
70                 tokenIds.size (),
71                 "error"
72             );
73             tokenIds.add (errorTokenId);
74             tokensMap.put ("error", errorTokenId);
75             STokenId embeddingTokenId = new STokenId (
76                 "PE",
77                 tokenIds.size (),
78                 "PE"
79             );
80             tokenIds.add (embeddingTokenId);
81             tokensMap.put ("PE", embeddingTokenId);
82         }
83         return tokenIds;
84     }
85
86     protected Lexer<STokenId> createLexer (LexerRestartInfo<STokenId> info) {
87         if (tokensMap == null) createTokenIds ();
88         return new SLexer (
89             getLanguage (),
90             tokensMap,
91             info
92         );
93     }
94
95     protected String JavaDoc mimeType () {
96         return mimeType;
97     }
98     
99     
100     // other methods ...........................................................
101

102     private Language language;
103     
104     private Language getLanguage () {
105         if (language == null)
106             try {
107                 language = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage (mimeType);
108             } catch (ParseException ex) {
109                 language = new Language (mimeType);
110             }
111         return language;
112     }
113 }
114
Popular Tags