1 19 20 package org.netbeans.modules.lexer.gen; 21 22 import java.lang.reflect.Field ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 import java.util.List ; 29 30 import javax.xml.parsers.SAXParser ; 31 import javax.xml.parsers.SAXParserFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 34 import org.xml.sax.Locator ; 35 import org.xml.sax.Attributes ; 36 import org.xml.sax.XMLReader ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.SAXParseException ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.helpers.DefaultHandler ; 41 42 import org.netbeans.api.lexer.TokenId; 43 import org.netbeans.modules.lexer.gen.util.LexerGenUtilities; 44 45 56 57 public class DescriptionReader extends DefaultHandler { 58 59 private static final String LANGUAGE_ELEM = "Language"; 61 private static final String TOKEN_ID_ELEM = "TokenId"; 62 private static final String HIDDEN_TOKEN_TYPE_ELEM = "HiddenTokenType"; 63 private static final String CATEGORY_ELEM = "Category"; 64 private static final String SAMPLE_TEXT_ELEM = "SampleText"; 65 private static final String COMMENT_ELEM = "Comment"; 66 67 private static final String NAME_ATTR = "name"; 68 private static final String TOKEN_TYPE_ATTR = "tokenType"; 69 private static final String SAMPLE_TEXT_CHECK_ATTR = "sampleTextCheck"; 70 private static final String CASE_INSENSITIVE_ATTR = "caseInsensitive"; 71 private static final String RESET_SAMPLES_ATTR = "resetSamples"; 72 73 private String systemId; 74 75 76 protected LanguageData languageData; 77 78 82 private boolean processingHiddenTokenTypes; 83 84 85 protected MutableTokenId id; 86 87 88 private boolean inCommentElement; 89 90 91 private boolean inSampleTextElement; 92 93 97 public DescriptionReader(String systemId) { 98 this.systemId = systemId; 99 } 100 101 110 public synchronized void applyTo(LanguageData languageData) throws SAXException , IOException { 111 112 this.languageData = languageData; 113 114 try { 115 SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 116 117 processingHiddenTokenTypes = true; 118 119 XMLReader reader = parser.getXMLReader(); 120 reader.setContentHandler(this); 121 reader.parse(new InputSource (systemId)); 123 processingHiddenTokenTypes = false; 124 125 reader.parse(new InputSource (systemId)); 127 } catch (ParserConfigurationException e) { 128 throw new SAXException (e); 129 } 130 131 this.languageData = null; 132 } 133 134 public void startElement(String uri, String localName, String qname, 135 Attributes attributes) throws SAXException { 136 if (LANGUAGE_ELEM.equals(qname)) { 137 139 } else if (TOKEN_ID_ELEM.equals(qname)) { 140 if (!processingHiddenTokenTypes) { 141 String name = empty2nullFromSource(attributes.getValue(NAME_ATTR)); 143 id = languageData.findId(name); 144 if (id == null) { 145 id = languageData.newId(name); 146 } 147 148 String tokenTypeName = empty2nullFromSource(attributes.getValue(TOKEN_TYPE_ATTR)); 150 if (tokenTypeName != null) { 151 id.updateByTokenType(tokenTypeName); 152 } 153 154 if (toBoolean(attributes.getValue(RESET_SAMPLES_ATTR))) { 156 id.resetSamples(); 157 } 158 159 id.setCaseInsensitive(toBoolean(attributes.getValue(CASE_INSENSITIVE_ATTR))); 161 162 String 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 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 ("Unknown element qname=" + qname); 195 } 196 197 } 198 199 200 public void endElement(String uri, String localName, String 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 219 public void characters (char ch[], int start, int length) throws SAXException { 220 if (id != null) { 221 if (inCommentElement) { 222 String comment = empty2nullFromSource(new String (ch, start, length)); 223 if (comment != null) { 224 id.setComment(comment); 225 } 226 227 } else if (inSampleTextElement) { 228 if (length > 0) { id.addSampleText(empty2nullFromSource(new String (ch, start, length))); 230 } 231 } 232 } 233 } 234 235 private static String empty2null(String s) { 236 if ("".equals(s)) { 237 s = null; 238 } 239 240 return s; 241 } 242 243 private static String empty2nullFromSource(String 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 s) { 252 return "true".equals(s); 253 } 254 255 } 256 257 | Popular Tags |