1 | /* |
2 | * Copyright (c) 2005 The PseudoQ Project. |
3 | * |
4 | * This file is part of PseudoQ. |
5 | * |
6 | * PseudoQ is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU Lesser General Public License as published by |
8 | * the Free Software Foundation; either version 2.1 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * PseudoQ is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public License |
17 | * along with PseudoQ; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19 | */ |
20 | |
21 | package net.sourceforge.pseudoq.model.io; |
22 | |
23 | import java.io.InputStream; |
24 | |
25 | import org.xml.sax.InputSource; |
26 | |
27 | /** |
28 | * Custom entity resolver to allow validating puzzle files against a local copy |
29 | * of the DTD. |
30 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
31 | */ |
32 | public class CustomEntityResolver implements org.xml.sax.EntityResolver { |
33 | /** Log4J logger */ |
34 | private static final org.apache.log4j.Logger log = |
35 | org.apache.log4j.LogManager.getLogger(CustomEntityResolver.class); |
36 | |
37 | /** Puzzle DTD's public ID. */ |
38 | public static final String PUZZLE_PUBLICID = "-//PseudoQ//DTD Puzzle v1.0//EN"; |
39 | /** Puzzle DTD's system ID. */ |
40 | public static final String PUZZLE_SYSTEMID = |
41 | "http://pseudoq.sourceforge.net/dtds/pseudoq-puzzle-1.0.dtd"; |
42 | /** Path to local DTD. */ |
43 | public static final String PUZZLE_DTD = |
44 | "net/sourceforge/pseudoq/model/io/pseudoq-puzzle-1.0.dtd"; |
45 | |
46 | /** Creates a new instance of CustomEntityResolver */ |
47 | public CustomEntityResolver() { |
48 | } |
49 | |
50 | public InputSource resolveEntity(String publicId, String systemId) |
51 | throws org.xml.sax.SAXException, java.io.IOException { |
52 | InputSource ret = null; |
53 | |
54 | log.debug("Resolve PUBLIC '" + publicId + "' SYSTEM '" + systemId + "'"); |
55 | if (PUZZLE_PUBLICID.equals(publicId) || PUZZLE_SYSTEMID.equals(systemId)) { |
56 | InputStream in = getClass().getClassLoader().getResourceAsStream(PUZZLE_DTD); |
57 | ret = new InputSource(in); |
58 | log.debug("using local copy of DTD"); |
59 | } |
60 | |
61 | return ret; |
62 | } |
63 | |
64 | } |