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 | import org.apache.xml.serialize.OutputFormat; |
26 | import org.apache.xml.serialize.XMLSerializer; |
27 | |
28 | /** |
29 | * Puzzle saver class which writes out to a file. |
30 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
31 | */ |
32 | public class PuzzleFileWriter { |
33 | /** Log4J logger */ |
34 | private static final org.apache.log4j.Logger log = |
35 | org.apache.log4j.LogManager.getLogger(PuzzleFileWriter.class); |
36 | |
37 | /** Prevent creation of instances, as everything's static. */ |
38 | private PuzzleFileWriter() { |
39 | } |
40 | |
41 | /** |
42 | * Serialise a puzzle to the specified file. |
43 | */ |
44 | public static void write(String filename, Puzzle puzzle) |
45 | throws java.io.IOException { |
46 | PuzzleDOMBuilder builder = new PuzzleDOMBuilder(new CustomEntityResolver()); |
47 | org.w3c.dom.Document document = builder.buildDocument(puzzle); |
48 | if (document != null) { |
49 | // java.io.Writer out = new java.io.BufferedWriter(new java.io.FileWriter(selectedFile)); |
50 | java.io.OutputStream out = new java.io.BufferedOutputStream(new java.io.FileOutputStream(filename)); |
51 | OutputFormat format = new OutputFormat(document); |
52 | format.setLineWidth(80); |
53 | format.setIndenting(true); |
54 | format.setIndent(2); |
55 | format.setStandalone(true); |
56 | format.setMediaType("application/xml"); |
57 | XMLSerializer serializer = new XMLSerializer(out, format); |
58 | // serializer.setOutputByteStream(out); |
59 | serializer.serialize(document); |
60 | out.close(); |
61 | } else { |
62 | log.error("No document to save!"); |
63 | } |
64 | } |
65 | |
66 | } |