XMLSerializer.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.utils.serialization;

  21. import java.io.OutputStream;
  22. import java.io.Writer;
  23. import java.lang.annotation.Annotation;
  24. import java.util.List;
  25. import java.util.Set;


  26. import net.sf.json.JSONArray;
  27. import net.sf.json.JSONObject;
  28. import net.sf.json.JsonConfig;


  29. /**
  30.  * Contiene utility per effettuare la serializzazione di un oggetto
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */

  36. public class XMLSerializer implements ISerializer {

  37.     private JsonConfig jsonConfig;
  38.     private net.sf.json.xml.XMLSerializer xmlSerializer;

  39.     public XMLSerializer(){
  40.         this(new SerializationConfig());
  41.     }
  42.    
  43.     public XMLSerializer(SerializationConfig config){
  44.         XMLSerializer filtroInternoOggettiFiltratiDiversiByteArray = null;
  45.         if(config.getFilter()!=null) {
  46.             if(config.getFilter().sizeFiltersByValue()>0 || config.getFilter().sizeFiltersByName()>0) {
  47.                
  48.                 SerializationConfig conf = new SerializationConfig();
  49.                 conf.setFilter(new Filter());
  50.                 conf.setExcludes(config.getExcludes());
  51.                 conf.setIdBuilder(config.getIdBuilder());
  52.                 filtroInternoOggettiFiltratiDiversiByteArray = new XMLSerializer(conf);
  53.             }
  54.         }
  55.        
  56.         JsonConfig jsonConfig = new JsonConfig();
  57.         jsonConfig.setJsonPropertyFilter(new PropertyFilter(config.getFilter(),config.getIdBuilder(),filtroInternoOggettiFiltratiDiversiByteArray));
  58.         if(config.getExcludes()!=null){
  59.             jsonConfig.setExcludes(config.getExcludes().toArray(new String[]{}));
  60.         }
  61.         this.jsonConfig = jsonConfig;
  62.         this.xmlSerializer = new net.sf.json.xml.XMLSerializer();
  63.     }
  64.    
  65.    
  66.     @Override
  67.     public String getObject(Object o) throws IOException{
  68.         try{
  69.             Utilities.normalizeDateObjects(o);
  70.             if( (o instanceof Enum) ){
  71.                 JSONArray jsonArray = JSONArray.fromObject( o , this.jsonConfig );
  72.                 return this.xmlSerializer.write(jsonArray);
  73.             }
  74.             else if( (o != null && o.getClass().isArray()) ){
  75.                 JSONArray jsonArray = new JSONArray();
  76.                 Object[] array = (Object[]) o;
  77.                 for (int i = 0; i < array.length; i++) {
  78.                     Object object = array[i];
  79.                     jsonArray.add(JSONObject.fromObject( object , this.jsonConfig ));
  80.                 }
  81.                 return this.xmlSerializer.write(jsonArray);
  82.             }
  83.             else if( (o instanceof List) ){
  84.                 JSONArray jsonArray = new JSONArray();
  85.                 List<?> list = (List<?>) o;
  86.                 for (int i = 0; i < list.size(); i++) {
  87.                     Object object = list.get(i);
  88.                     jsonArray.add(JSONObject.fromObject( object , this.jsonConfig ));
  89.                 }
  90.                 return this.xmlSerializer.write(jsonArray);
  91.             }
  92.             else if( (o instanceof Set) ){
  93.                 Set<?> set = (Set<?>) o;
  94.                 return this.getObject(set.toArray());
  95.             }
  96.             else if((o instanceof Annotation) || o != null && o.getClass().isAnnotation())
  97.                 throw new IOException("'object' is an Annotation.");
  98.             else {
  99.                 JSONObject jsonObject = JSONObject.fromObject( o , this.jsonConfig );
  100.                 return this.xmlSerializer.write(jsonObject);
  101.             }
  102.         }catch(Exception e){
  103.             throw new IOException(e.getMessage(),e);
  104.         }
  105.     }
  106.    
  107.     @Override
  108.     public void writeObject(Object o,OutputStream out) throws IOException{
  109.         try{
  110.             //Utilities.normalizeDateObjects(o);
  111.             String s = this.getObject(o);
  112.             out.write(s.getBytes());
  113.         }catch(Exception e){
  114.             throw new IOException(e.getMessage(),e);
  115.         }
  116.     }
  117.    
  118.     @Override
  119.     public void writeObject(Object o,Writer out) throws IOException{
  120.         try{
  121.             //Utilities.normalizeDateObjects(o);
  122.             String s = this.getObject(o);
  123.             out.write(s);
  124.         }catch(Exception e){
  125.             throw new IOException(e.getMessage(),e);
  126.         }
  127.     }
  128. }