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;
    18
    19  import java.io.IOException;
    20  import java.util.List;
    21  import java.util.Map;
    22  import java.util.TreeMap;
    23
    24  import junit.framework.TestCase;
    25
    26  import org.yaml.snakeyaml.constructor.AbstractConstruct;
    27  import org.yaml.snakeyaml.constructor.Constructor;
    28  import org.yaml.snakeyaml.nodes.MappingNode;
    29  import org.yaml.snakeyaml.nodes.Node;
    30  import org.yaml.snakeyaml.nodes.SequenceNode;
    31  import org.yaml.snakeyaml.nodes.Tag;
    32  import org.yaml.snakeyaml.representer.Represent;
    33  import org.yaml.snakeyaml.representer.Representer;
    34
    35  /**
    36   * Test Example 2.24 from the YAML specification
    37   *
    38   * @author py4fun
    39   * @see http://yaml.org/spec/1.1/
    40   */
    41  public class Example2_24Test extends TestCase {
    42      class MyConstructor extends Constructor {
    43          public MyConstructor() {
    44              this.yamlConstructors.put(new Tag("tag:clarkevans.com,2002:shape"),
    45                      new ConstructShape());
    46              this.yamlConstructors.put(new Tag("tag:clarkevans.com,2002:circle"),
    47                      new ConstructCircle());
    48              this.yamlConstructors.put(new Tag("tag:clarkevans.com,2002:line"), new ConstructLine());
    49              this.yamlConstructors.put(new Tag("tag:clarkevans.com,2002:label"),
    50                      new ConstructLabel());
    51          }
    52
    53          private class ConstructShape extends AbstractConstruct {
    54              @SuppressWarnings("unchecked")
    55              public Object construct(Node node) {
    56                  SequenceNode snode = (SequenceNode) node;
    57                  List<Entity> values = (List<Entity>) constructSequence(snode);
    58                  Shape shape = new Shape(values);
    59                  return shape;
    60              }
    61          }
    62
    63          private class ConstructCircle extends AbstractConstruct {
    64              @SuppressWarnings("unchecked")
    65              public Object construct(Node node) {
    66                  MappingNode mnode = (MappingNode) node;
    67                  Map<Object, Object> values = constructMapping(mnode);
    68                  Circle circle = new Circle((Map<String, Integer>) values.get("center"),
    69                          (Integer) values.get("radius"));
    70                  return circle;
    71              }
    72          }
    73
    74          private class ConstructLine extends AbstractConstruct {
    75              @SuppressWarnings("unchecked")
    76              public Object construct(Node node) {
    77                  MappingNode mnode = (MappingNode) node;
    78                  Map<Object, Object> values = constructMapping(mnode);
    79                  Line line = new Line((Map<String, Integer>) values.get("start"),
    80                          (Map<String, Integer>) values.get("finish"));
    81                  return line;
    82              }
    83          }
    84
    85          private class ConstructLabel extends AbstractConstruct {
    86              @SuppressWarnings("unchecked")
    87              public Object construct(Node node) {
    88                  MappingNode mnode = (MappingNode) node;
    89                  Map<Object, Object> values = constructMapping(mnode);
    90                  Label label = new Label((Map<String, Integer>) values.get("start"),
    91                          (Integer) values.get("color"), (String) values.get("text"));
    92                  return label;
    93              }
    94          }
    95      }
    96
    97      class MyRepresenter extends Representer {
    98          public MyRepresenter() {
    99              this.representers.put(Shape.class, new RepresentShape());
    100             this.representers.put(Circle.class, new RepresentCircle());
    101             this.representers.put(Line.class, new RepresentLine());
    102             this.representers.put(Label.class, new RepresentLabel());
    103             this.representers.put(HexInteger.class, new RepresentHex());
    104         }
    105
    106         private class RepresentShape implements Represent {
    107             public Node representData(Object data) {
    108                 Shape shape = (Shape) data;
    109                 List<Entity> value = shape.getEntities();
    110                 return representSequence(new Tag("!shape"), value, Boolean.FALSE);
    111             }
    112         }
    113
    114         private class RepresentCircle implements Represent {
    115             public Node representData(Object data) {
    116                 Circle circle = (Circle) data;
    117                 Map<String, Object> map = new TreeMap<String, Object>();
    118                 map.put("center", circle.getCenter());
    119                 map.put("radius", circle.getRadius());
    120                 return representMapping(new Tag("!circle"), map, Boolean.FALSE);
    121             }
    122         }
    123
    124         private class RepresentLine implements Represent {
    125             public Node representData(Object data) {
    126                 Line line = (Line) data;
    127                 Map<String, Object> map = new TreeMap<String, Object>();
    128                 map.put("start", line.getStart());
    129                 map.put("finish", line.getFinish());
    130                 return representMapping(new Tag("!line"), map, Boolean.FALSE);
    131             }
    132         }
    133
    134         private class RepresentLabel implements Represent {
    135             public Node representData(Object data) {
    136                 Label label = (Label) data;
    137                 Map<String, Object> map = new TreeMap<String, Object>();
    138                 map.put("start", label.getStart());
    139                 map.put("color", new HexInteger(label.getColor()));
    140                 map.put("text", label.getText());
    141                 return representMapping(new Tag("!label"), map, Boolean.FALSE);
    142             }
    143         }
    144
    145         private class RepresentHex implements Represent {
    146             public Node representData(Object data) {
    147                 HexInteger hex = (HexInteger) data;
    148                 return representScalar(Tag.INT, "0x"
    149                         + Integer.toHexString(hex.getColor()).toUpperCase(), null);
    150             }
    151         }
    152     }
    153
    154     private class HexInteger {
    155         private Integer color;
    156
    157         public HexInteger(Integer color) {
    158             this.color = color;
    159         }
    160
    161         public Integer getColor() {
    162             return color;
    163         }
    164     }
    165
    166     private class Shape {
    167         private List<Entity> entities;
    168
    169         public List<Entity> getEntities() {
    170             return entities;
    171         }
    172
    173         public Shape(List<Entity> entities) {
    174             this.entities = entities;
    175         }
    176     }
    177
    178     private class Entity {
    179     }
    180
    181     private class Circle extends Entity {
    182         private Map<String, Integer> center;
    183         private Integer radius;
    184
    185         public Circle(Map<String, Integer> center, Integer radius) {
    186             this.center = center;
    187             this.radius = radius;
    188         }
    189
    190         public Map<String, Integer> getCenter() {
    191             return center;
    192         }
    193
    194         public Integer getRadius() {
    195             return radius;
    196         }
    197     }
    198
    199     private class Line extends Entity {
    200         private Map<String, Integer> start;
    201         private Map<String, Integer> finish;
    202
    203         public Line(Map<String, Integer> start, Map<String, Integer> finish) {
    204             this.start = start;
    205             this.finish = finish;
    206         }
    207
    208         public Map<String, Integer> getStart() {
    209             return start;
    210         }
    211
    212         public Map<String, Integer> getFinish() {
    213             return finish;
    214         }
    215     }
    216
    217     private class Label extends Entity {
    218         private Map<String, Integer> start;
    219         private Integer color;
    220         private String text;
    221
    222         public Label(Map<String, Integer> start, Integer color, String text) {
    223             this.start = start;
    224             this.color = color;
    225             this.text = text;
    226         }
    227
    228         public Map<String, Integer> getStart() {
    229             return start;
    230         }
    231
    232         public Integer getColor() {
    233             return color;
    234         }
    235
    236         public String getText() {
    237             return text;
    238         }
    239     }
    240
    241     public void testExample_2_24() throws IOException {
    242         Yaml yaml = new Yaml(new MyConstructor());
    243         Shape shape = (Shape) yaml.load(Util.getLocalResource("specification/example2_24.yaml"));
    244         assertNotNull(shape);
    245         yaml = new Yaml(new MyRepresenter());
    246         String output = yaml.dump(shape);
    247         String etalon = Util.getLocalResource("specification/example2_24_dumped.yaml");
    248         assertEquals(etalon, output);
    249     }
    250 }