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.eventi.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.eventi.Evento;
  25. import org.openspcoop2.core.eventi.DatiEventoGenerico;

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

  31. import javax.xml.bind.JAXBElement;

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


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




  125.     /*
  126.      =================================================================================
  127.      Object: evento
  128.      =================================================================================
  129.     */
  130.    
  131.     /**
  132.      * Serialize to file system in <var>fileName</var> the object <var>evento</var> of type {@link org.openspcoop2.core.eventi.Evento}
  133.      *
  134.      * @param fileName Xml file to serialize the object <var>evento</var>
  135.      * @param evento Object to be serialized in xml file <var>fileName</var>
  136.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  137.      */
  138.     public void write(String fileName,Evento evento) throws SerializerException {
  139.         this.objToXml(fileName, Evento.class, evento, false);
  140.     }
  141.     /**
  142.      * Serialize to file system in <var>fileName</var> the object <var>evento</var> of type {@link org.openspcoop2.core.eventi.Evento}
  143.      *
  144.      * @param fileName Xml file to serialize the object <var>evento</var>
  145.      * @param evento Object to be serialized in xml file <var>fileName</var>
  146.      * @param prettyPrint if true output the XML with indenting
  147.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  148.      */
  149.     public void write(String fileName,Evento evento,boolean prettyPrint) throws SerializerException {
  150.         this.objToXml(fileName, Evento.class, evento, prettyPrint);
  151.     }
  152.    
  153.     /**
  154.      * Serialize to file system in <var>file</var> the object <var>evento</var> of type {@link org.openspcoop2.core.eventi.Evento}
  155.      *
  156.      * @param file Xml file to serialize the object <var>evento</var>
  157.      * @param evento Object to be serialized in xml file <var>fileName</var>
  158.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  159.      */
  160.     public void write(File file,Evento evento) throws SerializerException {
  161.         this.objToXml(file, Evento.class, evento, false);
  162.     }
  163.     /**
  164.      * Serialize to file system in <var>file</var> the object <var>evento</var> of type {@link org.openspcoop2.core.eventi.Evento}
  165.      *
  166.      * @param file Xml file to serialize the object <var>evento</var>
  167.      * @param evento Object to be serialized in xml file <var>fileName</var>
  168.      * @param prettyPrint if true output the XML with indenting
  169.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  170.      */
  171.     public void write(File file,Evento evento,boolean prettyPrint) throws SerializerException {
  172.         this.objToXml(file, Evento.class, evento, prettyPrint);
  173.     }
  174.    
  175.     /**
  176.      * Serialize to output stream <var>out</var> the object <var>evento</var> of type {@link org.openspcoop2.core.eventi.Evento}
  177.      *
  178.      * @param out OutputStream to serialize the object <var>evento</var>
  179.      * @param evento Object to be serialized in xml file <var>fileName</var>
  180.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  181.      */
  182.     public void write(OutputStream out,Evento evento) throws SerializerException {
  183.         this.objToXml(out, Evento.class, evento, false);
  184.     }
  185.     /**
  186.      * Serialize to output stream <var>out</var> the object <var>evento</var> of type {@link org.openspcoop2.core.eventi.Evento}
  187.      *
  188.      * @param out OutputStream to serialize the object <var>evento</var>
  189.      * @param evento Object to be serialized in xml file <var>fileName</var>
  190.      * @param prettyPrint if true output the XML with indenting
  191.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  192.      */
  193.     public void write(OutputStream out,Evento evento,boolean prettyPrint) throws SerializerException {
  194.         this.objToXml(out, Evento.class, evento, prettyPrint);
  195.     }
  196.            
  197.     /**
  198.      * Serialize to byte array the object <var>evento</var> of type {@link org.openspcoop2.core.eventi.Evento}
  199.      *
  200.      * @param evento Object to be serialized
  201.      * @return Object to be serialized in byte array
  202.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  203.      */
  204.     public byte[] toByteArray(Evento evento) throws SerializerException {
  205.         return this.objToXml(Evento.class, evento, false).toByteArray();
  206.     }
  207.     /**
  208.      * Serialize to byte array the object <var>evento</var> of type {@link org.openspcoop2.core.eventi.Evento}
  209.      *
  210.      * @param evento Object to be serialized
  211.      * @param prettyPrint if true output the XML with indenting
  212.      * @return Object to be serialized in byte array
  213.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  214.      */
  215.     public byte[] toByteArray(Evento evento,boolean prettyPrint) throws SerializerException {
  216.         return this.objToXml(Evento.class, evento, prettyPrint).toByteArray();
  217.     }
  218.    
  219.     /**
  220.      * Serialize to String the object <var>evento</var> of type {@link org.openspcoop2.core.eventi.Evento}
  221.      *
  222.      * @param evento Object to be serialized
  223.      * @return Object to be serialized as String
  224.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  225.      */
  226.     public String toString(Evento evento) throws SerializerException {
  227.         return this.objToXml(Evento.class, evento, false).toString();
  228.     }
  229.     /**
  230.      * Serialize to String the object <var>evento</var> of type {@link org.openspcoop2.core.eventi.Evento}
  231.      *
  232.      * @param evento Object to be serialized
  233.      * @param prettyPrint if true output the XML with indenting
  234.      * @return Object to be serialized as String
  235.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  236.      */
  237.     public String toString(Evento evento,boolean prettyPrint) throws SerializerException {
  238.         return this.objToXml(Evento.class, evento, prettyPrint).toString();
  239.     }
  240.    
  241.    
  242.    
  243.     /*
  244.      =================================================================================
  245.      Object: dati-evento-generico
  246.      =================================================================================
  247.     */
  248.    
  249.     /**
  250.      * Serialize to file system in <var>fileName</var> the object <var>datiEventoGenerico</var> of type {@link org.openspcoop2.core.eventi.DatiEventoGenerico}
  251.      *
  252.      * @param fileName Xml file to serialize the object <var>datiEventoGenerico</var>
  253.      * @param datiEventoGenerico Object to be serialized in xml file <var>fileName</var>
  254.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  255.      */
  256.     public void write(String fileName,DatiEventoGenerico datiEventoGenerico) throws SerializerException {
  257.         this.objToXml(fileName, DatiEventoGenerico.class, datiEventoGenerico, false);
  258.     }
  259.     /**
  260.      * Serialize to file system in <var>fileName</var> the object <var>datiEventoGenerico</var> of type {@link org.openspcoop2.core.eventi.DatiEventoGenerico}
  261.      *
  262.      * @param fileName Xml file to serialize the object <var>datiEventoGenerico</var>
  263.      * @param datiEventoGenerico Object to be serialized in xml file <var>fileName</var>
  264.      * @param prettyPrint if true output the XML with indenting
  265.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  266.      */
  267.     public void write(String fileName,DatiEventoGenerico datiEventoGenerico,boolean prettyPrint) throws SerializerException {
  268.         this.objToXml(fileName, DatiEventoGenerico.class, datiEventoGenerico, prettyPrint);
  269.     }
  270.    
  271.     /**
  272.      * Serialize to file system in <var>file</var> the object <var>datiEventoGenerico</var> of type {@link org.openspcoop2.core.eventi.DatiEventoGenerico}
  273.      *
  274.      * @param file Xml file to serialize the object <var>datiEventoGenerico</var>
  275.      * @param datiEventoGenerico Object to be serialized in xml file <var>fileName</var>
  276.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  277.      */
  278.     public void write(File file,DatiEventoGenerico datiEventoGenerico) throws SerializerException {
  279.         this.objToXml(file, DatiEventoGenerico.class, datiEventoGenerico, false);
  280.     }
  281.     /**
  282.      * Serialize to file system in <var>file</var> the object <var>datiEventoGenerico</var> of type {@link org.openspcoop2.core.eventi.DatiEventoGenerico}
  283.      *
  284.      * @param file Xml file to serialize the object <var>datiEventoGenerico</var>
  285.      * @param datiEventoGenerico Object to be serialized in xml file <var>fileName</var>
  286.      * @param prettyPrint if true output the XML with indenting
  287.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  288.      */
  289.     public void write(File file,DatiEventoGenerico datiEventoGenerico,boolean prettyPrint) throws SerializerException {
  290.         this.objToXml(file, DatiEventoGenerico.class, datiEventoGenerico, prettyPrint);
  291.     }
  292.    
  293.     /**
  294.      * Serialize to output stream <var>out</var> the object <var>datiEventoGenerico</var> of type {@link org.openspcoop2.core.eventi.DatiEventoGenerico}
  295.      *
  296.      * @param out OutputStream to serialize the object <var>datiEventoGenerico</var>
  297.      * @param datiEventoGenerico Object to be serialized in xml file <var>fileName</var>
  298.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  299.      */
  300.     public void write(OutputStream out,DatiEventoGenerico datiEventoGenerico) throws SerializerException {
  301.         this.objToXml(out, DatiEventoGenerico.class, datiEventoGenerico, false);
  302.     }
  303.     /**
  304.      * Serialize to output stream <var>out</var> the object <var>datiEventoGenerico</var> of type {@link org.openspcoop2.core.eventi.DatiEventoGenerico}
  305.      *
  306.      * @param out OutputStream to serialize the object <var>datiEventoGenerico</var>
  307.      * @param datiEventoGenerico Object to be serialized in xml file <var>fileName</var>
  308.      * @param prettyPrint if true output the XML with indenting
  309.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  310.      */
  311.     public void write(OutputStream out,DatiEventoGenerico datiEventoGenerico,boolean prettyPrint) throws SerializerException {
  312.         this.objToXml(out, DatiEventoGenerico.class, datiEventoGenerico, prettyPrint);
  313.     }
  314.            
  315.     /**
  316.      * Serialize to byte array the object <var>datiEventoGenerico</var> of type {@link org.openspcoop2.core.eventi.DatiEventoGenerico}
  317.      *
  318.      * @param datiEventoGenerico Object to be serialized
  319.      * @return Object to be serialized in byte array
  320.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  321.      */
  322.     public byte[] toByteArray(DatiEventoGenerico datiEventoGenerico) throws SerializerException {
  323.         return this.objToXml(DatiEventoGenerico.class, datiEventoGenerico, false).toByteArray();
  324.     }
  325.     /**
  326.      * Serialize to byte array the object <var>datiEventoGenerico</var> of type {@link org.openspcoop2.core.eventi.DatiEventoGenerico}
  327.      *
  328.      * @param datiEventoGenerico Object to be serialized
  329.      * @param prettyPrint if true output the XML with indenting
  330.      * @return Object to be serialized in byte array
  331.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  332.      */
  333.     public byte[] toByteArray(DatiEventoGenerico datiEventoGenerico,boolean prettyPrint) throws SerializerException {
  334.         return this.objToXml(DatiEventoGenerico.class, datiEventoGenerico, prettyPrint).toByteArray();
  335.     }
  336.    
  337.     /**
  338.      * Serialize to String the object <var>datiEventoGenerico</var> of type {@link org.openspcoop2.core.eventi.DatiEventoGenerico}
  339.      *
  340.      * @param datiEventoGenerico Object to be serialized
  341.      * @return Object to be serialized as String
  342.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  343.      */
  344.     public String toString(DatiEventoGenerico datiEventoGenerico) throws SerializerException {
  345.         return this.objToXml(DatiEventoGenerico.class, datiEventoGenerico, false).toString();
  346.     }
  347.     /**
  348.      * Serialize to String the object <var>datiEventoGenerico</var> of type {@link org.openspcoop2.core.eventi.DatiEventoGenerico}
  349.      *
  350.      * @param datiEventoGenerico Object to be serialized
  351.      * @param prettyPrint if true output the XML with indenting
  352.      * @return Object to be serialized as String
  353.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  354.      */
  355.     public String toString(DatiEventoGenerico datiEventoGenerico,boolean prettyPrint) throws SerializerException {
  356.         return this.objToXml(DatiEventoGenerico.class, datiEventoGenerico, prettyPrint).toString();
  357.     }
  358.    
  359.    
  360.    

  361. }