src/de/uapcore/sudoku/DocumentHandler.java

changeset 9
576e7a2861ae
parent 8
e70a0e3555fb
child 10
369903afbb29
     1.1 --- a/src/de/uapcore/sudoku/DocumentHandler.java	Fri Feb 01 10:18:47 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,110 +0,0 @@
     1.4 -/*
     1.5 - * Copyright 2013 Mike Becker. All rights reserved.
     1.6 - * 
     1.7 - * Redistribution and use in source and binary forms, with or without
     1.8 - * modification, are permitted provided that the following conditions are met:
     1.9 - * 
    1.10 - * 1. Redistributions of source code must retain the above copyright
    1.11 - *    notice, this list of conditions and the following disclaimer.
    1.12 - * 
    1.13 - * 2. Redistributions in binary form must reproduce the above copyright
    1.14 - *    notice, this list of conditions and the following disclaimer in the
    1.15 - *    documentation and/or other materials provided with the distribution.
    1.16 - * 
    1.17 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.18 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.19 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.20 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.21 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.22 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.23 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.24 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.25 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.26 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.27 - * POSSIBILITY OF SUCH DAMAGE.
    1.28 - */
    1.29 -
    1.30 -package de.uapcore.sudoku;
    1.31 -
    1.32 -import java.io.BufferedReader;
    1.33 -import java.io.BufferedWriter;
    1.34 -import java.io.FileInputStream;
    1.35 -import java.io.FileOutputStream;
    1.36 -import java.io.IOException;
    1.37 -import java.io.InputStreamReader;
    1.38 -import java.io.OutputStreamWriter;
    1.39 -import java.util.regex.Matcher;
    1.40 -import java.util.regex.Pattern;
    1.41 -
    1.42 -/**
    1.43 - *
    1.44 - * @author mike
    1.45 - */
    1.46 -public class DocumentHandler {
    1.47 -    
    1.48 -    private String filename;
    1.49 -    
    1.50 -    public void load(Field field) throws IOException {
    1.51 -        if (!isFilenameSet()) {
    1.52 -            throw new IOException("no filename supplied");
    1.53 -        }
    1.54 -        int row = 0;
    1.55 -        try (BufferedReader in = new BufferedReader(
    1.56 -                new InputStreamReader(new FileInputStream(filename)))) {
    1.57 -            Pattern pat = Pattern.compile("^\\s*(?:[1-9_] ){8}[1-9_]\\s*$");
    1.58 -            String line;
    1.59 -            while ((line = in.readLine()) != null) {
    1.60 -                if (line.matches("^\\s*$")) {
    1.61 -                    continue;
    1.62 -                }
    1.63 -                Matcher m = pat.matcher(line);
    1.64 -                if (m.matches()) {
    1.65 -                    String c[] = line.trim().split(" ");
    1.66 -                    if (c.length != 9) {
    1.67 -                        break;
    1.68 -                    }
    1.69 -                    for (int i = 0 ; i < 9 ; i++) {
    1.70 -                        field.setCellValue(i, row,
    1.71 -                                c[i].equals("_") ? 0 : Integer.valueOf(c[i]));
    1.72 -                    }
    1.73 -                    row++;
    1.74 -                } else {
    1.75 -                    break;
    1.76 -                }
    1.77 -            }
    1.78 -            if (row != 9) {
    1.79 -                throw new IOException("Kein Sudoku-Feld enthalten!");
    1.80 -            }
    1.81 -        }
    1.82 -        field.setAllCellsModified(false);
    1.83 -    }
    1.84 -    
    1.85 -    public void save(Field field) throws IOException {
    1.86 -        if (!isFilenameSet()) {
    1.87 -            throw new IOException("no filename supplied");
    1.88 -        }
    1.89 -        
    1.90 -        try (BufferedWriter out = new BufferedWriter(
    1.91 -                new OutputStreamWriter(new FileOutputStream(filename)))) {
    1.92 -            for (int i = 0 ; i < 9 ; i++) {
    1.93 -                int[] row = field.getRow(i);
    1.94 -                for (int j = 0 ; j < 9 ; j++) {
    1.95 -                    out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_');
    1.96 -                    out.append(j == 8 ? '\n': ' ');
    1.97 -                }
    1.98 -            }
    1.99 -        }
   1.100 -    }
   1.101 -    
   1.102 -    public void setFilename(String filename) {
   1.103 -        this.filename = filename;
   1.104 -    }
   1.105 -    
   1.106 -    public void clearFilename() {
   1.107 -        filename = null;
   1.108 -    }
   1.109 -    
   1.110 -    public boolean isFilenameSet() {
   1.111 -        return filename != null;
   1.112 -    }
   1.113 -}

mercurial