source: java/main/src/main/java/com/framsticks/parsers/F0Parser.java @ 97

Last change on this file since 97 was 97, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • add proper exception passing between communication sides:

if exception occur during handling client request, it is
automatically passed as comment to error response.

it may be used to snoop communication between peers

  • fix algorithm choosing text controls in GUI
  • allow GUI testing in virtual frame buffer (xvfb)

FEST had some problem with xvfb but workaround was found

supports tab-completion based on requests history

CHANGELOG:
Further improve handling of exceptions in GUI.

Add StatusBar? implementing ExceptionResultHandler?.

Make completion processing asynchronous.

Minor changes.

Improve completion in console.

Improve history in InteractiveConsole?.

First working version of DirectConsole?.

Minor changes.

Make Connection.address non final.

It is more suitable to use in configuration.

Improvement of consoles.

Improve PopupMenu? and closing of FrameJoinable?.

Fix BrowserTest?.

Found bug with FEST running under xvfb.

JButtonFixture.click() is not working under xvfb.
GuiTest? has wrapper which uses JButton.doClick() directly.

Store CompositeParam? param in TreeNode?.

Simplify ClientSideManagedConnection? connecting.

There is now connectedFunctor needed, ApplicationRequests? can be
send right after creation. They are buffered until the version
and features are negotiated.

Narow down interface of ClientSideManagedConnection?.

Allow that connection specialization send only
ApplicationRequests?.

Improve policy of text control choosing.

Change name of Genotype in BrowserTest?.

Make BrowserTest? change name of Genotype.

Minor change.

First working draft of TrackConsole?.

Simplify Consoles.

More improvements with gui joinables.

Unify initialization on gui joinables.

More rework of Frame based entities.

Refactorize structure of JFrames based entities.

Extract GuiTest? from BrowserBaseTest?.

Reorganize Console classes structure.

Add Collection view to JoinableCollection?.

Configure timeout in testing.

Minor changes.

Rework connections hierarchy.

Add Mode to the get operation.

Make get and set in Tree take PrimitiveParam?.

Unify naming of operations.

Make RunAt? use the given ExceptionHandler?.

It wraps the virtual runAt() method call with
try-catch passing exception to handler.

Force RunAt? to include ExceptionHandler?.

Improve ClientAtServer?.

Minor change.

Another sweep with FindBugs?.

Rename Instance to Tree.

Minor changes.

Minor changes.

Further clarify semantics of Futures.

Add FutureHandler?.

FutureHandler? is refinement of Future, that proxifies
exception handling to ExceptionResultHandler? given
at construction time.

Remove StateFunctor? (use Future<Void> instead).

Make Connection use Future<Void>.

Unparametrize *ResponseFuture?.

Remove StateCallback? not needed anymore.

Distinguish between sides of ResponseFuture?.

Base ResponseCallback? on Future (now ResponseFuture?).

Make asynchronous store taking Future for flags.

Implement storeValue in ObjectInstance?.

