1 16 17 package org.apache.taglibs.i18n; 18 19 import java.io.IOException ; 20 import java.text.Format ; 21 import java.text.DateFormat ; 22 import java.util.Enumeration ; 23 import java.util.Locale ; 24 25 import javax.servlet.ServletRequest ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspTagException ; 28 import javax.servlet.jsp.JspWriter ; 29 import javax.servlet.jsp.PageContext ; 30 import javax.servlet.jsp.tagext.Tag ; 31 import javax.servlet.jsp.tagext.TagSupport ; 32 33 39 public abstract class FormatTagSupport extends TagSupport { 40 41 protected static String _tagname = "i18n:"; 42 43 44 private Object value; 45 46 private Locale locale; 47 48 private String defaultText = ""; 49 50 51 public int doStartTag() throws JspException { 54 return EVAL_BODY_INCLUDE; 55 } 56 57 public int doEndTag() throws JspException { 58 try { 59 Object value = getValue(); 60 JspWriter out = pageContext.getOut(); 61 String text = null; 62 if ( value != null ) { 63 Format formatter = getFormat(); 64 if ( formatter == null ) { 65 throw new JspTagException ( 66 this._tagname + 67 " tag, could not find valid Format instance"); 68 } 69 text = formatter.format( value ); 70 } 71 else { 72 text = getDefaultText(); 73 } 74 if ( text != null ) { 75 out.print( text ); 76 } 77 } 78 catch ( IOException e ) { 79 handleIOException( e ); 80 } 81 return EVAL_PAGE; 82 } 83 84 public void release() { 85 super.release(); 86 value = null; 87 locale = null; 88 defaultText = ""; 89 } 90 91 public Object getValue() { 94 return value; 95 } 96 97 public void setValue( Object value ) { 98 this.value = value; 99 } 100 101 105 public Locale getLocale() { 106 if ( locale == null ) { 107 return findLocale(); 108 } 109 return locale; 110 } 111 112 public void setLocale( Locale locale ) { 113 this.locale = locale; 114 } 115 116 public String getDefaultText() { 117 return defaultText; 118 } 119 120 public void setDefaultText( String defaultText ) { 121 this.defaultText = defaultText; 122 } 123 124 125 128 130 protected abstract Format getFormat(); 131 132 protected void handleIOException( IOException e ) throws JspException { 133 pageContext.getServletContext().log( "Caught: IOException: " + e ); 134 throw new JspTagException ( 135 this._tagname + " tag, IOException: " + e ); 136 } 137 138 143 private Locale findLocale() { 144 LocaleTag localeTag = (LocaleTag) findAncestorWithClass( this, LocaleTag.class ); 146 if ( localeTag != null ) { 147 return localeTag.getLocale(); 148 } 149 else { 150 return pageContext.getResponse().getLocale(); 151 } 152 } 153 154 155 158 protected int getStyleCode( String style ) { 159 if ( "short".equalsIgnoreCase( style ) ) { 160 return DateFormat.SHORT; 161 } 162 else if ( "medium".equalsIgnoreCase( style ) ) { 163 return DateFormat.MEDIUM; 164 } 165 else if ( "long".equalsIgnoreCase( style ) ) { 166 return DateFormat.LONG; 167 } 168 else if ( "full".equalsIgnoreCase( style ) ) { 169 return DateFormat.FULL; 170 } 171 else { 172 return DateFormat.SHORT; 173 } 174 } 175 } 176 177 178 | Popular Tags |