30 ******************************************************************************* |
31 ******************************************************************************* |
31 */ |
32 */ |
32 package sun.util.locale; |
33 package sun.util.locale; |
33 |
34 |
34 import java.util.Collections; |
35 import java.util.Collections; |
|
36 import java.util.Map; |
35 import java.util.Map.Entry; |
37 import java.util.Map.Entry; |
36 import java.util.Set; |
38 import java.util.Set; |
37 import java.util.SortedMap; |
39 import java.util.SortedMap; |
38 import java.util.SortedSet; |
40 import java.util.SortedSet; |
39 import java.util.TreeMap; |
|
40 import java.util.TreeSet; |
|
41 |
41 |
42 public class UnicodeLocaleExtension extends Extension { |
42 public class UnicodeLocaleExtension extends Extension { |
43 public static final char SINGLETON = 'u'; |
43 public static final char SINGLETON = 'u'; |
44 |
44 |
45 private static final SortedSet<String> EMPTY_SORTED_SET = new TreeSet<String>(); |
45 private final Set<String> attributes; |
46 private static final SortedMap<String, String> EMPTY_SORTED_MAP = new TreeMap<String, String>(); |
46 private final Map<String, String> keywords; |
47 |
47 |
48 private SortedSet<String> _attributes = EMPTY_SORTED_SET; |
48 public static final UnicodeLocaleExtension CA_JAPANESE |
49 private SortedMap<String, String> _keywords = EMPTY_SORTED_MAP; |
49 = new UnicodeLocaleExtension("ca", "japanese"); |
|
50 public static final UnicodeLocaleExtension NU_THAI |
|
51 = new UnicodeLocaleExtension("nu", "thai"); |
50 |
52 |
51 public static final UnicodeLocaleExtension CA_JAPANESE; |
53 private UnicodeLocaleExtension(String key, String value) { |
52 public static final UnicodeLocaleExtension NU_THAI; |
54 super(SINGLETON, key + "-" + value); |
53 |
55 attributes = Collections.emptySet(); |
54 static { |
56 keywords = Collections.singletonMap(key, value); |
55 CA_JAPANESE = new UnicodeLocaleExtension(); |
|
56 CA_JAPANESE._keywords = new TreeMap<String, String>(); |
|
57 CA_JAPANESE._keywords.put("ca", "japanese"); |
|
58 CA_JAPANESE._value = "ca-japanese"; |
|
59 |
|
60 NU_THAI = new UnicodeLocaleExtension(); |
|
61 NU_THAI._keywords = new TreeMap<String, String>(); |
|
62 NU_THAI._keywords.put("nu", "thai"); |
|
63 NU_THAI._value = "nu-thai"; |
|
64 } |
|
65 |
|
66 private UnicodeLocaleExtension() { |
|
67 super(SINGLETON); |
|
68 } |
57 } |
69 |
58 |
70 UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords) { |
59 UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords) { |
71 this(); |
60 super(SINGLETON); |
72 if (attributes != null && attributes.size() > 0) { |
61 if (attributes != null) { |
73 _attributes = attributes; |
62 this.attributes = attributes; |
|
63 } else { |
|
64 this.attributes = Collections.emptySet(); |
74 } |
65 } |
75 if (keywords != null && keywords.size() > 0) { |
66 if (keywords != null) { |
76 _keywords = keywords; |
67 this.keywords = keywords; |
|
68 } else { |
|
69 this.keywords = Collections.emptyMap(); |
77 } |
70 } |
78 |
71 |
79 if (_attributes.size() > 0 || _keywords.size() > 0) { |
72 if (!this.attributes.isEmpty() || !this.keywords.isEmpty()) { |
80 StringBuilder sb = new StringBuilder(); |
73 StringBuilder sb = new StringBuilder(); |
81 for (String attribute : _attributes) { |
74 for (String attribute : this.attributes) { |
82 sb.append(LanguageTag.SEP).append(attribute); |
75 sb.append(LanguageTag.SEP).append(attribute); |
83 } |
76 } |
84 for (Entry<String, String> keyword : _keywords.entrySet()) { |
77 for (Entry<String, String> keyword : this.keywords.entrySet()) { |
85 String key = keyword.getKey(); |
78 String key = keyword.getKey(); |
86 String value = keyword.getValue(); |
79 String value = keyword.getValue(); |
87 |
80 |
88 sb.append(LanguageTag.SEP).append(key); |
81 sb.append(LanguageTag.SEP).append(key); |
89 if (value.length() > 0) { |
82 if (value.length() > 0) { |
90 sb.append(LanguageTag.SEP).append(value); |
83 sb.append(LanguageTag.SEP).append(value); |
91 } |
84 } |
92 } |
85 } |
93 _value = sb.substring(1); // skip leading '-' |
86 setValue(sb.substring(1)); // skip leading '-' |
94 } |
87 } |
95 } |
88 } |
96 |
89 |
97 public Set<String> getUnicodeLocaleAttributes() { |
90 public Set<String> getUnicodeLocaleAttributes() { |
98 return Collections.unmodifiableSet(_attributes); |
91 if (attributes == Collections.EMPTY_SET) { |
|
92 return attributes; |
|
93 } |
|
94 return Collections.unmodifiableSet(attributes); |
99 } |
95 } |
100 |
96 |
101 public Set<String> getUnicodeLocaleKeys() { |
97 public Set<String> getUnicodeLocaleKeys() { |
102 return Collections.unmodifiableSet(_keywords.keySet()); |
98 if (keywords == Collections.EMPTY_MAP) { |
|
99 return Collections.emptySet(); |
|
100 } |
|
101 return Collections.unmodifiableSet(keywords.keySet()); |
103 } |
102 } |
104 |
103 |
105 public String getUnicodeLocaleType(String unicodeLocaleKey) { |
104 public String getUnicodeLocaleType(String unicodeLocaleKey) { |
106 return _keywords.get(unicodeLocaleKey); |
105 return keywords.get(unicodeLocaleKey); |
107 } |
106 } |
108 |
107 |
109 public static boolean isSingletonChar(char c) { |
108 public static boolean isSingletonChar(char c) { |
110 return (SINGLETON == AsciiUtil.toLower(c)); |
109 return (SINGLETON == LocaleUtils.toLower(c)); |
111 } |
110 } |
112 |
111 |
113 public static boolean isAttribute(String s) { |
112 public static boolean isAttribute(String s) { |
114 // 3*8alphanum |
113 // 3*8alphanum |
115 return (s.length() >= 3) && (s.length() <= 8) && AsciiUtil.isAlphaNumericString(s); |
114 int len = s.length(); |
|
115 return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s); |
116 } |
116 } |
117 |
117 |
118 public static boolean isKey(String s) { |
118 public static boolean isKey(String s) { |
119 // 2alphanum |
119 // 2alphanum |
120 return (s.length() == 2) && AsciiUtil.isAlphaNumericString(s); |
120 return (s.length() == 2) && LocaleUtils.isAlphaNumericString(s); |
121 } |
121 } |
122 |
122 |
123 public static boolean isTypeSubtag(String s) { |
123 public static boolean isTypeSubtag(String s) { |
124 // 3*8alphanum |
124 // 3*8alphanum |
125 return (s.length() >= 3) && (s.length() <= 8) && AsciiUtil.isAlphaNumericString(s); |
125 int len = s.length(); |
|
126 return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s); |
126 } |
127 } |
127 } |
128 } |