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