Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * This program is free software; you can redistribute it and/or modify it under the
   * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
   * Foundation.
   *
   * You should have received a copy of the GNU Lesser General Public License along with this
   * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
   * or from the Free Software Foundation, Inc.,
   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * Copyright (c) 2006 - 2009 Pentaho Corporation..  All rights reserved.
  */
 package org.pentaho.metadata.messages;
 
 
 public class LocaleHelper {
 
     private static final ThreadLocal<LocalethreadLocales = new ThreadLocal<Locale>();
 
     private static Locale defaultLocale;
 
     public static final String UTF_8 = "UTF-8"//$NON-NLS-1$
     private static String encoding = ;
 
     public static final String LEFT_TO_RIGHT = "LTR"//$NON-NLS-1$
     private static String textDirection = ;
 
     public static void setDefaultLocale(Locale newLocale) {
          = newLocale;
     }
 
     public static Locale getDefaultLocale() {
         return ;
     }
 
     public static void setLocale(Locale newLocale) {
         .set(newLocale);
     }
 
     public static Locale getLocale() {
         Locale rtn = (Locale.get();
         if (rtn != null) {
             return rtn;
         }
          = Locale.getDefault();
         setLocale();
         return ;
     }
 
     public static void setSystemEncoding(String encoding) {
     	. = encoding;
     }
 
     public static void setTextDirection(String textDirection) {
         // TODO make this ThreadLocal
     	. = textDirection;
     }
 
     public static String getSystemEncoding() {
         return ;
     }
 
     public static String getTextDirection() {
         // TODO make this ThreadLocal
         return ;
     }
 
     public static String getClosestLocaleString localeString locales[] ) {
         // see if this locale is supported
       iflocales == null || locales.length == 0 ) {
         return locale;
       }
       iflocale == null || locale.length() == 0 ) {
         return locales[ 0 ];
       }
       String localeLanguage = locale.substring(0, 2);
       String localeCountry = (locale.length() > 4) ? locale.substring(0, 5) : localeLanguage;
         int looseMatch = -1;
         int closeMatch = -1;
         int exactMatch = -1;
         forint idx=0; idx<locales.lengthidx++ ) {
           iflocales[idx].equalslocale ) ) {
             exactMatch = idx;
             break;
           }
           else iflocales[idx].length() > 1 && locales[idx].substring(0, 2).equalslocaleLanguage ) ) {
             looseMatch = idx;
           }
           else iflocales[idx].length() > 4 && locales[idx].substring(0, 5).equalslocaleCountry ) ) {
            closeMatch = idx;
          }
        }
        ifexactMatch != -1 ) {
          // do nothing we have an exact match
        }
        else ifcloseMatch != - 1) {
          locale = localescloseMatch ];
        }
        else iflooseMatch != - 1) {
          locale = localeslooseMatch ];
        }
        else {
          // no locale is close , just go with the first?
          locale = locales[ 0 ];
        }
        return locale;
    }
  // From the BI-Platform LocaleHelper
    
    
This method is called to convert strings from ISO-8859-1 (post/get parameters for example) into the default system locale.

Parameters:
isoString
Returns:
Re-encoded string
    public static String convertISOStringToSystemDefaultEncoding(String isoString) {
      return convertEncodedStringToSystemDefaultEncoding("ISO-8859-1"isoString); //$NON-NLS-1$
    }
    
    
This method converts strings from a known encoding into a string encoded by the system default encoding.

Parameters:
fromEncoding
encodedStr
Returns:
Re-encoded string
    public static String convertEncodedStringToSystemDefaultEncoding(String fromEncodingString encodedStr) {
      return convertStringEncoding(encodedStrfromEncoding, LocaleHelper.getSystemEncoding());
    }

    
This method converts an ISO-8859-1 encoded string to a UTF-8 encoded string.

Parameters:
isoString
Returns:
Re-encoded string
    public static String isoToUtf8(String isoString) {
      return convertStringEncoding(isoString"ISO-8859-1""UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
     }
    
    
This method converts a UTF8-encoded string to ISO-8859-1

Parameters:
utf8String
Returns:
Re-encoded string
    public static String utf8ToIso(String utf8String) {
      return convertStringEncoding(utf8String"UTF-8""ISO-8859-1"); //$NON-NLS-1$ //$NON-NLS-2$
     }
    
    
This method converts strings between various encodings.

Parameters:
sourceString
sourceEncoding
targetEncoding
Returns:
Re-encoded string.
    public static String convertStringEncoding(String sourceStringString sourceEncodingString targetEncoding) {
      String targetString = null;
      if (null != sourceString && !sourceString.equals("")) { //$NON-NLS-1$
        try {
          byte[] stringBytesSource = sourceString.getBytes(sourceEncoding);
          targetString = new String(stringBytesSourcetargetEncoding);
        } catch(UnsupportedEncodingException e) {
          throw new RuntimeException(e); 
        } 
      } else {
        targetString = sourceString
      } return targetString
    }
    
    

Parameters:
aString
Returns:
true if the provided string is completely within the US-ASCII character set.
    public static boolean isAscii(String aString) {
      return isWithinCharset(aString"US-ASCII"); //$NON-NLS-1$
    }

    
    

Parameters:
aString
Returns:
true if the provided string is completely within the Latin-1 character set (ISO-8859-1).
    public static boolean isLatin1(String aString) {
      return isWithinCharset(aString"ISO-8859-1"); //$NON-NLS-1$
    }

    

Parameters:
aString
charsetTarget
Returns:
true if the provided string is completely within the target character set.
    public static boolean isWithinCharset(String aStringString charsetTarget) {
      byte[] stringBytes = aString.getBytes();
      CharsetDecoder decoder = Charset.forName(charsetTarget).newDecoder();
      try {
        decoder.decode(ByteBuffer.wrap(stringBytes));
        return true;
      } catch (CharacterCodingException ignored) {
      }
      return false;
    }
New to GrepCode? Check out our FAQ X