Charset.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.resources;

  21. import java.io.Serializable;
  22. import java.util.List;

  23. import org.apache.commons.lang.CharEncoding;

  24. /**
  25. * CharsetEncoding
  26. *
  27. * @author Andrea Poli (apoli@link.it)
  28. * @author $Author$
  29. * @version $Rev$, $Date$
  30. */
  31. public enum Charset implements Serializable , Cloneable {

  32.     ISO_8859_1 (CharEncoding.ISO_8859_1),
  33.     UTF_8 (CharEncoding.UTF_8),
  34.     UTF_16 (CharEncoding.UTF_16),
  35.     UTF_16BE (CharEncoding.UTF_16BE),
  36.     UTF_16LE (CharEncoding.UTF_16LE),
  37.     US_ASCII (CharEncoding.US_ASCII);
  38.    
  39.    
  40.     /** Value */
  41.     private String value;
  42.     public String getValue()
  43.     {
  44.         return this.value;
  45.     }


  46.     /** Official Constructor */
  47.     Charset(String value)
  48.     {
  49.         this.value = value;
  50.     }


  51.    
  52.     @Override
  53.     public String toString(){
  54.         return this.value;
  55.     }
  56.     public boolean equals(String value){
  57.         if(value==null)
  58.             return false;
  59.         return value.equals(this.getValue());  
  60.     }
  61.    
  62.        
  63.    
  64.     /** compatibility with the generated bean (reflection) */
  65.     public boolean equals(Object object,List<String> fieldsNotCheck){
  66.         if( !(object instanceof Charset) ){
  67.             throw new RuntimeException("Wrong type: "+object.getClass().getName());
  68.         }
  69.         return this.equals(((Charset)object));
  70.     }
  71.     public String toString(boolean reportHTML){
  72.         return toString();
  73.     }
  74.     public String toString(boolean reportHTML,List<String> fieldsNotIncluded){
  75.         return toString();
  76.     }
  77.     public String diff(Object object,StringBuilder bf,boolean reportHTML){
  78.         return bf.toString();
  79.     }
  80.     public String diff(Object object,StringBuilder bf,boolean reportHTML,List<String> fieldsNotIncluded){
  81.         return bf.toString();
  82.     }
  83.    
  84.    
  85.     /** Utilities */
  86.    
  87.     public static String[] toArray(){
  88.         String[] res = new String[values().length];
  89.         int i=0;
  90.         for (Charset tmp : values()) {
  91.             res[i]=tmp.getValue();
  92.             i++;
  93.         }
  94.         return res;
  95.     }  
  96.     public static String[] toStringArray(){
  97.         String[] res = new String[values().length];
  98.         int i=0;
  99.         for (Charset tmp : values()) {
  100.             res[i]=tmp.toString();
  101.             i++;
  102.         }
  103.         return res;
  104.     }
  105.     public static String[] toEnumNameArray(){
  106.         String[] res = new String[values().length];
  107.         int i=0;
  108.         for (Charset tmp : values()) {
  109.             res[i]=tmp.name();
  110.             i++;
  111.         }
  112.         return res;
  113.     }
  114.    
  115.     public static boolean contains(String value){
  116.         return toEnumConstant(value)!=null;
  117.     }
  118.    
  119.     public static Charset toEnumConstant(String value){
  120.         Charset res = null;
  121.         if(Charset.ISO_8859_1.getValue().equals(value)){
  122.             res = Charset.ISO_8859_1;
  123.         }else if(Charset.US_ASCII.getValue().equals(value)){
  124.             res = Charset.US_ASCII;
  125.         }else if(Charset.UTF_8.getValue().equals(value)){
  126.             res = Charset.UTF_8;
  127.         }else if(Charset.UTF_16.getValue().equals(value)){
  128.             res = Charset.UTF_16;
  129.         }else if(Charset.UTF_16BE.getValue().equals(value)){
  130.             res = Charset.UTF_16BE;
  131.         }else if(Charset.UTF_16LE.getValue().equals(value)){
  132.             res = Charset.UTF_16LE;
  133.         }
  134.         return res;
  135.     }
  136.    
  137.     public static Charset toEnumConstantFromString(String value){
  138.         Charset res = null;
  139.         if(Charset.ISO_8859_1.toString().equals(value)){
  140.             res = Charset.ISO_8859_1;
  141.         }else if(Charset.US_ASCII.toString().equals(value)){
  142.             res = Charset.US_ASCII;
  143.         }else if(Charset.UTF_8.toString().equals(value)){
  144.             res = Charset.UTF_8;
  145.         }else if(Charset.UTF_16.toString().equals(value)){
  146.             res = Charset.UTF_16;
  147.         }else if(Charset.UTF_16BE.toString().equals(value)){
  148.             res = Charset.UTF_16BE;
  149.         }else if(Charset.UTF_16LE.toString().equals(value)){
  150.             res = Charset.UTF_16LE;
  151.         }
  152.         return res;
  153.     }
  154.    
  155. }