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;
17  
18  import java.io.StringWriter;
19  import java.io.Writer;
20  
21  import org.yaml.snakeyaml.DumperOptions.FlowStyle;
22  import org.yaml.snakeyaml.introspector.BeanAccess;
23  import org.yaml.snakeyaml.nodes.Tag;
24  import org.yaml.snakeyaml.representer.Representer;
25  
26  /**
27   * Convenience utility to serialize JavaBeans.
28   * 
29   * @deprecated use Yaml.dumpAs(data, Tag.MAP) instead
30   */
31  public class JavaBeanDumper {
32      private boolean useGlobalTag;
33      private FlowStyle flowStyle;
34      private DumperOptions options;
35      private Representer representer;
36      private final BeanAccess beanAccess;
37  
38      /**
39       * Create Dumper for JavaBeans
40       * 
41       * @param useGlobalTag
42       *            true to emit the global tag with the class name
43       */
44      public JavaBeanDumper(boolean useGlobalTag, BeanAccess beanAccess) {
45          this.useGlobalTag = useGlobalTag;
46          this.beanAccess = beanAccess;
47          this.flowStyle = FlowStyle.BLOCK;
48      }
49  
50      public JavaBeanDumper(boolean useGlobalTag) {
51          this(useGlobalTag, BeanAccess.DEFAULT);
52      }
53  
54      public JavaBeanDumper(BeanAccess beanAccess) {
55          this(false, beanAccess);
56      }
57  
58      /**
59       * Create Dumper for JavaBeans. Use "tag:yaml.org,2002:map" as the root tag.
60       */
61      public JavaBeanDumper() {
62          this(BeanAccess.DEFAULT);
63      }
64  
65      public JavaBeanDumper(Representer representer, DumperOptions options) {
66          if (representer == null) {
67              throw new NullPointerException("Representer must be provided.");
68          }
69          if (options == null) {
70              throw new NullPointerException("DumperOptions must be provided.");
71          }
72          this.options = options;
73          this.representer = representer;
74          this.beanAccess = null; // bean access in not used if representer
75          // supplied
76      }
77  
78      /**
79       * Serialize JavaBean
80       * 
81       * @param data
82       *            JavaBean instance to serialize
83       * @param output
84       *            destination
85       */
86      public void dump(Object data, Writer output) {
87          DumperOptions doptions;
88          if (this.options == null) {
89              doptions = new DumperOptions();
90              if (!useGlobalTag) {
91                  doptions.setExplicitRoot(Tag.MAP);
92              }
93              doptions.setDefaultFlowStyle(flowStyle);
94          } else {
95              doptions = this.options;
96          }
97          Representer repr;
98          if (this.representer == null) {
99              repr = new Representer();
100             repr.getPropertyUtils().setBeanAccess(beanAccess);
101         } else {
102             repr = this.representer;
103         }
104         Yaml dumper = new Yaml(repr, doptions);
105         dumper.dump(data, output);
106     }
107 
108     /**
109      * Serialize JavaBean
110      * 
111      * @param data
112      *            JavaBean instance to serialize
113      * @return serialized YAML document
114      */
115     public String dump(Object data) {
116         StringWriter buffer = new StringWriter();
117         dump(data, buffer);
118         return buffer.toString();
119     }
120 
121     public boolean isUseGlobalTag() {
122         return useGlobalTag;
123     }
124 
125     public void setUseGlobalTag(boolean useGlobalTag) {
126         this.useGlobalTag = useGlobalTag;
127     }
128 
129     public FlowStyle getFlowStyle() {
130         return flowStyle;
131     }
132 
133     public void setFlowStyle(FlowStyle flowStyle) {
134         this.flowStyle = flowStyle;
135     }
136 }