View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.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   * Simple common abstraction for Text files, that consist of a series of lines.
34   * <p>
35   * Ignoring lines that are empty, deemed to be comments, or are duplicates of prior lines.
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                      // TODO - bad form calling derived method from base class constructor
71                      process(line.trim());
72                  }
73              }
74          }
75      }
76  
77      public void addUniqueLine(String line)
78      {
79          if (lines.contains(line))
80          {
81              // skip
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 }