KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > util > DTDEntityResolver


1 /*
2  * Copyright (c) 2004 Your Corporation. All Rights Reserved.
3  */

4 package org.jfox.ioc.util;
5
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import org.xml.sax.EntityResolver JavaDoc;
11 import org.xml.sax.InputSource JavaDoc;
12 import org.xml.sax.SAXException JavaDoc;
13
14 /**
15  * @author <a HREF="mailto:yy.young@gmail.com">Young Yang</a>
16  */

17
18 public class DTDEntityResolver implements EntityResolver JavaDoc {
19     // public id -> local dtd name
20
private static Properties JavaDoc pubToLocal = new Properties JavaDoc();
21     private final static String JavaDoc DTD_PROPERTIES = "dtd.properties";
22     private final static ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
23     
24     static {
25         InputStream JavaDoc fin = null;
26         try {
27             fin = cl.getResourceAsStream(DTD_PROPERTIES); //new FileInputStream(DTD_PATH + "dtd.properties");
28
pubToLocal.load(fin);
29         }
30         catch(Exception JavaDoc e) {
31             e.printStackTrace();
32         }
33         finally {
34             if(fin != null) {
35                 try {
36                     fin.close();
37                 }
38                 catch(Exception JavaDoc e) {
39                     e.printStackTrace();
40                 }
41             }
42         }
43     }
44
45
46     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
47         if(publicId == null) return null;
48         // let it be
49
if(!pubToLocal.containsKey(publicId)) return null;
50
51         String JavaDoc dtd = pubToLocal.getProperty(publicId);
52         try {
53 // FileInputStream dtdStream = new FileInputStream(DTD_PATH + dtd);
54
InputSource JavaDoc inputSource = new InputSource JavaDoc(cl.getResourceAsStream(dtd));
55             return inputSource;
56         }
57         catch(Exception JavaDoc e) {
58             e.printStackTrace();
59             return null;
60         }
61     }
62
63     public static void main(String JavaDoc[] args) {
64
65     }
66 }
67
68
69
70
Popular Tags