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