1 package net.sourceforge.ejtools.deploy.xml; 2 3 import java.io.BufferedInputStream ; 4 import java.io.ByteArrayInputStream ; 5 import java.io.ByteArrayOutputStream ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.util.Hashtable ; 9 10 import org.xml.sax.EntityResolver ; 11 import org.xml.sax.InputSource ; 12 13 19 public class DTDResolver implements EntityResolver 20 { 21 protected static Hashtable dtds = new Hashtable (); 22 23 public DTDResolver() { 24 } 25 26 public InputSource resolveEntity (String publicId, String systemId) 27 { 28 byte[] data = (byte[])dtds.get(publicId); 29 if(data != null) { 30 return new InputSource (new ByteArrayInputStream (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 name) { 45 InputStream 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 source) { 56 byte[] buf = new byte[1024]; 57 try { 58 BufferedInputStream in = new BufferedInputStream (source); 59 ByteArrayOutputStream out = new ByteArrayOutputStream (); 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 e) { 67 return null; 68 } 69 } 70 } 71 | Popular Tags |