001: /*
002: * (C) Copyright 2005 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.taglib;
022:
023: import javax.portlet.PortletSession;
024: import javax.portlet.RenderRequest;
025: import javax.servlet.jsp.JspException;
026: import javax.servlet.jsp.JspWriter;
027: import javax.servlet.jsp.tagext.TagSupport;
028:
029: import com.nabhinc.portlet.mvcportlet.core.Constants;
030:
031: /**
032: * Tag for displaying String attribute values set in the specified scope.
033: * If an attribute has multiple values, e.g. checkbox values, a comma
034: * separated String of the values is shown.
035: *
036: * @author Padmanabh Dabke
037: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
038: * @since 1.0
039: */
040: public class StringAttribTag extends TagSupport {
041:
042: private String saName = null;
043: private String saScope = Constants.PORTLET_SESSION_SCOPE;
044:
045: public void release() {
046: super .release();
047: saName = null;
048: saScope = Constants.PORTLET_SESSION_SCOPE;
049: }
050:
051: public String getName() {
052: return saName;
053: }
054:
055: public void setName(String name) {
056: saName = name;
057: }
058:
059: public String getScope() {
060: return saScope;
061: }
062:
063: public void setScope(String sc) {
064: if (Constants.REQUEST_SCOPE.equals(sc)
065: || Constants.PORTLET_SESSION_SCOPE.equals(sc)
066: || Constants.APPLICATION_SESSION_SCOPE.equals(sc)
067: || Constants.PORTLET_CONTEXT_SCOPE.equals(sc)) {
068: saScope = sc;
069: } else {
070: throw new IllegalArgumentException(
071: "Invalid scope attribute: "
072: + sc
073: + ". Attribute scope must be one of: request, portlet_session, application_session, portlet_context");
074: }
075: }
076:
077: public int doStartTag() throws JspException {
078:
079: try {
080: RenderRequest request = (RenderRequest) pageContext
081: .getRequest().getAttribute("javax.portlet.request");
082: JspWriter out = pageContext.getOut();
083: String[] value = null;
084:
085: if (Constants.REQUEST_SCOPE.equals(saScope)) {
086: value = (String[]) request.getAttribute(saName);
087: } else if (Constants.PORTLET_SESSION_SCOPE.equals(saScope)) {
088: value = (String[]) request.getPortletSession()
089: .getAttribute(saName);
090: } else if (Constants.APPLICATION_SESSION_SCOPE
091: .equals(saScope)) {
092: value = (String[]) request.getPortletSession()
093: .getAttribute(saName,
094: PortletSession.APPLICATION_SCOPE);
095: } else {
096: value = (String[]) request.getPortletSession()
097: .getPortletContext().getAttribute(saName);
098: }
099: if (value != null) {
100: if (value.length > 0) {
101: StringBuffer sb = new StringBuffer();
102: sb.append(value[0]);
103: for (int i = 1; i < value.length; i++) {
104: sb.append(",");
105: sb.append(value[i]);
106: }
107: out.print(sb.toString());
108: out.flush();
109: }
110: }
111: } catch (Exception ex) {
112: throw new JspException(ex);
113: }
114:
115: return SKIP_BODY;
116: }
117:
118: public int doEndTag() {
119: return EVAL_PAGE;
120: }
121:
122: }
|