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