1 19 20 package org.netbeans.modules.project.libraries; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.util.Stack ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import org.openide.xml.XMLUtil; 29 import org.xml.sax.Attributes ; 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.EntityResolver ; 32 import org.xml.sax.ErrorHandler ; 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.Locator ; 35 import org.xml.sax.SAXException ; 36 import org.xml.sax.SAXParseException ; 37 import org.xml.sax.XMLReader ; 38 import org.xml.sax.helpers.AttributesImpl ; 39 40 51 public class LibraryDeclarationParser implements ContentHandler , EntityResolver { 52 53 private StringBuffer buffer; 54 55 private LibraryDeclarationConvertor parslet; 56 57 private LibraryDeclarationHandler handler; 58 59 private Stack <Object []> context; 60 61 62 68 public LibraryDeclarationParser(final LibraryDeclarationHandler handler, final LibraryDeclarationConvertor parslet) { 69 this.parslet = parslet; 70 this.handler = handler; 71 buffer = new StringBuffer (111); 72 context = new Stack <Object []>(); 73 } 74 75 79 public final void setDocumentLocator(Locator locator) { 80 } 81 82 86 public final void startDocument() throws SAXException { 87 } 88 89 93 public final void endDocument() throws SAXException { 94 } 95 96 100 public final void startElement(String ns, String name, String qname, Attributes attrs) throws SAXException { 101 dispatch(true); 102 context.push(new Object [] {qname, new AttributesImpl (attrs)}); 103 if ("volume".equals(qname)) { 104 handler.start_volume(attrs); 105 } else if ("library".equals(qname)) { 106 handler.start_library(attrs); 107 } 108 } 109 110 114 public final void endElement(String ns, String name, String qname) throws SAXException { 115 dispatch(false); 116 context.pop(); 117 if ("volume".equals(qname)) { 118 handler.end_volume(); 119 } else if ("library".equals(qname)) { 120 handler.end_library(); 121 } 122 } 123 124 128 public final void characters(char[] chars, int start, int len) throws SAXException { 129 buffer.append(chars, start, len); 130 } 131 132 136 public final void ignorableWhitespace(char[] chars, int start, int len) throws SAXException { 137 } 138 139 143 public final void processingInstruction(String target, String data) throws SAXException { 144 } 145 146 150 public final void startPrefixMapping(final String prefix, final String uri) throws SAXException { 151 } 152 153 157 public final void endPrefixMapping(final String prefix) throws SAXException { 158 } 159 160 164 public final void skippedEntity(String name) throws SAXException { 165 } 166 167 private void dispatch(final boolean fireOnlyIfMixed) throws SAXException { 168 if (fireOnlyIfMixed && buffer.length() == 0) return; 170 Object [] ctx = context.peek(); 171 String here = (String ) ctx[0]; 172 Attributes attrs = (Attributes ) ctx[1]; 173 if ("description".equals(here)) { 174 if (fireOnlyIfMixed) throw new IllegalStateException ("Unexpected characters() event! (Missing DTD?)"); 175 handler.handle_description (buffer.length() == 0 ? null : buffer.toString(), attrs); 176 } else if ("type".equals(here)) { 177 if (fireOnlyIfMixed) throw new IllegalStateException ("Unexpected characters() event! (Missing DTD?)"); 178 handler.handle_type(buffer.length() == 0 ? null : buffer.toString(), attrs); 179 } else if ("resource".equals(here)) { 180 if (fireOnlyIfMixed) throw new IllegalStateException ("Unexpected characters() event! (Missing DTD?)"); 181 handler.handle_resource(parslet.parseResource(buffer.length() == 0 ? null : buffer.toString()), attrs); 182 } else if ("name".equals(here)) { 183 if (fireOnlyIfMixed) throw new IllegalStateException ("Unexpected characters() event! (Missing DTD?)"); 184 handler.handle_name(buffer.length() == 0 ? null : buffer.toString(), attrs); 185 } else if ("localizing-bundle".equals(here)) { 186 if (fireOnlyIfMixed) throw new IllegalStateException ("Unexpected characters() event! (Missing DTD?)"); 187 handler.handle_localizingBundle(buffer.length() == 0 ? null : buffer.toString(), attrs); 188 } else { 189 } 191 buffer.delete(0, buffer.length()); 192 } 193 194 203 public void parse(final InputSource input) throws SAXException , ParserConfigurationException , IOException { 204 parse(input, this); 205 } 206 207 216 public void parse(final URL url) throws SAXException , ParserConfigurationException , IOException { 217 parse(new InputSource (url.toExternalForm()), this); 218 } 219 220 229 public static void parse(final InputSource input, final LibraryDeclarationHandler handler, final LibraryDeclarationConvertor parslet) throws SAXException , ParserConfigurationException , IOException { 230 parse(input, new LibraryDeclarationParser(handler, parslet)); 231 } 232 233 242 public static void parse(final URL url, final LibraryDeclarationHandler handler, final LibraryDeclarationConvertor parslet) throws SAXException , ParserConfigurationException , IOException { 243 parse(new InputSource (url.toExternalForm()), handler, parslet); 244 } 245 246 private static void parse(final InputSource input, final LibraryDeclarationParser recognizer) throws SAXException , ParserConfigurationException , IOException { 247 try { 248 XMLReader parser = XMLUtil.createXMLReader(false, false); 249 parser.setContentHandler(recognizer); 250 parser.setErrorHandler(recognizer.getDefaultErrorHandler()); 251 parser.setEntityResolver(recognizer); 252 parser.parse(input); 253 } finally { 254 if (!recognizer.context.empty()) { 256 recognizer.context.clear(); 257 } 258 if (recognizer.buffer.length() > 0) { 259 recognizer.buffer.delete(0, recognizer.buffer.length()); 260 } 261 } 262 } 263 264 269 protected ErrorHandler getDefaultErrorHandler() { 270 return new ErrorHandler () { 271 public void error(SAXParseException ex) throws SAXException { 272 throw ex; 273 } 274 275 public void fatalError(SAXParseException ex) throws SAXException { 276 throw ex; 277 } 278 279 public void warning(SAXParseException ex) throws SAXException { 280 } 282 }; 283 284 } 285 286 288 public InputSource resolveEntity (String publicId, String systemId) 289 throws SAXException { 290 if ("-//NetBeans//DTD Library Declaration 1.0//EN".equals(publicId)) { 291 InputStream is = new ByteArrayInputStream (new byte[0]); 292 return new InputSource (is); 293 } 294 return null; } 296 } 297 298 | Popular Tags |