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 examples;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.Util;
24  import org.yaml.snakeyaml.Yaml;
25  import org.yaml.snakeyaml.constructor.AbstractConstruct;
26  import org.yaml.snakeyaml.constructor.Construct;
27  import org.yaml.snakeyaml.constructor.Constructor;
28  import org.yaml.snakeyaml.error.YAMLException;
29  import org.yaml.snakeyaml.nodes.Node;
30  import org.yaml.snakeyaml.nodes.Tag;
31  
32  public class IgnoreTagsExampleTest extends TestCase {
33      @SuppressWarnings("unchecked")
34      public void testLoad() {
35          String input = Util.getLocalResource("examples/unknown-tags-example.yaml");
36          // System.out.println(input);
37          Yaml yaml = new Yaml(new MyConstructor());
38          Map<String, Object> result = (Map<String, Object>) yaml.load(input);
39          // Check the result
40          assertNotNull(result);
41          assertEquals(3, result.size());
42          assertEquals("123", result.get("aaa"));
43          //
44          List<Object> bbb = (List<Object>) result.get("bbb");
45          assertEquals(2, bbb.size());
46          assertEquals(new Integer(111), bbb.get(0));
47          assertEquals("ddd", bbb.get(1));
48          //
49          Map<String, Object> ccc = (Map<String, Object>) result.get("ccc");
50          assertEquals(2, ccc.size());
51          assertEquals(1.0, ccc.get("x"));
52          assertEquals(3.1416, ccc.get("y"));
53      }
54  
55      private class MyConstructor extends Constructor {
56          private Construct original;
57  
58          public MyConstructor() {
59              original = this.yamlConstructors.get(null);
60              this.yamlConstructors.put(null, new IgnoringConstruct());
61          }
62  
63          private class IgnoringConstruct extends AbstractConstruct {
64              public Object construct(Node node) {
65                  if (node.getTag().startsWith("!KnownTag")) {
66                      return original.construct(node);
67                  } else {
68                      switch (node.getNodeId()) {
69                      case scalar:
70                          return yamlConstructors.get(Tag.STR).construct(node);
71                      case sequence:
72                          return yamlConstructors.get(Tag.SEQ).construct(node);
73                      case mapping:
74                          return yamlConstructors.get(Tag.MAP).construct(node);
75                      default:
76                          throw new YAMLException("Unexpected node");
77                      }
78                  }
79              }
80          }
81      }
82  }