001: /*
002: * Copyright 2004-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.taglib.core;
017:
018: import org.apache.myfaces.shared_impl.util.ClassUtils;
019:
020: import javax.el.ELException;
021: import javax.el.ValueExpression;
022: import javax.faces.component.UIComponent;
023: import javax.faces.context.FacesContext;
024: import javax.faces.webapp.UIComponentClassicTagBase;
025: import javax.faces.webapp.UIComponentELTag;
026: import javax.servlet.jsp.JspException;
027: import javax.servlet.jsp.tagext.Tag;
028: import javax.servlet.jsp.tagext.TagSupport;
029:
030: /**
031: * @author Andreas Berger (latest modification by $Author: baranda $)
032: * @version $Revision: 467970 $ $Date: 2006-10-26 14:02:03 +0200 (Do, 26 Okt 2006) $
033: * @since 1.2
034: */
035: public abstract class GenericListenerTag<_Holder, _Listener> extends
036: TagSupport {
037: private ValueExpression _type = null;
038: private ValueExpression _binding = null;
039: private Class<_Holder> _holderClazz;
040:
041: protected GenericListenerTag(Class<_Holder> holderClazz) {
042: super ();
043: _holderClazz = holderClazz;
044: }
045:
046: public void setType(ValueExpression type) {
047: _type = type;
048: }
049:
050: public void setBinding(ValueExpression binding) {
051: _binding = binding;
052: }
053:
054: public void release() {
055: super .release();
056: _type = null;
057: _binding = null;
058: }
059:
060: protected abstract void addListener(_Holder holder,
061: _Listener listener);
062:
063: public int doStartTag() throws JspException {
064: UIComponentClassicTagBase componentTag = UIComponentELTag
065: .getParentUIComponentClassicTagBase(pageContext);
066: if (componentTag == null) {
067: throw new JspException("no parent UIComponentTag found");
068: }
069:
070: if (_type == null) {
071: throw new JspException("type attribute not set");
072: }
073:
074: if (!componentTag.getCreated()) {
075: return Tag.SKIP_BODY;
076: }
077:
078: if (_type == null) {
079: throw new JspException("type attribute not set");
080: }
081:
082: _Holder holder = null;
083: UIComponent component = componentTag.getComponentInstance();
084: try {
085: holder = (_Holder) component;
086: } catch (ClassCastException e) {
087: throw new JspException("Component "
088: + ((UIComponent) holder).getId()
089: + " is not instance of " + _holderClazz.getName());
090: }
091:
092: FacesContext facesContext = FacesContext.getCurrentInstance();
093: _Listener listener;
094: // type and/or binding must be specified
095: try {
096: if (null != _binding) {
097: try {
098: listener = (_Listener) _binding
099: .getValue(facesContext.getELContext());
100: if (null != listener) {
101: addListener(holder, listener);
102: // no need for further processing
103: return Tag.SKIP_BODY;
104: }
105: } catch (ELException e) {
106: throw new JspException(
107: "Exception while evaluating the binding attribute of Component "
108: + component.getId(), e);
109: }
110: }
111: if (null != _type) {
112: String className;
113: if (_type.isLiteralText()) {
114: className = _type.getExpressionString();
115: } else {
116: className = (String) _type.getValue(facesContext
117: .getELContext());
118: }
119: listener = (_Listener) ClassUtils
120: .newInstance(className);
121: if (null != _binding) {
122: try {
123: _binding.setValue(facesContext.getELContext(),
124: listener);
125: } catch (ELException e) {
126: throw new JspException(
127: "Exception while evaluating the binding attribute of Component "
128: + component.getId(), e);
129: }
130: }
131: addListener(holder, listener);
132: }
133: } catch (ClassCastException e) {
134: throw new JspException(e);
135: }
136:
137: return Tag.SKIP_BODY;
138: }
139: }
|