001: ///////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020: // $Id: PageAttributes.java 1741 2007-10-03 15:46:13Z manuel_gay $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.commons.attributes;
025:
026: import java.util.Enumeration;
027: import java.util.Map;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.jsp.PageContext;
031:
032: import org.makumba.AttributeNotFoundException;
033: import org.makumba.Attributes;
034: import org.makumba.LogicException;
035:
036: public class PageAttributes implements Attributes {
037: public static PageAttributes getAttributes(PageContext pc) {
038: if (pc.getAttribute(RequestAttributes.ATTRIBUTES_NAME) == null)
039: pc.setAttribute(RequestAttributes.ATTRIBUTES_NAME,
040: new PageAttributes(pc));
041: return (PageAttributes) pc
042: .getAttribute(RequestAttributes.ATTRIBUTES_NAME);
043: }
044:
045: PageContext pageContext;
046:
047: PageAttributes(PageContext pageContext) {
048: this .pageContext = pageContext;
049: }
050:
051: static public void setAttribute(PageContext pc, String var, Object o) {
052: if (o != null) {
053: pc.setAttribute(var, o);
054: pc.removeAttribute(var + "_null");
055: } else {
056: pc.removeAttribute(var);
057: pc.setAttribute(var + "_null", "null");
058: }
059: }
060:
061: /**
062: * @see org.makumba.Attributes#setAttribute(java.lang.String, java.lang.Object)
063: */
064: public Object setAttribute(String s, Object o)
065: throws LogicException {
066: return RequestAttributes.getAttributes(
067: (HttpServletRequest) pageContext.getRequest())
068: .setAttribute(s, o);
069: }
070:
071: /**
072: * @see org.makumba.Attributes#removeAttribute(java.lang.String)
073: */
074: public void removeAttribute(String s) throws LogicException {
075: RequestAttributes.getAttributes(
076: (HttpServletRequest) pageContext.getRequest())
077: .removeAttribute(s);
078: }
079:
080: /**
081: * @see org.makumba.Attributes#hasAttribute(java.lang.String)
082: */
083: public boolean hasAttribute(String s) {
084: try {
085: return (RequestAttributes.getAttributes(
086: (HttpServletRequest) pageContext.getRequest())
087: .hasAttribute(s) || checkPageForAttribute(s) != RequestAttributes.notFound);
088: } catch (LogicException e) {
089: return false;
090: }
091: }
092:
093: /**
094: * @see java.lang.Object#toString()
095: */
096: public String toString() {
097: String[] scopeNames = { "Session", "Application", "Request",
098: "Page" };
099: int[] scopes = { PageContext.SESSION_SCOPE,
100: PageContext.APPLICATION_SCOPE,
101: PageContext.REQUEST_SCOPE, PageContext.PAGE_SCOPE };
102: String s = "";
103: for (int i = 0; i < scopes.length; i++) {
104: s += scopeNames[i] + ": {";
105: Enumeration enumeration = pageContext
106: .getAttributeNamesInScope(scopes[i]);
107: while (enumeration.hasMoreElements()) {
108: String name = (String) enumeration.nextElement();
109: s += name + "=";
110: Object value = pageContext
111: .getAttribute(name, scopes[i]);
112: if (value instanceof PageAttributes) { // don't print if type is from this class --> avoid endless loop
113: s += value.getClass();
114: } else {
115: s += value;
116: }
117: if (enumeration.hasMoreElements()) {
118: s += ", ";
119: }
120: }
121: s += "}\n";
122: }
123: return s;
124: }
125:
126: /**
127: * @see org.makumba.Attributes#getAttribute(java.lang.String)
128: */
129: public Object getAttribute(String s) throws LogicException {
130: RequestAttributes reqAttrs = RequestAttributes
131: .getAttributes((HttpServletRequest) pageContext
132: .getRequest());
133:
134: Object o = reqAttrs.checkSessionForAttribute(s);
135: if (o != RequestAttributes.notFound)
136: return o;
137:
138: o = reqAttrs.checkServletLoginForAttribute(s);
139: if (o != RequestAttributes.notFound)
140: return o;
141:
142: o = checkPageForAttribute(s);
143: if (o != RequestAttributes.notFound)
144: return o;
145:
146: o = reqAttrs.checkLogicForAttribute(s);
147: if (o != RequestAttributes.notFound)
148: return o;
149:
150: o = reqAttrs.checkParameterForAttribute(s);
151: if (o != RequestAttributes.notFound)
152: return o;
153:
154: throw new AttributeNotFoundException(s, false);
155:
156: }
157:
158: public Object checkPageForAttribute(String s) {
159: String snull = s + "_null";
160:
161: Object value = pageContext.getAttribute(s);
162: if (value != null)
163: return value;
164: if (pageContext.getAttribute(snull) != null)
165: return null;
166: return RequestAttributes.notFound;
167: }
168: }
|