AbstractSerializerBase.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.generic_project.serializer;


  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileNotFoundException;
  24. import java.io.OutputStream;

  25. import org.openspcoop2.generic_project.exception.SerializerException;
  26. import org.openspcoop2.generic_project.io.IOUtilities;
  27. import org.openspcoop2.utils.resources.FileSystemUtilities;
  28. import org.openspcoop2.utils.xml.PrettyPrintXMLUtils;
  29. import org.openspcoop2.utils.xml.XMLUtils;

  30. /**
  31.  * AbstractSerializer
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public abstract class AbstractSerializerBase {


  38.     private void createFile(String fileName) throws Exception{
  39.         File f = new File(fileName);
  40.         if(f.exists()==false){
  41.             IOUtilities.mkdirParentDirectory(fileName);
  42.             boolean create = f.createNewFile();
  43.             if(create==false){
  44.                 throw new FileNotFoundException("File ["+fileName+"] cannot exists and creating failure");
  45.             }
  46.         }
  47.     }
  48.    
  49.    
  50.    
  51.     // FileName+Object
  52.     public <T> void objToXml(String fileName,Class<T> c,T object) throws SerializerException{
  53.         objToXml(fileName, c, object, false);
  54.     }
  55.     public <T> void objToXml(String fileName,Class<T> c,T object,boolean pretty) throws SerializerException{
  56.         try{
  57.             this.createFile(fileName);
  58.             this._objToXml(fileName, c, object, pretty);
  59.         }catch(Exception e){
  60.             throw new SerializerException(e);
  61.         }
  62.     }
  63.     protected abstract <T> void _objToXml(String fileName,Class<T> c,T object,boolean pretty) throws Exception;
  64.    
  65.        
  66.     // OutputStream+Object
  67.     public <T> void objToXml(OutputStream out,Class<T> c,T object) throws SerializerException{
  68.         objToXml(out, c, object, false);
  69.     }
  70.     public <T> void objToXml(OutputStream out,Class<T> c,T object,boolean pretty) throws SerializerException{
  71.         try{
  72.             _objToXml(out,c,object,pretty);
  73.         }catch(Exception e){
  74.             throw new SerializerException(e.getMessage(),e);
  75.         }
  76.     }
  77.     protected abstract <T> void _objToXml(OutputStream out,Class<T> c,T object,boolean pretty) throws Exception;

  78.    
  79.     // File+byte[]
  80.     public void objToXml(String fileName,byte[]contenuto) throws SerializerException{
  81.         objToXml(fileName,contenuto,false);
  82.     }
  83.     public void objToXml(String fileName,byte[]contenuto,boolean pretty) throws SerializerException{
  84.         try{
  85.             this.createFile(fileName);
  86.             if(pretty){
  87.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  88.                 PrettyPrintXMLUtils.prettyPrintWithTrAX(XMLUtils.getInstance().newDocument(contenuto), bout, false);
  89.                 bout.flush();
  90.                 bout.close();
  91.                 FileSystemUtilities.writeFile(fileName, bout.toByteArray());
  92.             }
  93.             else{
  94.                 FileSystemUtilities.writeFile(fileName, contenuto);
  95.             }
  96.         }catch(Exception e){
  97.             throw new SerializerException(e);
  98.         }
  99.     }
  100.    
  101.    
  102.     // ToByteArray
  103.     public <T> byte[] objToXmlAsByteArray(Class<T> c,T object) throws SerializerException{
  104.         return objToXmlAsByteArray(c,object,false);
  105.     }
  106.     public <T> byte[] objToXmlAsByteArray(Class<T> c,T object,boolean pretty) throws SerializerException{
  107.         try{
  108.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  109.             this._objToXml(bout, c, object, pretty);
  110.             bout.flush();
  111.             bout.close();
  112.             return bout.toByteArray();
  113.         }catch(Exception e){
  114.             throw new SerializerException(e.getMessage(),e);
  115.         }
  116.     }
  117.    
  118.    
  119.     // ToString
  120.     public <T> String objToXmlAsString(Class<T> c,T object) throws SerializerException{
  121.         return objToXmlAsString(c,object,false);
  122.     }
  123.     public <T> String objToXmlAsString(Class<T> c,T object,boolean pretty) throws SerializerException{
  124.         try{
  125.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  126.             this._objToXml(bout, c, object, pretty);
  127.             bout.flush();
  128.             bout.close();
  129.             return bout.toString();
  130.         }catch(Exception e){
  131.             throw new SerializerException(e.getMessage(),e);
  132.         }
  133.     }
  134.    
  135.    
  136. }