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 org.yaml.snakeyaml.resolver;
18  
19  import java.awt.Point;
20  import java.util.ArrayList;
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.regex.Pattern;
25  
26  import junit.framework.TestCase;
27  
28  import org.yaml.snakeyaml.DumperOptions;
29  import org.yaml.snakeyaml.Yaml;
30  import org.yaml.snakeyaml.constructor.AbstractConstruct;
31  import org.yaml.snakeyaml.constructor.Constructor;
32  import org.yaml.snakeyaml.nodes.Node;
33  import org.yaml.snakeyaml.nodes.ScalarNode;
34  import org.yaml.snakeyaml.nodes.Tag;
35  import org.yaml.snakeyaml.representer.Represent;
36  import org.yaml.snakeyaml.representer.Representer;
37  
38  public class ResolverTest extends TestCase {
39  
40      @SuppressWarnings("unchecked")
41      public void testAddImplicitResolver() {
42          Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter());
43          Pattern regexp = Pattern.compile("\\d\\d-\\d\\d-\\d\\d\\d");
44          yaml.addImplicitResolver(new Tag(Tag.PREFIX + "Phone"), regexp, "0123456789");
45          Phone phone1 = new Phone("12-34-567");
46          Phone phone2 = new Phone("11-22-333");
47          Phone phone3 = new Phone("44-55-777");
48          List<Phone> etalonList = new ArrayList<Phone>();
49          etalonList.add(phone1);
50          etalonList.add(phone2);
51          etalonList.add(phone3);
52          String output = yaml.dump(etalonList);
53          assertEquals("[12-34-567, 11-22-333, 44-55-777]\n", output);
54          List<Phone> parsedList = (List<Phone>) yaml.load(output);
55          assertEquals(3, parsedList.size());
56          assertEquals(phone1, parsedList.get(0));
57          assertEquals(phone2, parsedList.get(1));
58          assertEquals(phone3, parsedList.get(2));
59          assertEquals(etalonList, parsedList);
60      }
61  
62      public void testAddImplicitResolver2() {
63          Yaml yaml = new Yaml(new PointRepresenter());
64          Pattern regexp = Pattern.compile("\\d\\d-\\d\\d-\\d\\d\\d");
65          yaml.addImplicitResolver(new Tag(Tag.PREFIX + "Phone"), regexp, "\0");
66          Pattern regexp2 = Pattern.compile("x\\d_y\\d");
67          // try any scalar, and not only those which start with 'x'
68          yaml.addImplicitResolver(new Tag(Tag.PREFIX + "Point"), regexp2, null);
69          Map<String, Object> map = new LinkedHashMap<String, Object>();
70          map.put("a", new Phone("12-34-567"));
71          map.put("b", new Point(1, 5));
72          String output = yaml.dump(map);
73          assertEquals("{a: 12-34-567, b: x1_y5}\n", output);
74      }
75  
76      class Phone {
77          private String number;
78  
79          public Phone(String n) {
80              this.number = n;
81          }
82  
83          public String getNumber() {
84              return number;
85          }
86  
87          @Override
88          public boolean equals(Object obj) {
89              if (!(obj instanceof Phone)) {
90                  return false;
91              }
92              return toString().equals(obj.toString());
93          }
94  
95          @Override
96          public String toString() {
97              return "Phone: " + number;
98          }
99      }
100 
101     class MyRepresenter extends Representer {
102         public MyRepresenter() {
103             this.representers.put(Phone.class, new RepresentPhone());
104         }
105 
106         private class RepresentPhone implements Represent {
107             public Node representData(Object data) {
108                 Phone phone = (Phone) data;
109                 String value = phone.getNumber();
110                 return representScalar(new Tag(Tag.PREFIX + "Phone"), value);
111             }
112         }
113     }
114 
115     class MyConstructor extends Constructor {
116         public MyConstructor() {
117             this.yamlConstructors.put(new Tag(Tag.PREFIX + "Phone"), new ConstructPhone());
118         }
119 
120         private class ConstructPhone extends AbstractConstruct {
121             public Object construct(Node node) {
122                 String val = (String) constructScalar((ScalarNode) node);
123                 return new Phone(val);
124             }
125         }
126     }
127 
128     class PointRepresenter extends Representer {
129         public PointRepresenter() {
130             this.representers.put(Point.class, new RepresentPoint());
131             this.representers.put(Phone.class, new RepresentPhone());
132         }
133 
134         private class RepresentPoint implements Represent {
135             public Node representData(Object data) {
136                 Point phone = (Point) data;
137                 String value = "x" + (int) phone.getX() + "_y" + (int) phone.getY();
138                 return representScalar(new Tag(Tag.PREFIX + "Point"), value);
139             }
140         }
141 
142         private class RepresentPhone implements Represent {
143             public Node representData(Object data) {
144                 Phone phone = (Phone) data;
145                 String value = phone.getNumber();
146                 return representScalar(new Tag(Tag.PREFIX + "Phone"), value);
147             }
148         }
149     }
150 
151     /**
152      * Parse scalars as Strings
153      */
154     @SuppressWarnings({ "unchecked", "deprecation" })
155     public void testStringResolver() {
156         Yaml yaml = new Yaml(new Constructor(), new Representer(), new DumperOptions(),
157                 new Resolver(false));
158         List<Object> output = (List<Object>) yaml.load("[ '1.00', 1.00, !!float '1.00' ]");
159         assertEquals("1.00", output.get(0));
160         assertEquals("1.00", output.get(1));
161         assertEquals(1.0, output.get(2));
162     }
163 }