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