• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package examples;
15 
16 import java.util.List;
17 import java.util.Map;
18 import junit.framework.TestCase;
19 import org.yaml.snakeyaml.Yaml;
20 import org.yaml.snakeyaml.constructor.Constructor;
21 import org.yaml.snakeyaml.nodes.MappingNode;
22 import org.yaml.snakeyaml.nodes.NodeId;
23 
24 /**
25  * Example for http://code.google.com/p/snakeyaml/wiki/howto
26  */
27 public class SelectiveConstructorTest extends TestCase {
28 
29   class SelectiveConstructor extends Constructor {
30 
SelectiveConstructor()31     public SelectiveConstructor() {
32       // define a custom way to create a mapping node
33       yamlClassConstructors.put(NodeId.mapping, new MyPersistentObjectConstruct());
34     }
35 
36     class MyPersistentObjectConstruct extends Constructor.ConstructMapping {
37 
38       @Override
constructJavaBean2ndStep(MappingNode node, Object object)39       protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
40         Class<?> type = node.getType();
41         if (type.equals(MyPersistentObject.class)) {
42           // create a map
43           Map<Object, Object> map = constructMapping(node);
44           String id = (String) map.get("id");
45           return new MyPersistentObject(id, 17);
46         } else {
47           // create JavaBean
48           return super.constructJavaBean2ndStep(node, object);
49         }
50       }
51     }
52   }
53 
testConstructor()54   public void testConstructor() {
55     Yaml yaml = new Yaml(new SelectiveConstructor());
56     List<?> data =
57         yaml.load("- 1\n- 2\n- !!examples.MyPersistentObject {amount: 222, id: persistent}");
58     // System.out.println(data);
59     assertEquals(3, data.size());
60     MyPersistentObject myObject = (MyPersistentObject) data.get(2);
61     assertEquals(17, myObject.getAmount());
62     assertEquals("persistent", myObject.getId());
63   }
64 }
65 
66 
67 class MyPersistentObject {
68 
69   private String id;
70   private int amount;
71 
MyPersistentObject()72   public MyPersistentObject() {
73     this.id = "noid";
74     this.amount = 222;
75   }
76 
MyPersistentObject(String id, int amount)77   public MyPersistentObject(String id, int amount) {
78     this.id = id;
79     this.amount = amount;
80   }
81 
getId()82   public String getId() {
83     return id;
84   }
85 
setId(String id)86   public void setId(String id) {
87     this.id = id;
88   }
89 
getAmount()90   public int getAmount() {
91     return amount;
92   }
93 
setAmount(int amount)94   public void setAmount(int amount) {
95     this.amount = amount;
96   }
97 
98   @Override
toString()99   public String toString() {
100     return "MyPersistentObject [id=" + id + ", amount=" + amount + "]";
101   }
102 }
103