1 16 17 package org.apache.taglibs.standard.lang.jpath.expression; 18 19 import java.text.DateFormat ; 20 import java.text.SimpleDateFormat ; 21 import java.util.Date ; 22 import java.util.Locale ; 23 import java.util.StringTokenizer ; 24 25 import javax.servlet.jsp.PageContext ; 26 27 import org.apache.taglibs.standard.lang.jpath.adapter.ConversionException; 28 import org.apache.taglibs.standard.lang.jpath.adapter.Convert; 29 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext; 30 import org.apache.taglibs.standard.lang.jpath.adapter.JSPDate; 31 32 39 public class FormatDateFunction extends SimpleNode { 40 41 48 public FormatDateFunction(int id) { 49 super(id); 50 } 51 52 60 public FormatDateFunction(Parser p, int id) { 61 super(p, id); 62 } 63 64 74 public String toNormalizedString() { 75 76 String normalized = ""; 77 78 normalized = "format-date(" + jjtGetChild(0).toNormalizedString() 79 + jjtGetChild(1).toNormalizedString() + "," + ")"; 80 81 return normalized; 82 } 83 84 101 public Object evaluate(PageContext pageContext, IterationContext icontext) 102 throws EvaluationException { 103 104 String formatted; 105 106 try { 107 JSPDate arg = 108 Convert.toJSPDate(jjtGetChild(0).evaluate(pageContext, 109 icontext)); 110 String pattern = 111 Convert.toString(jjtGetChild(1).evaluate(pageContext, 112 icontext)); 113 DateFormat form; 114 115 if (jjtGetNumChildren() > 2) { 116 String arg3 = 117 Convert.toString(jjtGetChild(2).evaluate(pageContext, 118 icontext)); 119 Locale locale = getLocale(arg3); 120 121 form = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); 122 } else { 123 form = DateFormat.getInstance(); 124 } 125 126 try { 127 ((SimpleDateFormat ) form).applyPattern(pattern); 128 129 formatted = form.format(new Date (arg.getTime().longValue())); 130 } catch (IllegalArgumentException iae) { 131 formatted = new String (""); 132 } 133 } catch (ConversionException ce) { 134 throw new EvaluationException(this, ce.getMessage()); 135 } 136 137 return formatted; 138 } 139 140 149 private Locale getLocale(String arg) { 150 151 Locale result; 152 StringTokenizer st = new StringTokenizer (arg, "_"); 153 String language = null; 154 String country = null; 155 String variant = null; 156 157 if (st.hasMoreTokens()) { 158 language = st.nextToken(); 159 160 if (st.hasMoreTokens()) { 161 country = st.nextToken(); 162 163 if (st.hasMoreTokens()) { 164 variant = st.nextToken(); 165 result = new Locale (language, country, variant); 166 } else { 167 result = new Locale (language, country); 168 } 169 } else { 170 result = new Locale (language, ""); 171 } 172 } else { 173 result = Locale.getDefault(); 174 } 175 176 return result; 177 } 178 } 179 | Popular Tags |