universe@5: /* universe@5: * Copyright 2013 Mike Becker. All rights reserved. universe@5: * universe@5: * Redistribution and use in source and binary forms, with or without universe@5: * modification, are permitted provided that the following conditions are met: universe@5: * universe@5: * 1. Redistributions of source code must retain the above copyright universe@5: * notice, this list of conditions and the following disclaimer. universe@5: * universe@5: * 2. Redistributions in binary form must reproduce the above copyright universe@5: * notice, this list of conditions and the following disclaimer in the universe@5: * documentation and/or other materials provided with the distribution. universe@5: * universe@5: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@5: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@5: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@5: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@5: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@5: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@5: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@5: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@5: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@5: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@5: * POSSIBILITY OF SUCH DAMAGE. universe@5: */ universe@5: universe@5: package de.uapcore.sudoku; universe@5: universe@9: import java.io.*; universe@6: import java.util.regex.Matcher; universe@6: import java.util.regex.Pattern; universe@5: universe@5: /** universe@11: * Implements load and save routines. universe@5: */ universe@5: public class DocumentHandler { universe@5: universe@5: private String filename; universe@10: universe@10: /** universe@10: * Loads data into the specified field. universe@10: * universe@10: * @param field the field to populated with the loaded data universe@10: * @throws IOException if loading fails or no file name has been set before universe@10: * @see #setFilename(String) universe@10: */ universe@5: public void load(Field field) throws IOException { universe@5: if (!isFilenameSet()) { universe@5: throw new IOException("no filename supplied"); universe@5: } universe@6: int row = 0; universe@6: try (BufferedReader in = new BufferedReader( universe@6: new InputStreamReader(new FileInputStream(filename)))) { universe@6: Pattern pat = Pattern.compile("^\\s*(?:[1-9_] ){8}[1-9_]\\s*$"); universe@6: String line; universe@6: while ((line = in.readLine()) != null) { universe@6: if (line.matches("^\\s*$")) { universe@6: continue; universe@6: } universe@6: Matcher m = pat.matcher(line); universe@6: if (m.matches()) { universe@12: String[] c = line.trim().split(" "); universe@6: if (c.length != 9) { universe@6: break; universe@6: } universe@6: for (int i = 0 ; i < 9 ; i++) { universe@6: field.setCellValue(i, row, universe@12: c[i].equals("_") ? 0 : Integer.parseInt(c[i])); universe@6: } universe@6: row++; universe@6: } else { universe@6: break; universe@6: } universe@6: } universe@6: if (row != 9) { universe@6: throw new IOException("Kein Sudoku-Feld enthalten!"); universe@6: } universe@6: } universe@7: field.setAllCellsModified(false); universe@5: } universe@10: universe@10: /** universe@10: * Saves the specified field to a file. universe@22: * On success, the modified state of all cells is set to false. universe@10: * universe@10: * @param field the field to save universe@10: * @throws IOException if saving fails or the file name has not been set before universe@10: * @see #setFilename(String) universe@10: */ universe@5: public void save(Field field) throws IOException { universe@5: if (!isFilenameSet()) { universe@5: throw new IOException("no filename supplied"); universe@5: } universe@5: universe@5: try (BufferedWriter out = new BufferedWriter( universe@5: new OutputStreamWriter(new FileOutputStream(filename)))) { universe@5: for (int i = 0 ; i < 9 ; i++) { universe@5: int[] row = field.getRow(i); universe@5: for (int j = 0 ; j < 9 ; j++) { universe@5: out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_'); universe@5: out.append(j == 8 ? '\n': ' '); universe@5: } universe@5: } universe@5: } universe@22: field.setAllCellsModified(false); universe@5: } universe@10: universe@10: /** universe@10: * Sets the file name for loading and saving data. universe@10: * universe@10: * @param filename the file name universe@10: */ universe@5: public void setFilename(String filename) { universe@5: this.filename = filename; universe@5: } universe@10: universe@10: /** universe@10: * Clears the file name. universe@10: */ universe@5: public void clearFilename() { universe@5: filename = null; universe@5: } universe@10: universe@10: /** universe@10: * Checks whether a file name has been set. universe@10: * universe@10: * @return true if a file name is known, false otherwise universe@10: */ universe@5: public boolean isFilenameSet() { universe@5: return filename != null; universe@5: } universe@5: }