ObjectFormatter.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.expression.impl.formatter;

  21. import java.util.ArrayList;
  22. import java.util.Iterator;
  23. import java.util.List;

  24. import org.openspcoop2.generic_project.exception.ExpressionException;
  25. import org.openspcoop2.utils.TipiDatabase;

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

  34.     private List<ITypeFormatter<?>> typesFormatter = new ArrayList<ITypeFormatter<?>>();
  35.        
  36.     public ObjectFormatter() throws ExpressionException{
  37.         try{
  38.             // default
  39.             this.typesFormatter.add(new BooleanTypeFormatter());
  40.             this.typesFormatter.add(new CalendarTypeFormatter());
  41.             this.typesFormatter.add(new DateTypeFormatter());
  42.             this.typesFormatter.add(new TimestampTypeFormatter());
  43.             this.typesFormatter.add(new CharacterTypeFormatter());
  44.             this.typesFormatter.add(new StringTypeFormatter());
  45.             this.typesFormatter.add(new ByteTypeFormatter());
  46.             this.typesFormatter.add(new ShortTypeFormatter());
  47.             this.typesFormatter.add(new IntegerTypeFormatter());
  48.             this.typesFormatter.add(new LongTypeFormatter());
  49.             this.typesFormatter.add(new DoubleTypeFormatter());
  50.             this.typesFormatter.add(new FloatTypeFormatter());
  51.             this.typesFormatter.add(new EnumTypeFormatter());
  52.             this.typesFormatter.add(new URITypeFormatter());
  53.             this.typesFormatter.add(new ByteArrayTypeFormatter());
  54.         }catch(Exception e){
  55.             throw new ExpressionException(e);
  56.         }
  57.     }
  58.     public ObjectFormatter(ITypeFormatter<?> ... types){
  59.         for (int i = 0; i < types.length; i++) {
  60.             this.typesFormatter.add(types[i]);
  61.         }
  62.     }
  63.    
  64.     @Override
  65.     public void isSupported(Object o) throws ExpressionException{
  66.         boolean supported = false;
  67.         for (Iterator<ITypeFormatter<?>> iterator = this.typesFormatter.iterator(); iterator.hasNext();) {
  68.             ITypeFormatter<?> type = iterator.next();
  69.             try{
  70.                 o.getClass().asSubclass(type.getTypeSupported());
  71.                 supported = true;
  72.                 break;
  73.             }catch(Exception e){}
  74.         }
  75.         if(!supported){
  76.             throwTypeUnsupported(o);
  77.         }
  78.     }
  79.    
  80.     @Override
  81.     @SuppressWarnings("unchecked")
  82.     public String toString(Object o) throws ExpressionException {
  83.         for (Iterator<ITypeFormatter<?>> iterator = this.typesFormatter.iterator(); iterator.hasNext();) {
  84.             @SuppressWarnings("rawtypes")
  85.             ITypeFormatter type = iterator.next();
  86.             boolean isType = false;
  87.             try{
  88.                 o.getClass().asSubclass(type.getTypeSupported());
  89.                 isType = true;
  90.             }catch(Exception e){isType=false;}
  91.             if(isType){
  92.                 return type.toString(o);
  93.             }
  94.         }
  95.         throwTypeUnsupported(o);
  96.         return null;
  97.     }
  98.    
  99.     @Override
  100.     public String toSQLString(Object o) throws ExpressionException {
  101.         return this.toSQLString(o, TipiDatabase.DEFAULT);
  102.     }
  103.     @Override
  104.     @SuppressWarnings("unchecked")
  105.     public String toSQLString(Object o, TipiDatabase databaseType) throws ExpressionException {
  106.         for (Iterator<ITypeFormatter<?>> iterator = this.typesFormatter.iterator(); iterator.hasNext();) {
  107.             @SuppressWarnings("rawtypes")
  108.             ITypeFormatter type = iterator.next();
  109.             boolean isType = false;
  110.             try{
  111.                 o.getClass().asSubclass(type.getTypeSupported());
  112.                 isType = true;
  113.             }catch(Exception e){isType=false;}
  114.             if(isType){
  115.                 return type.toSQLString(o, databaseType);
  116.             }
  117.         }
  118.         throwTypeUnsupported(o);
  119.         return null;
  120.     }
  121.    
  122.     @Override
  123.     @SuppressWarnings("unchecked")
  124.     public Object toObject(String o,Class<?> c) throws ExpressionException {
  125.         for (Iterator<ITypeFormatter<?>> iterator = this.typesFormatter.iterator(); iterator.hasNext();) {
  126.             @SuppressWarnings("rawtypes")
  127.             ITypeFormatter type = iterator.next();
  128.             boolean isType = false;
  129.             try{
  130.                 c.asSubclass(type.getTypeSupported());
  131.                 isType = true;
  132.             }catch(Exception e){isType=false;}
  133.             if(isType){
  134.                 type.toObject(o,c);
  135.             }
  136.         }
  137.         throwTypeUnsupported(c);
  138.         return null;
  139.     }
  140.    
  141.     private void throwTypeUnsupported(Object o) throws ExpressionException {
  142.         StringBuilder bf = new StringBuilder();
  143.         for (Iterator<ITypeFormatter<?>> iterator = this.typesFormatter.iterator(); iterator.hasNext();) {
  144.             ITypeFormatter<?> type = iterator.next();
  145.             if(bf.length()>0){
  146.                 bf.append(" , ");
  147.             }
  148.             bf.append(type.getTypeSupported());
  149.         }
  150.         throw new ExpressionException("Unsupported ["+o.getClass().getName()+"] type. Supported types are: "+bf.toString());
  151.     }
  152. }