|
37 | 37 | */
|
38 | 38 | public abstract class FormatSupport extends TagSupport {
|
39 | 39 |
|
40 |
| - private static final long serialVersionUID = 1L; |
41 |
| - |
42 |
| - /** The value attribute. */ |
43 |
| - protected Object value; |
44 |
| - /** The pattern attribute. */ |
45 |
| - protected String pattern; |
46 |
| - /** The style attribute. */ |
47 |
| - protected String style; |
48 |
| - /** The zoneId attribute. */ |
49 |
| - protected ZoneId zoneId; |
50 |
| - /** The locale attribute. */ |
51 |
| - protected Locale locale; |
52 |
| - /** The var attribute. */ |
53 |
| - private String var; |
54 |
| - /** The scope attribute. */ |
55 |
| - private int scope; |
56 |
| - |
57 |
| - /** |
58 |
| - * Constructor. |
59 |
| - */ |
60 |
| - public FormatSupport() { |
61 |
| - super(); |
62 |
| - init(); |
63 |
| - } |
64 |
| - |
65 |
| - private void init() { |
66 |
| - var = null; |
67 |
| - value = null; |
68 |
| - pattern = null; |
69 |
| - style = null; |
70 |
| - zoneId = null; |
71 |
| - locale = null; |
72 |
| - scope = PageContext.PAGE_SCOPE; |
73 |
| - } |
| 40 | + private static final long serialVersionUID = 1L; |
| 41 | + |
| 42 | + /** The value attribute. */ |
| 43 | + protected Object value; |
| 44 | + /** The pattern attribute. */ |
| 45 | + protected String pattern; |
| 46 | + /** The style attribute. */ |
| 47 | + protected String style; |
| 48 | + /** The zoneId attribute. */ |
| 49 | + protected ZoneId zoneId; |
| 50 | + /** The locale attribute. */ |
| 51 | + protected Locale locale; |
| 52 | + /** The var attribute. */ |
| 53 | + private String var; |
| 54 | + /** The scope attribute. */ |
| 55 | + private int scope; |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructor. |
| 59 | + */ |
| 60 | + public FormatSupport() { |
| 61 | + super(); |
| 62 | + init(); |
| 63 | + } |
| 64 | + |
| 65 | + private void init() { |
| 66 | + var = null; |
| 67 | + value = null; |
| 68 | + pattern = null; |
| 69 | + style = null; |
| 70 | + zoneId = null; |
| 71 | + locale = null; |
| 72 | + scope = PageContext.PAGE_SCOPE; |
| 73 | + } |
74 | 74 |
|
75 | 75 | @SuppressWarnings("UnusedDeclaration")
|
76 |
| - public void setVar(String var) { |
77 |
| - this.var = var; |
78 |
| - } |
| 76 | + public void setVar(String var) { |
| 77 | + this.var = var; |
| 78 | + } |
79 | 79 |
|
80 | 80 | @SuppressWarnings("UnusedDeclaration")
|
81 |
| - public void setScope(String scope) { |
82 |
| - this.scope = Util.getScope(scope); |
83 |
| - } |
84 |
| - |
85 |
| - /* |
86 |
| - * Formats the given instant or partial. |
87 |
| - */ |
88 |
| - public int doEndTag() throws JspException { |
89 |
| - if (value == null) { |
90 |
| - if (var != null) { |
91 |
| - pageContext.removeAttribute(var, scope); |
92 |
| - } |
93 |
| - return EVAL_PAGE; |
94 |
| - } |
95 |
| - |
96 |
| - // Create formatter |
97 |
| - DateTimeFormatter formatter; |
98 |
| - if (pattern != null) { |
99 |
| - formatter = DateTimeFormatter.ofPattern(pattern); |
100 |
| - } else if (style != null) { |
101 |
| - formatter = Util.createFormatterForStyle(style); |
102 |
| - } else { |
103 |
| - // use a medium date (no time) style by default; same as jstl |
104 |
| - formatter = Util.createFormatterForStyle("M-"); |
105 |
| - } |
106 |
| - |
107 |
| - // set formatter locale |
108 |
| - Locale locale = this.locale; |
109 |
| - if (locale == null) { |
110 |
| - locale = Util.getFormattingLocale(pageContext, true, |
| 81 | + public void setScope(String scope) { |
| 82 | + this.scope = Util.getScope(scope); |
| 83 | + } |
| 84 | + |
| 85 | + /* |
| 86 | + * Formats the given instant or partial. |
| 87 | + */ |
| 88 | + public int doEndTag() throws JspException { |
| 89 | + if (value == null) { |
| 90 | + if (var != null) { |
| 91 | + pageContext.removeAttribute(var, scope); |
| 92 | + } |
| 93 | + return EVAL_PAGE; |
| 94 | + } |
| 95 | + |
| 96 | + // Create formatter |
| 97 | + DateTimeFormatter formatter; |
| 98 | + if (pattern != null) { |
| 99 | + formatter = DateTimeFormatter.ofPattern(pattern); |
| 100 | + } else if (style != null) { |
| 101 | + formatter = Util.createFormatterForStyle(style); |
| 102 | + } else { |
| 103 | + // use a medium date (no time) style by default; same as jstl |
| 104 | + formatter = Util.createFormatterForStyle("M-"); |
| 105 | + } |
| 106 | + |
| 107 | + // set formatter locale |
| 108 | + Locale locale = this.locale; |
| 109 | + if (locale == null) { |
| 110 | + locale = Util.getFormattingLocale(pageContext, true, |
111 | 111 | DateFormat.getAvailableLocales());
|
112 |
| - } |
113 |
| - if (locale != null) { |
114 |
| - formatter = formatter.withLocale(locale); |
115 |
| - } |
116 |
| - |
117 |
| - // set formatter timezone |
118 |
| - ZoneId zoneId = this.zoneId; |
119 |
| - if (zoneId == null) { |
120 |
| - zoneId = ZoneIdSupport.getZoneId(pageContext, this); |
121 |
| - } |
122 |
| - if (zoneId != null) { |
123 |
| - formatter = formatter.withZone(zoneId); |
124 |
| - } else { |
125 |
| - if (value instanceof Instant || |
| 112 | + } |
| 113 | + if (locale != null) { |
| 114 | + formatter = formatter.withLocale(locale); |
| 115 | + } |
| 116 | + |
| 117 | + // set formatter timezone |
| 118 | + ZoneId zoneId = this.zoneId; |
| 119 | + if (zoneId == null) { |
| 120 | + zoneId = ZoneIdSupport.getZoneId(pageContext, this); |
| 121 | + } |
| 122 | + if (zoneId != null) { |
| 123 | + formatter = formatter.withZone(zoneId); |
| 124 | + } else { |
| 125 | + if (value instanceof Instant || |
126 | 126 | value instanceof LocalDateTime ||
|
127 | 127 | value instanceof OffsetDateTime ||
|
128 | 128 | value instanceof OffsetTime ||
|
129 | 129 | value instanceof LocalTime)
|
130 |
| - // these time objects may need a zone to resolve some patterns |
131 |
| - // and/or styles, and as there is no zone we revert to the |
132 |
| - // system default zone |
133 |
| - formatter = formatter.withZone(ZoneId.systemDefault()); |
134 |
| - } |
135 |
| - |
136 |
| - // format value |
137 |
| - String formatted; |
138 |
| - if (value instanceof TemporalAccessor) { |
139 |
| - formatted = formatter.format((TemporalAccessor) value); |
140 |
| - } else { |
141 |
| - throw new JspException( |
| 130 | + // these time objects may need a zone to resolve some patterns |
| 131 | + // and/or styles, and as there is no zone we revert to the |
| 132 | + // system default zone |
| 133 | + formatter = formatter.withZone(ZoneId.systemDefault()); |
| 134 | + } |
| 135 | + |
| 136 | + // format value |
| 137 | + String formatted; |
| 138 | + if (value instanceof TemporalAccessor) { |
| 139 | + formatted = formatter.format((TemporalAccessor) value); |
| 140 | + } else { |
| 141 | + throw new JspException( |
142 | 142 | "value attribute of format tag must be a TemporalAccessor," +
|
143 | 143 | " was: " + value.getClass().getName());
|
144 |
| - } |
145 |
| - |
146 |
| - if (var != null) { |
147 |
| - pageContext.setAttribute(var, formatted, scope); |
148 |
| - } else { |
149 |
| - try { |
150 |
| - pageContext.getOut().print(formatted); |
151 |
| - } catch (IOException ioe) { |
152 |
| - throw new JspTagException(ioe.toString(), ioe); |
153 |
| - } |
154 |
| - } |
155 |
| - |
156 |
| - return EVAL_PAGE; |
157 |
| - } |
158 |
| - |
159 |
| - // Releases any resources we may have (or inherit) |
160 |
| - public void release() { |
161 |
| - init(); |
162 |
| - } |
| 144 | + } |
| 145 | + |
| 146 | + if (var != null) { |
| 147 | + pageContext.setAttribute(var, formatted, scope); |
| 148 | + } else { |
| 149 | + try { |
| 150 | + pageContext.getOut().print(formatted); |
| 151 | + } catch (IOException ioe) { |
| 152 | + throw new JspTagException(ioe.toString(), ioe); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return EVAL_PAGE; |
| 157 | + } |
| 158 | + |
| 159 | + // Releases any resources we may have (or inherit) |
| 160 | + public void release() { |
| 161 | + init(); |
| 162 | + } |
163 | 163 | }
|
0 commit comments