View Javadoc

1   /**
2    * Copyright (c) 2008-2012, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Example to process a family of tags with the same prefix with one constructor
30   * (PrefixConstruct)
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              // define tags which begin with !org.yaml.
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       * Process tags which start with '!org.yaml.'
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       * This has more priority then PrefixConstruct
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      * Process unrecognised tags
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 }