ContentFormatter.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.monitor.engine.utils;

  21. import java.sql.Timestamp;
  22. import java.text.SimpleDateFormat;
  23. import java.util.ArrayList;
  24. import java.util.Calendar;
  25. import java.util.Collection;
  26. import java.util.Date;
  27. import java.util.Iterator;
  28. import java.util.List;

  29. import org.openspcoop2.monitor.engine.exceptions.EngineException;
  30. import org.openspcoop2.utils.date.DateManager;
  31. import org.openspcoop2.utils.date.DateUtils;

  32. /**
  33.  * ContentFormatter
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class ContentFormatter {

  40.     private static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
  41.    
  42.     public static void isSupported(Object o) throws EngineException{
  43.         if(
  44.             ! (
  45.                 (o instanceof String) ||
  46.                 (o instanceof Integer) ||
  47.                 (o instanceof Long) ||
  48.                 (o instanceof Double) ||
  49.                 (o instanceof Float) ||
  50.                 (o instanceof Boolean) ||
  51.                 (o instanceof Date) ||
  52.                 (o instanceof Calendar) ||
  53.                 (o instanceof Timestamp)
  54.             )
  55.         ){
  56.             throw new EngineException("Tipo ["+o.getClass().getName()+"] non supportato. I tipi supportati sono: "+
  57.                     String.class.getName()+","+
  58.                     Integer.class.getName()+","+
  59.                     Long.class.getName()+","+
  60.                     Double.class.getName()+","+
  61.                     Float.class.getName()+","+
  62.                     Boolean.class.getName()+","+
  63.                     Date.class.getName()+","+
  64.                     Calendar.class.getName()+","+
  65.                     Timestamp.class.getName()+"");
  66.         }  
  67.     }
  68.     public static void isSupported(Object ... collections) throws EngineException{
  69.         for (int i = 0; i < collections.length; i++) {
  70.             ContentFormatter.isSupported(collections[i]);
  71.         }      
  72.     }
  73.     public static void isSupported(Collection<?> collections) throws EngineException{
  74.         for (Iterator<?> iterator = collections.iterator(); iterator.hasNext();) {
  75.             Object object = iterator.next();
  76.             ContentFormatter.isSupported(object);
  77.         }      
  78.     }
  79.    
  80.     public static Collection<String> toString(Object [] o) throws EngineException{
  81.         List<String> l = new ArrayList<>();
  82.         for (int i = 0; i < o.length; i++) {
  83.             l.add(toString(o[i]));
  84.         }
  85.         return l;
  86.     }
  87.     public static String toString(Object o) throws EngineException{
  88.         isSupported(o);
  89.         String oClassName = o.getClass().getName() + "";
  90.         if(oClassName.equals(String.class.getName())){
  91.             return (String)o;
  92.         }
  93.         else if(oClassName.equals(int.class.getName()) ||
  94.                 oClassName.equals(Integer.class.getName())){
  95.             return toString((Integer)o);
  96.         }
  97.         else if(oClassName.equals(long.class.getName()) ||
  98.                 oClassName.equals(Long.class.getName())){
  99.             return toString((Long)o);
  100.         }
  101.         else if(oClassName.equals(double.class.getName()) ||
  102.                 oClassName.equals(Double.class.getName())){
  103.             return toString((Double)o);
  104.         }
  105.         else if(oClassName.equals(float.class.getName()) ||
  106.                 oClassName.equals(Float.class.getName())){
  107.             return toString((Float)o);
  108.         }
  109.         else if(oClassName.equals(boolean.class.getName()) ||
  110.                 oClassName.equals(Boolean.class.getName())){
  111.             return toString((Boolean)o);
  112.         }
  113.         else if(oClassName.equals(Date.class.getName())){
  114.             return toString((Date)o);
  115.         }
  116.         else if(oClassName.equals(Calendar.class.getName())){
  117.             return toString((Calendar)o);
  118.         }
  119.         else if(oClassName.equals(Timestamp.class.getName())){
  120.             return toString((Timestamp)o);
  121.         }
  122.         throw new EngineException("Tipo ["+oClassName+"] non supportato");
  123.     }
  124.    
  125.     public static String toString(int intValue){
  126.         return intValue+"";
  127.     }
  128.     public static String toString(Integer intValue){
  129.         return ContentFormatter.toString(intValue.intValue());
  130.     }
  131.    
  132.    
  133.     public static String toString(long longValue){
  134.         return longValue+"";
  135.     }
  136.     public static String toString(Long longValue){
  137.         return ContentFormatter.toString(longValue.longValue());
  138.     }
  139.    
  140.    
  141.     public static String toString(double doubleValue){
  142.         return doubleValue+"";
  143.     }
  144.     public static String toString(Double doubleValue){
  145.         return ContentFormatter.toString(doubleValue.doubleValue());
  146.     }
  147.    
  148.    
  149.     public static String toString(float floatValue){
  150.         return floatValue+"";
  151.     }
  152.     public static String toString(Float floatValue){
  153.         return ContentFormatter.toString(floatValue.floatValue());
  154.     }
  155.    
  156.    
  157.     public static String toString(boolean b){
  158.         if(b)
  159.             return "t";
  160.         else
  161.             return "f";
  162.     }
  163.     public static String toString(Boolean b){
  164.         return ContentFormatter.toString(b.booleanValue());
  165.     }
  166.    

  167.     public static String toString(Date date){
  168.         SimpleDateFormat dateformat = DateUtils.getDefaultDateTimeFormatter(DATE_FORMAT);
  169.         return dateformat.format(date);
  170.     }
  171.    
  172.     public static String toString(Calendar calendar){
  173.         SimpleDateFormat dateformat = DateUtils.getDefaultDateTimeFormatter(DATE_FORMAT);
  174.         return dateformat.format(calendar.getTime());
  175.     }
  176.    
  177.     public static String toString(Timestamp t){
  178.         SimpleDateFormat dateformat = DateUtils.getDefaultDateTimeFormatter(DATE_FORMAT);
  179.         return dateformat.format(t);
  180.     }
  181.    
  182.     public static Integer toInteger(String v) throws EngineException{
  183.         try{
  184.             Integer i = null;
  185.             if(v!=null){
  186.                 i = Integer.parseInt(v);
  187.             }
  188.             return i;
  189.         }catch(Exception e){
  190.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  191.         }
  192.     }
  193.     public static int toPrimitiveInt(String v) throws EngineException{
  194.         try{
  195.             int i = 0;
  196.             if(v!=null){
  197.                 i = Integer.parseInt(v);
  198.             }
  199.             return i;
  200.         }catch(Exception e){
  201.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  202.         }
  203.     }
  204.     public static Long toLong(String v) throws EngineException{
  205.         try{
  206.             Long l = null;
  207.             if(v!=null){
  208.                 l = Long.parseLong(v);
  209.             }
  210.             return l;
  211.         }catch(Exception e){
  212.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  213.         }
  214.     }
  215.     public static long toPrimitiveLong(String v) throws EngineException{
  216.         try{
  217.             long l = 0;
  218.             if(v!=null){
  219.                 l = Long.parseLong(v);
  220.             }
  221.             return l;
  222.         }catch(Exception e){
  223.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  224.         }
  225.     }
  226.     public static Double toDouble(String v) throws EngineException{
  227.         try{
  228.             Double d = null;
  229.             if(v!=null){
  230.                 d = Double.parseDouble(v);
  231.             }
  232.             return d;
  233.         }catch(Exception e){
  234.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  235.         }
  236.     }
  237.     public static double toPrimitiveDouble(String v) throws EngineException{
  238.         try{
  239.             double d = 0;
  240.             if(v!=null){
  241.                 d = Double.parseDouble(v);
  242.             }
  243.             return d;
  244.         }catch(Exception e){
  245.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  246.         }
  247.     }
  248.     public static Float toFloat(String v) throws EngineException{
  249.         try{
  250.             Float f = null;
  251.             if(v!=null){
  252.                 f = Float.parseFloat(v);
  253.             }
  254.             return f;
  255.         }catch(Exception e){
  256.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  257.         }
  258.     }
  259.     public static float toPrimitiveFloat(String v) throws EngineException{
  260.         try{
  261.             float f = 0;
  262.             if(v!=null){
  263.                 f = Float.parseFloat(v);
  264.             }
  265.             return f;
  266.         }catch(Exception e){
  267.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  268.         }
  269.     }
  270.     public static Boolean toBoolean(String v) throws EngineException{
  271.         try{
  272.             Boolean b = null;
  273.             if(v!=null){
  274.                 b = Boolean.parseBoolean(v);
  275.             }
  276.             return b;
  277.         }catch(Exception e){
  278.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  279.         }
  280.     }
  281.     public static boolean toPrimitiveBoolean(String v) throws EngineException{
  282.         try{
  283.             boolean b = false;
  284.             if(v!=null){
  285.                 b = Boolean.parseBoolean(v);
  286.             }
  287.             return b;
  288.         }catch(Exception e){
  289.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  290.         }
  291.     }
  292.     public static Date toDate(String v) throws EngineException{
  293.         try{
  294.             Date d = null;
  295.             if(v!=null){
  296.                 SimpleDateFormat dateformat = DateUtils.getDefaultDateTimeFormatter(DATE_FORMAT);
  297.                 d = dateformat.parse(v);
  298.             }
  299.             return d;
  300.         }catch(Exception e){
  301.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  302.         }
  303.     }
  304.     public static Calendar toCalendar(String v) throws EngineException{
  305.         try{
  306.             Calendar c = null;
  307.             if(v!=null){
  308.                 c = DateManager.getCalendar();
  309.                 c.setTime(toDate(v));
  310.             }
  311.             return c;
  312.         }catch(Exception e){
  313.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  314.         }
  315.     }
  316.     public static Timestamp toTimestamp(String v) throws EngineException{
  317.         try{
  318.             Timestamp t = null;
  319.             if(v!=null){
  320.                 t = new Timestamp(toDate(v).getTime());
  321.             }
  322.             return t;
  323.         }catch(Exception e){
  324.             throw new EngineException("Converting error: "+ e.getMessage(),e);
  325.         }
  326.     }
  327.        
  328. }