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 java.io.ByteArrayInputStream; 021import java.io.InputStream; 022 023import org.apache.logging.log4j.Logger; 024import org.apache.logging.log4j.status.StatusLogger; 025import org.apache.logging.log4j.util.Constants; 026import org.xml.sax.EntityResolver; 027import org.xml.sax.InputSource; 028 029/** 030 * An {@link EntityResolver} specifically designed to return 031 * <code>log4j.dtd</code> which is embedded within the log4j jar 032 * file. 033 */ 034public class Log4jEntityResolver implements EntityResolver { 035 private static final Logger LOGGER = StatusLogger.getLogger(); 036 private static final String PUBLIC_ID = "-//APACHE//DTD LOG4J 1.2//EN"; 037 038 @Override 039 public InputSource resolveEntity(String publicId, String systemId) { 040 if (systemId.endsWith("log4j.dtd") || PUBLIC_ID.equals(publicId)) { 041 Class<?> clazz = getClass(); 042 InputStream in = clazz.getResourceAsStream("/org/apache/log4j/xml/log4j.dtd"); 043 if (in == null) { 044 LOGGER.warn("Could not find [log4j.dtd] using [{}] class loader, parsed without DTD.", 045 clazz.getClassLoader()); 046 in = new ByteArrayInputStream(Constants.EMPTY_BYTE_ARRAY); 047 } 048 return new InputSource(in); 049 } 050 return null; 051 } 052}