File size: 8.0 KB
Line 
1package com.framsticks.parsers;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.InputStreamReader;
7import java.text.ParseException;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.List;
11
12import com.framsticks.model.Model;
13import com.framsticks.model.f0.Schema;
14
15import static com.framsticks.params.SimpleAbstractAccess.*;
16
17import com.framsticks.params.Flags;
18import com.framsticks.params.Param;
19import com.framsticks.params.PrimitiveParam;
20import com.framsticks.util.FramsticksException;
21import com.framsticks.util.io.Encoding;
22import com.framsticks.util.lang.Containers;
23import com.framsticks.util.lang.Pair;
24import com.framsticks.util.lang.Strings;
25import org.apache.log4j.Logger;
26
27import com.framsticks.params.FramsClass;
28import com.framsticks.params.AccessInterface;
29
30/**
31 * The class Parser is used to parse genotype encoded in f0 representation.
32 */
33public class F0Parser {
34
35        private final static Logger log = Logger.getLogger(F0Parser.class);
36
37        /** The schema proper for f0 representation. */
38        protected final Schema schema;
39        protected final InputStream is;
40        protected final List<AccessInterface> result = new ArrayList<AccessInterface>();
41        int lineNumber = 0;
42
43        public F0Parser(Schema schema, InputStream is) {
44                assert schema != null;
45                assert is != null;
46                this.schema = schema;
47                this.is = is;
48        }
49
50        protected AccessInterface processLine(String line) {
51                try {
52
53                        Pair<String, String> p = Strings.splitIntoPair(line, ':', "");
54                        String classId = p.first.trim();
55                        FramsClass framsClass = schema.getFramsClass(classId);
56                        if (framsClass == null) {
57                                throw new Exception("unknown class id: " + classId);
58                        }
59                        AccessInterface access = schema.getRegistry().createAccess(classId, framsClass);
60                        access.select(access.createAccessee());
61                        for (Exception e : loadFromLine(access, p.second)) {
62                                warn(lineNumber, "however entry was added", e);
63                        }
64                        return access;
65                } catch (Exception e) {
66                        warn(lineNumber, "entry was not added", e);
67                }
68                return null;
69        }
70
71        /**
72         * Parses the stream with genotype in f0 representation. The correctness of
73         * genotype is checked. IO and syntax exceptions interrupts parsing and no
74         * result is returned. Other exceptions, connected with schema validation
75         * cause that certain object or it's parameter is ignored (appropriate
76         * communicate informs user about it). Inappropriate values in numeric
77         * fields (bigger than maximum or smaller than minimum values) are
78         * communicated by warnings and set to minimum / maximum value.
79         *
80         * @return the list
81         * @throws IOException
82         *             Signals that an I/O exception has occurred.
83         * @throws ParseException
84         *             the parse exception
85         */
86        public List<AccessInterface> parse() {
87
88                try (InputStreamReader reader = new InputStreamReader(is, Encoding.getDefaultCharset())) {
89                        BufferedReader br = new BufferedReader(reader);
90                        while (br.ready()) {
91                                ++lineNumber;
92                                String line = br.readLine();
93                                line = (line == null ? "" : line.trim());
94                                if (lineNumber == 1) {
95                                        if (!"//0".equals(line)) {
96                                                log.warn("stream should begin with \"//0\" in the first line");
97                                        } else {
98                                                continue;
99                                        }
100                                }
101                                if (line.equals("")) {
102                                        continue;
103                                }
104                                if (line.startsWith("#")) {
105                                        continue;
106                                }
107                                AccessInterface access = processLine(line);
108                                if (access != null) {
109                                        result.add(access);
110                                }
111                        }
112
113                        /** If no 'm' (Model) line was found, than simulate it on the beginning of the result.*/
114                        if (result.isEmpty() || !(result.get(0) instanceof Model)) {
115                                result.add(0, processLine("m:"));
116                        }
117                } catch (IOException e) {
118                        throw new FramsticksException().msg("failed to parse f0").arg("parser", this).arg("schema", schema).cause(e);
119                }
120
121                return result;
122        }
123
124        private static void warn(int lineNumber, String message, Exception e) {
125                log.warn("in line " + lineNumber + " the following error occurred (" + message + "): " + e);
126        }
127
128        /** Breaks string into entries.*/
129        public List<Entry> breakIntoEntries(String parameters) throws Exception {
130                // tokenize
131                boolean inQuotes = false;
132                char previousChar = ',';
133                List<Entry> result = new ArrayList<Entry>();
134                StringBuilder stringBuilder = new StringBuilder();
135                String key = null;
136                if (parameters.trim().length() > 0) {
137                        for (char currentChar : parameters.toCharArray()) {
138                                if (!inQuotes && (currentChar == '=') && (key == null)) {
139                                        key = stringBuilder.toString().trim();
140                                        stringBuilder = new StringBuilder();
141                                } else if (!inQuotes && currentChar == ',') {
142                                        if (previousChar == ',') {
143                                                result.add(new Entry(key, null));
144                                        } else {
145                                                result.add(new Entry(key, stringBuilder.toString().trim()));
146                                        }
147                                        stringBuilder = new StringBuilder();
148                                        key = null;
149                                } else if (currentChar == '"') {
150                                        if (previousChar == '\\') {
151                                                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
152                                                stringBuilder.append(currentChar);
153                                        } else {
154                                                inQuotes = !inQuotes;
155                                        }
156                                } else {
157                                        stringBuilder.append(currentChar);
158                                }
159
160                                previousChar = currentChar;
161                        }
162
163                        result.add(new Entry(key, stringBuilder.toString().trim()));
164
165                        if (inQuotes) {
166                                throw new Exception("Double quotes expected while end of line met");
167                        }
168                }
169                return result;
170        }
171
172        public List<Exception> loadFromLine(AccessInterface access, String parameters) throws Exception {
173
174                List<Entry> entries = breakIntoEntries(parameters);
175
176                List<Exception> exceptions = new ArrayList<Exception>();
177
178                Collection<Param> paramsC = access.getParams();
179                Param[] params = paramsC.toArray(new Param[] {null});
180                if (params.length == 0) {
181                        return exceptions;
182                }
183                for (PrimitiveParam<?> p : Containers.filterInstanceof(paramsC, PrimitiveParam.class)) {
184                        Object def = p.getDef(Object.class);
185                        if (def != null) {
186                                access.set(p, def);
187                        }
188                }
189
190                int number = -1;
191                Integer nextParamNumber = 0;
192                for (Entry pair : entries) {
193                        ++number;
194                        try {
195                                Param currentParam;
196                                if (pair.key != null) {
197                                        currentParam = access.getParam(pair.key);
198                                        if (currentParam == null) {
199                                                nextParamNumber = null;
200                                                throw new Exception("no parameter with such id: " + pair.key);
201                                        }
202                                } else {
203                                        if (nextParamNumber == null || ((params[nextParamNumber].getFlags() & Flags.CANOMITNAME) == 0)) {
204                                                nextParamNumber = null;
205                                                throw new Exception(
206                                                                "parameter with offset: "
207                                                                                + number
208                                                                                + " is not set, "
209                                                                                + "because it's definition or definition of the previous param "
210                                                                                + "does not contain flag, which allows to skip the name (flag 1024)");
211                                        }
212                                        currentParam = params[nextParamNumber];
213                                }
214                                if (currentParam != null) {
215                                        if (pair.value != null) {
216                                                PrimitiveParam<?> vp = (PrimitiveParam<?>) currentParam;
217                                                int setFlag = access.set(vp, pair.value);
218                                                if ((setFlag & Flags.PSET_HITMIN) != 0) {
219                                                        exceptions.add(createBoundaryHitException(access, vp, pair.value, Flags.PSET_HITMIN));
220                                                }
221
222                                                if ((setFlag & Flags.PSET_HITMAX) != 0) {
223                                                        exceptions.add(createBoundaryHitException(access, vp, pair.value, Flags.PSET_HITMAX));
224                                                }
225
226                                                if ((setFlag & Flags.PSET_RONLY) != 0) {
227                                                        throw (new Exception("tried to set a read-only attribute \""
228                                                                + currentParam.getId()
229                                                                + "\" in class \"" + access.getId() + "\""));
230                                                }
231                                        }
232                                        nextParamNumber = null;
233                                        for (int j = params.length - 1; j > 0; --j) {
234                                                if (params[j - 1] == currentParam) {
235                                                        nextParamNumber = j;
236                                                }
237                                        }
238                                }
239
240                        } catch (Exception e) {
241                                exceptions.add(e);
242                        }
243                }
244                return exceptions;
245        }
246
247        private static Exception createBoundaryHitException(AccessInterface access, PrimitiveParam<?> param, String value, int flag) {
248                boolean minimum = (flag & Flags.PSET_HITMIN) != 0;
249                String boundary = (minimum ? param.getMin(Object.class) : param.getMax(Object.class)).toString();
250                String name =  (minimum ? "minimum" : "maximum");
251                return new Exception("Tried to set attribute \""
252                                + param.getId()
253                                + "\" in class \""
254                                + access.getId()
255                                + "\" to value which exceeds " + name + " ("
256                                + value
257                                + "), truncated to: "
258                                + boundary);
259        }
260}
Note: See TracBrowser for help on using the repository browser.