1 | package com.framsticks.parsers; |
---|
2 | |
---|
3 | import com.framsticks.model.Model; |
---|
4 | import com.framsticks.model.f0.Schema; |
---|
5 | import com.framsticks.params.*; |
---|
6 | import com.framsticks.util.Misc; |
---|
7 | import com.framsticks.util.lang.Containers; |
---|
8 | import static com.framsticks.util.lang.Containers.filterInstanceof; |
---|
9 | import static com.framsticks.params.ParamFlags.*; |
---|
10 | |
---|
11 | /** |
---|
12 | * Author: Piotr Śniegowski |
---|
13 | */ |
---|
14 | public class F0Writer { |
---|
15 | |
---|
16 | protected final Schema schema; |
---|
17 | protected final Sink sink; |
---|
18 | protected final Model model; |
---|
19 | protected boolean omitDefaults = true; |
---|
20 | |
---|
21 | public F0Writer(Schema schema, Model model, Sink sink) { |
---|
22 | this.schema = schema; |
---|
23 | this.model = model; |
---|
24 | this.sink = sink; |
---|
25 | } |
---|
26 | |
---|
27 | public F0Writer setOmitDefaults(boolean omitDefaults) { |
---|
28 | this.omitDefaults = omitDefaults; |
---|
29 | return this; |
---|
30 | } |
---|
31 | |
---|
32 | protected void write(Access access) { |
---|
33 | if (access instanceof ListAccess) { |
---|
34 | for (ValueParam p : Containers.filterInstanceof(access.getParams(), ValueParam.class)) { |
---|
35 | write(schema.getRegistry().prepareAccess((CompositeParam) p, false).select(access.get(p, Object.class))); |
---|
36 | } |
---|
37 | return; |
---|
38 | } |
---|
39 | StringBuilder line = new StringBuilder(); |
---|
40 | line.append(access.getTypeId()).append(":"); |
---|
41 | boolean placeComma = false; |
---|
42 | boolean contiguous = true; |
---|
43 | |
---|
44 | for (ValueParam param : filterInstanceof(access.getParams(), ValueParam.class)) { |
---|
45 | if (param instanceof CompositeParam) { |
---|
46 | Access a = schema.getRegistry().prepareAccess((CompositeParam) param, false); |
---|
47 | a.select(access.get((ValueParam) param, Object.class)); |
---|
48 | write(a); |
---|
49 | continue; |
---|
50 | } |
---|
51 | Object value = access.get(param, Object.class); |
---|
52 | if (omitDefaults && (param instanceof PrimitiveParam) && Misc.equals(value, ((PrimitiveParam<?>) param).getDef(Object.class))) { |
---|
53 | contiguous = false; |
---|
54 | continue; |
---|
55 | } |
---|
56 | if (value == null) { |
---|
57 | contiguous = false; |
---|
58 | continue; |
---|
59 | } |
---|
60 | |
---|
61 | if (placeComma) { |
---|
62 | line.append(","); |
---|
63 | } else { |
---|
64 | placeComma = true; |
---|
65 | } |
---|
66 | |
---|
67 | if ((!contiguous) || ((param.getFlags() & CANOMITNAME) == 0)) { |
---|
68 | line.append(param.getId()).append("="); |
---|
69 | contiguous = true; |
---|
70 | } |
---|
71 | String printed = value.toString().replace("\"", "\\\""); |
---|
72 | if (printed.contains(",")) { |
---|
73 | line.append("\"").append(printed).append("\""); |
---|
74 | } else { |
---|
75 | line.append(value); |
---|
76 | } |
---|
77 | } |
---|
78 | sink.print(line).breakLine(); |
---|
79 | } |
---|
80 | |
---|
81 | public void write() { |
---|
82 | Access access = schema.getRegistry().createAccess("m"); |
---|
83 | access.select(model); |
---|
84 | write(access); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | } |
---|