1 16 17 package org.apache.taglibs.i18n; 18 19 import java.io.IOException ; 20 21 import java.util.ResourceBundle ; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.JspTagException ; 25 import javax.servlet.jsp.PageContext ; 26 import javax.servlet.jsp.tagext.BodyTag ; 27 import javax.servlet.jsp.tagext.BodyTagSupport ; 28 29 30 37 public abstract class ConditionalTagSupport extends BodyTagSupport 38 { 39 40 protected static String _tagname = "i18n:"; 41 42 private String _key = null; 44 private ResourceBundle _bundle = null; 45 private String _bundleRef = null; 46 47 50 public void setKey(String key) 51 { 52 _key = key; 53 } 54 55 60 public void setBundleRef(String varName) 61 { 62 _bundleRef = varName; 63 } 64 65 70 public void setBundle(ResourceBundle aBundle) 71 { 72 _bundle = aBundle; 73 } 74 75 78 public ResourceBundle getBundle() 79 { 80 if ( _bundleRef != null ) { 81 _bundle = (ResourceBundle )pageContext.getAttribute(_bundleRef); 82 } 83 if ( _bundle == null ) { 84 BundleTag bundleTag = (BundleTag)this.findAncestorWithClass(this,BundleTag.class); 85 if (bundleTag != null) { 86 _bundle = bundleTag.getBundle(); 87 } 88 } 89 if ( _bundle == null ) { 90 _bundle = ResourceHelper.getBundle(pageContext); 91 } 92 return _bundle; 93 } 94 95 98 public void release() 99 { 100 super.release(); 101 _key = null; 102 _bundle = null; 103 } 104 105 108 public abstract boolean shouldEvaluate() throws JspException ; 109 110 115 protected String getValue() throws JspException 116 { 117 if ( _key == null) { 119 throw new JspTagException ( 120 this._tagname + " tag, requires a key attribute."); 121 } 122 123 ResourceBundle bundle = this.getBundle(); 125 if ( this.getBundle() == null) { 126 throw new JspTagException ( 127 this._tagname + " tag, no bundle found for use with tag"); 128 } 129 130 String value = null; 131 try { 132 value = bundle.getString(_key); 133 } catch (java.util.MissingResourceException e) { 134 value = null; 135 } 136 return value; 137 } 138 139 142 public int doStartTag() throws JspException 143 { 144 int returnValue; 145 146 if ( this.shouldEvaluate() ) { 148 returnValue = BodyTag.EVAL_BODY_TAG; 149 } else { 150 returnValue = BodyTag.SKIP_BODY; 151 } 152 153 return returnValue; 154 } 155 156 164 public int doEndTag() throws JspException 165 { 166 try { 167 if (bodyContent != null) { 168 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 169 } 170 } catch (java.io.IOException ioe) { 171 throw new JspTagException ( 172 this._tagname + " tag, IO Error: " + ioe.getMessage()); 173 } 174 175 return BodyTag.EVAL_PAGE; 177 } 178 179 } 180 | Popular Tags |