Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0ae62e14cc2b460b3ba636df933b3c92575066a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package org.eclipse.xtend.backend.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


/**
 * This class converts the contents of a resource to a list. It removes empty lines (i.e. lines containing
 *  only whitespace), and it trims leading and trailing whitespace. It also removes comments, i.e. parts of
 *  a line following a '#' character.<br>
 * 
 * The contents of the file are read using the default locale of the platform.
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public final class ResourceToList {
    final List<String> _result = new ArrayList<String>();
   
    /**
     * @param in may be null, in which case the resulting list is empty.
     */
    public ResourceToList (InputStream in) {
        if (in == null)
            return;
        
        final BufferedReader br = new BufferedReader (new InputStreamReader (in));
        
        String line = null;
        try {
            while ((line = br.readLine()) != null)
                processLine (line);
        }
        catch (IOException e) {
            ErrorHandler.handle(e);
        }
    }
    
    private void processLine (String line) {
        line = stripComment (line);
        line = line.trim ();
        if (line.isEmpty())
            return;
        
        _result.add (line);
    }
    
    private String stripComment (String line) {
        final int startComment = line.indexOf ('#');
        if (startComment < 0)
            return line;
        
        return line.substring (startComment);
    }
    
    public List<String> getResult () {
        return _result;
    }
}

Back to the top