1 package com.icl.saxon; 2 import com.icl.saxon.tree.TreeBuilder; 3 import com.icl.saxon.om.NamePool; 4 import com.icl.saxon.om.DocumentInfo; 5 import com.icl.saxon.output.ContentHandlerProxy; 6 7 import javax.xml.parsers.SAXParserFactory ; 8 import javax.xml.transform.*; 9 import javax.xml.transform.sax.*; 10 import java.util.Properties ; 11 12 import org.xml.sax.*; 13 import org.xml.sax.helpers.*; 14 import org.xml.sax.ext.*; 15 16 import java.io.IOException ; 17 18 19 20 25 26 public class Filter implements XMLFilter { 27 28 Controller controller; 29 XMLReader parser; 30 ContentHandler contentHandler; LexicalHandler lexicalHandler; 33 34 35 40 41 protected Filter(Controller controller) { 42 this.controller = controller; 43 } 44 45 46 50 59 60 public void setParent (XMLReader parent) { 61 parser = parent; 62 } 63 64 74 75 public XMLReader getParent() { 76 return parser; 77 } 78 79 83 106 107 public boolean getFeature (String name) 108 throws SAXNotRecognizedException, SAXNotSupportedException { 109 if (name.equals("http://xml.org/sax/features/namespaces")) { 110 return true; 111 } else if (name.equals("http://xml.org/sax/features/namespace-prefixes")) { 112 return false; 113 } else { 114 throw new SAXNotRecognizedException(name); 115 } 116 } 117 118 119 146 147 public void setFeature (String name, boolean value) 148 throws SAXNotRecognizedException, SAXNotSupportedException { 149 if (name.equals("http://xml.org/sax/features/namespaces")) { 150 if (!value) { 151 throw new SAXNotSupportedException(name); 152 } 153 } else if (name.equals("http://xml.org/sax/features/namespace-prefixes")) { 154 if (value) { 155 throw new SAXNotSupportedException(name); 156 } 157 } else { 158 throw new SAXNotRecognizedException(name); 159 } 160 } 161 162 190 191 public Object getProperty (String name) 192 throws SAXNotRecognizedException, SAXNotSupportedException { 193 if (name.equals("http://xml.org/sax/properties/lexical-handler")) { 194 return lexicalHandler; 195 } else { 196 throw new SAXNotRecognizedException(name); 197 } 198 } 199 200 201 229 230 public void setProperty (String name, Object value) 231 throws SAXNotRecognizedException, SAXNotSupportedException { 232 if (name.equals("http://xml.org/sax/properties/lexical-handler")) { 233 if (value instanceof LexicalHandler) { 234 lexicalHandler = (LexicalHandler)value; 235 } else { 236 throw new SAXNotSupportedException( 237 "Lexical Handler must be instance of org.xml.sax.ext.LexicalHandler"); 238 } 239 } else { 240 throw new SAXNotRecognizedException(name); 241 } 242 } 243 244 249 250 public void setContentHandler(ContentHandler handler) { 251 contentHandler = handler; 252 if (handler instanceof LexicalHandler && lexicalHandler==null) { 253 lexicalHandler = (LexicalHandler)handler; 254 } 255 } 256 257 260 261 public ContentHandler getContentHandler() { 262 return contentHandler; 263 } 264 265 266 281 282 public void setEntityResolver (EntityResolver resolver) { 283 } 285 286 287 294 295 public EntityResolver getEntityResolver () { 296 return null; 297 } 298 299 300 315 316 public void setDTDHandler (DTDHandler handler) { 317 } 319 320 321 328 329 public DTDHandler getDTDHandler () { 330 return null; 331 } 332 333 334 335 353 354 public void setErrorHandler (ErrorHandler handler) { 355 } 357 358 365 public ErrorHandler getErrorHandler () { 366 return null; 367 } 368 369 386 387 public void parse (InputSource input) throws IOException , SAXException { 388 if (parser==null) { 389 try { 390 parser = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 391 } catch (Exception err) { 392 throw new SAXException(err); 393 } 394 } 395 SAXSource source = new SAXSource(); 396 source.setInputSource(input); 397 source.setXMLReader(parser); 398 ContentHandlerProxy result = new ContentHandlerProxy(); 399 result.setUnderlyingContentHandler(contentHandler); 400 if (lexicalHandler!=null) { 401 result.setLexicalHandler(lexicalHandler); 402 } 403 try { 404 controller.transform(source, result); 405 } catch (TransformerException err) { 406 Throwable cause = err.getException(); 407 if (cause != null && cause instanceof SAXException) { 408 throw (SAXException)cause; 409 } else if (cause != null && cause instanceof IOException ) { 410 throw (IOException )cause; 411 } else { 412 throw new SAXException(err); 413 } 414 } 415 416 417 } 418 419 441 442 public void parse (String systemId) throws IOException , SAXException { 443 InputSource input = new InputSource(systemId); 444 parse(input); 445 } 446 447 448 449 } 450 451 | Popular Tags |