KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > util > EntityResolverImpl


1 package org.sapia.soto.util;
2
3 import org.sapia.soto.Env;
4
5 import org.xml.sax.EntityResolver JavaDoc;
6 import org.xml.sax.InputSource JavaDoc;
7 import org.xml.sax.SAXException JavaDoc;
8
9 import java.io.File JavaDoc;
10 import java.io.FileNotFoundException JavaDoc;
11 import java.io.IOException JavaDoc;
12
13
14 /**
15  * @author Yanick Duchesne
16  *
17  * <dl>
18  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class EntityResolverImpl implements EntityResolver JavaDoc {
24   private Env _env;
25   private EntityResolver JavaDoc _parent;
26
27   public EntityResolverImpl(Env env) {
28     _env = env;
29   }
30
31   public EntityResolverImpl(Env env, EntityResolver JavaDoc parent) {
32     _env = env;
33     _parent = parent;
34   }
35
36   /**
37    * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
38    */

39   public InputSource JavaDoc resolveEntity(String JavaDoc href, String JavaDoc base)
40     throws SAXException JavaDoc, IOException JavaDoc {
41     try {
42       if (Utils.hasScheme(href)) {
43         return new InputSource JavaDoc(_env.getResourceHandlerFor(href).getResource(href));
44       } else {
45         String JavaDoc uri;
46
47         if (href == null) {
48           uri = base;
49         } else if (base == null) {
50           uri = href;
51         } else {
52           uri = base + File.separator + href;
53         }
54
55         return new InputSource JavaDoc(_env.getResourceHandlerFor(uri).getResource(uri));
56       }
57     } catch (FileNotFoundException JavaDoc e) {
58       if (_parent == null) {
59         throw new SAXException JavaDoc("Could not resolve URI: " + href, e);
60       }
61
62       return _parent.resolveEntity(href, base);
63     } catch (IOException JavaDoc e) {
64       throw new SAXException JavaDoc("Could not resolve URI: " + href, e);
65     }
66   }
67 }
68
Popular Tags