Java tutorial
/* * Copyright (c) 2007 NTT DATA Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.terasoluna.fw.web.taglib; import java.math.BigDecimal; import java.text.DecimalFormat; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; import jp.terasoluna.fw.util.StringUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * <p><code>decimal</code>^ONX?B</p> * * <p> * ??A??_t?ltH?[}bg?o?A * XNveBO?`?B<br> * </p> * <p> * tH?[}bg??lf?[^?A<code>java.math.BigDecimal</code> * ^?A <code>java.lang.String</code> ^T|?[g?B * <code>java.lang.String</code> ^???A<code>BigDecimal</code> * RXgN^\Kv?B * </p> * * <p> * <code>BigDecimal</code> RXgN^s\???A * <code>NumberFormatException</code> X??[?B * </p> * * <br> * * <h5>^OT|?[g?</h5> * <p><code>DecimalTag</code> ?A?T|?[g?B</p> * <div align="center"> * <table width="90%" border="1" bgcolor="#FFFFFF"> * <tr> * <td> <b>?</b> </td> * <td> <b>ftHgl</b> </td> * <td> <b>K?{?</b> </td> * <td> <b>?s</b> </td> * <td> <b>Tv</b> </td> * </tr> * <tr> * <td><code>id</code></td> * <td></td> * <td> <code>false</code> </td> * <td> <code>false</code> </td> * <td align="left">tH?[}bgX|X?o?A * XNveBO?Zbg?w?B * tH?[}bgXNveBO?Zbg???A * <code>filter</code> ?wHTML * GXP?[v?B * </td> * </tr> * <tr> * <td><code>filter</code></td> * <td><code>true</code></td> * <td> <code>false</code> </td> * <td> <code>true</code> </td> * <td align="left">tH?[}bg?o??AHTML * GXP?[vw?B?A<code>id</code> ? * w???A?B * </td> * </tr> * <tr> * <td><code>ignore</code></td> * <td><code>false</code></td> * <td> <code>false</code> </td> * <td> <code>true</code> </td> * <td align="left"><code>name</code> ?w bean * w?B<code>false</code> * w?Abean <code>JspException</code> * ?B * </td> * </tr> * <tr> * <td><code>name</code></td> * <td></td> * <td> <code>false</code> </td> * <td> <code>true</code> </td> * <td align="left">tH?[}bg?v?peB?beanO?B * <code>property</code> ?w?A * <code>name</code> ?wCX^X * tH?[}bg??B???ACX^X?g * <code>java.math.BigDecimal</code> ^?A * <code>java.lang.String</code> ^?iE? * <code>BigDecimal</code> RXgN^\?j * Kv?B<code>value</code> * ?w???A?B * </td> * </tr> * <tr> * <td><code>property</code></td> * <td></td> * <td> <code>false</code> </td> * <td> <code>true</code> </td> * <td align="left"><code>name</code> ?w bean * ANZXv?peBO?B<code>value</code> ? * w???B * </td> * </tr> * <tr> * <td><code>scope</code></td> * <td>?i<code>findAttribute()</code> ?\bh????j</td> * <td> <code>false</code> </td> * <td> <code>true</code> </td> * <td align="left"><code>name</code> ?wbean * ??XR?[v?B * </td> * </tr> * <tr> * <td><code>value</code></td> * <td></td> * <td> <code>false</code> </td> * <td> <code>true</code> </td> * <td align="left">tH?[}bg?B?AE? * <code>BigDecimal</code> * RXgN^\Kv?B<code>value</code> * ?w???A<code>name</code> * ??A <code>property</code> ??B * </td> * </tr> * <tr> * <td><code>pattern</code></td> * <td></td> * <td> <code>true</code> </td> * <td> <code>true</code> </td> * <td align="left">tH?[}bg?o`?B<code>pattern</code> * ?w?o`?A<code>DecimalFormat</code> * NXp^?[?B???A<code>DecimalFormat</code> * NXhL?gQ??B * </td> * </tr> * <tr> * <td><code>scale</code></td> * <td></td> * <td> <code>false</code> </td> * <td> <code>true</code> </td> * <td align="left">???_??B<code>n</code> * w???A?? <code>n + 1</code> ?B * ?[h<code>round</code>?w?B<code>round</code> * ?w???Al?s?B * </td> * </tr> * <tr> * <td><code>round</code></td> * <td></td> * <td> <code>false</code> </td> * <td> <code>true</code> </td> * <td align="left">?[h?B<code>scale</code>? * w?AL?B<code>ROUND_HALF_UP</code>?il?j?A * <code>ROUND_FLOOR</code>?i??j?A<code>ROUND_CEILING</code> * ?i???j ?\?BftHg<code>ROUND_HALF_UP * </code>?s?BR?Ow???A * <code>IllegalArgumentException</code>X??[?B * </td> * </tr> * </table> * </div> * <h5>JX^^OXNveBO?</h5> * <p> * <code>DecimalTag</code> ?T|?[g?B * </p> * <div align="center"> * <table width="90%" border="1"> * <tr> * <td><b>?</b></td> * <td><b>^</b></td> * <td><b>L</b></td> * <td><b>L?q</b></td> * </tr> * <tr> * <td>JX^^O <code>id</code> ?wO</td> * <td><code>String</code></td> * <td>Jn^O?~</td> * <td align="left">JX^^O?o?AXNveBO? * ?????B * </td> * </tr> * </table> * </div> */ public class DecimalTag extends TagSupport { /** * VAo?[WID */ private static final long serialVersionUID = -2317420257734211793L; /** * ?ONX?B */ private static Log log = LogFactory.getLog(DecimalTag.class); /** * y?[WpXNveBO?O?B * */ protected String id = null; /** * XNveBO?O?B * * @return XNveBO? */ @Override public String getId() { return this.id; } /** * XNveBO?O??B * * @param id XNveBO?O */ @Override public void setId(String id) { this.id = id; } /** * HTML?o?tB^?[?BftHg * <code>true</code>?B */ protected boolean filter = true; /** * ?oHTMLtB^?[?B * * @return ?oHTMLtB^?[?? <code>true</code> */ public boolean getFilter() { return this.filter; } /** * ?oHTMLtB^?[??B * * @param filter ?oHTMLtB^?[?? * <code>true</code> */ public void setFilter(boolean filter) { this.filter = filter; } /** * bean?AP?i?o?j?B * ???AO?BftHg <code>false</code> * ?iO?j?B */ protected boolean ignore = false; /** * bean???B * * @return ?? <code>true</code> */ public boolean getIgnore() { return this.ignore; } /** * bean????B * * @param ignore ?? <code>true</code> */ public void setIgnore(boolean ignore) { this.ignore = ignore; } /** * tH?[}bg?f?[^beanO?B * */ protected String name = null; /** * tH?[}bg?f?[^beanO?B * * @return beanO */ public String getName() { return this.name; } /** * tH?[}bg?f?[^beanO??B * * @param name beanO */ public void setName(String name) { this.name = name; } /** * wbeanANZXv?peBO?B * */ protected String property = null; /** * wbeanANZXv?peBO?B * * @return wbeanANZXv?peBO */ public String getProperty() { return this.property; } /** * wbeanANZXv?peBO??B * * @param property wbeanANZXv?peBO */ public void setProperty(String property) { this.property = property; } /** * wbean?XR?[v?B * */ protected String scope = null; /** * wbean?XR?[v?B * * @return XR?[v */ public String getScope() { return this.scope; } /** * wbean?XR?[v??B * * @param scope wbean?XR?[v */ public void setScope(String scope) { this.scope = scope; } /** * ?otH?[}bgwp^?[?B * <code>DecimalFormat</code> ?w?B * */ protected String pattern = null; /** * ?otH?[}bgwp^?[?B * * @return ?otH?[}bgwp^?[ */ public String getPattern() { return this.pattern; } /** * ?otH?[}bgwp^?[??B * * @param pattern p^?[ */ public void setPattern(String pattern) { this.pattern = pattern; } /** * tH?[}bg?l?i?j?B * */ protected String value = null; /** * tH?[}bg?l?B * * @return tH?[}bg?l */ public String getValue() { return this.value; } /** * tH?[}bg?l??B * * @param value tH?[}bg?l */ public void setValue(String value) { this.value = value; } /** * l??_??B<code>n</code> w???A * ?? <code>n + 1</code> l?B * */ protected int scale = -1; /** * l??_??B * * @return ??_? */ public int getScale() { return this.scale; } /** * l??_???B * * @param scale ??_? */ public void setScale(int scale) { this.scale = scale; } /** * ?[h?B * */ protected String round = null; /** * ?[h?B * * @return ?[h */ public String getRound() { return this.round; } /** * ?[h??B * * @param round ?[h */ public void setRound(String round) { this.round = round; } /** * ^O]Jn?\bh?B * * @return ???w?B? <code>SKIP_BODY</code> * @throws JspException JSPO */ @Override public int doStartTag() throws JspException { Object value = this.value; if (value == null) { // bean???Av?beanbNAbv // ???A^?[ if (ignore) { if (TagUtil.lookup(pageContext, name, scope) == null) { return SKIP_BODY; // ?o } } // v?v?peBlbNAbv value = TagUtil.lookup(pageContext, name, property, scope); if (value == null) { return SKIP_BODY; // ?o } } // v?peBlString^xBigDecimal BigDecimal bd = null; if (value instanceof String) { String trimed = StringUtil.rtrim((String) value); if ("".equals(trimed)) { return SKIP_BODY; // ?o } bd = new BigDecimal(trimed); } else if (value instanceof BigDecimal) { bd = (BigDecimal) value; } else { return SKIP_BODY; // ?o } // ??_?w?? if (scale >= 0) { // round???[h?s?i???l?j if (round != null) { if ("ROUND_FLOOR".equalsIgnoreCase(round)) { bd = bd.setScale(scale, BigDecimal.ROUND_FLOOR); } else if ("ROUND_CEILING".equalsIgnoreCase(round)) { bd = bd.setScale(scale, BigDecimal.ROUND_CEILING); } else if ("ROUND_HALF_UP".equalsIgnoreCase(round)) { bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP); } else { log.error("Please set a rounding mode"); throw new IllegalArgumentException("Please set a rounding mode"); } } else { bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP); } } // tH?[}bg DecimalFormat df = new DecimalFormat(pattern); String output = df.format(bd); if (id != null) { // idw?AXNveBO?p // y?[WXR?[vZbg?B pageContext.setAttribute(id, output); } else { // idw?Av?peBlC^vg // ?BK?tB^?B if (filter) { TagUtil.write(pageContext, TagUtil.filter(output)); } else { TagUtil.write(pageContext, output); } } return SKIP_BODY; } /** * A?P?[g?B * */ @Override public void release() { super.release(); id = null; filter = true; ignore = false; name = null; property = null; scope = null; pattern = null; value = null; scale = -1; } }