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 it.cnipa.collprofiles.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 it.cnipa.collprofiles.OperationType;
  25. import it.cnipa.collprofiles.OperationListType;
  26. import it.cnipa.collprofiles.EgovDecllElement;

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

  32. import javax.xml.bind.JAXBElement;

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


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




  126.     /*
  127.      =================================================================================
  128.      Object: operationType
  129.      =================================================================================
  130.     */
  131.    
  132.     /**
  133.      * Serialize to file system in <var>fileName</var> the object <var>operationType</var> of type {@link it.cnipa.collprofiles.OperationType}
  134.      *
  135.      * @param fileName Xml file to serialize the object <var>operationType</var>
  136.      * @param operationType Object to be serialized in xml file <var>fileName</var>
  137.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  138.      */
  139.     public void write(String fileName,OperationType operationType) throws SerializerException {
  140.         this.objToXml(fileName, OperationType.class, operationType, false);
  141.     }
  142.     /**
  143.      * Serialize to file system in <var>fileName</var> the object <var>operationType</var> of type {@link it.cnipa.collprofiles.OperationType}
  144.      *
  145.      * @param fileName Xml file to serialize the object <var>operationType</var>
  146.      * @param operationType Object to be serialized in xml file <var>fileName</var>
  147.      * @param prettyPrint if true output the XML with indenting
  148.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  149.      */
  150.     public void write(String fileName,OperationType operationType,boolean prettyPrint) throws SerializerException {
  151.         this.objToXml(fileName, OperationType.class, operationType, prettyPrint);
  152.     }
  153.    
  154.     /**
  155.      * Serialize to file system in <var>file</var> the object <var>operationType</var> of type {@link it.cnipa.collprofiles.OperationType}
  156.      *
  157.      * @param file Xml file to serialize the object <var>operationType</var>
  158.      * @param operationType Object to be serialized in xml file <var>fileName</var>
  159.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  160.      */
  161.     public void write(File file,OperationType operationType) throws SerializerException {
  162.         this.objToXml(file, OperationType.class, operationType, false);
  163.     }
  164.     /**
  165.      * Serialize to file system in <var>file</var> the object <var>operationType</var> of type {@link it.cnipa.collprofiles.OperationType}
  166.      *
  167.      * @param file Xml file to serialize the object <var>operationType</var>
  168.      * @param operationType Object to be serialized in xml file <var>fileName</var>
  169.      * @param prettyPrint if true output the XML with indenting
  170.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  171.      */
  172.     public void write(File file,OperationType operationType,boolean prettyPrint) throws SerializerException {
  173.         this.objToXml(file, OperationType.class, operationType, prettyPrint);
  174.     }
  175.    
  176.     /**
  177.      * Serialize to output stream <var>out</var> the object <var>operationType</var> of type {@link it.cnipa.collprofiles.OperationType}
  178.      *
  179.      * @param out OutputStream to serialize the object <var>operationType</var>
  180.      * @param operationType Object to be serialized in xml file <var>fileName</var>
  181.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  182.      */
  183.     public void write(OutputStream out,OperationType operationType) throws SerializerException {
  184.         this.objToXml(out, OperationType.class, operationType, false);
  185.     }
  186.     /**
  187.      * Serialize to output stream <var>out</var> the object <var>operationType</var> of type {@link it.cnipa.collprofiles.OperationType}
  188.      *
  189.      * @param out OutputStream to serialize the object <var>operationType</var>
  190.      * @param operationType Object to be serialized in xml file <var>fileName</var>
  191.      * @param prettyPrint if true output the XML with indenting
  192.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  193.      */
  194.     public void write(OutputStream out,OperationType operationType,boolean prettyPrint) throws SerializerException {
  195.         this.objToXml(out, OperationType.class, operationType, prettyPrint);
  196.     }
  197.            
  198.     /**
  199.      * Serialize to byte array the object <var>operationType</var> of type {@link it.cnipa.collprofiles.OperationType}
  200.      *
  201.      * @param operationType Object to be serialized
  202.      * @return Object to be serialized in byte array
  203.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  204.      */
  205.     public byte[] toByteArray(OperationType operationType) throws SerializerException {
  206.         return this.objToXml(OperationType.class, operationType, false).toByteArray();
  207.     }
  208.     /**
  209.      * Serialize to byte array the object <var>operationType</var> of type {@link it.cnipa.collprofiles.OperationType}
  210.      *
  211.      * @param operationType Object to be serialized
  212.      * @param prettyPrint if true output the XML with indenting
  213.      * @return Object to be serialized in byte array
  214.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  215.      */
  216.     public byte[] toByteArray(OperationType operationType,boolean prettyPrint) throws SerializerException {
  217.         return this.objToXml(OperationType.class, operationType, prettyPrint).toByteArray();
  218.     }
  219.    
  220.     /**
  221.      * Serialize to String the object <var>operationType</var> of type {@link it.cnipa.collprofiles.OperationType}
  222.      *
  223.      * @param operationType Object to be serialized
  224.      * @return Object to be serialized as String
  225.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  226.      */
  227.     public String toString(OperationType operationType) throws SerializerException {
  228.         return this.objToXml(OperationType.class, operationType, false).toString();
  229.     }
  230.     /**
  231.      * Serialize to String the object <var>operationType</var> of type {@link it.cnipa.collprofiles.OperationType}
  232.      *
  233.      * @param operationType Object to be serialized
  234.      * @param prettyPrint if true output the XML with indenting
  235.      * @return Object to be serialized as String
  236.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  237.      */
  238.     public String toString(OperationType operationType,boolean prettyPrint) throws SerializerException {
  239.         return this.objToXml(OperationType.class, operationType, prettyPrint).toString();
  240.     }
  241.    
  242.    
  243.    
  244.     /*
  245.      =================================================================================
  246.      Object: operationListType
  247.      =================================================================================
  248.     */
  249.    
  250.     /**
  251.      * Serialize to file system in <var>fileName</var> the object <var>operationListType</var> of type {@link it.cnipa.collprofiles.OperationListType}
  252.      *
  253.      * @param fileName Xml file to serialize the object <var>operationListType</var>
  254.      * @param operationListType Object to be serialized in xml file <var>fileName</var>
  255.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  256.      */
  257.     public void write(String fileName,OperationListType operationListType) throws SerializerException {
  258.         this.objToXml(fileName, OperationListType.class, operationListType, false);
  259.     }
  260.     /**
  261.      * Serialize to file system in <var>fileName</var> the object <var>operationListType</var> of type {@link it.cnipa.collprofiles.OperationListType}
  262.      *
  263.      * @param fileName Xml file to serialize the object <var>operationListType</var>
  264.      * @param operationListType Object to be serialized in xml file <var>fileName</var>
  265.      * @param prettyPrint if true output the XML with indenting
  266.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  267.      */
  268.     public void write(String fileName,OperationListType operationListType,boolean prettyPrint) throws SerializerException {
  269.         this.objToXml(fileName, OperationListType.class, operationListType, prettyPrint);
  270.     }
  271.    
  272.     /**
  273.      * Serialize to file system in <var>file</var> the object <var>operationListType</var> of type {@link it.cnipa.collprofiles.OperationListType}
  274.      *
  275.      * @param file Xml file to serialize the object <var>operationListType</var>
  276.      * @param operationListType Object to be serialized in xml file <var>fileName</var>
  277.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  278.      */
  279.     public void write(File file,OperationListType operationListType) throws SerializerException {
  280.         this.objToXml(file, OperationListType.class, operationListType, false);
  281.     }
  282.     /**
  283.      * Serialize to file system in <var>file</var> the object <var>operationListType</var> of type {@link it.cnipa.collprofiles.OperationListType}
  284.      *
  285.      * @param file Xml file to serialize the object <var>operationListType</var>
  286.      * @param operationListType Object to be serialized in xml file <var>fileName</var>
  287.      * @param prettyPrint if true output the XML with indenting
  288.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  289.      */
  290.     public void write(File file,OperationListType operationListType,boolean prettyPrint) throws SerializerException {
  291.         this.objToXml(file, OperationListType.class, operationListType, prettyPrint);
  292.     }
  293.    
  294.     /**
  295.      * Serialize to output stream <var>out</var> the object <var>operationListType</var> of type {@link it.cnipa.collprofiles.OperationListType}
  296.      *
  297.      * @param out OutputStream to serialize the object <var>operationListType</var>
  298.      * @param operationListType Object to be serialized in xml file <var>fileName</var>
  299.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  300.      */
  301.     public void write(OutputStream out,OperationListType operationListType) throws SerializerException {
  302.         this.objToXml(out, OperationListType.class, operationListType, false);
  303.     }
  304.     /**
  305.      * Serialize to output stream <var>out</var> the object <var>operationListType</var> of type {@link it.cnipa.collprofiles.OperationListType}
  306.      *
  307.      * @param out OutputStream to serialize the object <var>operationListType</var>
  308.      * @param operationListType Object to be serialized in xml file <var>fileName</var>
  309.      * @param prettyPrint if true output the XML with indenting
  310.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  311.      */
  312.     public void write(OutputStream out,OperationListType operationListType,boolean prettyPrint) throws SerializerException {
  313.         this.objToXml(out, OperationListType.class, operationListType, prettyPrint);
  314.     }
  315.            
  316.     /**
  317.      * Serialize to byte array the object <var>operationListType</var> of type {@link it.cnipa.collprofiles.OperationListType}
  318.      *
  319.      * @param operationListType Object to be serialized
  320.      * @return Object to be serialized in byte array
  321.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  322.      */
  323.     public byte[] toByteArray(OperationListType operationListType) throws SerializerException {
  324.         return this.objToXml(OperationListType.class, operationListType, false).toByteArray();
  325.     }
  326.     /**
  327.      * Serialize to byte array the object <var>operationListType</var> of type {@link it.cnipa.collprofiles.OperationListType}
  328.      *
  329.      * @param operationListType Object to be serialized
  330.      * @param prettyPrint if true output the XML with indenting
  331.      * @return Object to be serialized in byte array
  332.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  333.      */
  334.     public byte[] toByteArray(OperationListType operationListType,boolean prettyPrint) throws SerializerException {
  335.         return this.objToXml(OperationListType.class, operationListType, prettyPrint).toByteArray();
  336.     }
  337.    
  338.     /**
  339.      * Serialize to String the object <var>operationListType</var> of type {@link it.cnipa.collprofiles.OperationListType}
  340.      *
  341.      * @param operationListType Object to be serialized
  342.      * @return Object to be serialized as String
  343.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  344.      */
  345.     public String toString(OperationListType operationListType) throws SerializerException {
  346.         return this.objToXml(OperationListType.class, operationListType, false).toString();
  347.     }
  348.     /**
  349.      * Serialize to String the object <var>operationListType</var> of type {@link it.cnipa.collprofiles.OperationListType}
  350.      *
  351.      * @param operationListType Object to be serialized
  352.      * @param prettyPrint if true output the XML with indenting
  353.      * @return Object to be serialized as String
  354.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  355.      */
  356.     public String toString(OperationListType operationListType,boolean prettyPrint) throws SerializerException {
  357.         return this.objToXml(OperationListType.class, operationListType, prettyPrint).toString();
  358.     }
  359.    
  360.    
  361.    
  362.     /*
  363.      =================================================================================
  364.      Object: egovDecllElement
  365.      =================================================================================
  366.     */
  367.    
  368.     /**
  369.      * Serialize to file system in <var>fileName</var> the object <var>egovDecllElement</var> of type {@link it.cnipa.collprofiles.EgovDecllElement}
  370.      *
  371.      * @param fileName Xml file to serialize the object <var>egovDecllElement</var>
  372.      * @param egovDecllElement Object to be serialized in xml file <var>fileName</var>
  373.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  374.      */
  375.     public void write(String fileName,EgovDecllElement egovDecllElement) throws SerializerException {
  376.         this.objToXml(fileName, EgovDecllElement.class, egovDecllElement, false);
  377.     }
  378.     /**
  379.      * Serialize to file system in <var>fileName</var> the object <var>egovDecllElement</var> of type {@link it.cnipa.collprofiles.EgovDecllElement}
  380.      *
  381.      * @param fileName Xml file to serialize the object <var>egovDecllElement</var>
  382.      * @param egovDecllElement Object to be serialized in xml file <var>fileName</var>
  383.      * @param prettyPrint if true output the XML with indenting
  384.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  385.      */
  386.     public void write(String fileName,EgovDecllElement egovDecllElement,boolean prettyPrint) throws SerializerException {
  387.         this.objToXml(fileName, EgovDecllElement.class, egovDecllElement, prettyPrint);
  388.     }
  389.    
  390.     /**
  391.      * Serialize to file system in <var>file</var> the object <var>egovDecllElement</var> of type {@link it.cnipa.collprofiles.EgovDecllElement}
  392.      *
  393.      * @param file Xml file to serialize the object <var>egovDecllElement</var>
  394.      * @param egovDecllElement Object to be serialized in xml file <var>fileName</var>
  395.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  396.      */
  397.     public void write(File file,EgovDecllElement egovDecllElement) throws SerializerException {
  398.         this.objToXml(file, EgovDecllElement.class, egovDecllElement, false);
  399.     }
  400.     /**
  401.      * Serialize to file system in <var>file</var> the object <var>egovDecllElement</var> of type {@link it.cnipa.collprofiles.EgovDecllElement}
  402.      *
  403.      * @param file Xml file to serialize the object <var>egovDecllElement</var>
  404.      * @param egovDecllElement Object to be serialized in xml file <var>fileName</var>
  405.      * @param prettyPrint if true output the XML with indenting
  406.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  407.      */
  408.     public void write(File file,EgovDecllElement egovDecllElement,boolean prettyPrint) throws SerializerException {
  409.         this.objToXml(file, EgovDecllElement.class, egovDecllElement, prettyPrint);
  410.     }
  411.    
  412.     /**
  413.      * Serialize to output stream <var>out</var> the object <var>egovDecllElement</var> of type {@link it.cnipa.collprofiles.EgovDecllElement}
  414.      *
  415.      * @param out OutputStream to serialize the object <var>egovDecllElement</var>
  416.      * @param egovDecllElement Object to be serialized in xml file <var>fileName</var>
  417.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  418.      */
  419.     public void write(OutputStream out,EgovDecllElement egovDecllElement) throws SerializerException {
  420.         this.objToXml(out, EgovDecllElement.class, egovDecllElement, false);
  421.     }
  422.     /**
  423.      * Serialize to output stream <var>out</var> the object <var>egovDecllElement</var> of type {@link it.cnipa.collprofiles.EgovDecllElement}
  424.      *
  425.      * @param out OutputStream to serialize the object <var>egovDecllElement</var>
  426.      * @param egovDecllElement Object to be serialized in xml file <var>fileName</var>
  427.      * @param prettyPrint if true output the XML with indenting
  428.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  429.      */
  430.     public void write(OutputStream out,EgovDecllElement egovDecllElement,boolean prettyPrint) throws SerializerException {
  431.         this.objToXml(out, EgovDecllElement.class, egovDecllElement, prettyPrint);
  432.     }
  433.            
  434.     /**
  435.      * Serialize to byte array the object <var>egovDecllElement</var> of type {@link it.cnipa.collprofiles.EgovDecllElement}
  436.      *
  437.      * @param egovDecllElement Object to be serialized
  438.      * @return Object to be serialized in byte array
  439.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  440.      */
  441.     public byte[] toByteArray(EgovDecllElement egovDecllElement) throws SerializerException {
  442.         return this.objToXml(EgovDecllElement.class, egovDecllElement, false).toByteArray();
  443.     }
  444.     /**
  445.      * Serialize to byte array the object <var>egovDecllElement</var> of type {@link it.cnipa.collprofiles.EgovDecllElement}
  446.      *
  447.      * @param egovDecllElement Object to be serialized
  448.      * @param prettyPrint if true output the XML with indenting
  449.      * @return Object to be serialized in byte array
  450.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  451.      */
  452.     public byte[] toByteArray(EgovDecllElement egovDecllElement,boolean prettyPrint) throws SerializerException {
  453.         return this.objToXml(EgovDecllElement.class, egovDecllElement, prettyPrint).toByteArray();
  454.     }
  455.    
  456.     /**
  457.      * Serialize to String the object <var>egovDecllElement</var> of type {@link it.cnipa.collprofiles.EgovDecllElement}
  458.      *
  459.      * @param egovDecllElement Object to be serialized
  460.      * @return Object to be serialized as String
  461.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  462.      */
  463.     public String toString(EgovDecllElement egovDecllElement) throws SerializerException {
  464.         return this.objToXml(EgovDecllElement.class, egovDecllElement, false).toString();
  465.     }
  466.     /**
  467.      * Serialize to String the object <var>egovDecllElement</var> of type {@link it.cnipa.collprofiles.EgovDecllElement}
  468.      *
  469.      * @param egovDecllElement Object to be serialized
  470.      * @param prettyPrint if true output the XML with indenting
  471.      * @return Object to be serialized as String
  472.      * @throws SerializerException The exception that is thrown when an error occurs during serialization
  473.      */
  474.     public String toString(EgovDecllElement egovDecllElement,boolean prettyPrint) throws SerializerException {
  475.         return this.objToXml(EgovDecllElement.class, egovDecllElement, prettyPrint).toString();
  476.     }
  477.    
  478.    
  479.    

  480. }