1
2
3
4
5
6
7
8
9
10
11 package psiprobe.jsp;
12
13 import jakarta.servlet.jsp.JspException;
14 import jakarta.servlet.jsp.JspWriter;
15 import jakarta.servlet.jsp.tagext.BodyTagSupport;
16
17 import java.io.IOException;
18
19 import org.apache.commons.text.StringEscapeUtils;
20
21
22
23
24 public class OutTag extends BodyTagSupport {
25
26
27 private static final long serialVersionUID = 1L;
28
29
30 private int maxLength = -1;
31
32
33 private boolean ellipsisRight = true;
34
35
36 private transient Object value;
37
38
39
40
41
42
43 public Object getValue() {
44 return value;
45 }
46
47
48
49
50
51
52 public void setValue(Object value) {
53 this.value = value;
54 }
55
56
57
58
59
60
61 public int getMaxLength() {
62 return maxLength;
63 }
64
65
66
67
68
69
70 public void setMaxLength(int maxLength) {
71 this.maxLength = maxLength;
72 }
73
74
75
76
77
78
79 public boolean isEllipsisRight() {
80 return ellipsisRight;
81 }
82
83
84
85
86
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
109
110
111
112
113
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 }