1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml;
17
18 import java.util.List;
19 import java.util.Map;
20 import java.util.TreeMap;
21
22 import junit.framework.TestCase;
23
24 import org.yaml.snakeyaml.constructor.AbstractConstruct;
25 import org.yaml.snakeyaml.constructor.Constructor;
26 import org.yaml.snakeyaml.nodes.MappingNode;
27 import org.yaml.snakeyaml.nodes.Node;
28 import org.yaml.snakeyaml.nodes.SequenceNode;
29 import org.yaml.snakeyaml.nodes.Tag;
30 import org.yaml.snakeyaml.representer.Represent;
31 import org.yaml.snakeyaml.representer.Representer;
32
33
34
35
36
37
38
39 public class Example2_24Test extends TestCase {
40 class MyConstructor extends Constructor {
41 public MyConstructor() {
42 this.yamlConstructors.put(new Tag("tag:clarkevans.com,2002:shape"),
43 new ConstructShape());
44 this.yamlConstructors.put(new Tag("tag:clarkevans.com,2002:circle"),
45 new ConstructCircle());
46 this.yamlConstructors.put(new Tag("tag:clarkevans.com,2002:line"), new ConstructLine());
47 this.yamlConstructors.put(new Tag("tag:clarkevans.com,2002:label"),
48 new ConstructLabel());
49 }
50
51 private class ConstructShape extends AbstractConstruct {
52 @SuppressWarnings("unchecked")
53 public Object construct(Node node) {
54 SequenceNode snode = (SequenceNode) node;
55 List<Entity> values = (List<Entity>) constructSequence(snode);
56 Shape shape = new Shape(values);
57 return shape;
58 }
59 }
60
61 private class ConstructCircle extends AbstractConstruct {
62 @SuppressWarnings("unchecked")
63 public Object construct(Node node) {
64 MappingNode mnode = (MappingNode) node;
65 Map<Object, Object> values = constructMapping(mnode);
66 Circle circle = new Circle((Map<String, Integer>) values.get("center"),
67 (Integer) values.get("radius"));
68 return circle;
69 }
70 }
71
72 private class ConstructLine extends AbstractConstruct {
73 @SuppressWarnings("unchecked")
74 public Object construct(Node node) {
75 MappingNode mnode = (MappingNode) node;
76 Map<Object, Object> values = constructMapping(mnode);
77 Line line = new Line((Map<String, Integer>) values.get("start"),
78 (Map<String, Integer>) values.get("finish"));
79 return line;
80 }
81 }
82
83 private class ConstructLabel extends AbstractConstruct {
84 @SuppressWarnings("unchecked")
85 public Object construct(Node node) {
86 MappingNode mnode = (MappingNode) node;
87 Map<Object, Object> values = constructMapping(mnode);
88 Label label = new Label((Map<String, Integer>) values.get("start"),
89 (Integer) values.get("color"), (String) values.get("text"));
90 return label;
91 }
92 }
93 }
94
95 class MyRepresenter extends Representer {
96 public MyRepresenter() {
97 this.representers.put(Shape.class, new RepresentShape());
98 this.representers.put(Circle.class, new RepresentCircle());
99 this.representers.put(Line.class, new RepresentLine());
100 this.representers.put(Label.class, new RepresentLabel());
101 this.representers.put(HexInteger.class, new RepresentHex());
102 }
103
104 private class RepresentShape implements Represent {
105 public Node representData(Object data) {
106 Shape shape = (Shape) data;
107 List<Entity> value = shape.getEntities();
108 return representSequence(new Tag("!shape"), value, Boolean.FALSE);
109 }
110 }
111
112 private class RepresentCircle implements Represent {
113 public Node representData(Object data) {
114 Circle circle = (Circle) data;
115 Map<String, Object> map = new TreeMap<String, Object>();
116 map.put("center", circle.getCenter());
117 map.put("radius", circle.getRadius());
118 return representMapping(new Tag("!circle"), map, Boolean.FALSE);
119 }
120 }
121
122 private class RepresentLine implements Represent {
123 public Node representData(Object data) {
124 Line line = (Line) data;
125 Map<String, Object> map = new TreeMap<String, Object>();
126 map.put("start", line.getStart());
127 map.put("finish", line.getFinish());
128 return representMapping(new Tag("!line"), map, Boolean.FALSE);
129 }
130 }
131
132 private class RepresentLabel implements Represent {
133 public Node representData(Object data) {
134 Label label = (Label) data;
135 Map<String, Object> map = new TreeMap<String, Object>();
136 map.put("start", label.getStart());
137 map.put("color", new HexInteger(label.getColor()));
138 map.put("text", label.getText());
139 return representMapping(new Tag("!label"), map, Boolean.FALSE);
140 }
141 }
142
143 private class RepresentHex implements Represent {
144 public Node representData(Object data) {
145 HexInteger hex = (HexInteger) data;
146 return representScalar(Tag.INT, "0x"
147 + Integer.toHexString(hex.getColor()).toUpperCase(), null);
148 }
149 }
150 }
151
152 private class HexInteger {
153 private Integer color;
154
155 public HexInteger(Integer color) {
156 this.color = color;
157 }
158
159 public Integer getColor() {
160 return color;
161 }
162 }
163
164 private class Shape {
165 private List<Entity> entities;
166
167 public List<Entity> getEntities() {
168 return entities;
169 }
170
171 public Shape(List<Entity> entities) {
172 this.entities = entities;
173 }
174 }
175
176 private class Entity {
177 }
178
179 private class Circle extends Entity {
180 private Map<String, Integer> center;
181 private Integer radius;
182
183 public Circle(Map<String, Integer> center, Integer radius) {
184 this.center = center;
185 this.radius = radius;
186 }
187
188 public Map<String, Integer> getCenter() {
189 return center;
190 }
191
192 public Integer getRadius() {
193 return radius;
194 }
195 }
196
197 private class Line extends Entity {
198 private Map<String, Integer> start;
199 private Map<String, Integer> finish;
200
201 public Line(Map<String, Integer> start, Map<String, Integer> finish) {
202 this.start = start;
203 this.finish = finish;
204 }
205
206 public Map<String, Integer> getStart() {
207 return start;
208 }
209
210 public Map<String, Integer> getFinish() {
211 return finish;
212 }
213 }
214
215 private class Label extends Entity {
216 private Map<String, Integer> start;
217 private Integer color;
218 private String text;
219
220 public Label(Map<String, Integer> start, Integer color, String text) {
221 this.start = start;
222 this.color = color;
223 this.text = text;
224 }
225
226 public Map<String, Integer> getStart() {
227 return start;
228 }
229
230 public Integer getColor() {
231 return color;
232 }
233
234 public String getText() {
235 return text;
236 }
237 }
238
239 public void testExample_2_24() {
240 Yaml yaml = new Yaml(new MyConstructor());
241 Shape shape = (Shape) yaml.load(Util.getLocalResource("specification/example2_24.yaml"));
242 assertNotNull(shape);
243 yaml = new Yaml(new MyRepresenter());
244 String output = yaml.dump(shape);
245 String etalon = Util.getLocalResource("specification/example2_24_dumped.yaml");
246 assertEquals(etalon, output);
247 }
248 }