KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > source > ResourceSource


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

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 JavaDoc;
17 import org.xml.sax.InputSource JavaDoc;
18 import org.xml.sax.SAXException JavaDoc;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  * Description of a source which is described by the resource protocol
25  * which gets a resource from the classloader.
26  *
27  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
28  * @version CVS $Revision: 1.2 $ $Date: 2002/01/08 12:55:51 $
29  */

30
31 public final class ResourceSource
32 implements Composable, Source, XMLizable
33 {
34     /** The system identifier */
35     private String JavaDoc systemId;
36
37     /** Location of the resource */
38     private String JavaDoc location;
39
40     /** The ComponentManager needed for streaming */
41     private ComponentManager manager;
42
43     /**
44      * Constructor
45      */

46     public ResourceSource(String JavaDoc 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     /**
59      * Return an <code>InputStream</code> object to read from the source.
60      */

61     public InputStream JavaDoc getInputStream()
62     throws IOException JavaDoc
63     {
64         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
65         if (loader == null)
66         {
67             loader = this.getClass().getClassLoader();
68         }
69         return loader.getResourceAsStream( this.location );
70     }
71
72     /**
73      * Stream content to a content handler or to an XMLConsumer.
74      *
75      * @throws ResourceNotFoundException if file not found or
76      * HTTP location does not exist.
77      * @throws SAXException if failed to parse source document.
78      */

79     public void toSAX(ContentHandler JavaDoc handler)
80     throws SAXException JavaDoc
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 JavaDoc e)
89         {
90             // Preserve original exception
91
throw e;
92         } catch (Exception JavaDoc e)
93         {
94             throw new SAXException JavaDoc("Exception during processing of "
95                                           + this.systemId, e);
96         } finally
97         {
98             if (parser != null) this.manager.release(parser);
99         }
100     }
101
102     /**
103      * Return the unique identifer for this source
104      */

105     public String JavaDoc getSystemId()
106     {
107         return this.systemId;
108     }
109
110 }
111
Popular Tags