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