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