KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > NBSLanguageReader


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;
21
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import org.netbeans.api.languages.ASTItem;
25 import org.netbeans.api.languages.ParseException;
26 import org.netbeans.api.languages.CharInput;
27 import org.netbeans.modules.languages.parser.TokenInput;
28 import org.netbeans.api.languages.ASTToken;
29 import java.io.BufferedReader JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36 import org.netbeans.api.languages.ASTNode;
37 import org.netbeans.api.languages.ParseException;
38 import org.netbeans.modules.languages.parser.Pattern;
39 import org.netbeans.api.languages.ASTToken;
40 import org.netbeans.modules.languages.parser.TokenInput;
41 import org.netbeans.modules.languages.parser.StringInput;
42 import org.openide.ErrorManager;
43 import org.openide.filesystems.FileObject;
44
45     
46     
47 /**
48  *
49  * @author Jan Jancura
50  */

51 public class NBSLanguageReader {
52     
53     public static Language readLanguage (
54         FileObject fo,
55         String JavaDoc mimeType
56     ) throws ParseException, IOException JavaDoc {
57         BufferedReader JavaDoc reader = null;
58         try {
59             return readLanguage (fo.toString (), fo.getInputStream (), mimeType);
60         } finally {
61             if (reader != null)
62                 reader.close ();
63         }
64     }
65     
66     public static Language readLanguage (
67         String JavaDoc fileName,
68         InputStream JavaDoc inputStream,
69         String JavaDoc mimeType
70     ) throws ParseException, IOException JavaDoc {
71         BufferedReader JavaDoc reader = null;
72         try {
73             InputStreamReader JavaDoc r = new InputStreamReader JavaDoc (inputStream);
74             reader = new BufferedReader JavaDoc (r);
75             StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
76             String JavaDoc line = reader.readLine ();
77             while (line != null) {
78                 sb.append (line).append ('\n');
79                 line = reader.readLine ();
80             }
81             return readLanguage (fileName, sb.toString (), mimeType);
82         } finally {
83             if (reader != null)
84                 reader.close ();
85         }
86     }
87     
88     public static Language readLanguage (
89         String JavaDoc fo,
90         String JavaDoc s,
91         String JavaDoc mimeType
92     ) {
93         CharInput input = new StringInput (s, fo.toString ());
94         try {
95             Language language = new Language (mimeType);
96             Language nbsLanguage = NBSLanguage.getNBSLanguage ();
97             TokenInput tokenInput = TokenInput.create (
98                 mimeType,
99                 nbsLanguage.getParser (),
100                 input,
101                 Collections.EMPTY_SET
102             );
103             ASTNode node = nbsLanguage.getAnalyser ().read (tokenInput, false);
104             if (node == null)
105                 System.out.println("Can not parse " + fo);
106             else
107             if (node.getChildren ().isEmpty ())
108                 System.out.println("Can not parse " + fo + " " + node.getNT ());
109             readBody (node, language);
110             return language;
111         } catch (ParseException ex) {
112             ErrorManager.getDefault ().notify (ex);
113             return new Language (mimeType);
114         }
115     }
116     
117     private static void readBody (
118         ASTNode root,
119         Language language
120     ) throws ParseException {
121         Iterator JavaDoc it = root.getChildren ().iterator ();
122         while (it.hasNext ()) {
123             Object JavaDoc o = it.next ();
124             if (o instanceof ASTToken) continue;
125             ASTNode node = (ASTNode) o;
126             if (node.getNT ().equals ("token"))
127                 readToken (node, language, null);
128             else
129             if (node.getNT ().equals ("tokenState"))
130                 readTokenState (node, language);
131             else
132             if (node.getNT ().equals ("grammarRule"))
133                 readGrammarRule (node, language);
134             else
135             if (node.getNT ().equals ("command"))
136                 readCommand (node, language);
137             else
138                 throw new ParseException (
139                     "Unknown grammar rule (" + node.getNT () + ")."
140                 );
141         }
142     }
143     
144     private static void readToken (
145         ASTNode node,
146         Language language,
147         String JavaDoc state
148     ) throws ParseException {
149         String JavaDoc startState = null;
150         String JavaDoc endState = null;
151         Pattern pattern = null;
152         Feature properties = null;
153         String JavaDoc name = node.getTokenType ("identifier").getIdentifier ();
154         ASTNode pnode = node.getNode ("token2.properties");
155         if (pnode != null) {
156             properties = readProperties (null, null, pnode);
157 // startState = getString (properties, "start_state", false);
158
// endState = getString (properties, "end_state", false);
159
// pattern = (Pattern) properties.get ("pattern");
160
startState = (String JavaDoc) properties.getValue ("start_state");
161             endState = (String JavaDoc) properties.getValue ("end_state");
162             pattern = properties.getPattern ("pattern");
163         } else {
164             String JavaDoc patternString = node.getNode ("token2.regularExpression").getAsText ().trim ();
165             endState = node.getTokenTypeIdentifier ("token2.token3.state.identifier");
166             pattern = Pattern.create (patternString);
167         }
168         if (startState != null && state != null)
169             throw new ParseException ("Start state should not be specified inside token group block!");
170         if (startState == null) startState = state;
171         if (endState == null) endState = state;
172         language.addToken (
173             startState,
174             name,
175             pattern,
176             endState,
177             properties
178         );
179     }
180     
181     private static void readGrammarRule (
182         ASTNode node,
183         Language language
184     ) throws ParseException {
185 // String nt = node.getTokenTypeIdentifier ("identifier");
186
// ASTNode rightSide = node.getNode ("rightSide");
187
language.addRule (node);
188     }
189     
190     private static void readTokenState (
191         ASTNode node,
192         Language language
193     ) throws ParseException {
194         String JavaDoc startState = node.getTokenTypeIdentifier ("state.identifier");
195         ASTNode n = node.getNode ("tokenState1.token");
196         if (n != null)
197             readToken (n, language, startState);
198         else
199             readTokenGroup (node.getNode ("tokenState1.tokenGroup"), language, startState);
200     }
201     
202     private static void readTokenGroup (
203         ASTNode node,
204         Language language,
205         String JavaDoc startState
206     ) throws ParseException {
207         Iterator JavaDoc it = node.getNode ("tokensInGroup").getChildren ().iterator ();
208         while (it.hasNext ()) {
209             Object JavaDoc o = it.next ();
210             if (o instanceof ASTToken) continue;
211             ASTNode n = (ASTNode) o;
212             readToken (n, language, startState);
213         }
214     }
215     
216     private static void readCommand (
217         ASTNode commandNode,
218         Language language
219     ) throws ParseException {
220         String JavaDoc keyword = commandNode.getTokenTypeIdentifier ("keyword");
221         ASTNode command0Node = commandNode.getNode ("command0");
222         ASTNode selectorNode = command0Node.getNode ("selector");
223         Selector selector = null;
224         Feature feature = null;
225         if (selectorNode != null) {
226             ASTNode classNode = selectorNode.getNode ("class");
227             selector = Selector.create (readClass (classNode));
228             ASTNode command1Node = command0Node.getNode ("command1");
229             ASTNode valueNode = command1Node.getNode ("value");
230             if (valueNode != null)
231                 feature = readValue (keyword, selector, valueNode);
232             else
233                 feature = Feature.create (keyword, selector);
234         } else {
235             ASTNode valueNode = command0Node.getNode ("value");
236             feature = readValue (keyword, selector, valueNode);
237         }
238         language.addFeature (feature);
239     }
240     
241     private static Feature readValue (
242         String JavaDoc keyword,
243         Selector selector,
244         ASTNode valueNode
245     ) throws ParseException {
246         ASTNode propertiesNode = valueNode.getNode ("properties");
247         if (propertiesNode != null)
248             return readProperties (keyword, selector, propertiesNode);
249         ASTNode classNode = valueNode.getNode ("class");
250         if (classNode != null)
251             return Feature.createMethodCallFeature (keyword, selector, readClass (classNode));
252         String JavaDoc s = valueNode.getTokenTypeIdentifier ("string");
253         s = s.substring (1, s.length () - 1);
254         return Feature.createExpressionFeature (keyword, selector, c (s));
255     }
256     
257     private static Feature readProperties (
258         String JavaDoc keyword,
259         Selector selector,
260         ASTNode node
261     ) throws ParseException {
262         Map JavaDoc<String JavaDoc,String JavaDoc> methods = new HashMap JavaDoc<String JavaDoc,String JavaDoc> ();
263         Map JavaDoc<String JavaDoc,String JavaDoc> expressions = new HashMap JavaDoc<String JavaDoc,String JavaDoc> ();
264         Map JavaDoc<String JavaDoc,Pattern> patterns = new HashMap JavaDoc<String JavaDoc,Pattern> ();
265         
266         Iterator JavaDoc it = node.getChildren ().iterator ();
267         while (it.hasNext ()) {
268             Object JavaDoc o = it.next ();
269             if (o instanceof ASTToken) continue;
270             ASTNode n = (ASTNode) o;
271             String JavaDoc key = n.getTokenTypeIdentifier ("identifier");
272             String JavaDoc value = n.getTokenTypeIdentifier ("propertyValue.string");
273             if (value != null) {
274                 value = value.substring (1, value.length () - 1);
275                 expressions.put (key, c (value));
276             } else
277             if (n.getNode ("propertyValue.class") != null) {
278                 value = readClass (n.getNode ("propertyValue.class"));
279                 methods.put (key, value);
280             } else {
281                 value = n.getNode ("propertyValue.regularExpression").getAsText ().trim ();
282                 Pattern pattern = Pattern.create (value);
283                 patterns.put (key, pattern);
284             }
285         }
286         return Feature.create (keyword, selector, expressions, methods, patterns);
287     }
288     
289     private static String JavaDoc readClass (ASTNode cls) {
290         StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
291         sb.append (cls.getTokenTypeIdentifier ("identifier"));
292         Iterator JavaDoc<ASTItem> it = cls.getNode ("class1").getChildren ().iterator ();
293         while (it.hasNext ()) {
294             ASTToken token = (ASTToken) it.next ();
295             if (token.getIdentifier ().equals ("."))
296                 sb.append ('.');
297             else
298             if (token.getType ().equals ("identifier"))
299                 sb.append (token.getIdentifier ());
300         }
301         return sb.toString ();
302     }
303     
304     
305     private static String JavaDoc c (String JavaDoc s) {
306         s = s.replace ("\\n", "\n");
307         s = s.replace ("\\r", "\r");
308         s = s.replace ("\\t", "\t");
309         s = s.replace ("\\\"", "\"");
310         s = s.replace ("\\\'", "\'");
311         s = s.replace ("\\\\", "\\");
312         return s;
313     }
314 }
315
Popular Tags