View Javadoc

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