JaxbSerializer.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.core.commons.search.utils.serializer;

  21. import java.io.File;
  22. import java.io.OutputStream;

  23. import org.openspcoop2.utils.beans.WriteToSerializerType;
  24. import org.openspcoop2.generic_project.exception.SerializerException;

  25. import javax.xml.bind.JAXBElement;

  26. /**    
  27.  * XML Serializer of beans with jaxb
  28.  *
  29.  * @author Poli Andrea (poli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class JaxbSerializer extends AbstractSerializer {

  34.     @Override
  35.     protected WriteToSerializerType getType(){
  36.         return WriteToSerializerType.XML_JAXB;
  37.     }
  38.    
  39.    
  40.     /**
  41.      * Serialize to file system in <var>fileName</var> the object <var>jaxbElement</var> of type {@link javax.xml.bind.JAXBElement}
  42.      *
  43.      * @param fileName Xml file to serialize the object <var>jaxbElement</var>
  44.      * @param jaxbElement Object to be serialized in xml file <var>fileName</var>
  45.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  46.      */
  47.     public <T> void write(String fileName,JAXBElement<T> jaxbElement) throws SerializerException {
  48.         this.objToXml(fileName, jaxbElement.getValue().getClass(), jaxbElement, false);
  49.     }
  50.     /**
  51.      * Serialize to file system in <var>fileName</var> the object <var>jaxbElement</var> of type {@link javax.xml.bind.JAXBElement}
  52.      *
  53.      * @param fileName Xml file to serialize the object <var>jaxbElement</var>
  54.      * @param jaxbElement Object to be serialized in xml file <var>fileName</var>
  55.      * @param prettyPrint if true output the XML with indenting
  56.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  57.      */
  58.     public <T> void write(String fileName,JAXBElement<T> jaxbElement,boolean prettyPrint) throws SerializerException {
  59.         this.objToXml(fileName, jaxbElement.getValue().getClass(), jaxbElement, prettyPrint);
  60.     }
  61.    
  62.     /**
  63.      * Serialize to file system in <var>file</var> the object <var>jaxbElement</var> of type {@link javax.xml.bind.JAXBElement}
  64.      *
  65.      * @param file Xml file to serialize the object <var>jaxbElement</var>
  66.      * @param jaxbElement Object to be serialized in xml file <var>fileName</var>
  67.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  68.      */
  69.     public <T> void write(File file,JAXBElement<T> jaxbElement) throws SerializerException {
  70.         this.objToXml(file, jaxbElement.getValue().getClass(), jaxbElement, false);
  71.     }
  72.     /**
  73.      * Serialize to file system in <var>file</var> the object <var>jaxbElement</var> of type {@link javax.xml.bind.JAXBElement}
  74.      *
  75.      * @param file Xml file to serialize the object <var>jaxbElement</var>
  76.      * @param jaxbElement Object to be serialized in xml file <var>fileName</var>
  77.      * @param prettyPrint if true output the XML with indenting
  78.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  79.      */
  80.     public <T> void write(File file,JAXBElement<T> jaxbElement,boolean prettyPrint) throws SerializerException {
  81.         this.objToXml(file, jaxbElement.getValue().getClass(), jaxbElement, prettyPrint);
  82.     }
  83.    
  84.     /**
  85.      * Serialize to output stream <var>out</var> the object <var>jaxbElement</var> of type {@link javax.xml.bind.JAXBElement}
  86.      *
  87.      * @param out OutputStream to serialize the object <var>jaxbElement</var>
  88.      * @param jaxbElement Object to be serialized in xml file <var>fileName</var>
  89.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  90.      */
  91.     public <T> void write(OutputStream out,JAXBElement<T> jaxbElement) throws SerializerException {
  92.         this.objToXml(out, jaxbElement.getValue().getClass(), jaxbElement, false);
  93.     }
  94.     /**
  95.      * Serialize to output stream <var>out</var> the object <var>jaxbElement</var> of type {@link javax.xml.bind.JAXBElement}
  96.      *
  97.      * @param out OutputStream to serialize the object <var>jaxbElement</var>
  98.      * @param jaxbElement Object to be serialized in xml file <var>fileName</var>
  99.      * @param prettyPrint if true output the XML with indenting
  100.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  101.      */
  102.     public <T> void write(OutputStream out,JAXBElement<T> jaxbElement,boolean prettyPrint) throws SerializerException {
  103.         this.objToXml(out, jaxbElement.getValue().getClass(), jaxbElement, prettyPrint);
  104.     }
  105.            
  106.     /**
  107.      * Serialize to byte array the object <var>jaxbElement</var> of type {@link javax.xml.bind.JAXBElement}
  108.      *
  109.      * @param jaxbElement Object to be serialized
  110.      * @return Object to be serialized in byte array
  111.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  112.      */
  113.     public <T> byte[] toByteArray(JAXBElement<T> jaxbElement) throws SerializerException {
  114.         return this.objToXml(jaxbElement.getValue().getClass(), jaxbElement, false).toByteArray();
  115.     }
  116.     /**
  117.      * Serialize to byte array the object <var>jaxbElement</var> of type {@link javax.xml.bind.JAXBElement}
  118.      *
  119.      * @param jaxbElement Object to be serialized
  120.      * @param prettyPrint if true output the XML with indenting
  121.      * @return Object to be serialized in byte array
  122.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  123.      */
  124.     public <T> byte[] toByteArray(JAXBElement<T> jaxbElement,boolean prettyPrint) throws SerializerException {
  125.         return this.objToXml(jaxbElement.getValue().getClass(), jaxbElement, prettyPrint).toByteArray();
  126.     }
  127.    
  128.     /**
  129.      * Serialize to String the object <var>jaxbElement</var> of type {@link javax.xml.bind.JAXBElement}
  130.      *
  131.      * @param jaxbElement Object to be serialized
  132.      * @return Object to be serialized as String
  133.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  134.      */
  135.     public <T> String toString(JAXBElement<T> jaxbElement) throws SerializerException {
  136.         return this.objToXml(jaxbElement.getValue().getClass(), jaxbElement, false).toString();
  137.     }
  138.     /**
  139.      * Serialize to String the object <var>jaxbElement</var> of type {@link javax.xml.bind.JAXBElement}
  140.      *
  141.      * @param jaxbElement Object to be serialized
  142.      * @param prettyPrint if true output the XML with indenting
  143.      * @return Object to be serialized as String
  144.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  145.      */
  146.     public <T> String toString(JAXBElement<T> jaxbElement,boolean prettyPrint) throws SerializerException {
  147.         return this.objToXml(jaxbElement.getValue().getClass(), jaxbElement, prettyPrint).toString();
  148.     }  
  149. }