Projekt

Obecné

Profil

Stáhnout (5.23 KB) Statistiky
| Větev: | Revize:
1
package data;
2

    
3
import java.io.*;
4
import java.nio.charset.Charset;
5
import java.nio.file.Files;
6
import java.util.ArrayList;
7
import java.util.List;
8

    
9
/**
10
 *
11
 * Třída pro základní čtení a parsovani zdrojových souborů
12
 * - issue #7315
13
 * @author Daniel Stus, Marek Sobota, Lukas Scurko, Jan Jirman
14
 * @version 1.0
15
 */
16
public class DataLoader {
17

    
18
    //--------------------------------------- ATRIBUTY: -------------------------------------------------//
19
    //---------------------------------------------------------------------------------------------------//
20

    
21
    /** Jméno načteného souboru */
22
    private  String fileName;
23
    /** Hashmapa načtených dat */
24
    private ArrayList<String[]> loadedData = new ArrayList<>();
25
    /** Proměnná souboru */
26
    private File file;
27
    /** Proměnná root (JDF) složky */
28
    private File directory;
29

    
30
    //-------------------------------------- KONSTRUKTOR: -----------------------------------------------//
31
    //---------------------------------------------------------------------------------------------------//
32

    
33
    /**
34
     * Konstruktor DataLoaderu - Inicializuje slozku se zdrojovymi soubory pro cteni
35
     * @param directory
36
     */
37
    public DataLoader(File directory) {
38
        this.directory = directory;
39
        this.file = null;
40
        this.fileName = "";
41
    }
42

    
43
    //------------------------------------ METODY: Get() ------------------------------------------------//
44
    //---------------------------------------------------------------------------------------------------//
45

    
46
    /**
47
     * Vrací pole dat souboru
48
     * @return pole dat souboru
49
     */
50
    public ArrayList<String[]> getLoadedData() {
51
        return loadedData;
52
    }
53

    
54
    /**
55
     * Vraci hodnotu atributu fileName obsahujici jmeno souboru
56
     *
57
     * @return jméno souboru zpracovávaného metodami
58
     */
59
    public String getFileName() {
60
        return fileName;
61
    }
62

    
63
    /**
64
     * Vrací instanci souboru
65
     * @return intance souboru
66
     */
67
    public File getFile() {
68
        return file;
69
    }
70

    
71
    /**
72
     * Vrací instanci root (JDF) složky
73
     * @return intance root (JDF) složka
74
     */
75
    public File getDirectory(){
76
        return directory;
77
    }
78

    
79
    //----------------------------------------- METODY: -------------------------------------------------//
80
    //---------------------------------------------------------------------------------------------------//
81

    
82
    /**
83
     * Metoda načte všechna data ze souboru
84
     * @param fileName jméno souboru, ze kterého se bude číst
85
     */
86
    public ArrayList<String[]> loadData(String fileName){
87
        ArrayList<String[]> dataArrayList;
88
        String currLine;
89

    
90
        //soubor
91
        File file = new File(directory.getAbsolutePath()+"/"+fileName);
92
        if(!file.isFile()){
93
            System.out.println("Soubor neexistuje..");
94
            return null;
95
        }
96

    
97
        //nastavení atributů
98
        setAttributes(file, fileName);
99

    
100
        //čtení souboru
101
        try {
102
            BufferedReader BuffReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "Cp1250"));
103
            while((currLine = BuffReader.readLine()) != null) {
104
                //celý záznam - jedná řádka
105
                String[] record = parseLine(currLine);
106
                //přídání do arraylistu loadedData
107
                loadedData.add(record);
108
            }
109
            BuffReader.close();
110

    
111
        } catch (FileNotFoundException e) {
112
            e.printStackTrace();
113
        } catch (UnsupportedEncodingException e) {
114
            e.printStackTrace();
115
        } catch (IOException e) {
116
            e.printStackTrace();
117
        }
118
        return loadedData;
119
    }
120

    
121
    /**
122
     * Načte seznam linek z uživatelského soubor a vrátí jej jako pole řetězců
123
     *
124
     * @param lineFile cesta k uživatelskému souboru se seznamem linek
125
     */
126
    public List<String> loadLines(File lineFile) {
127

    
128
        List<String> lineList = null;
129
        Charset charset = Charset.defaultCharset();
130

    
131
        //soubor
132
        File file = lineFile;
133
        if(!file.isFile()){
134
            System.out.println("Soubor neexistuje..");
135
            return null;
136
        }
137

    
138
        //nastavení atributů
139
        setAttributes(file, fileName);
140

    
141

    
142
        try {
143
            lineList = Files.readAllLines(file.toPath(), charset);
144
        } catch (IOException e) {
145
            e.printStackTrace();
146
        }
147

    
148
      return lineList;
149
    }
150

    
151
    /**,
152
     * Nastavení zbývajících atributů pro DataLoader
153
     * @param file instance souboru
154
     * @param fileName název souborů
155
     */
156
    private void setAttributes(File file, String fileName){
157
        this.file = file;
158
        this.fileName = fileName;
159
    }
160

    
161
    /**
162
     * Zpracuje a vrátí jeden záznam (řádku) jako pole hodnot
163
     *
164
     * @param line řádka ke zpracování
165
     * @return pole se záznamem
166
     */
167
    public String[] parseLine(String line) {
168
        int startIndex = 1;
169
        int shift = 2;
170

    
171
        /*
172
         Odstraní ze záznamu úvodní uvozovku a konečnou uvozovku se středníkem
173
         příprava pro následující rozdělení záznamu pomocí metody split
174
         */
175
        String subString = line.substring(startIndex, line.length()-shift);
176

    
177
        String[] entry = subString.split("\",\"");
178

    
179
        return entry;
180
    }
181
}
(3-3/8)