1 8 package org.apache.avalon.excalibur.source; 9 10 import org.apache.avalon.framework.component.Composable; 11 import org.apache.avalon.framework.component.ComponentManager; 12 import org.apache.avalon.excalibur.xml.Parser; 13 import org.apache.avalon.excalibur.xml.XMLConsumer; 14 import org.apache.avalon.excalibur.xml.XMLizable; 15 16 import org.xml.sax.ContentHandler ; 17 import org.xml.sax.InputSource ; 18 import org.xml.sax.SAXException ; 19 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 23 30 31 public final class ResourceSource 32 implements Composable, Source, XMLizable 33 { 34 35 private String systemId; 36 37 38 private String location; 39 40 41 private ComponentManager manager; 42 43 46 public ResourceSource(String systemId) 47 { 48 this.systemId = systemId; 49 final int pos = systemId.indexOf("://"); 50 this.location = systemId.substring(pos+3); 51 } 52 53 public void compose(ComponentManager manager ) 54 { 55 this.manager = manager; 56 } 57 58 61 public InputStream getInputStream() 62 throws IOException 63 { 64 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 65 if (loader == null) 66 { 67 loader = this.getClass().getClassLoader(); 68 } 69 return loader.getResourceAsStream( this.location ); 70 } 71 72 79 public void toSAX(ContentHandler handler) 80 throws SAXException 81 { 82 Parser parser = null; 83 try { 84 parser = (Parser)this.manager.lookup(Parser.ROLE); 85 86 parser.parse( SourceUtil.getInputSource(this), handler); 87 } 88 catch (SAXException e) 89 { 90 throw e; 92 } catch (Exception e) 93 { 94 throw new SAXException ("Exception during processing of " 95 + this.systemId, e); 96 } finally 97 { 98 if (parser != null) this.manager.release(parser); 99 } 100 } 101 102 105 public String getSystemId() 106 { 107 return this.systemId; 108 } 109 110 } 111 | Popular Tags |