1 5 6 package com.hp.hpl.jena.rdf.arp; 7 8 import org.xml.sax.*; 9 import org.xml.sax.ext.*; 10 11 22 public class SAX2RDFImpl extends XMLHandler implements LexicalHandler, 23 ContentHandler, ErrorHandler { 24 private int depth; 25 private ARPRunnable rdf; 26 final private String lang; 27 SAX2RDFImpl(String base, String l) { 28 lang = l; 29 } 30 31 protected void initParse(String b) throws MalformedURIException { 32 super.initParse(b); 33 if (lang != null && !lang.equals("")) 34 documentContext = documentContext.withLang(lang); 35 } 36 41 public void endCDATA() throws SAXException { 42 if (depth > 0) 43 super.endCDATA(); 44 45 } 46 47 52 public void endDTD() throws SAXException { 53 if (depth > 0) 54 super.endDTD(); 55 56 } 57 58 63 public void startCDATA() throws SAXException { 64 if (depth > 0) 65 super.startCDATA(); 66 67 } 68 69 74 public void comment(char[] ch, int start, int length) throws SAXParseException { 75 if (depth > 0) 76 super.comment(ch, start, length); 77 78 } 79 80 85 public void endEntity(String name) throws SAXException { 86 if (depth > 0) 87 super.endEntity(name); 88 89 } 90 91 96 public void startEntity(String name) throws SAXException { 97 if (depth > 0) 98 super.startEntity(name); 99 100 } 101 102 108 public void startDTD(String name, String publicId, String systemId) 109 throws SAXException { 110 if (depth > 0) 111 super.startDTD(name, publicId, systemId); 112 113 } 114 115 120 public void endDocument() throws SAXException { 121 if (depth > 0) 122 super.endDocument(); 123 124 } 125 126 131 public void startDocument() throws SAXException { 132 if (depth > 0) 133 super.startDocument(); 134 135 } 136 137 142 public void characters(char[] ch, int start, int length) 143 throws SAXException { 144 if (depth > 0) 145 super.characters(ch, start, length); 146 147 } 148 149 154 public void ignorableWhitespace(char[] ch, int start, int length) 155 throws SAXException { 156 if (depth > 0) 157 super.ignorableWhitespace(ch, start, length); 158 159 } 160 161 166 public void endPrefixMapping(String prefix) { 167 if (depth > 0) 168 super.endPrefixMapping(prefix); 169 170 } 171 172 177 public void skippedEntity(String name) throws SAXException { 178 if (depth > 0) 179 super.skippedEntity(name); 180 181 } 182 183 184 185 191 public void processingInstruction(String target, String data) 192 throws SAXException { 193 if (depth > 0) 194 super.processingInstruction(target, data); 195 196 } 197 198 204 public void startPrefixMapping(String prefix, String uri) { 205 super.startPrefixMapping(prefix, uri); 206 207 } 208 209 215 public void endElement(String namespaceURI, String localName, String qName) 216 throws SAXException { 217 if (depth <= 0) { 218 fatalError(new SAXParseException("Unmatched end tag: " + qName, 220 getLocator())); 221 } 222 super.endElement(namespaceURI, localName, qName); 223 if (--depth == 0) { 224 close(); 225 } 226 227 } 228 229 void close() throws SAXException { 230 if (pipe() != null) { 231 pipe().close(); 232 if (!pipe().exactlyExhausted()) { 233 this.getHandlers().getErrorHandler().error(new SAXParseException("Too many XML events for RDF grammar; error condition not thought through, please report on jena-dev.",getLocator())); 237 } 238 } 239 endBnodeScope(); 240 } 241 242 248 public void startElement(String namespaceURI, String localName, 249 String qName, Attributes atts) throws SAXException { 250 if (depth++==0) { 251 rdf = new ARPRunnable() { 253 public void run() throws ParseException { 254 RDFParser p = new RDFParser(pipe, SAX2RDFImpl.this); 256 if (getOptions().getEmbedding()) 257 p.embeddedFile(documentContext); 258 else 259 p.rdfFile(documentContext); 260 261 } 262 }; 263 pipe = new PushMePullYouPipe( rdf); 264 ((PushMePullYouPipe)pipe).start(); 265 266 267 } 268 super.startElement(namespaceURI, localName, qName, atts); 269 270 } 271 272 277 public void error(SAXParseException exception) throws SAXParseException { 278 if (depth > 0) 279 super.error(exception); 280 281 } 282 283 287 public void fatalError(SAXParseException exception) throws SAXException { 288 if (depth > 0) 289 try { 290 super.fatalError(exception); 291 } 292 catch (FatalParsingErrorException e){ 293 pipe().close(); 294 throw exception; 295 } 296 } 297 298 private PushMePullYouPipe pipe() { 299 return ((PushMePullYouPipe)pipe); 300 } 301 302 307 public void warning(SAXParseException exception) throws SAXParseException { 308 if (depth > 0) 309 super.warning(exception); 310 311 } 312 } 313 314 339 340 | Popular Tags |