aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/yaml/snakeyaml/constructor/PrefixConstructorTest.java
blob: 6a6a5e0602490785e4c598f7c531e16d406bd2a2 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * Copyright (c) 2008, http://www.snakeyaml.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.yaml.snakeyaml.constructor;

import java.util.List;

import junit.framework.TestCase;

import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.SequenceNode;
import org.yaml.snakeyaml.nodes.Tag;

/**
 * Example to process a family of tags with the same prefix with one constructor
 * (PrefixConstruct)
 */
public class PrefixConstructorTest extends TestCase {

    @SuppressWarnings("unchecked")
    public void test1() {
        Yaml yaml = new Yaml(new CustomConstructor());
        String input = "- !org.yaml.Foo 123\n- !org.yaml.Bar 456\n- !org.yaml.Exact 789\n- !Immutable [aaa, bbb]";
        List<Extra> list = (List<Extra>) yaml.load(input);
        assertEquals(4, list.size());
        Extra foo = list.get(0);
        assertEquals("Foo", foo.getName());
        assertEquals("123", foo.getValue());
        //
        Extra bar = list.get(1);
        assertEquals("Bar", bar.getName());
        assertEquals("456", bar.getValue());
        //
        Extra item = list.get(2);
        assertEquals("Item", item.getName());
        assertEquals("789", item.getValue());
        //
        Extra immut = list.get(3);
        assertEquals("aaa", immut.getName());
        assertEquals("bbb", immut.getValue());
    }

    private class CustomConstructor extends SafeConstructor {
        public CustomConstructor() {
            // define tags which begin with !org.yaml.
            String prefix = "!org.yaml.";
            this.yamlMultiConstructors.put(prefix, new PrefixConstruct(prefix,
                    CustomConstructor.this));
            this.yamlConstructors.put(null, new ConstructUnknown(CustomConstructor.this));
            this.yamlConstructors.put(new Tag("!org.yaml.Exact"), new ExactConstruct(
                    CustomConstructor.this));
        }
    }

    /**
     * Process tags which start with '!org.yaml.'
     */
    private class PrefixConstruct extends AbstractConstruct {
        private String prefix;
        private BaseConstructor con;

        public PrefixConstruct(String prefix, BaseConstructor con) {
            this.prefix = prefix;
            this.con = con;
        }

        public Object construct(Node node) {
            String suffix = node.getTag().getValue().substring(prefix.length());
            return new Extra(suffix, con.constructScalar((ScalarNode) node).toString());
        }
    }

    /**
     * This has more priority then PrefixConstruct
     */
    private class ExactConstruct extends AbstractConstruct {
        private BaseConstructor con;

        public ExactConstruct(BaseConstructor con) {
            this.con = con;
        }

        public Object construct(Node node) {
            return new Extra("Item", con.constructScalar((ScalarNode) node).toString());
        }
    }

    /**
     * Process unrecognised tags
     */
    private class ConstructUnknown extends AbstractConstruct {
        private BaseConstructor con;

        public ConstructUnknown(BaseConstructor con) {
            this.con = con;
        }

        @SuppressWarnings("unchecked")
        public Object construct(Node node) {
            List<String> list = (List<String>) con.constructSequence((SequenceNode) node);
            return new Extra(list.get(0), list.get(1));
        }
    }

    private class Extra {
        private String name;
        private String value;

        public Extra(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }
}