1 package org.apache.torque.engine.database.transform; 2 3 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.URL ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.xml.sax.EntityResolver ; 26 import org.xml.sax.InputSource ; 27 import org.xml.sax.SAXException ; 28 29 37 public class DTDResolver implements EntityResolver 38 { 39 40 public static final String WEB_SITE_DTD 41 = "http://db.apache.org/torque/dtd/database_3_2.dtd"; 42 43 44 private InputSource databaseDTD = null; 45 46 47 private static Log log = LogFactory.getLog(DTDResolver.class); 48 49 52 public DTDResolver() 53 throws SAXException 54 { 55 try 56 { 57 InputStream dtdStream 58 = getClass().getResourceAsStream("database.dtd"); 59 60 if (dtdStream != null) 64 { 65 databaseDTD = new InputSource (dtdStream); 66 } 67 else 68 { 69 log.warn("Could not locate database.dtd"); 70 } 71 } 72 catch (Exception ex) 73 { 74 throw new SAXException ("Could not get stream for database.dtd", ex); 75 } 76 } 77 78 87 public InputSource resolveEntity(String publicId, String systemId) 88 throws IOException 89 { 90 if (databaseDTD != null && WEB_SITE_DTD.equals(systemId)) 91 { 92 String pkg = getClass().getName().substring(0, 93 getClass().getName().lastIndexOf('.')); 94 log.info("Resolver: used database.dtd from '" 95 + pkg + "' package"); 96 return databaseDTD; 97 } 98 else if (systemId == null || "".equals(systemId.trim())) 99 { 100 log.info("Resolver: used '" + WEB_SITE_DTD + '\''); 101 return getInputSource(WEB_SITE_DTD); 102 } 103 else 104 { 105 log.info("Resolver: used '" + systemId + '\''); 106 return getInputSource(systemId); 107 } 108 } 109 110 116 private InputSource getInputSource(String urlString) 117 throws IOException 118 { 119 URL url = new URL (urlString); 120 return new InputSource (url.openStream()); 121 } 122 } 123 | Popular Tags |