1 16 17 package org.apache.taglibs.datetime; 18 19 import java.util.Locale ; 20 import java.util.ResourceBundle ; 21 import java.util.MissingResourceException ; 22 import java.text.DateFormatSymbols ; 23 import javax.servlet.jsp.tagext.TagSupport ; 24 import javax.servlet.jsp.PageContext ; 25 import javax.servlet.jsp.JspException ; 26 27 49 public class DateFormatSymbolsTag extends TagSupport { 50 51 private int scope = PageContext.PAGE_SCOPE; 52 private String localeRef = null; 53 private boolean locale_flag = false; 54 private String baseName = null; 55 56 private ResourceBundle bundle; 57 private DateFormatSymbols dateFormatSymbols; 58 59 public void setScope(String scope) { 60 if (scope.toLowerCase().equals("application")) { 61 this.scope = PageContext.APPLICATION_SCOPE; 62 } else if (scope.toLowerCase().equals("session")) { 63 this.scope = PageContext.SESSION_SCOPE; 64 } else if (scope.toLowerCase().equals("request")) { 65 this.scope = PageContext.REQUEST_SCOPE; 66 } else if (scope.toLowerCase().equals("page")) { 67 this.scope = PageContext.PAGE_SCOPE; 68 } 69 } 70 71 public void setBaseName(String baseName) { 72 this.baseName = baseName; 73 } 74 75 public void setLocale(boolean locale) { 76 this.locale_flag = locale; 77 } 78 79 public void setLocaleRef(String localeRef) { 80 this.localeRef = localeRef; 81 } 82 83 public int doStartTag() throws JspException { 84 Locale locale = null; 85 if (localeRef != null) { 86 locale = (Locale ) pageContext.findAttribute(localeRef); 87 } else if (locale_flag) { 88 locale = pageContext.getRequest().getLocale(); 89 } else { 90 dateFormatSymbols = new DateFormatSymbols (); 91 } 92 findBundle(locale); 93 buildDateFormatSymbols(locale); 94 if (this.getId() != null) { 95 pageContext.setAttribute(this.getId(), dateFormatSymbols, scope); 96 } 97 return SKIP_BODY; 98 } 99 100 public void release() { 101 super.release(); 102 scope = PageContext.PAGE_SCOPE; 103 localeRef = null; 104 locale_flag = false; 105 baseName = null; 106 } 107 108 private void buildDateFormatSymbols(Locale locale) { 109 if (locale != null) { 110 dateFormatSymbols = new DateFormatSymbols (locale); 111 } else { 112 dateFormatSymbols = new DateFormatSymbols (); 113 } 114 try { 116 String customAmPm = bundle.getString("ampm"); 117 if ("true".equals(customAmPm)) { 118 String ampm[] = new String [2]; 119 for (int i = 0; i < 2; i++) { 120 ampm[i] = bundle.getString("ampm" + i); 121 } 122 dateFormatSymbols.setAmPmStrings(ampm); 123 } 124 } catch (MissingResourceException e) { 125 } 126 127 try { 129 String customEras = bundle.getString("eras"); 130 if ("true".equals(customEras)) { 131 String eras[] = new String [2]; 132 for (int i = 0; i < 2; i++) { 133 eras[i] = bundle.getString("eras" + i); 134 } 135 dateFormatSymbols.setEras(eras); 136 } 137 } catch (MissingResourceException e) { 138 } 139 140 try { 142 String customShortMonth = bundle.getString("shortmonth"); 143 if ("true".equals(customShortMonth)) { 144 String shortmonth[] = new String [12]; 145 for (int i = 0; i < 12; i++) { 146 shortmonth[i] = bundle.getString("shortmonth" + i); 147 } 148 dateFormatSymbols.setShortMonths(shortmonth); 149 } 150 } catch (MissingResourceException e) { 151 } 152 153 try { 155 String customMonth = bundle.getString("month"); 156 if ("true".equals(customMonth)) { 157 String month[] = new String [12]; 158 for (int i = 0; i < 12; i++) { 159 month[i] = bundle.getString("month" + i); 160 } 161 dateFormatSymbols.setMonths(month); 162 } 163 } catch (MissingResourceException e) { 164 } 165 166 try { 168 String customWeekdays = bundle.getString("weekdays"); 169 if ("true".equals(customWeekdays)) { 170 String weekdays[] = new String [7]; 171 for (int i = 0; i < 7; i++) { 172 weekdays[i] = bundle.getString("weekdays" + i); 173 } 174 dateFormatSymbols.setWeekdays(weekdays); 175 } 176 } catch (MissingResourceException e) { 177 } 178 179 try { 181 String customShortWeekdays = bundle.getString("shortweekdays"); 182 if ("true".equals(customShortWeekdays)) { 183 String shortweekdays[] = new String [7]; 184 for (int i = 0; i < 7; i++) { 185 shortweekdays[i] = bundle.getString("shortweekdays" + i); 186 } 187 dateFormatSymbols.setShortWeekdays(shortweekdays); 188 } 189 } catch (MissingResourceException e) { 190 } 191 } 192 193 private void findBundle(Locale locale) { 194 if (locale != null) { 195 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 196 bundle = ResourceBundle.getBundle(baseName, locale, cl); 197 } else { 198 bundle = ResourceBundle.getBundle(baseName); 199 } 200 } 201 202 } 203 | Popular Tags |