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