1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml.constructor;
18
19 import java.util.List;
20
21 import junit.framework.TestCase;
22
23 import org.yaml.snakeyaml.Yaml;
24 import org.yaml.snakeyaml.nodes.Node;
25 import org.yaml.snakeyaml.nodes.ScalarNode;
26 import org.yaml.snakeyaml.nodes.SequenceNode;
27 import org.yaml.snakeyaml.nodes.Tag;
28
29
30
31
32
33 public class PrefixConstructorTest extends TestCase {
34
35 @SuppressWarnings("unchecked")
36 public void test1() {
37 Yaml yaml = new Yaml(new CustomConstructor());
38 String input = "- !org.yaml.Foo 123\n- !org.yaml.Bar 456\n- !org.yaml.Exact 789\n- !Immutable [aaa, bbb]";
39 List<Extra> list = (List<Extra>) yaml.load(input);
40 assertEquals(4, list.size());
41 Extra foo = list.get(0);
42 assertEquals("Foo", foo.getName());
43 assertEquals("123", foo.getValue());
44
45 Extra bar = list.get(1);
46 assertEquals("Bar", bar.getName());
47 assertEquals("456", bar.getValue());
48
49 Extra item = list.get(2);
50 assertEquals("Item", item.getName());
51 assertEquals("789", item.getValue());
52
53 Extra immut = list.get(3);
54 assertEquals("aaa", immut.getName());
55 assertEquals("bbb", immut.getValue());
56 }
57
58 private class CustomConstructor extends SafeConstructor {
59 public CustomConstructor() {
60
61 String prefix = "!org.yaml.";
62 this.yamlMultiConstructors.put(prefix, new PrefixConstruct(prefix,
63 CustomConstructor.this));
64 this.yamlConstructors.put(null, new ConstructUnknown(CustomConstructor.this));
65 this.yamlConstructors.put(new Tag("!org.yaml.Exact"), new ExactConstruct(
66 CustomConstructor.this));
67 }
68 }
69
70
71
72
73 private class PrefixConstruct extends AbstractConstruct {
74 private String prefix;
75 private BaseConstructor con;
76
77 public PrefixConstruct(String prefix, BaseConstructor con) {
78 this.prefix = prefix;
79 this.con = con;
80 }
81
82 public Object construct(Node node) {
83 String suffix = node.getTag().getValue().substring(prefix.length());
84 return new Extra(suffix, con.constructScalar((ScalarNode) node).toString());
85 }
86 }
87
88
89
90
91 private class ExactConstruct extends AbstractConstruct {
92 private BaseConstructor con;
93
94 public ExactConstruct(BaseConstructor con) {
95 this.con = con;
96 }
97
98 public Object construct(Node node) {
99 return new Extra("Item", con.constructScalar((ScalarNode) node).toString());
100 }
101 }
102
103
104
105
106 private class ConstructUnknown extends AbstractConstruct {
107 private BaseConstructor con;
108
109 public ConstructUnknown(BaseConstructor con) {
110 this.con = con;
111 }
112
113 @SuppressWarnings("unchecked")
114 public Object construct(Node node) {
115 List<String> list = (List<String>) con.constructSequence((SequenceNode) node);
116 return new Extra(list.get(0), list.get(1));
117 }
118 }
119
120 private class Extra {
121 private String name;
122 private String value;
123
124 public Extra(String name, String value) {
125 this.name = name;
126 this.value = value;
127 }
128
129 public String getValue() {
130 return value;
131 }
132
133 public String getName() {
134 return name;
135 }
136 }
137 }