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: package com.sun.portal.portletcontainercommon.descriptor;
014:
015: import java.util.List;
016: import java.util.ArrayList;
017: import java.util.Iterator;
018: import java.util.logging.Logger;
019: import java.util.logging.Level;
020:
021: import org.jdom.Element;
022: import org.jdom.Namespace;
023: import com.sun.portal.log.common.PortalLogger;
024:
025: /**
026: * This class provides access to the default values of the portlet
027: * preferences.
028: */
029: public class PreferenceDescriptor {
030:
031: // Prefrence Descriptor Element Names
032: public static final String PREF_NAME = "name";
033: public static final String PREF_VALUE = "value";
034: public static final String READ_ONLY = "read-only";
035: public static final String MULTI_VALUE = "multi-value";
036:
037: // Global variables
038: private String _prefName;
039: private List _prefValues = new ArrayList();
040: private List _descriptions = new ArrayList();
041: private boolean _readOnly = false;
042: private boolean _multiValue = false;
043: private String _portletName;
044:
045: // Create a logger for this class
046: private static Logger debugLogger = PortalLogger
047: .getLogger(PreferenceDescriptor.class);
048:
049: public PreferenceDescriptor(Logger logger, String portletName) {
050: _portletName = portletName;
051: }
052:
053: /**
054: * Loads the preference descriptor.
055: * <P>
056: * @param element the preference element
057: */
058: public void load(Element element, Namespace namespace) {
059: _prefName = element.getChildTextTrim(PREF_NAME, namespace);
060: if (_prefName == null) {
061: debugLogger.log(Level.WARNING, "PSPL_PCCCSPPCCD0011",
062: _portletName);
063: }
064: List prefValueElements = element.getChildren(PREF_VALUE,
065: namespace);
066: if (prefValueElements.isEmpty()) {
067: debugLogger.log(Level.WARNING, "PSPL_PCCCSPPCCD0012",
068: _prefName);
069: } else {
070: Iterator i = prefValueElements.iterator();
071: while (i.hasNext()) {
072: Element prefValueElement = (Element) i.next();
073: _prefValues.add(prefValueElement.getTextTrim());
074: }
075: }
076:
077: List descriptionElements = element.getChildren(
078: PortletAppDescriptor.DESCRIPTION, namespace);
079: for (Iterator i = descriptionElements.iterator(); i.hasNext();) {
080: Element descriptionElement = (Element) i.next();
081: _descriptions.add(descriptionElement.getTextTrim());
082: }
083: String readOnly = element
084: .getChildTextTrim(READ_ONLY, namespace);
085: if (readOnly != null && readOnly.equals("true")) {
086: _readOnly = true;
087: }
088:
089: Element multiValueElement = element.getChild(MULTI_VALUE,
090: namespace);
091: if (multiValueElement != null) {
092: _multiValue = true;
093: }
094: }
095:
096: /**
097: * Returns the preference name.
098: * @return A <code>String</code>.
099: */
100: public String getPrefName() {
101: return _prefName;
102: }
103:
104: /**
105: * Returns the preference values as a <code>List</code>.
106: * @return A <code>List</code> of <code>String</code>s.
107: */
108: public List getPrefValues() {
109: return _prefValues;
110: }
111:
112: /**
113: * Returns the description. If description is not define, null is returned.
114: */
115: public String getDescription() {
116: String description = null;
117: if (!_descriptions.isEmpty()) {
118: description = (String) _descriptions.get(0);
119: }
120: return description;
121: }
122:
123: /**
124: * Returns the descriptions in a <code>List</code>.
125: * <P>
126: * @return A <code>List</code> of the <code>String</code>s. The
127: * returned value could be empty list if description is not defined.
128: */
129: public List getDescriptions() {
130: return _descriptions;
131: }
132:
133: /**
134: * Returns if the preference is modifiable. If <read-only> is
135: * not defined in the descriptor for a portlet preference, false is return.
136: */
137: public boolean getReadOnly() {
138: return _readOnly;
139: }
140:
141: /**
142: * Returns if the preference has multi-values.
143: */
144: public boolean getMultiValueFlag() {
145: return _multiValue;
146: }
147:
148: /**
149: * The toString method.
150: * <P>
151: * @return the <code>String</code> representation of the portlet
152: * preference descriptor.
153: */
154: public String toString() {
155: StringBuffer sb = new StringBuffer("PreferenceDescriptor [");
156:
157: sb.append(" preference name [");
158: if (_prefName != null) {
159: sb.append(_prefName);
160: } else {
161: sb.append("NULL");
162: }
163: sb.append("]");
164:
165: sb.append(" preference values [");
166: Iterator iterator = _prefValues.iterator();
167: while (iterator.hasNext()) {
168: sb.append((String) iterator.next());
169: }
170: sb.append("]");
171:
172: sb.append(" description [");
173: if (!_descriptions.isEmpty()) {
174: sb.append((String) _descriptions.get(0));
175: } else {
176: sb.append("NULL");
177: }
178: sb.append("]");
179:
180: sb.append(" read-only [");
181: if (_readOnly) {
182: sb.append("fase");
183: } else {
184: sb.append("true");
185: }
186: sb.append("]");
187:
188: sb.append("]");
189: sb.append("\n");
190:
191: return (sb.toString());
192: }
193: }
|