KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > gen > DescriptionReader


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.lexer.gen;
21
22 import java.lang.reflect.Field JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.xml.parsers.SAXParser JavaDoc;
31 import javax.xml.parsers.SAXParserFactory JavaDoc;
32 import javax.xml.parsers.ParserConfigurationException JavaDoc;
33
34 import org.xml.sax.Locator JavaDoc;
35 import org.xml.sax.Attributes JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38 import org.xml.sax.SAXParseException JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.helpers.DefaultHandler JavaDoc;
41
42 import org.netbeans.api.lexer.TokenId;
43 import org.netbeans.modules.lexer.gen.util.LexerGenUtilities;
44
45 /**
46  * Updates the language data by reading and interpreting
47  * a given xml file with the language description updates.
48  * <BR>The xml file is read in two rounds. In the first
49  * round all the hidden token types elements
50  * are interpreted. In the second round the rest
51  * of the elements are interpreted.
52  *
53  * @author Miloslav Metelka
54  * @version 1.00
55  */

56
57 public class DescriptionReader extends DefaultHandler JavaDoc {
58     
59     // Elements names
60
private static final String JavaDoc LANGUAGE_ELEM = "Language";
61     private static final String JavaDoc TOKEN_ID_ELEM = "TokenId";
62     private static final String JavaDoc HIDDEN_TOKEN_TYPE_ELEM = "HiddenTokenType";
63     private static final String JavaDoc CATEGORY_ELEM = "Category";
64     private static final String JavaDoc SAMPLE_TEXT_ELEM = "SampleText";
65     private static final String JavaDoc COMMENT_ELEM = "Comment";
66     
67     private static final String JavaDoc NAME_ATTR = "name";
68     private static final String JavaDoc TOKEN_TYPE_ATTR = "tokenType";
69     private static final String JavaDoc SAMPLE_TEXT_CHECK_ATTR = "sampleTextCheck";
70     private static final String JavaDoc CASE_INSENSITIVE_ATTR = "caseInsensitive";
71     private static final String JavaDoc RESET_SAMPLES_ATTR = "resetSamples";
72
73     private String JavaDoc systemId;
74     
75     /** Active language data into which the input xml is parsed. */
76     protected LanguageData languageData;
77     
78     /**
79      * Whether processing HiddenTokenType elements (1st round)
80      * or the rest of the elements (2nd round).
81      */

82     private boolean processingHiddenTokenTypes;
83     
84     /** Currently processed tokenId. */
85     protected MutableTokenId id;
86     
87     /* Currently in Comment element. */
88     private boolean inCommentElement;
89
90     /* Currently in SampleText element. */
91     private boolean inSampleTextElement;
92     
93     /**
94      * Create the description reader over the given systemId.
95      * @param systemId identification of the source xml language description file.
96      */

97     public DescriptionReader(String JavaDoc systemId) {
98         this.systemId = systemId;
99     }
100
101     /**
102      * Parse the xml determined by systemId and update the languageData.
103      * @param languageData update the language data by updating/adding mutable
104      * tokenIds. If there is an TokenId element with a name not yet present
105      * in the languageData it will be added. Otherwise the attributes of existing
106      * mutable tokenId will be updated.
107      * @throws javax.xml.parsers.SAXException (also encapsulates ParserConfigurationException)
108      * and IOException
109      */

110     public synchronized void applyTo(LanguageData languageData) throws SAXException JavaDoc, IOException JavaDoc {
111         
112         this.languageData = languageData;
113
114         try {
115             SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
116             
117             processingHiddenTokenTypes = true;
118             
119             XMLReader JavaDoc reader = parser.getXMLReader();
120             reader.setContentHandler(this);
121             reader.parse(new InputSource JavaDoc(systemId)); // process hidden token types only
122

123             processingHiddenTokenTypes = false;
124
125             reader.parse(new InputSource JavaDoc(systemId)); // process tokenId elements
126

127         } catch (ParserConfigurationException JavaDoc e) {
128             throw new SAXException JavaDoc(e);
129         }
130         
131         this.languageData = null;
132     }
133     
134     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qname,
135     Attributes JavaDoc attributes) throws SAXException JavaDoc {
136         if (LANGUAGE_ELEM.equals(qname)) {
137             // no info
138

139         } else if (TOKEN_ID_ELEM.equals(qname)) {
140             if (!processingHiddenTokenTypes) {
141                 // Create a new tokenId or start updating the existing one
142
String JavaDoc name = empty2nullFromSource(attributes.getValue(NAME_ATTR));
143                 id = languageData.findId(name);
144                 if (id == null) {
145                     id = languageData.newId(name);
146                 }
147
148                 // Possibly update the tokenId by the given token type
149
String JavaDoc tokenTypeName = empty2nullFromSource(attributes.getValue(TOKEN_TYPE_ATTR));
150                 if (tokenTypeName != null) {
151                     id.updateByTokenType(tokenTypeName);
152                 }
153
154                 // Possibly reset the existing samples (usually got from token type)
155
if (toBoolean(attributes.getValue(RESET_SAMPLES_ATTR))) {
156                     id.resetSamples();
157                 }
158                 
159                 // Update case insensitivity
160
id.setCaseInsensitive(toBoolean(attributes.getValue(CASE_INSENSITIVE_ATTR)));
161
162                 // Possibly update type of sample text checking
163
String JavaDoc stc = empty2nullFromSource(attributes.getValue(SAMPLE_TEXT_CHECK_ATTR));
164                 if (stc != null) {
165                     id.setSampleTextCheck(stc);
166                 }
167             }
168         
169         } else if (HIDDEN_TOKEN_TYPE_ELEM.equals(qname)) {
170             if (processingHiddenTokenTypes) {
171                 String JavaDoc tokenTypeName = empty2nullFromSource(attributes.getValue(NAME_ATTR));
172                 MutableTokenId id = languageData.findIdByTokenTypeName(tokenTypeName);
173                 if (id != null) {
174                     languageData.remove(id);
175                 }
176             }
177
178         } else if (CATEGORY_ELEM.equals(qname)) {
179             if (!processingHiddenTokenTypes) {
180                 id.getCategoryNames().add(attributes.getValue(NAME_ATTR));
181             }
182
183         } else if (COMMENT_ELEM.equals(qname)) {
184             if (!processingHiddenTokenTypes) {
185                 inCommentElement = true;
186             }
187           
188         } else if (SAMPLE_TEXT_ELEM.equals(qname)) {
189             if (!processingHiddenTokenTypes) {
190                 inSampleTextElement = true;
191             }
192
193         } else {
194             throw new IllegalStateException JavaDoc("Unknown element qname=" + qname);
195         }
196         
197     }
198
199     /** End element. */
200     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qname) {
201         if (TOKEN_ID_ELEM.equals(qname)) {
202             if (!processingHiddenTokenTypes) {
203                 id = null;
204             }
205             
206         } else if (COMMENT_ELEM.equals(qname)) {
207             if (!processingHiddenTokenTypes) {
208                 inCommentElement = false;
209             }
210
211         } else if (SAMPLE_TEXT_ELEM.equals(qname)) {
212             if (!processingHiddenTokenTypes) {
213                 inSampleTextElement = false;
214             }
215         }
216     }
217     
218     /** Characters in element */
219     public void characters (char ch[], int start, int length) throws SAXException JavaDoc {
220         if (id != null) {
221             if (inCommentElement) {
222                 String JavaDoc comment = empty2nullFromSource(new String JavaDoc(ch, start, length));
223                 if (comment != null) {
224                     id.setComment(comment);
225                 }
226
227             } else if (inSampleTextElement) {
228                 if (length > 0) { // only non-empty SampleTexts are supported
229
id.addSampleText(empty2nullFromSource(new String JavaDoc(ch, start, length)));
230                 }
231             }
232         }
233     }
234
235     private static String JavaDoc empty2null(String JavaDoc s) {
236         if ("".equals(s)) {
237             s = null;
238         }
239
240         return s;
241     }
242     
243     private static String JavaDoc empty2nullFromSource(String JavaDoc s) {
244         s = empty2null(s);
245         if (s != null) {
246             s = LexerGenUtilities.fromSource(s);
247         }
248         return s;
249     }
250
251     private static boolean toBoolean(String JavaDoc s) {
252         return "true".equals(s);
253     }
254
255 }
256
257
Popular Tags