- Revision
- 76
- Author
- mauro
- Date
- 2007-05-24 02:44:19 -0500 (Thu, 24 May 2007)
Log Message
Added internal taglib files missed out in initial commit.
Added Paths
- trunk/extensions/taglib/src/main/java/org/waffle/taglib/internal/BasicIterationTag.java
- trunk/extensions/taglib/src/main/java/org/waffle/taglib/internal/BasicTag.java
Diff
Added: trunk/extensions/taglib/src/main/java/org/waffle/taglib/internal/BasicIterationTag.java (0 => 76)
--- trunk/extensions/taglib/src/main/java/org/waffle/taglib/internal/BasicIterationTag.java (rev 0) +++ trunk/extensions/taglib/src/main/java/org/waffle/taglib/internal/BasicIterationTag.java 2007-05-24 07:44:19 UTC (rev 76) @@ -0,0 +1,87 @@ +package org.waffle.taglib.internal; + +import java.io.IOException; +import java.util.Collection; +import java.util.Iterator; + +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.tagext.BodyTag; +import javax.servlet.jsp.tagext.BodyTagSupport; + +public abstract class BasicIterationTag<T> extends BodyTagSupport { + + private Iterator<T> iterator; + + private final boolean showIfEmpty; + + private Collection<T> iterable; + + public BasicIterationTag() { + this(true); + } + + public BasicIterationTag(boolean showIfEmpty) { + this.showIfEmpty = showIfEmpty; + } + + @Override + public int doStartTag() throws JspException { + this.iterable = getCollection(); + try { + if (notEmpty() || showIfEmpty) { + beginTag(); + } + if (iterable == null || iterable.isEmpty()) { + return BodyTag.SKIP_BODY; + } + this.iterator = iterable.iterator(); + beforeBodyFor(iterator.next()); + return BodyTag.EVAL_BODY_INCLUDE; + } catch (IOException e) { + throw new JspException(e); + } + } + + private boolean notEmpty() { + return iterable != null && !iterable.isEmpty(); + } + + protected abstract void beginTag() throws JspException, IOException; + + public abstract void beforeBodyFor(T current) throws JspException, IOException; + + protected abstract void endTag() throws JspException, IOException; + + protected abstract Collection<T> getCollection(); + + @Override + public int doEndTag() throws JspException { + try { + if (notEmpty() || showIfEmpty) { + endTag(); + } + } catch (IOException e) { + throw new JspException(e); + } + this.iterable = null; + this.iterator = null; + return BodyTag.EVAL_PAGE; + } + + @Override + public void doInitBody() throws JspException { + } + + public int doAfterBody() throws JspException { + if (!iterator.hasNext()) { + return BodyTag.EVAL_PAGE; + } + try { + beforeBodyFor(iterator.next()); + } catch (IOException e) { + throw new JspException(e); + } + return BodyTag.EVAL_BODY_AGAIN; + } + +}
Added: trunk/extensions/taglib/src/main/java/org/waffle/taglib/internal/BasicTag.java (0 => 76)
--- trunk/extensions/taglib/src/main/java/org/waffle/taglib/internal/BasicTag.java (rev 0) +++ trunk/extensions/taglib/src/main/java/org/waffle/taglib/internal/BasicTag.java 2007-05-24 07:44:19 UTC (rev 76) @@ -0,0 +1,248 @@ +package org.waffle.taglib.internal; + +import java.io.IOException; +import java.io.Writer; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.JspWriter; +import javax.servlet.jsp.PageContext; +import javax.servlet.jsp.el.ELException; +import javax.servlet.jsp.jstl.fmt.LocalizationContext; +import javax.servlet.jsp.tagext.BodyContent; +import javax.servlet.jsp.tagext.BodyTag; +import javax.servlet.jsp.tagext.Tag; + +import org.apache.taglibs.standard.tag.common.core.UrlSupport; +import org.apache.taglibs.standard.tag.common.fmt.BundleSupport; +import org.apache.taglibs.standard.tag.common.fmt.MessageSupport; + +import org.waffle.taglib.form.IterationResult; + +/** + * A basic tag. This class has been created in order to be extended. + * + * @author Guilherme Silveira + * @author Nico Steppat + */ +public abstract class BasicTag implements BodyTag { + + private Tag parent; + + protected PageContext pageContext; + + private BodyContent bodyContent; + + /** + * If true, the FormElement will be rendered. + */ + private boolean rendered; + + public BasicTag() { + release(); + } + + /** + * Searchs a parent tag that implements the selected type. + * + * @param <T> + * the type to search + * @param type + * the class type to search + * @return the tag found or null if not found + */ + @SuppressWarnings("unchecked") + protected <T> T findAncestor(Class<T> type) { + Tag current = parent; + while (current != null && !type.isAssignableFrom(current.getClass())) { + current = current.getParent(); + } + return (T) current; + } + + public Tag getParent() { + return parent; + } + + public void setRendered(boolean rendered) { + this.rendered = rendered; + + } + + public void release() { + /*parent = null; + pageContext = null;*/ + rendered = true; + } + + public void setPageContext(PageContext pageContext) { + this.pageContext = pageContext; + } + + public void setParent(Tag parent) { + this.parent = parent; + } + + /** + * Returns the i18n message for some key + * + * @param key + * the key to search for + * @return the i18n message + */ + public String getI18NMessage(String key) { + if (key == null || key.equals("")) { + return ""; + } + LocalizationContext locCtxt = getLocalizationContext(); + + if (locCtxt != null) { + ResourceBundle bundle = locCtxt.getResourceBundle(); + if (bundle != null) { + try { + return bundle.getString(key); + } catch (MissingResourceException mre) { + return MessageSupport.UNDEFINED_KEY + key + + MessageSupport.UNDEFINED_KEY; + } + } + } + return MessageSupport.UNDEFINED_KEY + key + + MessageSupport.UNDEFINED_KEY; + } + + protected LocalizationContext getLocalizationContext() { + return BundleSupport.getLocalizationContext(pageContext); + } + + /** + * Retrieves an encoded url from a basic one. + * + * @param url + * the original url + * @return the encoded url + * @throws JspException + */ + public String getAbsoluteUrl(String url) throws JspException { + return UrlSupport.resolveUrl(url, null, pageContext); + } + + /** + * Evaluates some _expression_. + */ + protected String evaluate(String _expression_) throws JspException { + try { + return (String) pageContext.getExpressionEvaluator().evaluate( + _expression_, String.class, + pageContext.getVariableResolver(), null); + } catch (ELException e) { + throw new JspException(e); + } + } + + /** + * Evaluates this _expression_ in EL. Returns the same as evaluate("${" + + * _expression_ + "}"); + * + * @param _expression_ + * _expression_ in EL. + */ + protected String evaluateEl(String _expression_) throws JspException { + return evaluate("${" + _expression_ + "}"); + } + + public void setBodyContent(BodyContent bodyContent) { + this.bodyContent = bodyContent; + } + + public void doInitBody() throws JspException { + // does nothing + } + + public int doAfterBody() throws JspException { + try { + IterationResult result = afterBody(pageContext.getOut()); + if (result == IterationResult.BODY_AGAIN) { + beforeBody(pageContext.getOut()); + } + return result.getTagResult(); + } catch (IOException ex) { + throw new JspException(ex); + } + } + + /** + * To be implemented by child classes that want to do something after the + * body. + * + * @param out + * the writer + * @throws IOException + */ + protected IterationResult afterBody(JspWriter out) throws IOException { + // does nothing + return IterationResult.PAGE; + } + + public int doEndTag() throws JspException { + if(!rendered) { + return IterationResult.PAGE.getTagResult(); + } + try { + end(pageContext.getOut()); + } catch (IOException e) { + throw new JspException(e); + } + return Tag.EVAL_PAGE; + } + + public int doStartTag() throws JspException { + if(!rendered) { + return IterationResult.PAGE.getTagResult(); + } + try { + IterationResult result = start(pageContext.getOut()); + if (result == IterationResult.BODY) { + beforeBody(pageContext.getOut()); + } + return result.getTagResult(); + } catch (IOException e) { + throw new JspException(e); + } + } + + /** + * Starts this tag once. + * + * @throws JspException + * @throws IOException + */ + protected abstract IterationResult start(Writer out) throws JspException, + IOException; + + /** + * Executes something before body evaluation. + * + * @throws JspException + * @throws IOException + */ + protected void beforeBody(Writer out) throws JspException, IOException { + // does nothing by default + } + + /** + * Ends this tag. + * + * @throws JspException + * @throws IOException + */ + protected void end(Writer out) throws JspException, IOException { + // does nothing by default + } + + public boolean isRendered() { + return rendered; + } + +} \ No newline at end of file
To unsubscribe from this list please visit:
