1 45 package org.openejb.alt.config; 46 47 import java.io.BufferedInputStream ; 48 import java.io.ByteArrayInputStream ; 49 import java.io.ByteArrayOutputStream ; 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.net.URL ; 53 import java.util.HashMap ; 54 55 import org.xml.sax.EntityResolver ; 56 import org.xml.sax.InputSource ; 57 import org.xml.sax.SAXException ; 58 59 68 public class DTDResolver implements EntityResolver { 69 public static HashMap dtds = new HashMap (); 70 71 static { 72 byte[] bytes = getDtd("ejb-jar_1_1.dtd"); 73 if(bytes != null) { 74 dtds.put("ejb-jar.dtd", bytes); 75 dtds.put("ejb-jar_1_1.dtd", bytes); 76 } 77 bytes = getDtd("ejb-jar_2_0.dtd"); 78 if(bytes != null) { 79 dtds.put("ejb-jar_2_0.dtd", bytes); 80 } 81 } 82 83 public static byte[] getDtd(String dtdName) { 84 try{ 85 URL dtd = new URL ("resource:/openejb/dtds/"+dtdName); 87 InputStream in = dtd.openStream(); 88 if (in == null) return null; 89 90 byte[] buf = new byte[512]; 91 92 in = new BufferedInputStream (in); 93 ByteArrayOutputStream out = new ByteArrayOutputStream (); 94 95 int count; 96 while((count = in.read(buf)) > -1) out.write(buf, 0, count); 97 98 in.close(); 99 out.close(); 100 101 return out.toByteArray(); 102 } catch (Throwable e){ 103 return null; 104 } 105 } 106 107 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , IOException { 108 109 int pos = systemId.lastIndexOf('/'); 110 if (pos != -1) { 111 systemId = systemId.substring(pos+1); 112 } 113 114 byte[] data = (byte[])dtds.get(systemId); 115 116 if (data != null) { 117 return new InputSource (new ByteArrayInputStream (data)); 118 } else { 119 return null; 120 } 121 } 122 } 123 | Popular Tags |