001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.html;
015
016import java.io.InputStream;
017import java.util.HashMap;
018import java.util.Map;
019
020import org.xml.sax.EntityResolver;
021import org.xml.sax.InputSource;
022
023public class XHTMLEntityResolver implements EntityResolver {
024 
025    // key: public id, value: relative path to DTD file
026    static Map<String, String> entityMap = new HashMap<String, String>();
027
028    static {
029        entityMap.put("-//W3C//DTD XHTML 1.0 Strict//EN", "/dtd/xhtml1-strict.dtd");
030        entityMap.put("-//W3C//ENTITIES Latin 1 for XHTML//EN", "/dtd/xhtml-lat1.ent");
031        entityMap.put("-//W3C//ENTITIES Symbols for XHTML//EN", "/dtd/xhtml-symbol.ent");
032        entityMap.put("-//W3C//ENTITIES Special for XHTML//EN", "/dtd/xhtml-special.ent");
033    }
034
035    public InputSource resolveEntity(String publicId, String systemId) {
036        // System.out.println(publicId);
037        final String relativePath = (String) entityMap.get(publicId);
038
039        if (relativePath != null) {
040            Class<?> clazz = getClass();
041            InputStream in = clazz.getResourceAsStream(relativePath);
042            if (in == null) {
043                return null;
044            } else {
045                return new InputSource(in);
046            }
047        } else {
048            return null;
049        }
050    }
051}