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