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.io.IOException;
20  import java.util.List;
21  import java.util.Map;
22  
23  import junit.framework.TestCase;
24  
25  import org.yaml.snakeyaml.Yaml;
26  import org.yaml.snakeyaml.constructor.Constructor;
27  import org.yaml.snakeyaml.nodes.MappingNode;
28  import org.yaml.snakeyaml.nodes.NodeId;
29  
30  /**
31   * Example for http://code.google.com/p/snakeyaml/wiki/howto
32   */
33  public class SelectiveConstructorTest extends TestCase {
34      class SelectiveConstructor extends Constructor {
35          public SelectiveConstructor() {
36              // define a custom way to create a mapping node
37              yamlClassConstructors.put(NodeId.mapping, new MyPersistentObjectConstruct());
38          }
39  
40          class MyPersistentObjectConstruct extends Constructor.ConstructMapping {
41              @Override
42              protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
43                  Class<?> type = node.getType();
44                  if (type.equals(MyPersistentObject.class)) {
45                      // create a map
46                      Map<Object, Object> map = constructMapping(node);
47                      String id = (String) map.get("id");
48                      return new MyPersistentObject(id, 17);
49                  } else {
50                      // create JavaBean
51                      return super.constructJavaBean2ndStep(node, object);
52                  }
53              }
54          }
55      }
56  
57      public void testConstructor() throws IOException {
58          Yaml yaml = new Yaml(new SelectiveConstructor());
59          List<?> data = (List<?>) yaml
60                  .load("- 1\n- 2\n- !!examples.MyPersistentObject {amount: 222, id: persistent}");
61          // System.out.println(data);
62          assertEquals(3, data.size());
63          MyPersistentObject myObject = (MyPersistentObject) data.get(2);
64          assertEquals(17, myObject.getAmount());
65          assertEquals("persistent", myObject.getId());
66      }
67  }
68  
69  class MyPersistentObject {
70      private String id;
71      private int amount;
72  
73      public MyPersistentObject() {
74          this.id = "noid";
75          this.amount = 222;
76      }
77  
78      public MyPersistentObject(String id, int amount) {
79          this.id = id;
80          this.amount = amount;
81      }
82  
83      public String getId() {
84          return id;
85      }
86  
87      public void setId(String id) {
88          this.id = id;
89      }
90  
91      public int getAmount() {
92          return amount;
93      }
94  
95      public void setAmount(int amount) {
96          this.amount = amount;
97      }
98  
99      @Override
100     public String toString() {
101         return "MyPersistentObject [id=" + id + ", amount=" + amount + "]";
102     }
103 }