View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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   * Simple common abstraction for Text files, that consist of a series of lines.
35   * <p>
36   * Ignoring lines that are empty, deemed to be comments, or are duplicates of prior lines.
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                  // TODO - bad form calling derived method from base class constructor
70                  process(line.trim());
71              }
72          }
73      }
74  
75      public void addUniqueLine(String line)
76      {
77          if (lines.contains(line))
78          {
79              // skip
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 }