01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05:
06: package com.sun.portal.providers.userinfo;
07:
08: import java.util.*;
09:
10: import javax.servlet.http.HttpServletRequest;
11:
12: import com.sun.portal.providers.context.ProviderContext;
13: import com.sun.portal.providers.context.ProviderContextException;
14: import com.sun.portal.providers.userinfo.tag.TagException;
15: import com.sun.portal.providers.userinfo.tag.TagWrapperException;
16: import com.sun.portal.providers.userinfo.tag.ReadTag;
17: import com.sun.portal.providers.userinfo.tag.WriteTag;
18: import com.sun.portal.providers.userinfo.tag.UnhandledTagException;
19:
20: /**
21: * this class handles getting and setting: preferredlocale<br>
22: * and handles getting: preferredlocale and localeList
23: */
24:
25: public class LocaleListTag implements ReadTag, WriteTag {
26: private ProviderContext context = null;
27: private String channel = null;
28:
29: public void init(String channel, ProviderContext context,
30: HttpServletRequest req) throws TagException {
31: this .channel = channel;
32:
33: if (context == null) {
34: throw new TagException(
35: "LocaleListTag.init(): provider context was null");
36: }
37: // set provider context
38: this .context = context;
39:
40: }
41:
42: public void set(String key, String newValue) throws TagException {
43: if (!key.equals("preferredlocale")) {
44: throw new UnhandledTagException("unhandled tag=" + key);
45: }
46:
47: context.setStringAttribute(key, newValue);
48: }
49:
50: public String get(String key) throws TagException {
51:
52: if (key.equals("localeList")) {
53: Map list = null;
54: try {
55: list = context.getCollectionProperty(channel,
56: "locales", true);
57: } catch (ProviderContextException pce) {
58: throw new TagException(
59: "LocaleListTag.get(): Failed to get property locales");
60: }
61: String selectedLocale = new String("");
62: StringBuffer localeList = new StringBuffer(
63: "<SELECT NAME=\"preferredlocale\">");
64: String userLocale = null;
65: userLocale = context.getStringAttribute("preferredlocale");
66:
67: if (userLocale == null || userLocale.length() == 0) {
68: userLocale = context.getLocale().toString();
69: }
70: String s = null;
71: String t = null;
72:
73: for (Iterator i = list.keySet().iterator(); i.hasNext();) {
74: s = (String) i.next();
75: if (s.equals(userLocale)) {
76: selectedLocale = "SELECTED";
77: } else {
78: selectedLocale = "";
79: }
80:
81: t = (String) list.get(s);
82:
83: localeList.append("<OPTION ").append(selectedLocale)
84: .append(" VALUE=\"").append(s).append("\">")
85: .append(t).append("</OPTION>");
86: }
87:
88: localeList.append("</SELECT>");
89: return localeList.toString();
90: } else {
91: throw new UnhandledTagException("unknown key=" + key);
92: }
93: }
94:
95: }
|