create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / help / tours / TextLineReader.java
1 package net.sf.openrocket.gui.help.tours;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.Reader;
8 import java.nio.charset.Charset;
9 import java.util.Iterator;
10 import java.util.NoSuchElementException;
11
12 import net.sf.openrocket.util.BugException;
13
14 /**
15  * Read from a Reader object one line at a time, ignoring blank lines,
16  * preceding and trailing whitespace and comment lines starting with '#'.
17  * 
18  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
19  */
20 public class TextLineReader implements Iterator<String> {
21         
22         private static final Charset UTF8 = Charset.forName("UTF-8");
23         
24
25
26         private final BufferedReader reader;
27         
28         private String next = null;
29         
30         /**
31          * Read from an input stream with UTF-8 character encoding.
32          */
33         public TextLineReader(InputStream inputStream) {
34                 this(new InputStreamReader(inputStream, UTF8));
35         }
36         
37         
38         /**
39          * Read from a reader.
40          */
41         public TextLineReader(Reader reader) {
42                 if (reader instanceof BufferedReader) {
43                         this.reader = (BufferedReader) reader;
44                 } else {
45                         this.reader = new BufferedReader(reader);
46                 }
47         }
48         
49         
50         /**
51          * Test whether the file has more lines available.
52          */
53         @Override
54         public boolean hasNext() {
55                 if (next != null) {
56                         return true;
57                 }
58                 
59                 try {
60                         next = readLine();
61                 } catch (IOException e) {
62                         throw new BugException(e);
63                 }
64                 
65                 return next != null;
66         }
67         
68         
69         /**
70          * Retrieve the next non-blank, non-comment line.
71          */
72         @Override
73         public String next() {
74                 if (hasNext()) {
75                         String ret = next;
76                         next = null;
77                         return ret;
78                 }
79                 
80                 throw new NoSuchElementException("End of file reached");
81         }
82         
83         
84         /**
85          * Peek what the next line would be.
86          */
87         public String peek() {
88                 if (hasNext()) {
89                         return next;
90                 }
91                 
92                 throw new NoSuchElementException("End of file reached");
93         }
94         
95         
96         private String readLine() throws IOException {
97                 
98                 while (true) {
99                         // Read the next line
100                         String line = reader.readLine();
101                         if (line == null) {
102                                 return null;
103                         }
104                         
105                         // Check whether to accept the line
106                         line = line.trim();
107                         if (line.length() > 0 && line.charAt(0) != '#') {
108                                 return line;
109                         }
110                 }
111                 
112         }
113         
114         
115         @Override
116         public void remove() {
117                 throw new UnsupportedOperationException("Remove not supported");
118         }
119         
120 }