001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013:
014: package com.sun.portal.providers.context;
015:
016: import java.util.List;
017: import java.util.ArrayList;
018: import java.util.Locale;
019: import java.util.Iterator;
020: import java.util.StringTokenizer;
021:
022: import com.sun.portal.providers.context.PropertiesFilter;
023:
024: /**
025: * This class implements the "locale" filter.
026: */
027:
028: public class LocalePropertiesFilter extends PropertiesFilter {
029:
030: /**
031: * Constructor. Do not call this directly. Instead use
032: * <code>PropertiesFilterFactory.get(LOCALE_PROPERTIESFILTER_CLASSNAME, ...)</code>.
033: *
034: * @see PropertiesFilterFactory#get(String, String, boolean)
035: */
036: public LocalePropertiesFilter() {
037: super ();
038: }
039:
040: /**
041: * CACHING IS DISABLED FOR NOW.
042: *
043: * Return true.
044: *
045: * @return true
046: *
047: public boolean isCachable() {
048: return true;
049: }
050: */
051:
052: /**
053: * Return "<code>locale</code>"
054: *
055: * @return "<code>locale</code>"
056: */
057: public String getCondition() {
058: return "locale";
059: }
060:
061: /**
062: * Does the given condition and value match this locale filter?
063: * This match method is based on a case-sensitive string comparison.
064: *
065: * In other words, the condition should be "locale" and value should
066: * exactly match the value that was used to instantiate this filter.
067: *
068: * @param condition filter condition. i.e. "locale"
069: * @param value locale suffix (i.e. en, US, etc.)
070: * @return boolean value indicating whether match has succeeded or not.
071: */
072: public boolean match(String condition, String value) {
073: return condition.toLowerCase().equals("locale")
074: && getValue().equals(value);
075: }
076:
077: /**
078: * A convenient method for converting Locale into a list of
079: * <code>LocalePropertiesFilters</code>.
080: *
081: * @param locale Locale object
082: * @param required boolean value that determines whether the filters are
083: * required or optional
084: * @return List of non-required (optional) <code>LocalePropertiesFilter</code>
085: */
086: public static List getLocalePropertiesFilters(Locale locale,
087: boolean required) throws PropertiesFilterException {
088: if (locale == null) {
089: return null;
090: }
091:
092: String localeString = locale.toString();
093: if (localeString == null) {
094: return null;
095: }
096:
097: List pflist = new ArrayList();
098: StringTokenizer tokenizer = new StringTokenizer(localeString,
099: "_");
100: while (tokenizer.hasMoreTokens()) {
101: PropertiesFilter pf = PropertiesFilterFactory
102: .get(
103: PropertiesFilterFactory.LOCALE_PROPERTIESFILTER_CLASSNAME,
104: tokenizer.nextToken(), required);
105:
106: pflist.add(pf);
107: }
108:
109: return pflist;
110: }
111:
112: /**
113: * A convenient method for converting LocalePropertiesFilters into
114: * a locale object.
115: *
116: * @param pflist List of LocalePropertiesFilter objects or other
117: * PropertiesFilter that are non-required.
118: * @return Locale object. Returns null if no locale can be instantiated
119: * from the given PropertiesFilters.
120: */
121: public static Locale getLocale(List pflist)
122: throws PropertiesFilterException {
123:
124: String lang = "";
125: String country = "";
126: String variant = "";
127:
128: Iterator i = pflist.iterator();
129: PropertiesFilter pf = (PropertiesFilter) i.next();
130:
131: //
132: // skip PropertiesFilter that are not LocalePropertiesFilters
133: // if it's a required filter, then return null
134: //
135: while (!(pf instanceof LocalePropertiesFilter)) {
136: if (pf.isRequired()) {
137: return null;
138: }
139: if (i.hasNext()) {
140: pf = (PropertiesFilter) i.next();
141: } else {
142: return null;
143: }
144: }
145: lang = pf.getValue();
146:
147: if (i.hasNext()) {
148: pf = (PropertiesFilter) i.next();
149: if (pf instanceof LocalePropertiesFilter) {
150: country = pf.getValue();
151: } else {
152: if (pf.isRequired()) {
153: return null;
154: }
155: }
156: }
157:
158: if (i.hasNext()) {
159: pf = (PropertiesFilter) i.next();
160: if (pf instanceof LocalePropertiesFilter) {
161: variant = pf.getValue();
162: } else {
163: if (pf.isRequired()) {
164: return null;
165: }
166: }
167: }
168:
169: return new Locale(lang, country, variant);
170: }
171: }
|