1 16 package org.apache.cocoon.taglib; 17 18 import java.util.Map ; 19 import java.io.IOException ; 20 21 import org.apache.avalon.excalibur.pool.Recyclable; 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.avalon.framework.parameters.Parameters; 24 import org.apache.cocoon.environment.Context; 25 import org.apache.cocoon.environment.ObjectModelHelper; 26 import org.apache.cocoon.environment.Request; 27 import org.apache.cocoon.environment.Session; 28 import org.apache.cocoon.environment.SourceResolver; 29 30 import org.xml.sax.Attributes ; 31 import org.xml.sax.SAXException ; 32 33 39 public abstract class TagSupport extends AbstractLogEnabled implements Tag, Recyclable { 40 protected Tag parent; 41 protected SourceResolver resolver; 42 protected Map objectModel; 43 protected Parameters parameters; 44 private Request request; 45 46 58 public static final Tag findAncestorWithClass(Tag from, Class klass) { 59 boolean isInterface = false; 60 61 if (from == null || klass == null || (!Tag.class.isAssignableFrom(klass) && !(isInterface = klass.isInterface()))) { 62 return null; 63 } 64 65 for (;;) { 66 Tag tag = from.getParent(); 67 68 if (tag == null) { 69 return null; 70 } 71 72 if ((isInterface && klass.isInstance(tag)) || klass.isAssignableFrom(tag.getClass())) { 73 return tag; 74 } 75 from = tag; 76 } 77 } 78 79 85 public int doEndTag(String namespaceURI, String localName, String qName) 86 throws SAXException { 87 return EVAL_PAGE; 88 } 89 90 100 public int doStartTag(String namespaceURI, String localName, String qName, Attributes atts) 101 throws SAXException { 102 return EVAL_BODY; 103 } 104 105 112 public final Object findAttribute(String name) { 113 if (request == null) 114 request = ObjectModelHelper.getRequest(objectModel); 115 Object o = request.getAttribute(name); 116 if (o != null) 117 return o; 118 119 Session session = request.getSession(false); 120 if (session != null) { 121 o = session.getAttribute(name); 122 if (o != null) 123 return o; 124 } 125 Context context = ObjectModelHelper.getContext(objectModel); 126 return context.getAttribute(name); 127 } 128 129 134 public final Tag getParent() { 135 return parent; 136 } 137 138 public void recycle() { 139 getLogger().debug("recycle"); 140 this.parent = null; 141 this.resolver = null; 142 this.objectModel = null; 143 this.parameters = null; 144 this.request = null; 145 } 146 147 156 public final void setParent(Tag parent) { 157 this.parent = parent; 158 } 159 160 164 public void setup(SourceResolver resolver, Map objectModel, Parameters parameters) 165 throws SAXException , IOException { 166 this.resolver = resolver; 167 this.objectModel = objectModel; 168 this.parameters = parameters; 169 } 170 } 171 | Popular Tags |