1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.joda.time.contrib.jsptag;
18
19 import java.io.IOException;
20 import java.text.DateFormat;
21 import java.util.Locale;
22
23 import javax.servlet.jsp.JspException;
24 import javax.servlet.jsp.JspTagException;
25 import javax.servlet.jsp.PageContext;
26 import javax.servlet.jsp.tagext.BodyTagSupport;
27
28 import org.joda.time.DateTime;
29 import org.joda.time.DateTimeZone;
30 import org.joda.time.format.DateTimeFormat;
31 import org.joda.time.format.DateTimeFormatter;
32
33
34
35
36
37
38
39
40 public abstract class ParseDateTimeSupport extends BodyTagSupport {
41
42
43 protected String value;
44
45 protected boolean valueSpecified;
46
47 protected String pattern;
48
49 protected String style;
50
51 protected DateTimeZone dateTimeZone;
52
53 protected Locale locale;
54
55 private String var;
56
57 private int scope;
58
59
60
61
62 public ParseDateTimeSupport() {
63 super();
64 init();
65 }
66
67 private void init() {
68 value = null;
69 valueSpecified = false;
70 pattern = null;
71 style = null;
72 dateTimeZone = null;
73 locale = null;
74 scope = PageContext.PAGE_SCOPE;
75 }
76
77 public void setVar(String var) {
78 this.var = var;
79 }
80
81 public void setScope(String scope) {
82 this.scope = Util.getScope(scope);
83 }
84
85 public int doEndTag() throws JspException {
86 String input = null;
87
88
89 if (valueSpecified) {
90
91 input = value;
92 } else {
93
94 if (bodyContent != null && bodyContent.getString() != null) {
95 input = bodyContent.getString().trim();
96 }
97 }
98
99 if ((input == null) || input.equals("")) {
100 if (var != null) {
101 pageContext.removeAttribute(var, scope);
102 }
103 return EVAL_PAGE;
104 }
105
106
107 DateTimeFormatter formatter;
108 if (pattern != null) {
109 formatter = DateTimeFormat.forPattern(pattern);
110 } else if (style != null) {
111 formatter = DateTimeFormat.forStyle(style);
112 } else {
113 formatter = DateTimeFormat.fullDateTime();
114 }
115
116
117 Locale locale = this.locale;
118 if (locale == null) {
119 locale = Util.getFormattingLocale(pageContext, this, true,
120 DateFormat.getAvailableLocales());
121 }
122 if (locale != null) {
123 formatter = formatter.withLocale(locale);
124 }
125
126
127 DateTimeZone tz = this.dateTimeZone;
128 if (tz == null) {
129 tz = DateTimeZoneSupport.getDateTimeZone(pageContext, this);
130 }
131 if (tz != null) {
132 formatter = formatter.withZone(tz);
133 }
134
135
136 DateTime parsed = null;
137 try {
138 parsed = formatter.parseDateTime(input);
139 } catch (IllegalArgumentException iae) {
140 throw new JspException(Resources.getMessage(
141 "PARSE_DATE_PARSE_ERROR", input), iae);
142 }
143
144 if (var != null) {
145 pageContext.setAttribute(var, parsed, scope);
146 } else {
147 try {
148 pageContext.getOut().print(parsed);
149 } catch (IOException ioe) {
150 throw new JspTagException(ioe.toString(), ioe);
151 }
152 }
153
154 return EVAL_PAGE;
155 }
156
157
158 public void release() {
159 init();
160 }
161
162 }