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 java.io.IOException;
14  
15  import javax.servlet.jsp.JspException;
16  import javax.servlet.jsp.JspWriter;
17  import javax.servlet.jsp.tagext.BodyTagSupport;
18  
19  import org.apache.commons.text.StringEscapeUtils;
20  
21  /**
22   * The Class OutTag.
23   */
24  public class OutTag extends BodyTagSupport {
25  
26    /** The Constant serialVersionUID. */
27    private static final long serialVersionUID = 1L;
28  
29    /** The max length. */
30    private int maxLength = -1;
31  
32    /** The ellipsis right. */
33    private boolean ellipsisRight = true;
34  
35    /** The value. */
36    private transient Object value;
37  
38    /**
39     * Gets the value.
40     *
41     * @return the value
42     */
43    public Object getValue() {
44      return value;
45    }
46  
47    /**
48     * Sets the value.
49     *
50     * @param value the new value
51     */
52    public void setValue(Object value) {
53      this.value = value;
54    }
55  
56    /**
57     * Gets the max length.
58     *
59     * @return the max length
60     */
61    public int getMaxLength() {
62      return maxLength;
63    }
64  
65    /**
66     * Sets the max length.
67     *
68     * @param maxLength the new max length
69     */
70    public void setMaxLength(int maxLength) {
71      this.maxLength = maxLength;
72    }
73  
74    /**
75     * Checks if is ellipsis right.
76     *
77     * @return true, if is ellipsis right
78     */
79    public boolean isEllipsisRight() {
80      return ellipsisRight;
81    }
82  
83    /**
84     * Sets the ellipsis right.
85     *
86     * @param ellipsisRight the new ellipsis right
87     */
88    public void setEllipsisRight(boolean ellipsisRight) {
89      this.ellipsisRight = ellipsisRight;
90    }
91  
92    @Override
93    public int doStartTag() throws JspException {
94      if (value != null) {
95        print(value.toString(), pageContext.getOut());
96        return SKIP_BODY;
97      }
98      return super.doStartTag();
99    }
100 
101   @Override
102   public int doAfterBody() throws JspException {
103     print(getBodyContent().getString().trim(), getBodyContent().getEnclosingWriter());
104     return SKIP_BODY;
105   }
106 
107   /**
108    * Prints the.
109    *
110    * @param displayValue the display value
111    * @param out the out
112    *
113    * @throws JspException the jsp exception
114    */
115   private void print(String displayValue, JspWriter out) throws JspException {
116     try {
117       if (maxLength != -1 && displayValue.length() > maxLength) {
118         String newValue;
119         if (ellipsisRight) {
120           newValue = displayValue.substring(0, maxLength - 3) + "...";
121         } else {
122           newValue = "..." + displayValue.substring(displayValue.length() - maxLength + 3);
123         }
124         String title = StringEscapeUtils.escapeHtml4(displayValue);
125         out.print("<span title=\"" + title + "\">" + newValue + "</span>");
126       } else {
127         out.print(displayValue);
128       }
129     } catch (IOException e) {
130       throw new JspException(e);
131     }
132   }
133 
134 }