1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.start;
20
21 import java.io.BufferedReader;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.ListIterator;
31 import java.util.regex.Pattern;
32
33
34
35
36
37
38 public class TextFile implements Iterable<String>
39 {
40 private final Path file;
41 private final List<String> lines = new ArrayList<>();
42
43 public TextFile(Path file) throws FileNotFoundException, IOException
44 {
45 this.file = file;
46 init();
47
48 if (!FS.canReadFile(file))
49 {
50 StartLog.debug("Skipping read of missing file: %s",file.toAbsolutePath());
51 return;
52 }
53
54 try (BufferedReader buf = Files.newBufferedReader(file,StandardCharsets.UTF_8))
55 {
56 String line;
57 while ((line = buf.readLine()) != null)
58 {
59 if (line.length() == 0)
60 {
61 continue;
62 }
63
64 if (line.charAt(0) == '#')
65 {
66 continue;
67 }
68
69
70 process(line.trim());
71 }
72 }
73 }
74
75 public void addUniqueLine(String line)
76 {
77 if (lines.contains(line))
78 {
79
80 return;
81 }
82 lines.add(line);
83 }
84
85 public Path getFile()
86 {
87 return file;
88 }
89
90 public List<String> getLineMatches(Pattern pattern)
91 {
92 List<String> ret = new ArrayList<>();
93 for (String line : lines)
94 {
95 if (pattern.matcher(line).matches())
96 {
97 ret.add(line);
98 }
99 }
100 return ret;
101 }
102
103 public List<String> getLines()
104 {
105 return lines;
106 }
107
108 public void init()
109 {
110 }
111
112 @Override
113 public Iterator<String> iterator()
114 {
115 return lines.iterator();
116 }
117
118 public ListIterator<String> listIterator()
119 {
120 return lines.listIterator();
121 }
122
123 public void process(String line)
124 {
125 addUniqueLine(line);
126 }
127 }