| 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 javax.xml.parsers.*; |
| 24 | |
| 25 | import net.sourceforge.pseudoq.model.Coordinate; |
| 26 | import net.sourceforge.pseudoq.model.Puzzle; |
| 27 | |
| 28 | import org.xml.sax.EntityResolver; |
| 29 | |
| 30 | import org.w3c.dom.*; |
| 31 | |
| 32 | /** |
| 33 | * Example: |
| 34 | * <pre> |
| 35 | * PuzzleDOMBuilder builder = new PuzzleDOMBuilder(puzzle); |
| 36 | * org.w3c.dom.Document document = builder.buildDocument(); |
| 37 | * </pre> |
| 38 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
| 39 | * @see org.w3c.dom.Document |
| 40 | */ |
| 41 | public class PuzzleDOMBuilder { |
| 42 | /** Log4J logger */ |
| 43 | private static final org.apache.log4j.Logger log = |
| 44 | org.apache.log4j.LogManager.getLogger(PuzzleDOMBuilder.class); |
| 45 | |
| 46 | /** Entity resolver to use while building documents. */ |
| 47 | private EntityResolver resolver = null; |
| 48 | |
| 49 | /** Creates a new instance of PuzzleDOMBuilder */ |
| 50 | public PuzzleDOMBuilder(EntityResolver resolver) { |
| 51 | this.resolver = resolver; |
| 52 | } |
| 53 | |
| 54 | public Document buildDocument(Puzzle puzzle) { |
| 55 | Document document = null; |
| 56 | |
| 57 | try { |
| 58 | DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); |
| 59 | builderFactory.setValidating(true); |
| 60 | DocumentBuilder builder = builderFactory.newDocumentBuilder(); |
| 61 | builder.setEntityResolver(resolver); |
| 62 | DOMImplementation domimpl = builder.getDOMImplementation(); |
| 63 | DocumentType doctype = domimpl.createDocumentType("puzzle", |
| 64 | "-//PseudoQ//DTD Puzzle v1.0//EN", |
| 65 | "http://pseudoq.sourceforge.net/dtds/pseudoq-puzzle-1.0.dtd"); |
| 66 | document = domimpl.createDocument(null, "puzzle", doctype); |
| 67 | //document = builder.newDocument(); |
| 68 | Element root = document.getDocumentElement(); |
| 69 | // Element root = document.createElement("puzzle"); |
| 70 | // document.appendChild(root); |
| 71 | root.setAttribute("type", puzzle.getType().name()); |
| 72 | root.setAttribute("version", "1.0"); |
| 73 | Element givens = document.createElement("givens"); |
| 74 | root.appendChild(givens); |
| 75 | for (Coordinate coord : puzzle.getGivens()) { |
| 76 | Element coordinate = document.createElement("coordinate"); |
| 77 | coordinate.setAttribute("row", Integer.toString(coord.getRow())); |
| 78 | coordinate.setAttribute("column", Integer.toString(coord.getColumn())); |
| 79 | givens.appendChild(coordinate); |
| 80 | } |
| 81 | Element grid = document.createElement("grid"); |
| 82 | root.appendChild(grid); |
| 83 | for (Coordinate coord : puzzle.getGrid().keySet()) { |
| 84 | Element cell = document.createElement("cell"); |
| 85 | Element coordinate = document.createElement("coordinate"); |
| 86 | coordinate.setAttribute("row", Integer.toString(coord.getRow())); |
| 87 | coordinate.setAttribute("column", Integer.toString(coord.getColumn())); |
| 88 | cell.appendChild(coordinate); |
| 89 | Element value = document.createElement("value"); |
| 90 | value.setTextContent(puzzle.getGrid().get(coord).toString()); |
| 91 | cell.appendChild(value); |
| 92 | grid.appendChild(cell); |
| 93 | } |
| 94 | if (puzzle.isSolved()) { |
| 95 | Element solution = document.createElement("solution"); |
| 96 | root.appendChild(solution); |
| 97 | Element grid2 = document.createElement("grid"); |
| 98 | solution.appendChild(grid2); |
| 99 | for (Coordinate coord : puzzle.getSolution().getGrid().keySet()) { |
| 100 | Element cell = document.createElement("cell"); |
| 101 | Element coordinate = document.createElement("coordinate"); |
| 102 | coordinate.setAttribute("row", Integer.toString(coord.getRow())); |
| 103 | coordinate.setAttribute("column", Integer.toString(coord.getColumn())); |
| 104 | cell.appendChild(coordinate); |
| 105 | Element value = document.createElement("value"); |
| 106 | value.setTextContent(puzzle.getSolution().getGrid().get(coord).toString()); |
| 107 | cell.appendChild(value); |
| 108 | grid2.appendChild(cell); |
| 109 | } |
| 110 | // TODO: solution steps and explanation (not yet implemented in model) |
| 111 | } |
| 112 | } catch (javax.xml.parsers.ParserConfigurationException e) { |
| 113 | log.error("Parser configuration problem: " + e.getLocalizedMessage(), e); |
| 114 | } |
| 115 | |
| 116 | return document; |
| 117 | } |
| 118 | |
| 119 | } |