View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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  package org.yaml.snakeyaml.immutable;
17  
18  import java.awt.Insets;
19  import java.awt.Rectangle;
20  import java.util.Arrays;
21  
22  import javax.swing.border.MatteBorder;
23  
24  import org.yaml.snakeyaml.nodes.Node;
25  import org.yaml.snakeyaml.nodes.Tag;
26  import org.yaml.snakeyaml.representer.Represent;
27  import org.yaml.snakeyaml.representer.Representer;
28  
29  public class ImmutablesRepresenter extends Representer {
30  
31      public ImmutablesRepresenter() {
32          super();
33          this.representers.put(java.awt.Color.class, new RepresentColor());
34          this.representers.put(Insets.class, new RepresentInsets());
35          this.representers.put(MatteBorder.class, new RepresentMatteBorder());
36          this.representers.put(Rectangle.class, new RepresentRectangle());
37      }
38  
39      class RepresentInsets implements Represent {
40  
41          public Node representData(Object data) {
42              Insets insets = (Insets) data;
43              return representSequence(
44                      getTag(data.getClass(), new Tag(data.getClass())),
45                      Arrays.asList(new Object[] { insets.top, insets.left, insets.bottom,
46                              insets.right }), true);
47          }
48  
49      }
50  
51      class RepresentRectangle implements Represent {
52  
53          public Node representData(Object data) {
54              Rectangle rect = (Rectangle) data;
55              return representSequence(getTag(data.getClass(), new Tag(data.getClass())),
56                      Arrays.asList(new Object[] { rect.x, rect.y, rect.width, rect.height }), true);
57          }
58  
59      }
60  
61      class RepresentMatteBorder implements Represent {
62  
63          public Node representData(Object data) {
64              MatteBorder mb = (MatteBorder) data;
65              return representSequence(getTag(data.getClass(), new Tag(data.getClass())),
66                      Arrays.asList(new Object[] { mb.getBorderInsets(), mb.getMatteColor() }), true);
67          }
68  
69      }
70  
71      class RepresentColor implements Represent {
72  
73          public Node representData(Object data) {
74              java.awt.Color color = (java.awt.Color) data;
75              return representSequence(
76                      getTag(data.getClass(), new Tag(data.getClass())),
77                      Arrays.asList(new Integer[] { color.getRed(), color.getGreen(),
78                              color.getBlue(), color.getAlpha() }), true);
79          }
80  
81      }
82  }