1 package org.apache.torque.engine.database.transform; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.io.IOException; 23 import java.io.InputStream; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.xml.sax.EntityResolver; 28 import org.xml.sax.InputSource; 29 import org.xml.sax.SAXException; 30 31 /** 32 * A resolver to get the database.dtd file for the XML parser from the jar. 33 * 34 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a> 35 * @author <a href="mailto:kschrader@karmalab.org">Kurt Schrader</a> 36 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> 37 * @version $Id: DTDResolver.java,v 1.1 2007-10-21 07:57:26 abyrne Exp $ 38 */ 39 public class DTDResolver implements EntityResolver 40 { 41 /** Where the DTD is located on the web. */ 42 public static final String WEB_SITE_DTD 43 = "http://db.apache.org/torque/dtd/database_3_3.dtd"; 44 45 /** Where the 3.2 DTD is located on the web. */ 46 public static final String WEB_SITE_DTD_3_2 47 = "http://db.apache.org/torque/dtd/database_3_2.dtd"; 48 49 /** Logging class from commons.logging */ 50 private static Log log = LogFactory.getLog(DTDResolver.class); 51 52 /** 53 * constructor 54 */ 55 public DTDResolver() 56 throws SAXException 57 { 58 } 59 60 /** 61 * An implementation of the SAX <code>EntityResolver</code> 62 * interface to be called by the XML parser. If the dtd is the 63 * current Torque DTD, the DTD is read from the generator jar. 64 * In all other cases, null is returned to indicate that the parser 65 * should open a regular connection to the systemId URI. 66 * 67 * @param publicId The public identifier of the external entity 68 * @param systemId The system identifier of the external entity 69 * @return An <code>InputSource</code> for the 70 * <code>database.dtd</code> file in the generator jar, or null. 71 */ 72 public InputSource resolveEntity(String publicId, String systemId) 73 throws IOException, SAXException 74 { 75 if (WEB_SITE_DTD.equals(systemId)) 76 { 77 return readFromClasspath("database.dtd"); 78 } 79 else if (WEB_SITE_DTD_3_2.equals(systemId)) 80 { 81 return readFromClasspath("database_3_2.dtd"); 82 } 83 else 84 { 85 log.debug("Resolver: used default behaviour"); 86 return null; 87 } 88 } 89 90 /** 91 * Reads the resource with the given name from the classpath. 92 * 93 * @param resourceName the name of the resource to read 94 * @return an Inputsource witht the content of the resource. 95 * 96 * @throws SAXException if the resource cannot be read. 97 */ 98 private InputSource readFromClasspath(String resourceName) 99 throws SAXException 100 { 101 try 102 { 103 InputStream dtdStream 104 = getClass().getResourceAsStream(resourceName); 105 106 // getResource was buggy on many systems including Linux, 107 // OSX, and some versions of windows in jdk1.3. 108 // getResourceAsStream works on linux, maybe others? 109 if (dtdStream != null) 110 { 111 String pkg = getClass().getName().substring(0, 112 getClass().getName().lastIndexOf('.')); 113 log.debug("Resolver: used " + resourceName + " from '" 114 + pkg + "' package"); 115 return new InputSource(dtdStream); 116 } 117 else 118 { 119 log.warn("Could not locate database.dtd"); 120 return null; 121 } 122 } 123 catch (Exception ex) 124 { 125 throw new SAXException( 126 "Could not get stream for " + resourceName, 127 ex); 128 } 129 } 130 }