AbstractSerializer.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.integrazione.utils.serializer;

  21. import org.openspcoop2.generic_project.exception.SerializerException;
  22. import org.openspcoop2.utils.beans.WriteToSerializerType;
  23. import org.openspcoop2.utils.xml.JaxbUtils;

  24. import org.openspcoop2.core.integrazione.EsitoRichiesta;

  25. import java.io.ByteArrayOutputStream;
  26. import java.io.FileOutputStream;
  27. import java.io.OutputStream;
  28. import java.io.File;
  29. import java.lang.reflect.Method;

  30. import javax.xml.bind.JAXBElement;

  31. /**    
  32.  * XML Serializer of beans
  33.  *
  34.  * @author Poli Andrea (poli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public abstract class AbstractSerializer {


  39.     protected abstract WriteToSerializerType getType();
  40.    
  41.     protected void _objToXml(OutputStream out, Class<?> c, Object object,
  42.             boolean prettyPrint) throws Exception {
  43.         if(object instanceof JAXBElement){
  44.             // solo per il tipo WriteToSerializerType.JAXB
  45.             JaxbUtils.objToXml(out, c, object, prettyPrint);
  46.         }else{
  47.             Method m = c.getMethod("writeTo", OutputStream.class, WriteToSerializerType.class);
  48.             m.invoke(object, out, this.getType());
  49.         }
  50.     }
  51.    
  52.     protected void objToXml(OutputStream out,Class<?> c,Object object,boolean prettyPrint) throws SerializerException{
  53.         try{
  54.             this._objToXml(out, c, object, prettyPrint);
  55.         }catch(Exception e){
  56.             throw new SerializerException(e.getMessage(), e);
  57.         }
  58.         finally{
  59.             try{
  60.                 out.flush();
  61.             }catch(Exception e){
  62.                 // ignore
  63.             }
  64.         }
  65.     }
  66.     protected void objToXml(String fileName,Class<?> c,Object object,boolean prettyPrint) throws SerializerException{
  67.         try{
  68.             this.objToXml(new File(fileName), c, object, prettyPrint);
  69.         }catch(Exception e){
  70.             throw new SerializerException(e.getMessage(), e);
  71.         }
  72.     }
  73.     protected void objToXml(File file,Class<?> c,Object object,boolean prettyPrint) throws SerializerException{
  74.         FileOutputStream fout = null;
  75.         try{
  76.             fout = new FileOutputStream(file);
  77.             this._objToXml(fout, c, object, prettyPrint);
  78.         }catch(Exception e){
  79.             throw new SerializerException(e.getMessage(), e);
  80.         }
  81.         finally{
  82.             try{
  83.                 if(fout!=null){
  84.                     fout.flush();
  85.                 }
  86.             }catch(Exception e){
  87.                 // ignore
  88.             }
  89.             try{
  90.                 if(fout!=null){
  91.                     fout.close();
  92.                 }
  93.             }catch(Exception e){
  94.                 // ignore
  95.             }
  96.         }
  97.     }
  98.     protected ByteArrayOutputStream objToXml(Class<?> c,Object object,boolean prettyPrint) throws SerializerException{
  99.         ByteArrayOutputStream bout = null;
  100.         try{
  101.             bout = new ByteArrayOutputStream();
  102.             this._objToXml(bout, c, object, prettyPrint);
  103.         }catch(Exception e){
  104.             throw new SerializerException(e.getMessage(), e);
  105.         }
  106.         finally{
  107.             try{
  108.                 if(bout!=null){
  109.                     bout.flush();
  110.                 }
  111.             }catch(Exception e){
  112.                 // ignore
  113.             }
  114.             try{
  115.                 if(bout!=null){
  116.                     bout.close();
  117.                 }
  118.             }catch(Exception e){
  119.                 // ignore
  120.             }
  121.         }
  122.         return bout;
  123.     }




  124.     /*
  125.      =================================================================================
  126.      Object: esito-richiesta
  127.      =================================================================================
  128.     */
  129.    
  130.     /**
  131.      * Serialize to file system in <var>fileName</var> the object <var>esitoRichiesta</var> of type {@link org.openspcoop2.core.integrazione.EsitoRichiesta}
  132.      *
  133.      * @param fileName Xml file to serialize the object <var>esitoRichiesta</var>
  134.      * @param esitoRichiesta Object to be serialized in xml file <var>fileName</var>
  135.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  136.      */
  137.     public void write(String fileName,EsitoRichiesta esitoRichiesta) throws SerializerException {
  138.         this.objToXml(fileName, EsitoRichiesta.class, esitoRichiesta, false);
  139.     }
  140.     /**
  141.      * Serialize to file system in <var>fileName</var> the object <var>esitoRichiesta</var> of type {@link org.openspcoop2.core.integrazione.EsitoRichiesta}
  142.      *
  143.      * @param fileName Xml file to serialize the object <var>esitoRichiesta</var>
  144.      * @param esitoRichiesta Object to be serialized in xml file <var>fileName</var>
  145.      * @param prettyPrint if true output the XML with indenting
  146.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  147.      */
  148.     public void write(String fileName,EsitoRichiesta esitoRichiesta,boolean prettyPrint) throws SerializerException {
  149.         this.objToXml(fileName, EsitoRichiesta.class, esitoRichiesta, prettyPrint);
  150.     }
  151.    
  152.     /**
  153.      * Serialize to file system in <var>file</var> the object <var>esitoRichiesta</var> of type {@link org.openspcoop2.core.integrazione.EsitoRichiesta}
  154.      *
  155.      * @param file Xml file to serialize the object <var>esitoRichiesta</var>
  156.      * @param esitoRichiesta Object to be serialized in xml file <var>fileName</var>
  157.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  158.      */
  159.     public void write(File file,EsitoRichiesta esitoRichiesta) throws SerializerException {
  160.         this.objToXml(file, EsitoRichiesta.class, esitoRichiesta, false);
  161.     }
  162.     /**
  163.      * Serialize to file system in <var>file</var> the object <var>esitoRichiesta</var> of type {@link org.openspcoop2.core.integrazione.EsitoRichiesta}
  164.      *
  165.      * @param file Xml file to serialize the object <var>esitoRichiesta</var>
  166.      * @param esitoRichiesta Object to be serialized in xml file <var>fileName</var>
  167.      * @param prettyPrint if true output the XML with indenting
  168.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  169.      */
  170.     public void write(File file,EsitoRichiesta esitoRichiesta,boolean prettyPrint) throws SerializerException {
  171.         this.objToXml(file, EsitoRichiesta.class, esitoRichiesta, prettyPrint);
  172.     }
  173.    
  174.     /**
  175.      * Serialize to output stream <var>out</var> the object <var>esitoRichiesta</var> of type {@link org.openspcoop2.core.integrazione.EsitoRichiesta}
  176.      *
  177.      * @param out OutputStream to serialize the object <var>esitoRichiesta</var>
  178.      * @param esitoRichiesta Object to be serialized in xml file <var>fileName</var>
  179.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  180.      */
  181.     public void write(OutputStream out,EsitoRichiesta esitoRichiesta) throws SerializerException {
  182.         this.objToXml(out, EsitoRichiesta.class, esitoRichiesta, false);
  183.     }
  184.     /**
  185.      * Serialize to output stream <var>out</var> the object <var>esitoRichiesta</var> of type {@link org.openspcoop2.core.integrazione.EsitoRichiesta}
  186.      *
  187.      * @param out OutputStream to serialize the object <var>esitoRichiesta</var>
  188.      * @param esitoRichiesta Object to be serialized in xml file <var>fileName</var>
  189.      * @param prettyPrint if true output the XML with indenting
  190.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  191.      */
  192.     public void write(OutputStream out,EsitoRichiesta esitoRichiesta,boolean prettyPrint) throws SerializerException {
  193.         this.objToXml(out, EsitoRichiesta.class, esitoRichiesta, prettyPrint);
  194.     }
  195.            
  196.     /**
  197.      * Serialize to byte array the object <var>esitoRichiesta</var> of type {@link org.openspcoop2.core.integrazione.EsitoRichiesta}
  198.      *
  199.      * @param esitoRichiesta Object to be serialized
  200.      * @return Object to be serialized in byte array
  201.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  202.      */
  203.     public byte[] toByteArray(EsitoRichiesta esitoRichiesta) throws SerializerException {
  204.         return this.objToXml(EsitoRichiesta.class, esitoRichiesta, false).toByteArray();
  205.     }
  206.     /**
  207.      * Serialize to byte array the object <var>esitoRichiesta</var> of type {@link org.openspcoop2.core.integrazione.EsitoRichiesta}
  208.      *
  209.      * @param esitoRichiesta Object to be serialized
  210.      * @param prettyPrint if true output the XML with indenting
  211.      * @return Object to be serialized in byte array
  212.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  213.      */
  214.     public byte[] toByteArray(EsitoRichiesta esitoRichiesta,boolean prettyPrint) throws SerializerException {
  215.         return this.objToXml(EsitoRichiesta.class, esitoRichiesta, prettyPrint).toByteArray();
  216.     }
  217.    
  218.     /**
  219.      * Serialize to String the object <var>esitoRichiesta</var> of type {@link org.openspcoop2.core.integrazione.EsitoRichiesta}
  220.      *
  221.      * @param esitoRichiesta Object to be serialized
  222.      * @return Object to be serialized as String
  223.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  224.      */
  225.     public String toString(EsitoRichiesta esitoRichiesta) throws SerializerException {
  226.         return this.objToXml(EsitoRichiesta.class, esitoRichiesta, false).toString();
  227.     }
  228.     /**
  229.      * Serialize to String the object <var>esitoRichiesta</var> of type {@link org.openspcoop2.core.integrazione.EsitoRichiesta}
  230.      *
  231.      * @param esitoRichiesta Object to be serialized
  232.      * @param prettyPrint if true output the XML with indenting
  233.      * @return Object to be serialized as String
  234.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  235.      */
  236.     public String toString(EsitoRichiesta esitoRichiesta,boolean prettyPrint) throws SerializerException {
  237.         return this.objToXml(EsitoRichiesta.class, esitoRichiesta, prettyPrint).toString();
  238.     }
  239.    
  240.    
  241.    

  242. }