1 | package com.framsticks.parsers; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.InputStream; |
---|
6 | import java.io.InputStreamReader; |
---|
7 | import java.text.ParseException; |
---|
8 | import java.util.ArrayList; |
---|
9 | import java.util.List; |
---|
10 | |
---|
11 | import com.framsticks.params.PropertiesAccess; |
---|
12 | import org.apache.log4j.Logger; |
---|
13 | |
---|
14 | import com.framsticks.params.FramsClass; |
---|
15 | import com.framsticks.params.AccessInterface; |
---|
16 | |
---|
17 | /** |
---|
18 | * The class Parser is used to parse genotype encoded in f0 representation. |
---|
19 | */ |
---|
20 | public class Parser { |
---|
21 | |
---|
22 | private final static Logger logger = Logger.getLogger(Parser.class); |
---|
23 | |
---|
24 | /** The schema proper for f0 representation. */ |
---|
25 | private Schema schema; |
---|
26 | |
---|
27 | public Parser(Schema schema) { |
---|
28 | if (schema == null) |
---|
29 | throw new NullPointerException( |
---|
30 | "Schema object passed to Parser object cannot be null!"); |
---|
31 | this.schema = schema; |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * Parses the stream with genotype in f0 representation. The correctness of |
---|
36 | * genotype is checked. IO and syntax exceptions interrupts parsing and no |
---|
37 | * result is returned. Other exceptions, connected with schema validation |
---|
38 | * cause that certain object or it's parameter is ignored (appropriate |
---|
39 | * communicate informs user about it). Inappropriate values in numeric |
---|
40 | * fields (bigger than maximum or smaller than minimum values) are |
---|
41 | * communicated by warnings and set to minimum / maximum value. |
---|
42 | * |
---|
43 | * @param is |
---|
44 | * the is |
---|
45 | * @return the list |
---|
46 | * @throws IOException |
---|
47 | * Signals that an I/O exception has occurred. |
---|
48 | * @throws ParseException |
---|
49 | * the parse exception |
---|
50 | */ |
---|
51 | public List<AccessInterface> parse(InputStream is) throws IOException, |
---|
52 | ParseException { |
---|
53 | List<AccessInterface> result = new ArrayList<AccessInterface>(); |
---|
54 | |
---|
55 | InputStreamReader isr = null; |
---|
56 | try { |
---|
57 | isr = new InputStreamReader(is, "UTF-8"); |
---|
58 | BufferedReader br = new BufferedReader(isr); |
---|
59 | int l = 0; |
---|
60 | while (br.ready()) { |
---|
61 | String line = br.readLine(); |
---|
62 | if (line == null) |
---|
63 | line = ""; |
---|
64 | else |
---|
65 | line = line.trim(); |
---|
66 | if (l == 0) { |
---|
67 | if (!"//0".equals(line)) { |
---|
68 | logger.warn("stream should begin with \"//0\" in first line"); |
---|
69 | } else { |
---|
70 | l++; |
---|
71 | continue; |
---|
72 | } |
---|
73 | } |
---|
74 | if (line.equals("")) { |
---|
75 | l++; |
---|
76 | continue; |
---|
77 | } |
---|
78 | try { |
---|
79 | int indexOfColon = line.indexOf(':'); |
---|
80 | if (indexOfColon < 0) { |
---|
81 | indexOfColon = line.length(); |
---|
82 | } |
---|
83 | String classId = line.substring(0, indexOfColon).trim(); |
---|
84 | FramsClass framsClass = schema.getMainClasses() |
---|
85 | .get(classId); |
---|
86 | if (framsClass == null) |
---|
87 | throw new Exception("Unknown class getId: " + classId); |
---|
88 | PropertiesAccess propertiesParam = new PropertiesAccess(framsClass); |
---|
89 | propertiesParam.select(propertiesParam.createAccessee()); |
---|
90 | List<Exception> exceptions = propertiesParam.load2(line); |
---|
91 | for (Exception e : exceptions) { |
---|
92 | logger.warn("In line " |
---|
93 | + Integer.valueOf(l + 1) |
---|
94 | + " the following error occured (however entry was added): " |
---|
95 | + e.getMessage()); |
---|
96 | } |
---|
97 | result.add(propertiesParam); |
---|
98 | } catch (Exception e) { |
---|
99 | logger.warn("In line " |
---|
100 | + Integer.valueOf(l + 1) |
---|
101 | + " the following error occured (entry was not added): " |
---|
102 | + e.getMessage()); |
---|
103 | } finally { |
---|
104 | l++; |
---|
105 | } |
---|
106 | } |
---|
107 | } finally { |
---|
108 | if (isr != null) |
---|
109 | isr.close(); |
---|
110 | } |
---|
111 | |
---|
112 | return result; |
---|
113 | } |
---|
114 | } |
---|