##### Coverage Report - org.yaml.snakeyaml.JavaBeanDumper

Classes in this File | Line Coverage | Branch Coverage | Complexity
JavaBeanDumper |
80% | 34/42
70% | 7/10
| 1.6363636363636365;1.636

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 | 3 |
         public JavaBeanDumper(boolean useGlobalTag, BeanAccess beanAccess) {
46 | 3 |
             this.useGlobalTag = useGlobalTag;
47 | 3 |
             this.beanAccess = beanAccess;
48 | 3 |
             this.flowStyle = FlowStyle.BLOCK;
49 | 3 |
         }
50 |  |
51 |  |
         public JavaBeanDumper(boolean useGlobalTag) {
52 | 0 |
             this(useGlobalTag, BeanAccess.DEFAULT);
53 | 0 |
         }
54 |  |
55 |  |
         public JavaBeanDumper(BeanAccess beanAccess) {
56 | 3 |
             this(false, beanAccess);
57 | 3 |
         }
58 |  |
59 |  |
         /**
60 |  |
          * Create Dumper for JavaBeans. Use "tag:yaml.org,2002:map" as the root tag.
61 |  |
          */
62 |  |
         public JavaBeanDumper() {
63 | 3 |
             this(BeanAccess.DEFAULT);
64 | 3 |
         }
65 |  |
66 | 2 |
         public JavaBeanDumper(Representer representer, DumperOptions options) {
67 | 2 |
             if (representer == null) {
68 | 1 |
                 throw new NullPointerException("Representer must be provided.");
69 |  |
             }
70 | 1 |
             if (options == null) {
71 | 1 |
                 throw new NullPointerException("DumperOptions must be provided.");
72 |  |
             }
73 | 0 |
             this.options = options;
74 | 0 |
             this.representer = representer;
75 | 0 |
             this.beanAccess = null; // bean access in not used if representer
76 |  |
             // supplied
77 | 0 |
         }
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 | 6 |
             if (this.options == null) {
90 | 6 |
                 doptions = new DumperOptions();
91 | 6 |
                 if (!useGlobalTag) {
92 | 5 |
                     doptions.setExplicitRoot(Tag.MAP);
93 |  |
                 }
94 | 6 |
                 doptions.setDefaultFlowStyle(flowStyle);
95 |  |
             } else {
96 | 0 |
                 doptions = this.options;
97 |  |
             }
98 |  |
             Representer repr;
99 | 6 |
             if (this.representer == null) {
100 | 6 |
                 repr = new Representer();
101 | 6 |
                 repr.getPropertyUtils().setBeanAccess(beanAccess);
102 |  |
             } else {
103 | 0 |
                 repr = this.representer;
104 |  |
             }
105 | 6 |
             Yaml dumper = new Yaml(repr, doptions);
106 | 6 |
             dumper.dump(data, output);
107 | 6 |
         }
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 | 4 |
             StringWriter buffer = new StringWriter();
118 | 4 |
             dump(data, buffer);
119 | 4 |
             return buffer.toString();
120 |  |
         }
121 |  |
122 |  |
         public boolean isUseGlobalTag() {
123 | 1 |
             return useGlobalTag;
124 |  |
         }
125 |  |
126 |  |
         public void setUseGlobalTag(boolean useGlobalTag) {
127 | 1 |
             this.useGlobalTag = useGlobalTag;
128 | 1 |
         }
129 |  |
130 |  |
         public FlowStyle getFlowStyle() {
131 | 1 |
             return flowStyle;
132 |  |
         }
133 |  |
134 |  |
         public void setFlowStyle(FlowStyle flowStyle) {
135 | 1 |
             this.flowStyle = flowStyle;
136 | 1 |
         }
137 |  |
     }