KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.netbeans.api.languages.LanguagesManager;
26 import org.netbeans.api.languages.ParseException;
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.modules.languages.Feature;
33 import org.netbeans.modules.languages.LanguagesManagerImpl;
34 import org.netbeans.spi.lexer.LanguageEmbedding;
35 import org.netbeans.spi.lexer.LanguageProvider;
36
37
38 /**
39  *
40  * @author Jan Jancura
41  */

42 public class SLanguageProvider extends LanguageProvider {
43     
44     /** Creates a new instance of SLanguageProvider */
45     public SLanguageProvider () {
46     }
47
48     public Language<STokenId> findLanguage (String JavaDoc mimePath) {
49 // System.out.println("findLanguage " + mimePath);
50
if (LanguagesManager.getDefault ().getSupportedMimeTypes ().contains (mimePath))
51             return new SLanguageHierarchy (mimePath).language ();
52         return null;
53     }
54
55     public LanguageEmbedding<? extends TokenId> findLanguageEmbedding (
56         Token token,
57         LanguagePath languagePath,
58         InputAttributes inputAttributes
59     ) {
60         String JavaDoc mimeType = languagePath.innerLanguage ().mimeType ();
61         Language<STokenId> language = getTokenImport (mimeType, token);
62         if (language == null)
63             language = getPreprocessorImport (languagePath, token);
64         if (language == null) return null;
65         Integer JavaDoc i = (Integer JavaDoc) token.getProperty ("startSkipLength");
66         int startSkipLength = i == null ? 0 : i.intValue ();
67         i = (Integer JavaDoc) token.getProperty ("endSkipLength");
68         int endSkipLength = i == null ? 0 : i.intValue ();
69         return LanguageEmbedding.create (
70             language,
71             startSkipLength,
72             endSkipLength
73         );
74     }
75
76     
77     // other methods ...........................................................
78

79     private static Map JavaDoc<String JavaDoc,Language<STokenId>> preprocessorImport = new HashMap JavaDoc<String JavaDoc,Language<STokenId>> ();
80     
81     private static Language<STokenId> getPreprocessorImport (LanguagePath languagePath, Token token) {
82         String JavaDoc tokenType = token.id ().name ();
83         if (!tokenType.equals ("PE")) return null;
84         String JavaDoc mimeType = languagePath.topLanguage ().mimeType ();
85         if (!preprocessorImport.containsKey (mimeType)) {
86             try {
87                 org.netbeans.modules.languages.Language language =
88                     ((LanguagesManagerImpl) LanguagesManager.getDefault ()).
89                     getLanguage (mimeType);
90                 Feature properties = language.getPreprocessorImport ();
91                 if (properties != null) {
92                     String JavaDoc innerMT = (String JavaDoc) properties.getValue ("mimeType");
93                     preprocessorImport.put (
94                         mimeType,
95                         (Language<STokenId>)Language.find (innerMT)
96                     );
97                 }
98             } catch (ParseException ex) {
99             }
100         }
101         return preprocessorImport.get (mimeType);
102     }
103     
104     private static Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,Language<STokenId>>> tokenImports = new HashMap JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,Language<STokenId>>> ();
105     
106     private static Language<STokenId> getTokenImport (String JavaDoc mimeType, Token token) {
107         String JavaDoc tokenType = token.id ().name ();
108         Map JavaDoc<String JavaDoc,Language<STokenId>> tokenTypeToLanguage = tokenImports.get (mimeType);
109         if (tokenTypeToLanguage == null) {
110             tokenTypeToLanguage = new HashMap JavaDoc<String JavaDoc,Language<STokenId>> ();
111             tokenImports.put (mimeType, tokenTypeToLanguage);
112             try {
113                 org.netbeans.modules.languages.Language language =
114                     ((LanguagesManagerImpl) LanguagesManager.getDefault ()).
115                     getLanguage (mimeType);
116                 Map JavaDoc<String JavaDoc,Feature> tokenImports = language.getTokenImports ();
117                 if (tokenImports != null) {
118                     Iterator JavaDoc<String JavaDoc> it = tokenImports.keySet ().iterator ();
119                     while (it.hasNext ()) {
120                         String JavaDoc tokenType2 = it.next ();
121                         Feature properties = tokenImports.get (tokenType2);
122                         String JavaDoc innerMT = (String JavaDoc) properties.getValue ("mimeType");
123                         tokenTypeToLanguage.put (
124                             tokenType2,
125                             (Language<STokenId>) Language.find (innerMT)
126                         );
127                     }
128                 }
129             } catch (ParseException ex) {
130             }
131         }
132         return tokenTypeToLanguage.get (tokenType);
133     }
134 }
135
Popular Tags