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@5: import java.io.BufferedWriter; universe@5: import java.io.FileOutputStream; universe@5: import java.io.IOException; universe@5: import java.io.OutputStreamWriter; universe@5: universe@5: /** universe@5: * universe@5: * @author mike universe@5: */ universe@5: public class DocumentHandler { universe@5: universe@5: private String filename; universe@5: universe@5: public void load(Field field) throws IOException { universe@5: if (!isFilenameSet()) { universe@5: throw new IOException("no filename supplied"); universe@5: } universe@5: // TODO: implement universe@5: } universe@5: 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@5: } universe@5: universe@5: public void setFilename(String filename) { universe@5: this.filename = filename; universe@5: } universe@5: universe@5: public void clearFilename() { universe@5: filename = null; universe@5: } universe@5: universe@5: public boolean isFilenameSet() { universe@5: return filename != null; universe@5: } universe@5: }