/* Copyright (c) 2002 by Oracle Corporation.  All Rights Reserved */

package oracle.portal.utils;

import java.util.Locale;

/**
 * Utility class containing methods useful when rendering content that can be 
 * displayed in multiple languages that may be Bi-Directional, i.e. some should 
 * be rendered from left to right (LTR) and others from right to left (RTL).
 */
public class BiDiUtils 
{
    public static final String RIGHT = "right";
    public static final String LEFT  = "left";

    public static final String BIDI_DIR_ATTRIBUTE = "DIR=\"RTL\"";
    public static final String NO_DIR_ATTRIBUTE = "DIR=\"LTR\"";

    /**
     * Constant representing the language code for RTL language Arabic.
     */
    public static final String ARABIC = "ar";

    /**
     * Constant representing the language code for RTL language Hebrew.
     */
    public static final String HEBREW = "iw";

    /**
     * Method to check whether a language is a BiDi language (i.e. rendered 
     * RTL).
     * @param l The locale object containing the current language setting.
     * @return boolean
     */
    public static boolean isBiDi(Locale l)
    {  
       return isBiDi(l.getLanguage());
    }

    /**
     * Method to check whether a language is a BiDi language (i.e. rendered 
     * RTL).
     * @param language A String representation of the current language setting.
     * @return boolean
     */
    public static boolean isBiDi(String language)
    { 
    
        if (language.equals(HEBREW) || language.equals(ARABIC))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Method to return the HTML attribute DIR=RTL when the language being used 
     * is a BiDi language.
     * @param l The locale object containing the current language setting.
     * @return String
     */
    public static String getDirAttribute(Locale l)
    {
      return getDirAttribute(l.getLanguage());
    }

    /**
     * Method to return the HTML attribute DIR=RTL when the language being used 
     * is a BiDi language.
     * @param language A String representation of the current language setting.
     * @return String
     */
    public static String getDirAttribute(String language)
    {
      if(isBiDi(language))
      {
          return BIDI_DIR_ATTRIBUTE;
      }
      else
      {
          return NO_DIR_ATTRIBUTE;
      }
    }

    /**
     * Method to return a left or right alignment value depending on whether the 
     * portal is being viewed in a BiDi language or not.
     * @param l         The locale object containing the current language 
     *                  setting.
     * @param alignment String to specify whether the alignment should be right 
     *                  or left if HTML was viewed in a LTR language.
     * @return String
     */
    public static String getAlignment(Locale l, String alignment)
    {
        return getAlignment(l.getLanguage(), alignment);
    }

    /**
     * Method to return a left or right alignment value depending on whether the 
     * portal is being viewed in a BiDi language or not.
     * @param l         The locale object containing the current language 
     *                  setting.
     * @param alignment String to specify whether the alignment should be right 
     *                  or left if HTML was viewed in a LTR language.
     * @return String
     */
    public static String getAlignment(String language, String alignment)
    {
        if (alignment.equals(LEFT))
        {
            if (isBiDi(language))
            {
                return RIGHT;
            }
            else
            {
                return LEFT;
            }
        }
        else
        {
            if (isBiDi(language))
            {
                return LEFT;
            }
            else
            {
                return RIGHT;
            }
        }
    }
}


