1   package eu.fbk.dkm.pikes.resources.mpqa;
2   
3   import com.glaforge.i18n.io.CharsetToolkit;
4   import com.google.common.base.Charsets;
5   import com.google.common.collect.ImmutableList;
6   import com.google.common.collect.Lists;
7   import com.google.common.io.CharStreams;
8   import eu.fbk.utils.core.CommandLine;
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  
12  import javax.annotation.Nullable;
13  import java.io.*;
14  import java.nio.charset.Charset;
15  import java.util.List;
16  
17  /**
18   * Created by alessio on 24/03/15.
19   */
20  public final class RecordSet {
21  
22  	public static final RecordSet EMPTY = new RecordSet(ImmutableList.<Record>of());
23  	private static final Logger LOGGER = LoggerFactory.getLogger(RecordSet.class);
24  
25  	private final List<Record> records;
26  
27  	public RecordSet(final Reader reader) throws IOException {
28  		final List<Record> records = Lists.newArrayList();
29  		for (final String line : CharStreams.readLines(reader)) {
30  			final String trimmedLine = line.trim();
31  			if (!trimmedLine.startsWith("#")) {
32  				final Record record = new Record(line);
33  				records.add(record);
34  			}
35  		}
36  		this.records = ImmutableList.copyOf(records);
37  	}
38  
39  	public RecordSet(final Iterable<? extends Record> records) {
40  		this.records = ImmutableList.copyOf(records);
41  	}
42  
43  	public List<Record> getRecords() {
44  		return this.records;
45  	}
46  
47  	public List<Record> getRecords(final String recordName) {
48  		final List<Record> result = Lists.newArrayList();
49  		for (final Record record : this.records) {
50  			if (record.getName().equals(recordName)) {
51  				result.add(record);
52  			}
53  		}
54  		return result;
55  	}
56  
57  	@Nullable
58  	public Record getRecord(final String recordName, final String key, final String value) {
59  		for (final Record record : this.records) {
60  			if (record.getName().equals(recordName) && value.equals(record.getValue(key))) {
61  				return record;
62  			}
63  		}
64  		return null;
65  	}
66  
67  	public String getRecordValue(final String recordName, final @Nullable String defaultValue) {
68  		for (final Record record : this.records) {
69  			if (record.getName().equals(recordName)) {
70  				return record.getValue();
71  			}
72  		}
73  		return defaultValue;
74  	}
75  
76  	public static RecordSet readFromFile(@Nullable final File file) throws IOException {
77  		if (file == null || !file.exists()) {
78  			return RecordSet.EMPTY;
79  		}
80  		try (Reader reader = read(file)) {
81  			return new RecordSet(reader);
82  		}
83  	}
84  
85  	public static BufferedReader read(final File mpqaFile) throws IOException {
86  		final Charset charset = CharsetToolkit.guessEncoding(mpqaFile, 4096, Charsets.UTF_8);
87  		return new BufferedReader(new InputStreamReader(new FileInputStream(mpqaFile), charset));
88  	}
89  
90  	public static void main(String[] args) throws IOException {
91  		final CommandLine cmd = CommandLine
92  				.parser()
93  				.withName("file-test")
94  				.withHeader("Check eu.fbk.dkm.pikes.resources.mpqa file")
95  				.withOption("i", "input-file", "the MPQA file", "FILE", CommandLine.Type.FILE_EXISTING, true, false, true)
96  				.withLogger(LoggerFactory.getLogger("eu.fbk.fssa")).parse(args);
97  
98  		final File inputFile = cmd.getOptionValue("i", File.class);
99  		final RecordSet annotations = RecordSet.readFromFile(inputFile);
100 
101 		for (Record record : annotations.getRecords()) {
102 			System.out.println(record.getName());
103 			System.out.println(record.getSpan());
104 			for (String attr : record.getValueMap().keySet()) {
105 				System.out.println(attr + " = " + record.getValueMap().get(attr));
106 			}
107 			System.out.println();
108 		}
109 	}
110 
111 }