001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. 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 */ 017 018package org.apache.log4j.xml; 019 020import org.apache.log4j.helpers.LogLog; 021import org.xml.sax.EntityResolver; 022import org.xml.sax.InputSource; 023 024import java.io.InputStream; 025import java.io.ByteArrayInputStream; 026 027/** 028 * An {@link EntityResolver} specifically designed to return 029 * <code>log4j.dtd</code> which is embedded within the log4j jar 030 * file. 031 * 032 * @author Paul Austin 033 * */ 034public class Log4jEntityResolver implements EntityResolver { 035 private static final String PUBLIC_ID = "-//APACHE//DTD LOG4J 1.2//EN"; 036 037 public InputSource resolveEntity (String publicId, String systemId) { 038 if (systemId.endsWith("log4j.dtd") || PUBLIC_ID.equals(publicId)) { 039 Class clazz = getClass(); 040 InputStream in = clazz.getResourceAsStream("/org/apache/log4j/xml/log4j.dtd"); 041 if (in == null) { 042 LogLog.warn("Could not find [log4j.dtd] using [" + clazz.getClassLoader() 043 + "] class loader, parsed without DTD."); 044 in = new ByteArrayInputStream(new byte[0]); 045 } 046 return new InputSource(in); 047 } else { 048 return null; 049 } 050 } 051}