Class LocaleIDParser


  • public final class LocaleIDParser
    extends java.lang.Object
    Utility class to parse and normalize locale ids (including POSIX style)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) java.lang.String baseName  
      private java.lang.StringBuilder buffer
      Temporary buffer for parsed sections of data.
      private boolean canonicalize  
      private static char COMMA  
      private static char DONE
      Character to indicate no more text is available in the id.
      private static char DOT  
      private boolean hadCountry  
      private static char HYPHEN  
      private char[] id
      Char array representing the locale ID.
      private int index
      Current position in id (while parsing).
      private static char ITEM_SEPARATOR  
      private static char KEYWORD_ASSIGN  
      private static char KEYWORD_SEPARATOR
      Parsing constants.
      (package private) java.util.Map<java.lang.String,​java.lang.String> keywords  
      private static int MAX_VARIANTS_LENGTH  
      private static char UNDERSCORE  
    • Constructor Summary

      Constructors 
      Constructor Description
      LocaleIDParser​(java.lang.String localeID)  
      LocaleIDParser​(java.lang.String localeID, boolean canonicalize)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void addSeparator()  
      private void append​(char c)
      Append c to the buffer.
      private void append​(java.lang.String s)
      Append the string to the buffer.
      private boolean atTerminator()
      Returns true if the character at index in the id is a terminator.
      void defaultKeywordValue​(java.lang.String keywordName, java.lang.String value)
      Set the keyword value only if it is not already set to something else.
      java.lang.String getBaseName()
      Returns the normalized base form of the locale id.
      java.lang.String getCountry()
      return the normalized country id, or the empty string.
      private java.util.Comparator<java.lang.String> getKeyComparator()  
      private java.lang.String getKeyword()  
      java.util.Map<java.lang.String,​java.lang.String> getKeywordMap()
      Returns a map of the keywords and values, or null if there are none.
      java.util.Iterator<java.lang.String> getKeywords()
      Returns an iterator over the keywords, or null if we have an empty map.
      java.lang.String getKeywordValue​(java.lang.String keywordName)
      Returns the value for the named keyword, or null if the keyword is not present.
      java.lang.String getLanguage()
      Returns the normalized language id, or the empty string.
      java.lang.String[] getLanguageScriptCountryVariant()
      Returns the language, script, country, and variant as separate strings.
      java.lang.String getName()
      Returns the normalized full form of the locale id.
      java.lang.String getScript()
      Returns the normalized script id, or the empty string.
      private java.lang.String getString​(int start)
      Returns the text in the buffer from start to blen as a String.
      private java.lang.String getValue()  
      java.lang.String getVariant()
      Returns the normalized variant id, or the empty string.
      private boolean haveExperimentalLanguagePrefix()
      Returns true if the start of the buffer has an experimental or private language prefix, the pattern '[ixIX][-_].' shows the syntax checked.
      private boolean haveKeywordAssign()
      Returns true if a value separator occurs at or after index.
      private static boolean isDoneOrItemSeparator​(char c)  
      private static boolean isDoneOrKeywordAssign​(char c)  
      private boolean isTerminator​(char c)
      Returns true if the character is a terminator (keyword separator, dot, or DONE).
      private boolean isTerminatorOrIDSeparator​(char c)
      Returns true if the character is a terminator or id separator.
      private char next()
      Returns the character at index in the id, and advance index.
      void parseBaseName()  
      private int parseCountry()
      Advance index past country, and accumulate normalized country in buffer.
      private int parseKeywords()
      Parse the keywords and return start of the string in the buffer.
      private int parseLanguage()
      Advance index past language, and accumulate normalized language code in buffer.
      private int parseScript()
      Advance index past script, and accumulate normalized script in buffer.
      private int parseVariant()
      Advance index past variant, and accumulate normalized variant in buffer.
      private void reset()  
      private void set​(int pos, java.lang.String s)
      Set the length of the buffer to pos, then append the string.
      void setBaseName​(java.lang.String baseName)  
      void setKeywordValue​(java.lang.String keywordName, java.lang.String value)
      Set the value for the named keyword, or unset it if value is null.
      private void setKeywordValue​(java.lang.String keywordName, java.lang.String value, boolean reset)
      Set the value for the named keyword, or unset it if value is null.
      private boolean setToKeywordStart()
      If we have keywords, advance index to the start of the keywords and return true, otherwise return false.
      private void skipCountry()
      Advance index past country.
      private void skipLanguage()
      Advance index past language.
      private void skipScript()
      Advance index past script.
      private void skipUntilTerminatorOrIDSeparator()
      Advance index until the next terminator or id separator, and leave it there.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • id

        private char[] id
        Char array representing the locale ID.
      • index

        private int index
        Current position in id (while parsing).
      • buffer

        private java.lang.StringBuilder buffer
        Temporary buffer for parsed sections of data.
      • canonicalize

        private boolean canonicalize
      • hadCountry

        private boolean hadCountry
      • keywords

        java.util.Map<java.lang.String,​java.lang.String> keywords
      • baseName

        java.lang.String baseName
      • KEYWORD_SEPARATOR

        private static final char KEYWORD_SEPARATOR
        Parsing constants.
        See Also:
        Constant Field Values
      • DONE

        private static final char DONE
        Character to indicate no more text is available in the id.
        See Also:
        Constant Field Values
    • Constructor Detail

      • LocaleIDParser

        public LocaleIDParser​(java.lang.String localeID)
      • LocaleIDParser

        public LocaleIDParser​(java.lang.String localeID,
                              boolean canonicalize)
    • Method Detail

      • reset

        private void reset()
      • append

        private void append​(char c)
        Append c to the buffer.
      • addSeparator

        private void addSeparator()
      • getString

        private java.lang.String getString​(int start)
        Returns the text in the buffer from start to blen as a String.
      • set

        private void set​(int pos,
                         java.lang.String s)
        Set the length of the buffer to pos, then append the string.
      • append

        private void append​(java.lang.String s)
        Append the string to the buffer.
      • next

        private char next()
        Returns the character at index in the id, and advance index. The returned character is DONE if index was at the limit of the buffer. The index is advanced regardless so that decrementing the index will always 'unget' the last character returned.
      • skipUntilTerminatorOrIDSeparator

        private void skipUntilTerminatorOrIDSeparator()
        Advance index until the next terminator or id separator, and leave it there.
      • atTerminator

        private boolean atTerminator()
        Returns true if the character at index in the id is a terminator.
      • isTerminator

        private boolean isTerminator​(char c)
        Returns true if the character is a terminator (keyword separator, dot, or DONE). Dot is a terminator because of the POSIX form, where dot precedes the codepage.
      • isTerminatorOrIDSeparator

        private boolean isTerminatorOrIDSeparator​(char c)
        Returns true if the character is a terminator or id separator.
      • haveExperimentalLanguagePrefix

        private boolean haveExperimentalLanguagePrefix()
        Returns true if the start of the buffer has an experimental or private language prefix, the pattern '[ixIX][-_].' shows the syntax checked.
      • haveKeywordAssign

        private boolean haveKeywordAssign()
        Returns true if a value separator occurs at or after index.
      • parseLanguage

        private int parseLanguage()
        Advance index past language, and accumulate normalized language code in buffer. Index must be at 0 when this is called. Index is left at a terminator or id separator. Returns the start of the language code in the buffer.
      • skipLanguage

        private void skipLanguage()
        Advance index past language. Index must be at 0 when this is called. Index is left at a terminator or id separator.
      • parseScript

        private int parseScript()
        Advance index past script, and accumulate normalized script in buffer. Index must be immediately after the language. If the item at this position is not a script (is not four characters long) leave index and buffer unchanged. Otherwise index is left at a terminator or id separator. Returns the start of the script code in the buffer (this may be equal to the buffer length, if there is no script).
      • skipScript

        private void skipScript()
        Advance index past script. Index must be immediately after the language and IDSeparator. If the item at this position is not a script (is not four characters long) leave index. Otherwise index is left at a terminator or id separator.
      • parseCountry

        private int parseCountry()
        Advance index past country, and accumulate normalized country in buffer. Index must be immediately after the script (if there is one, else language) and IDSeparator. Return the start of the country code in the buffer.
      • skipCountry

        private void skipCountry()
        Advance index past country. Index must be immediately after the script (if there is one, else language) and IDSeparator.
      • parseVariant

        private int parseVariant()
        Advance index past variant, and accumulate normalized variant in buffer. This ignores the codepage information from POSIX ids. Index must be immediately after the country or script. Index is left at the keyword separator or at the end of the text. Return the start of the variant code in the buffer. In standard form, we can have the following forms: ll__VVVV ll_CC_VVVV ll_Ssss_VVVV ll_Ssss_CC_VVVV This also handles POSIX ids, which can have the following forms (pppp is code page id): ll_CC.pppp --> ll_CC ll_CC.pppp@VVVV --> ll_CC_VVVV ll_CC@VVVV --> ll_CC_VVVV We identify this use of '@' in POSIX ids by looking for an '=' following the '@'. If there is one, we consider '@' to start a keyword list, instead of being part of a POSIX id. Note: since it was decided that we want an option to not handle POSIX ids, this becomes a bit more complex.
      • getLanguage

        public java.lang.String getLanguage()
        Returns the normalized language id, or the empty string.
      • getScript

        public java.lang.String getScript()
        Returns the normalized script id, or the empty string.
      • getCountry

        public java.lang.String getCountry()
        return the normalized country id, or the empty string.
      • getVariant

        public java.lang.String getVariant()
        Returns the normalized variant id, or the empty string.
      • getLanguageScriptCountryVariant

        public java.lang.String[] getLanguageScriptCountryVariant()
        Returns the language, script, country, and variant as separate strings.
      • setBaseName

        public void setBaseName​(java.lang.String baseName)
      • parseBaseName

        public void parseBaseName()
      • getBaseName

        public java.lang.String getBaseName()
        Returns the normalized base form of the locale id. The base form does not include keywords.
      • getName

        public java.lang.String getName()
        Returns the normalized full form of the locale id. The full form includes keywords if they are present.
      • setToKeywordStart

        private boolean setToKeywordStart()
        If we have keywords, advance index to the start of the keywords and return true, otherwise return false.
      • isDoneOrKeywordAssign

        private static boolean isDoneOrKeywordAssign​(char c)
      • isDoneOrItemSeparator

        private static boolean isDoneOrItemSeparator​(char c)
      • getKeyword

        private java.lang.String getKeyword()
      • getValue

        private java.lang.String getValue()
      • getKeyComparator

        private java.util.Comparator<java.lang.String> getKeyComparator()
      • getKeywordMap

        public java.util.Map<java.lang.String,​java.lang.String> getKeywordMap()
        Returns a map of the keywords and values, or null if there are none.
      • parseKeywords

        private int parseKeywords()
        Parse the keywords and return start of the string in the buffer.
      • getKeywords

        public java.util.Iterator<java.lang.String> getKeywords()
        Returns an iterator over the keywords, or null if we have an empty map.
      • getKeywordValue

        public java.lang.String getKeywordValue​(java.lang.String keywordName)
        Returns the value for the named keyword, or null if the keyword is not present.
      • defaultKeywordValue

        public void defaultKeywordValue​(java.lang.String keywordName,
                                        java.lang.String value)
        Set the keyword value only if it is not already set to something else.
      • setKeywordValue

        public void setKeywordValue​(java.lang.String keywordName,
                                    java.lang.String value)
        Set the value for the named keyword, or unset it if value is null. If keywordName itself is null, unset all keywords. If keywordName is not null, value must not be null.
      • setKeywordValue

        private void setKeywordValue​(java.lang.String keywordName,
                                     java.lang.String value,
                                     boolean reset)
        Set the value for the named keyword, or unset it if value is null. If keywordName itself is null, unset all keywords. If keywordName is not null, value must not be null. If reset is true, ignore any previous value for the keyword, otherwise do not change the keyword (including removal of one or all keywords).