1 package net.sf.saxon; 2 import net.sf.saxon.event.ContentHandlerProxy; 3 import org.xml.sax.*; 4 import org.xml.sax.ext.LexicalHandler ; 5 6 import javax.xml.parsers.SAXParserFactory ; 7 import javax.xml.transform.Transformer ; 8 import javax.xml.transform.TransformerException ; 9 import javax.xml.transform.sax.SAXSource ; 10 import java.io.IOException ; 11 12 13 14 19 20 public class Filter implements XMLFilter { 21 22 private Controller controller; 23 private XMLReader parser; 24 private ContentHandler contentHandler; private LexicalHandler lexicalHandler; 27 28 29 34 35 protected Filter(Controller controller) { 36 this.controller = controller; 37 } 38 39 40 44 53 54 public void setParent (XMLReader parent) { 55 parser = parent; 56 } 57 58 68 69 public XMLReader getParent() { 70 return parser; 71 } 72 73 77 100 101 public boolean getFeature (String name) 102 throws SAXNotRecognizedException, SAXNotSupportedException { 103 if (name.equals("http://xml.org/sax/features/namespaces")) { 104 return true; 105 } else if (name.equals("http://xml.org/sax/features/namespace-prefixes")) { 106 return false; 107 } else { 108 throw new SAXNotRecognizedException(name); 109 } 110 } 111 112 113 137 138 public void setFeature (String name, boolean value) 139 throws SAXNotRecognizedException, SAXNotSupportedException { 140 if (name.equals("http://xml.org/sax/features/namespaces")) { 141 if (!value) { 142 throw new SAXNotSupportedException(name); 143 } 144 } else if (name.equals("http://xml.org/sax/features/namespace-prefixes")) { 145 if (value) { 146 throw new SAXNotSupportedException(name); 147 } 148 } else { 149 throw new SAXNotRecognizedException(name); 150 } 151 } 152 153 179 180 public Object getProperty (String name) 181 throws SAXNotRecognizedException, SAXNotSupportedException { 182 if (name.equals("http://xml.org/sax/properties/lexical-handler")) { 183 return lexicalHandler; 184 } else { 185 throw new SAXNotRecognizedException(name); 186 } 187 } 188 189 190 216 217 public void setProperty (String name, Object value) 218 throws SAXNotRecognizedException, SAXNotSupportedException { 219 if (name.equals("http://xml.org/sax/properties/lexical-handler")) { 220 if (value instanceof LexicalHandler ) { 221 lexicalHandler = (LexicalHandler )value; 222 } else { 223 throw new SAXNotSupportedException( 224 "Lexical Handler must be instance of org.xml.sax.ext.LexicalHandler"); 225 } 226 } else { 227 throw new SAXNotRecognizedException(name); 228 } 229 } 230 231 236 237 public void setContentHandler(ContentHandler handler) { 238 contentHandler = handler; 239 if (handler instanceof LexicalHandler && lexicalHandler==null) { 240 lexicalHandler = (LexicalHandler )handler; 241 } 242 } 243 244 247 248 public ContentHandler getContentHandler() { 249 return contentHandler; 250 } 251 252 253 268 269 public void setEntityResolver (EntityResolver resolver) { 270 } 272 273 274 281 282 public EntityResolver getEntityResolver () { 283 return null; 284 } 285 286 287 302 303 public void setDTDHandler (DTDHandler handler) { 304 } 306 307 308 315 316 public DTDHandler getDTDHandler () { 317 return null; 318 } 319 320 321 322 340 341 public void setErrorHandler (ErrorHandler handler) { 342 } 344 345 352 public ErrorHandler getErrorHandler () { 353 return null; 354 } 355 356 373 374 public void parse (InputSource input) throws IOException , SAXException { 375 if (parser==null) { 376 try { 377 parser = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 378 } catch (Exception err) { 379 throw new SAXException(err); 380 } 381 } 382 SAXSource source = new SAXSource (); 383 source.setInputSource(input); 384 source.setXMLReader(parser); 385 ContentHandlerProxy result = new ContentHandlerProxy(); 386 result.setPipelineConfiguration(controller.makePipelineConfiguration()); 387 result.setUnderlyingContentHandler(contentHandler); 388 if (lexicalHandler!=null) { 389 result.setLexicalHandler(lexicalHandler); 390 } 391 try { 392 controller.transform(source, result); 393 } catch (TransformerException err) { 394 Throwable cause = err.getException(); 395 if (cause != null && cause instanceof SAXException) { 396 throw (SAXException)cause; 397 } else if (cause != null && cause instanceof IOException ) { 398 throw (IOException )cause; 399 } else { 400 throw new SAXException(err); 401 } 402 } 403 404 405 } 406 407 429 430 public void parse (String systemId) throws IOException , SAXException { 431 InputSource input = new InputSource(systemId); 432 parse(input); 433 } 434 435 436 441 442 public Transformer getTransformer() { 443 return controller; 444 } 445 446 447 } 448 449 | Popular Tags |