View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.jsp;
12  
13  import jakarta.servlet.jsp.JspException;
14  import jakarta.servlet.jsp.tagext.TagSupport;
15  
16  import java.io.IOException;
17  
18  import psiprobe.tools.SizeExpression;
19  
20  /**
21   * JSP tag to convert size from bytes into human readable form: KB, MB, GB or TB depending on how
22   * large the value in bytes is.
23   */
24  public class VolumeTag extends TagSupport {
25  
26    /** The Constant serialVersionUID. */
27    private static final long serialVersionUID = 1L;
28  
29    /** The value. */
30    private long value;
31  
32    /** The fractions. */
33    private int fractions;
34  
35    /**
36     * Sets the value.
37     *
38     * @param value the new value
39     */
40    public void setValue(long value) {
41      this.value = value;
42    }
43  
44    /**
45     * Gets the fractions.
46     *
47     * @return the fractions
48     */
49    public int getFractions() {
50      return fractions;
51    }
52  
53    /**
54     * Sets the fractions.
55     *
56     * @param fractions the new fractions
57     */
58    public void setFractions(int fractions) {
59      this.fractions = fractions;
60    }
61  
62    @Override
63    public int doStartTag() throws JspException {
64      String title = Long.toString(value);
65      String newValue = SizeExpression.format(value, fractions, true);
66      try {
67        pageContext.getOut().write("<span title=\"" + title + "\">" + newValue + "</span>");
68      } catch (IOException e) {
69        throw new JspException("Exception writing value to JspWriter", e);
70      }
71  
72      return EVAL_BODY_INCLUDE;
73    }
74  
75  }