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