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 net.sourceforge.pseudoq.model.Puzzle; |
24 | |
25 | /** |
26 | * Puzzle loader class which reads from a file. |
27 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
28 | */ |
29 | public class PuzzleFileReader { |
30 | /** Log4J logger */ |
31 | private static final org.apache.log4j.Logger log = |
32 | org.apache.log4j.LogManager.getLogger(PuzzleFileReader.class); |
33 | |
34 | /** Prevent creation of instances, as everything's static. */ |
35 | private PuzzleFileReader() { |
36 | } |
37 | |
38 | /** |
39 | * Deserialise the puzzle from the specified file. |
40 | */ |
41 | public static Puzzle read(String filename) |
42 | throws java.io.IOException, javax.xml.parsers.ParserConfigurationException { |
43 | try { |
44 | java.io.InputStream in = new java.io.BufferedInputStream(new java.io.FileInputStream(filename)); |
45 | javax.xml.parsers.DocumentBuilderFactory builderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); |
46 | // builderFactory.setValidating(false); |
47 | javax.xml.parsers.DocumentBuilder builder = builderFactory.newDocumentBuilder(); |
48 | builder.setEntityResolver(new CustomEntityResolver()); |
49 | org.w3c.dom.Document document = builder.parse(new org.xml.sax.InputSource(in)); |
50 | PuzzleScanner scanner = new PuzzleScanner(document); |
51 | final Puzzle puzzle = scanner.visitDocument(); |
52 | puzzle.setFilename(filename); |
53 | return puzzle; |
54 | } catch (javax.xml.parsers.ParserConfigurationException e) { |
55 | log.error("XML parser problem: " + e.getLocalizedMessage(), e); |
56 | throw e; |
57 | } catch (java.io.IOException e) { |
58 | log.error("IO Problem: " + e.getLocalizedMessage(), e); |
59 | throw e; |
60 | } catch (org.xml.sax.SAXException e) { |
61 | log.error("XML parser problem: " + e.getLocalizedMessage(), e); |
62 | throw new java.io.IOException("Invalid puzzle file"); |
63 | } |
64 | } |
65 | |
66 | } |