forked from Mobenga/ngen-xml-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventMapperExample3.java
91 lines (65 loc) · 3.17 KB
/
EventMapperExample3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.mobenga.ngen.xml.parser.example;
import com.mobenga.ngen.xml.parser.example.model.Event;
import com.mobenga.ngen.xml.parser.example.model.Market;
import com.mobenga.ngen.xml.parser.example.model.Outcome;
import com.mobenga.ngen.xml.parser.AttributeMapping;
import com.mobenga.ngen.xml.parser.ElementParserSettings;
import com.mobenga.ngen.xml.parser.Mappings;
import com.mobenga.ngen.xml.util.MappingUtil;
public class EventMapperExample3 implements Mappings {
@Override
public ElementParserSettings getSettings() {
return event();
}
private ElementParserSettings event() {
ElementParserSettings settings = new ElementParserSettings("event");
settings.setElementStartProcessor(objectBranch -> {
Event event = new Event();
objectBranch.put(Event.class, event);
});
settings.setAttributeMappings(
new AttributeMapping<>(Event.class, Event::setId, MappingUtil::getFirst, "id"),
new AttributeMapping<>(Event.class, Event::setName, MappingUtil::getFirst, "name")
);
settings.setSubElementParsers(market());
return settings;
}
private ElementParserSettings market() {
ElementParserSettings settings = new ElementParserSettings("market");
settings.setElementStartProcessor(objectBranch -> objectBranch.put(Market.class, new Market()));
settings.setAttributeMappings(
new AttributeMapping<>(Market.class, Market::setId, MappingUtil::getFirst, "id"),
new AttributeMapping<>(Market.class, Market::setName, MappingUtil::getFirst, "name")
);
settings.setSubElementParsers(selection());
settings.setElementEndProcessor(objectBranch -> {
Event event = objectBranch.getInstance(Event.class);
Market market = objectBranch.pop(Market.class);
event.getMarkets().add(market);
});
return settings;
}
private ElementParserSettings selection() {
ElementParserSettings settings = new ElementParserSettings("selection");
settings.setElementStartProcessor(objectBranch -> objectBranch.put(Outcome.class, new Outcome()));
settings.setAttributeMappings(
new AttributeMapping<>(Outcome.class, Outcome::setId, MappingUtil::getFirst, "id"),
new AttributeMapping<>(Outcome.class, Outcome::setName, MappingUtil::getFirst, "name")
);
settings.setSubElementParsers(price());
settings.setElementEndProcessor(objectBranch -> {
Market market = objectBranch.getInstance(Market.class);
Outcome outcome = objectBranch.pop(Outcome.class);
market.getOutcomes().add(outcome);
});
return settings;
}
private ElementParserSettings price() {
ElementParserSettings settings = new ElementParserSettings("price");
settings.setAttributeMappings(
new AttributeMapping<>(Outcome.class, Outcome::setOdds, MappingUtil::getFirst, "odds_frac"),
new AttributeMapping<>(Outcome.class, Outcome::setOddsDecimal, MappingUtil::getFirst, "odds_dec")
);
return settings;
}
}