001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 the ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.xml; 018 019import java.io.ByteArrayInputStream; 020import java.io.IOException; 021 022import org.xml.sax.InputSource; 023import org.xml.sax.SAXException; 024import org.xml.sax.helpers.DefaultHandler; 025 026/** 027 * Base class for SAX parser handlers. This class has a default implementation 028 * of {@link #resolveEntity(String, String)} that works around a bug in the Java 029 * SAX parser implementation: 030 * 031 * If {@link #resolveEntity(String, String)} returns <code>null</code> for a 032 * given DTD (the default behavior of {@link DefaultHandler}), the Java SAX 033 * parser will try to download that DTD. This will fail on machines that have no 034 * Internet connection or require a proxy to access the Internet or for DTDs 035 * where the URI is not a valid URL. This class' default implementation instead 036 * returns an empty {@link InputSource}, which effectively causes the parser to 037 * skip DTD validation. 038 * 039 * Please note that downloading the DTD from the Internet is against the DTD 040 * specification (as the DTD URI is not required to be a valid URL) and is 041 * highly error-prone, resulting in a hard to interpret error message. It is 042 * thus recommended you use this base class and never return <code>null</code> 043 * from {@link #resolveEntity(String, String)}. 044 */ 045public class OfflineSaxHandlerBase extends DefaultHandler { 046 047 /** 048 * {@inheritDoc} 049 * 050 * Default implementation that can be overwritten by subclasses. This 051 * implementation returns an empty document for any entity that should be 052 * resolved. This effectively causes the parser to skip DTD validation. 053 * 054 * @throws IOException 055 * may be thrown by subclasses. 056 * @throws SAXException 057 * may be thrown by subclasses. 058 */ 059 @Override 060 public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException { 061 return new InputSource(new ByteArrayInputStream(new byte[0])); 062 } 063 064}