1 | package com.framsticks.parsers; |
---|
2 | |
---|
3 | import com.framsticks.params.Source; |
---|
4 | import com.framsticks.util.io.Encoding; |
---|
5 | |
---|
6 | import java.io.*; |
---|
7 | |
---|
8 | |
---|
9 | public class FileSource implements Source { |
---|
10 | |
---|
11 | private BufferedReader reader; |
---|
12 | private final String filename; |
---|
13 | |
---|
14 | public FileSource(InputStream stream, String filename) { |
---|
15 | this.filename = filename; |
---|
16 | this.reader = new BufferedReader(new InputStreamReader(stream, Encoding.getFramsticksCharset())); |
---|
17 | } |
---|
18 | |
---|
19 | public FileSource(String filename) throws IOException { |
---|
20 | this(new FileInputStream(filename), filename); |
---|
21 | } |
---|
22 | |
---|
23 | public FileSource(InputStream stream) { |
---|
24 | this(stream, "<stream>"); |
---|
25 | } |
---|
26 | |
---|
27 | @Override |
---|
28 | public String readLine() |
---|
29 | { |
---|
30 | assert !isClosed(); |
---|
31 | try |
---|
32 | { |
---|
33 | return reader.readLine(); |
---|
34 | } catch (IOException ignored) { |
---|
35 | |
---|
36 | } |
---|
37 | return null; |
---|
38 | } |
---|
39 | |
---|
40 | @Override |
---|
41 | public String getFilename() |
---|
42 | { |
---|
43 | return filename; |
---|
44 | } |
---|
45 | |
---|
46 | @Override |
---|
47 | public String demangleInclude(String include) |
---|
48 | { |
---|
49 | if (!include.contains(java.io.File.separator)) { |
---|
50 | String currentFilePath = (new java.io.File(filename)).getParent(); |
---|
51 | if (!String.valueOf( |
---|
52 | currentFilePath.charAt(currentFilePath.length() - 1)) |
---|
53 | .equals(java.io.File.separator)) { |
---|
54 | currentFilePath = currentFilePath + java.io.File.separator; |
---|
55 | } |
---|
56 | include = currentFilePath + include; |
---|
57 | } |
---|
58 | return include; |
---|
59 | } |
---|
60 | |
---|
61 | @Override |
---|
62 | public Source openInclude(String include) |
---|
63 | { |
---|
64 | try |
---|
65 | { |
---|
66 | return new FileSource(include); |
---|
67 | } |
---|
68 | catch(IOException e) |
---|
69 | { |
---|
70 | |
---|
71 | } |
---|
72 | return null; |
---|
73 | } |
---|
74 | |
---|
75 | @Override |
---|
76 | public void close() |
---|
77 | { |
---|
78 | try { |
---|
79 | reader.close(); |
---|
80 | } catch (IOException e) { |
---|
81 | |
---|
82 | } |
---|
83 | reader = null; |
---|
84 | } |
---|
85 | |
---|
86 | @Override |
---|
87 | public boolean isClosed() { |
---|
88 | return reader == null; |
---|
89 | } |
---|
90 | |
---|
91 | @Override |
---|
92 | public String toString() { |
---|
93 | return filename; |
---|
94 | } |
---|
95 | |
---|
96 | } |
---|