KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > ejtools > deploy > xml > DTDResolver


1 package net.sourceforge.ejtools.deploy.xml;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.ByteArrayInputStream JavaDoc;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.util.Hashtable JavaDoc;
9
10 import org.xml.sax.EntityResolver JavaDoc;
11 import org.xml.sax.InputSource JavaDoc;
12
13 /**
14  * Description of the Class
15  *
16  * @author letiembl
17  * @created 13 décembre 2001
18  */

19 public class DTDResolver implements EntityResolver JavaDoc
20 {
21     protected static Hashtable JavaDoc dtds = new Hashtable JavaDoc();
22
23     public DTDResolver() {
24     }
25
26     public InputSource JavaDoc resolveEntity (String JavaDoc publicId, String JavaDoc systemId)
27     {
28         byte[] data = (byte[])dtds.get(publicId);
29         if(data != null) {
30             return new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(data));
31         } else {
32         }
33         return null;
34     }
35
36     static {
37         dtds.put("-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN", loadDTD("ejb-jar_1_1.dtd"));
38         dtds.put("-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN", loadDTD("ejb-jar_2_0.dtd"));
39         dtds.put("-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN", loadDTD("application_1_2.dtd"));
40         dtds.put("-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN", loadDTD("application_1_3.dtd"));
41         dtds.put("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN", loadDTD("web-app_2_3.dtd"));
42     }
43
44     private static byte[] loadDTD(String JavaDoc name) {
45         InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
46         if(in == null) {
47             in = DTDResolver.class.getClassLoader().getResourceAsStream(name);
48         }
49         if(in != null) {
50             return loadBytes(in);
51         }
52         return null;
53     }
54
55     private static byte[] loadBytes(InputStream JavaDoc source) {
56         byte[] buf = new byte[1024];
57         try {
58             BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(source);
59             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
60             int count;
61             while((count = in.read(buf)) > -1)
62                 out.write(buf, 0, count);
63             in.close();
64             out.close();
65             return out.toByteArray();
66         } catch(IOException JavaDoc e) {
67             return null;
68         }
69     }
70 }
71
Popular Tags