1
2
3
4
5
6
7
8
9
10 package eu.fbk.dkm.pikes.raid.mdfsa;
11
12 import com.hp.hpl.jena.rdf.model.Model;
13 import com.hp.hpl.jena.rdf.model.ModelFactory;
14 import org.w3c.dom.Document;
15
16 import javax.xml.parsers.DocumentBuilder;
17 import javax.xml.parsers.DocumentBuilderFactory;
18 import java.io.*;
19 import java.util.ArrayList;
20
21
22 public class FileManager {
23
24 private String fileName;
25 private File currentFile;
26 private String absolutePath;
27 private PrintWriter outputStream;
28 private BufferedReader inputStream;
29 private ArrayList rowsList;
30
31
32
33
34 public enum Mode {
35 READ, WRITE, APPEND
36 };
37
38 private Mode mode;
39
40
41
42
43
44
45 @SuppressWarnings("static-access")
46 public FileManager(String fileAbsolutePath, Mode m) {
47
48 this.absolutePath = fileAbsolutePath;
49 this.currentFile = new File(fileAbsolutePath);
50 this.fileName = this.currentFile.getName();
51 this.mode = m;
52
53 try {
54 if (this.mode == Mode.READ) {
55 InputStream iS = ClassLoader.getSystemClassLoader().getSystemResourceAsStream(fileAbsolutePath);
56 if(iS == null) {
57 this.inputStream = new BufferedReader(new FileReader(fileAbsolutePath));
58 } else {
59 Reader reader = new InputStreamReader(iS);
60 this.inputStream = new BufferedReader(reader);
61 }
62 } else if (this.mode == Mode.WRITE) {
63 this.outputStream = new PrintWriter(new BufferedWriter(new FileWriter(fileAbsolutePath)));
64 } else if (this.mode == Mode.APPEND) {
65 this.outputStream = new PrintWriter(new BufferedWriter(new FileWriter(fileAbsolutePath, true)));
66 }
67
68 } catch (IOException e) {
69 e.printStackTrace();
70 return;
71 }
72 }
73
74
75
76
77
78
79 public void write(String dataStream) {
80 if (this.mode == Mode.WRITE || this.mode == Mode.APPEND) {
81 this.outputStream.println(dataStream);
82 }
83 }
84
85
86
87
88
89
90
91 public String read() {
92 try {
93 if (this.mode == Mode.READ) {
94 String dataStream;
95 dataStream = this.inputStream.readLine();
96 return dataStream;
97 }
98 } catch (IOException e) {
99 e.printStackTrace();
100 return null;
101 }
102 return null;
103 }
104
105
106
107
108 public void close() {
109 try {
110 if (this.mode == Mode.READ) {
111 this.inputStream.close();
112 } else if (this.mode == Mode.WRITE || this.mode == Mode.APPEND) {
113 this.outputStream.flush();
114 this.outputStream.close();
115 }
116 } catch (IOException e) {
117 e.printStackTrace();
118 }
119
120 }
121
122
123
124
125
126 public ArrayList<String> importSimpleTextContent() {
127 this.rowsList = new ArrayList<String>();
128 String rowString = this.read();
129 while (rowString != null) {
130 this.rowsList.add(rowString);
131 rowString = this.read();
132 }
133 return this.rowsList;
134 }
135
136 public String importFullTextContent() {
137 String content = new String();
138 String dataStream = this.read();
139 while (dataStream != null) {
140 content = content.concat(dataStream + "\n");
141 dataStream = this.read();
142 }
143 return content;
144 }
145
146
147 public String importFullTextContent(String rowDelimiter) {
148 String content = new String();
149 String dataStream = this.read();
150 while (dataStream != null) {
151 content = content.concat(dataStream + rowDelimiter);
152 dataStream = this.read();
153 }
154 return content;
155 }
156
157
158
159
160 public void importTabSeparatedContent() {
161
162 }
163
164
165 public ArrayList<String[]> importStringSeparatedContent(String splitter) {
166
167 ArrayList<String[]> splittedData = new ArrayList<String[]>();
168 String dataStream = this.read();
169 while (dataStream != null) {
170 String[] content = dataStream.split(splitter);
171 splittedData.add(content);
172 dataStream = this.read();
173 }
174
175 return splittedData;
176 }
177
178 public Document importXMLContent() {
179 Document doc = null;
180 try {
181 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
182 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
183 doc = dBuilder.parse(this.currentFile);
184 } catch (Exception e) {
185 e.printStackTrace();
186 }
187 return doc;
188 }
189
190
191 public Model importRDFContent() {
192
193 Model model = ModelFactory.createDefaultModel();
194
195
196 InputStream in = com.hp.hpl.jena.util.FileManager.get().open(this.absolutePath);
197 if (in == null) {
198 throw new IllegalArgumentException("File: " + this.absolutePath + " not found");
199 }
200
201
202 model.read(in, null);
203 return model;
204 }
205
206
207 public String getFileName() {
208 return this.fileName;
209 }
210 }