Coverage Report - net.sf.practicalxml.converter.BeanConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanConverter
80%
4/5
N/A
1
 
 1  
 // Copyright 2008-2014 severally by the contributors
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package net.sf.practicalxml.converter;
 16  
 
 17  
 import org.w3c.dom.Document;
 18  
 import org.w3c.dom.Element;
 19  
 
 20  
 import net.sf.practicalxml.converter.bean.Bean2XmlConverter;
 21  
 import net.sf.practicalxml.converter.bean.Bean2XmlOptions;
 22  
 import net.sf.practicalxml.converter.bean.Xml2BeanConverter;
 23  
 import net.sf.practicalxml.converter.bean.Xml2BeanOptions;
 24  
 
 25  
 
 26  
 /**
 27  
  *  Converts objects that follow the bean specification to or from an XML
 28  
  *  representation. This class provides static facade methods for
 29  
  *  {@link net.sf.practicalxml.converter.bean.Bean2XmlConverter} and
 30  
  *  {@link net.sf.practicalxml.converter.bean.Xml2BeanConverter}. If static
 31  
  *  methods and throwaway objects offend you then use those classes directly.
 32  
  *  <p>
 33  
  *  <em>Note:</em>
 34  
  *  These conversions are intended for application-specific data transfer objects.
 35  
  *  All object access is via public getters and setters; private members are
 36  
  *  ignored. There are known issues with some JDK classes; those most likely to be
 37  
  *  used in a DTO are supported via hacks.
 38  
  *
 39  
  *  @since 1.1
 40  
  */
 41  0
 public class BeanConverter
 42  
 {
 43  
     /**
 44  
      *  Creates a new DOM document from the passed bean, in which all elements
 45  
      *  are members of the specified namespace and will inherit the root's
 46  
      *  prefix (if any).
 47  
      *
 48  
      *   @param bean        The source object. This can be any Java object:
 49  
      *                      bean, collection, or simple type.
 50  
      *   @param nsUri       The namespace of the root element. This will be
 51  
      *                      inherited by all child elements.
 52  
      *   @param rootName    The qualified name given to the root element of the
 53  
      *                      generated document. If a qualified name, all child
 54  
      *                      elements will inherit its prefix.
 55  
      *   @param options     Conversion options.
 56  
      */
 57  
     public static Document convertToXml(
 58  
             Object bean, String nsUri, String rootName, Bean2XmlOptions... options)
 59  
     {
 60  1
         return new Bean2XmlConverter(options)
 61  
                .convert(bean, nsUri, rootName)
 62  
                .getOwnerDocument();
 63  
     }
 64  
 
 65  
 
 66  
     /**
 67  
      *  Creates a new DOM document from the passed bean, without namespace.
 68  
      *
 69  
      *   @param bean        The source object. This can be any Java object:
 70  
      *                      bean, collection, or simple type.
 71  
      *   @param rootName    The name given to the root element of the produced
 72  
      *                      document.
 73  
      *   @param options     Conversion options.
 74  
      */
 75  
     public static Document convertToXml(
 76  
             Object bean, String rootName, Bean2XmlOptions... options)
 77  
     {
 78  50
         return new Bean2XmlConverter(options)
 79  
                .convert(bean, rootName)
 80  
                .getOwnerDocument();
 81  
     }
 82  
 
 83  
 
 84  
     /**
 85  
      *  Creates a new Java object from the root of the passed <code>Document
 86  
      *  </code>.
 87  
      *
 88  
      *   @param dom         The source document.
 89  
      *   @param klass       The desired class to instantiate and fill from this
 90  
      *                      document.
 91  
      *   @param options     Conversion options.
 92  
      */
 93  
     public static <T> T convertToJava(
 94  
             Document dom, Class<T> klass, Xml2BeanOptions... options)
 95  
     {
 96  51
         return convertToJava(dom.getDocumentElement(), klass, options);
 97  
     }
 98  
 
 99  
 
 100  
     /**
 101  
      *  Creates a new Java object from the the passed <code>Element</code>.
 102  
      *  This is useful when a DOM contains a tree of objects and you just
 103  
      *  want to convert one of them.
 104  
      *
 105  
      *   @param root        The source element -- this may or may not be the
 106  
      *                      root element of its document.
 107  
      *   @param klass       The desired class to instantiate and fill from this
 108  
      *                      document.
 109  
      *   @param options     Conversion options.
 110  
      */
 111  
     public static <T> T convertToJava(
 112  
             Element root, Class<T> klass, Xml2BeanOptions... options)
 113  
     {
 114  51
         return new Xml2BeanConverter(options).convert(root, klass);
 115  
     }
 116  
 }