AbstractDeserializerBase.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.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.InputStream;

  25. import org.openspcoop2.generic_project.exception.DeserializerException;

  26. /**
  27.  * AbstractDeserializer
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public abstract class AbstractDeserializerBase {

  34.     public <T> T xmlToObj(String fileName,Class<T> c) throws DeserializerException {
  35.         return this.xmlToObj(new File(fileName), c);
  36.     }
  37.     public <T> T xmlToObj(File file,Class<T> c) throws DeserializerException {
  38.         FileInputStream fin = null;
  39.         try{
  40.             fin = new FileInputStream(file);
  41.             return this.xmlToObj(fin, c);
  42.         }catch(Exception e){
  43.             throw new DeserializerException(e);
  44.         }
  45.         finally {
  46.             try{
  47.                 if(fin!=null) {
  48.                     fin.close();
  49.                 }
  50.             }catch(Exception eClose){
  51.                 // close
  52.             }
  53.         }
  54.     }
  55.     public <T> T xmlToObj(byte[] bytes,Class<T> c) throws DeserializerException {
  56.         ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
  57.         return this.xmlToObj(bin, c);
  58.     }
  59.     public <T> T xmlToObj(InputStream is,Class<T> c) throws DeserializerException {
  60.         try{
  61.             return (T) this._xmlToObj(is, c);
  62.         }catch(Exception e){
  63.             throw new DeserializerException(e);
  64.         }
  65.     }
  66.     protected abstract <T> T _xmlToObj(InputStream is,Class<T> c) throws Exception;
  67.    
  68.    
  69.     public <T> T xmlToObjByByteArray(byte[] xml,Class<T> c) throws DeserializerException {
  70.         ByteArrayInputStream bin = null;
  71.         try{
  72.             bin = new ByteArrayInputStream(xml);
  73.             return (T) this._xmlToObj(bin, c);
  74.         }catch(Exception e){
  75.             throw new DeserializerException(e);
  76.         }finally{
  77.             try{
  78.                 if(bin!=null) {
  79.                     bin.close();
  80.                 }
  81.             }catch(Exception e){
  82.                 // close
  83.             }
  84.         }
  85.     }
  86.     public <T> T xmlToObjByString(String xml,Class<T> c) throws DeserializerException {
  87.         try{
  88.             return this.xmlToObjByByteArray(xml.getBytes(), c);
  89.         }catch(Exception e){
  90.             throw new DeserializerException(e);
  91.         }
  92.     }
  93. }