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.math.BigDecimal;
20  import java.util.regex.Pattern;
21  
22  import junit.framework.TestCase;
23  
24  import org.yaml.snakeyaml.Yaml;
25  import org.yaml.snakeyaml.constructor.Constructor;
26  import org.yaml.snakeyaml.nodes.Node;
27  import org.yaml.snakeyaml.nodes.NodeId;
28  import org.yaml.snakeyaml.nodes.ScalarNode;
29  
30  /**
31   * http://code.google.com/p/snakeyaml/issues/detail?id=75
32   */
33  public class CustomBeanResolverTest extends TestCase {
34      private final Pattern CUSTOM_PATTERN = Pattern.compile("\\d+%");
35  
36      public void testOnlyBigDecimal() {
37          Yaml yaml = new Yaml(new BigBeanConstructor());
38          Foo foo = (Foo) yaml.load("bar: 50\nbaz: 35%\nbas: 1250");
39          assertEquals(50.0, foo.bar);
40          assertEquals("0.35", foo.baz.toString());
41          assertEquals("1250", foo.bas);
42      }
43  
44      public void testPrimitive() {
45          Yaml yaml = new Yaml(new BigBeanConstructor());
46          Foo foo = (Foo) yaml.load("bar: 50%\nbaz: 35%\nbas: 1250%\nbaw: 35");
47          assertEquals(0.5, foo.bar);
48          assertEquals("0.35", foo.baz.toString());
49          assertEquals("1250%", foo.bas);
50          assertEquals("35", foo.baw.toString());
51      }
52  
53      class BigBeanConstructor extends Constructor {
54          public BigBeanConstructor() {
55              super(Foo.class);
56              yamlClassConstructors.put(NodeId.scalar, new ConstructBig());
57          }
58  
59          private class ConstructBig extends ConstructScalar {
60              public Object construct(Node node) {
61                  if (node.getType().equals(BigDecimal.class)) {
62                      String val = (String) constructScalar((ScalarNode) node);
63                      if (CUSTOM_PATTERN.matcher(val).matches()) {
64                          return new BigDecimal(val.substring(0, val.length() - 1))
65                                  .divide(new BigDecimal(100));
66                      }
67                  } else if (node.getType().isAssignableFrom(double.class)) {
68                      String val = (String) constructScalar((ScalarNode) node);
69                      if (CUSTOM_PATTERN.matcher(val).matches()) {
70                          return new Double(val.substring(0, val.length() - 1)) / 100;
71                      }
72                  }
73                  return super.construct(node);
74              }
75          }
76      }
77  
78      public static class Foo {
79          public double bar = 0;
80          public BigDecimal baz;
81          public BigDecimal baw;
82          public String bas;
83      }
84  }