View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.classic.blackbox.html;
16  
17  import java.io.InputStream;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.xml.sax.EntityResolver;
22  import org.xml.sax.InputSource;
23  
24  public class XHTMLEntityResolver implements EntityResolver {
25  
26      // key: public id, value: relative path to DTD file
27      static Map<String, String> entityMap = new HashMap<String, String>();
28  
29      static {
30          entityMap.put("-//W3C//DTD XHTML 1.0 Strict//EN", "/dtd/xhtml1-strict.dtd");
31          entityMap.put("-//W3C//ENTITIES Latin 1 for XHTML//EN", "/dtd/xhtml-lat1.ent");
32          entityMap.put("-//W3C//ENTITIES Symbols for XHTML//EN", "/dtd/xhtml-symbol.ent");
33          entityMap.put("-//W3C//ENTITIES Special for XHTML//EN", "/dtd/xhtml-special.ent");
34      }
35  
36      public InputSource resolveEntity(String publicId, String systemId) {
37          // System.out.println(publicId);
38          final String relativePath = (String) entityMap.get(publicId);
39  
40          if (relativePath != null) {
41              Class<?> clazz = getClass();
42              InputStream in = clazz.getResourceAsStream(relativePath);
43              if (in == null) {
44                  return null;
45              } else {
46                  return new InputSource(in);
47              }
48          } else {
49              return null;
50          }
51      }
52  }