source:
java/main/src/main/java/com/framsticks/parsers/F0Writer.java
@
107
Last change on this file since 107 was 107, checked in by , 11 years ago | |
---|---|
File size: 2.3 KB |
Rev | Line | |
---|---|---|
[84] | 1 | package com.framsticks.parsers; |
2 | ||
3 | import com.framsticks.model.Model; | |
[88] | 4 | import com.framsticks.model.f0.Schema; |
[84] | 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; | |
[99] | 9 | import static com.framsticks.params.ParamFlags.*; |
[84] | 10 | |
11 | /** | |
12 | * Author: Piotr Śniegowski | |
13 | */ | |
14 | public class F0Writer { | |
15 | ||
16 | protected final Schema schema; | |
[102] | 17 | protected final Sink sink; |
[84] | 18 | protected final Model model; |
[85] | 19 | protected boolean omitDefaults = true; |
[84] | 20 | |
[102] | 21 | public F0Writer(Schema schema, Model model, Sink sink) { |
[84] | 22 | this.schema = schema; |
23 | this.model = model; | |
24 | this.sink = sink; | |
25 | } | |
26 | ||
[85] | 27 | public F0Writer setOmitDefaults(boolean omitDefaults) { |
28 | this.omitDefaults = omitDefaults; | |
[84] | 29 | return this; |
30 | } | |
31 | ||
[100] | 32 | protected void write(Access access) { |
[84] | 33 | if (access instanceof ListAccess) { |
34 | for (ValueParam p : Containers.filterInstanceof(access.getParams(), ValueParam.class)) { | |
[107] | 35 | write(schema.getRegistry().prepareAccess((CompositeParam) p, false).select(access.get(p, Object.class))); |
[84] | 36 | } |
37 | return; | |
38 | } | |
39 | StringBuilder line = new StringBuilder(); | |
[105] | 40 | line.append(access.getTypeId()).append(":"); |
[84] | 41 | boolean placeComma = false; |
42 | boolean contiguous = true; | |
43 | ||
44 | for (ValueParam param : filterInstanceof(access.getParams(), ValueParam.class)) { | |
45 | if (param instanceof CompositeParam) { | |
[107] | 46 | Access a = schema.getRegistry().prepareAccess((CompositeParam) param, false); |
[84] | 47 | a.select(access.get((ValueParam) param, Object.class)); |
48 | write(a); | |
49 | continue; | |
50 | } | |
51 | Object value = access.get(param, Object.class); | |
[87] | 52 | if (omitDefaults && (param instanceof PrimitiveParam) && Misc.equals(value, ((PrimitiveParam<?>) param).getDef(Object.class))) { |
[84] | 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 | ||
[99] | 67 | if ((!contiguous) || ((param.getFlags() & CANOMITNAME) == 0)) { |
[84] | 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 | } | |
[90] | 80 | |
[84] | 81 | public void write() { |
[100] | 82 | Access access = schema.getRegistry().createAccess("m"); |
[84] | 83 | access.select(model); |
84 | write(access); | |
85 | } | |
86 | ||
87 | ||
88 | } |
Note: See TracBrowser
for help on using the repository browser.