Skip to content

Commit 6f5a042

Browse files
committed
Fixes parsing bug, adds multiple parse tags.
Release v1.1.0. Fixes #1. Adds multiple parse tags.
1 parent eb8074a commit 6f5a042

17 files changed

+685
-132
lines changed

README.md

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ almost exactly as the tags in the original Joda-Time JSP Tags.
3333
Add the dependency to your project:
3434

3535
#### Gradle
36-
`compile 'net.sargue:java-time-jsptags:1.0.0'`
36+
`compile 'net.sargue:java-time-jsptags:1.1.0'`
3737

3838
#### Maven
3939

4040
```xml
4141
<dependency>
4242
<groupId>net.sargue</groupId>
4343
<artifactId>java-time-jsptags</artifactId>
44-
<version>1.0.0</version>
44+
<version>1.1.0</version>
4545
</dependency>
4646
```
4747

@@ -55,7 +55,7 @@ Declare the library as follows in your jsp pages:
5555

5656
#### Tags
5757

58-
##### FORMAT
58+
##### &lt;javatime:format&gt;
5959

6060
Example:
6161
```
@@ -65,7 +65,8 @@ Example:
6565
Formats any `java.util.Temporal` like `Instant`, `LocalDateTime`, `LocalDate`, `LocalTime`, etc.
6666
The `var` and `scope` attributes can be used to set the value of a variable instead of printing the result.
6767
The time zone may be specified using an attribute, an enclosing `<javatime:zoneId/>` tag,
68-
preceding `<javatime:setZoneId/>` tag, or via the `net.sargue.time.zoneId` scoped variable.
68+
preceding `<javatime:setZoneId/>` tag, or via the `net.sargue.time.zoneId` scoped variable. If the
69+
time zone is not specified it will default to the **system default time-zone**.
6970

7071
Attributes:
7172

@@ -77,16 +78,40 @@ Attributes:
7778
| locale | The locale to use for formatting. |
7879
| style | The style to use for formatting (see java.time format documentation for recognized style strings) |
7980
| pattern | The pattern to use for formatting (see java.time format documentation for recognized pattern strings) |
80-
| zoneId | The time zone to use for formatting. |
81+
| zoneId | The time zone to use for formatting. See comment above for fallback and defaults. |
8182

82-
##### PARSEINSTANT
83+
##### &lt;javatime:parseInstant&gt;
8384

8485
Example:
8586
```
8687
<javatime:parseInstant value="${dt}" style="MS" />
8788
```
8889

89-
Parses a string into an `Instant` object.
90+
Parses a string into an `java.time.Instant` object.
91+
The `var` and `scope` attributes can be used to set the value of a variable instead of printing the result.
92+
The time zone may be specified using an attribute, an enclosing `<javatime:zoneId/>` tag,
93+
preceding `<javatime:setZoneId/>` tag, or via the `net.sargue.time.zoneId` scoped variable.
94+
95+
Attributes:
96+
97+
| Attribute | Description |
98+
|:----------|:----------------------------------------------------------------------------------------------------------------------------------------------|
99+
| value | Required unless value is nested within tag. Must be a string which can be parsed into a `java.time.Instant` according to the parsing options specified. |
100+
| var | The scoped variable to set. |
101+
| scope | The scope of the variable to set. |
102+
| locale | The locale to use for parsing. |
103+
| style | The style to use for parsing (see java.time format documentation for recognized style strings) |
104+
| pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
105+
| zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |
106+
107+
##### &lt;javatime:parseLocalDateTime&gt;
108+
109+
Example:
110+
```
111+
<javatime:parseLocalDateTime value="${dt}" style="MS" />
112+
```
113+
114+
Parses a string into an `java.time.LocalDateTime` object.
90115
The `var` and `scope` attributes can be used to set the value of a variable instead of printing the result.
91116
The time zone may be specified using an attribute, an enclosing `<javatime:zoneId/>` tag,
92117
preceding `<javatime:setZoneId/>` tag, or via the `net.sargue.time.zoneId` scoped variable.
@@ -95,15 +120,63 @@ Attributes:
95120

96121
| Attribute | Description |
97122
|:----------|:----------------------------------------------------------------------------------------------------------------------------------------------|
98-
| value | Required unless value is nested within tag. Must be a string which can be parsed into a `Instant` according to the parsing options specified. |
123+
| value | Required unless value is nested within tag. Must be a string which can be parsed into a `java.time.LocalDateTime` according to the parsing options specified. |
99124
| var | The scoped variable to set. |
100125
| scope | The scope of the variable to set. |
101126
| locale | The locale to use for parsing. |
102127
| style | The style to use for parsing (see java.time format documentation for recognized style strings) |
103128
| pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
104-
| zoneId | The time zone to use for parsing. |
129+
| zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |
105130

106-
##### ZONEID
131+
##### &lt;javatime:parseLocalDate&gt;
132+
133+
Example:
134+
```
135+
<javatime:parseLocalDate value="28/10/2015" pattern="dd/MM/yyyy" />
136+
```
137+
138+
Parses a string into an `java.time.LocalDate` object.
139+
The `var` and `scope` attributes can be used to set the value of a variable instead of printing the result.
140+
The time zone may be specified using an attribute, an enclosing `<javatime:zoneId/>` tag,
141+
preceding `<javatime:setZoneId/>` tag, or via the `net.sargue.time.zoneId` scoped variable.
142+
143+
Attributes:
144+
145+
| Attribute | Description |
146+
|:----------|:----------------------------------------------------------------------------------------------------------------------------------------------|
147+
| value | Required unless value is nested within tag. Must be a string which can be parsed into a `java.time.LocalDate` according to the parsing options specified. |
148+
| var | The scoped variable to set. |
149+
| scope | The scope of the variable to set. |
150+
| locale | The locale to use for parsing. |
151+
| style | The style to use for parsing (see java.time format documentation for recognized style strings) |
152+
| pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
153+
| zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |
154+
155+
##### &lt;javatime:parseLocalTime&gt;
156+
157+
Example:
158+
```
159+
<javatime:parseLocalTime value="10:43" pattern="HH:mm" />
160+
```
161+
162+
Parses a string into an `java.time.LocalTime` object.
163+
The `var` and `scope` attributes can be used to set the value of a variable instead of printing the result.
164+
The time zone may be specified using an attribute, an enclosing `<javatime:zoneId/>` tag,
165+
preceding `<javatime:setZoneId/>` tag, or via the `net.sargue.time.zoneId` scoped variable.
166+
167+
Attributes:
168+
169+
| Attribute | Description |
170+
|:----------|:----------------------------------------------------------------------------------------------------------------------------------------------|
171+
| value | Required unless value is nested within tag. Must be a string which can be parsed into a `java.time.LocalTime` according to the parsing options specified. |
172+
| var | The scoped variable to set. |
173+
| scope | The scope of the variable to set. |
174+
| locale | The locale to use for parsing. |
175+
| style | The style to use for parsing (see java.time format documentation for recognized style strings) |
176+
| pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
177+
| zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |
178+
179+
##### &lt;javatime:zoneId&gt;
107180

108181
Example:
109182
```
@@ -119,7 +192,7 @@ The `<javatime:format/>` tag may override this value with an explicit `zoneId` a
119192
|:-----------------|:----------------------------------------------|
120193
| value (required) | The default time zone for nested tags to use. |
121194

122-
##### SETZONEID
195+
##### &lt;javatime:setZoneId&gt;
123196

124197
Example:
125198
```
@@ -143,6 +216,9 @@ Build is based on gradle. See build.gradle included in the repository.
143216

144217
### Changelog
145218

219+
##### v1.1.0
220+
Fixed issue #1, added more parse tags.
221+
146222
##### v1.0.0
147223
Some tests added. Minor refactorings and no functionality changed.
148224
Some documentation. Moved to gradle build. Preparing to publish to Maven Central.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: 'signing'
44

55
group = 'net.sargue'
66
archivesBaseName = 'java-time-jsptags'
7-
version = '1.0.0'
7+
version = '1.1.0'
88

99
def NEXUS_USERNAME = hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ''
1010
def NEXUS_PASSWORD = hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ''

src/main/java/net/sargue/time/jsptags/FormatSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright 1999-2004 The Apache Software Foundation.
3-
* Modifications, Copyright 2005 Stephen Colebourne, 2014 Sergi Baila
3+
* Modifications, Copyright 2005 Stephen Colebourne, 2014-2015 Sergi Baila
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

src/main/java/net/sargue/time/jsptags/FormatTag.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright 1999-2004 The Apache Software Foundation.
3-
* Modifications, Copyright 2005 Stephen Colebourne, 2014 Sergi Baila
3+
* Modifications, Copyright 2005 Stephen Colebourne, 2014-2015 Sergi Baila
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -18,14 +18,13 @@
1818

1919
import javax.servlet.jsp.JspTagException;
2020
import java.time.ZoneId;
21-
import java.time.ZoneOffset;
2221
import java.util.Locale;
2322

2423
/**
2524
* <p>
2625
* A handler for &lt;format&gt; that supports rtexprvalue-based attributes.
2726
* </p>
28-
*
27+
*
2928
* @author Jan Luehe
3029
* @author Jim Newsham
3130
* @author Sergi Baila
@@ -35,35 +34,36 @@ public class FormatTag extends FormatSupport {
3534

3635
/**
3736
* Sets the value attribute.
38-
*
37+
*
3938
* @param value the value
4039
*/
41-
public void setValue(Object value) throws JspTagException {
40+
public void setValue(Object value) {
4241
this.value = value;
4342
}
4443

4544
/**
4645
* Sets the style attribute.
47-
*
46+
*
4847
* @param style the style
4948
*/
50-
public void setStyle(String style) throws JspTagException {
49+
public void setStyle(String style) {
5150
this.style = style;
5251
}
5352

5453
/**
5554
* Sets the pattern attribute.
56-
*
55+
*
5756
* @param pattern the pattern
5857
*/
59-
public void setPattern(String pattern) throws JspTagException {
58+
public void setPattern(String pattern) {
6059
this.pattern = pattern;
6160
}
6261

6362
/**
6463
* Sets the zone attribute.
65-
*
64+
*
6665
* @param dtz the zone
66+
* @throws JspTagException incorrect zone or dtz parameter
6767
*/
6868
public void setZoneId(Object dtz) throws JspTagException {
6969
if (dtz == null || (dtz instanceof String && ((String) dtz).isEmpty())) {
@@ -74,19 +74,20 @@ public void setZoneId(Object dtz) throws JspTagException {
7474
try {
7575
this.zoneId = ZoneId.of((String) dtz);
7676
} catch (IllegalArgumentException iae) {
77-
this.zoneId = ZoneOffset.UTC;
77+
throw new JspTagException("Incorrect Zone: " + dtz);
7878
}
7979
} else
8080
throw new JspTagException("Can only accept ZoneId or String objects.");
8181
}
8282

8383
/**
8484
* Sets the style attribute.
85-
*
85+
*
8686
* @param loc the locale
87+
* @throws JspTagException parameter not a Locale or String
8788
*/
8889
public void setLocale(Object loc) throws JspTagException {
89-
if (loc == null || (loc instanceof String && ((String) loc).isEmpty())) {
90+
if (loc == null) {
9091
this.locale = null;
9192
} else if (loc instanceof Locale) {
9293
this.locale = (Locale) loc;

src/main/java/net/sargue/time/jsptags/JavaTimeTagLibraryValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright 1999-2004 The Apache Software Foundation.
3-
* Modifications, Copyright 2005 Stephen Colebourne, 2014 Sergi Baila
3+
* Modifications, Copyright 2005 Stephen Colebourne, 2014-2015 Sergi Baila
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
Lines changed: 9 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright 1999-2004 The Apache Software Foundation.
3-
* Modifications, Copyright 2005 Stephen Colebourne, 2014 Sergi Baila
3+
* Modifications, Copyright 2005 Stephen Colebourne, 2014-2015 Sergi Baila
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -16,88 +16,23 @@
1616
*/
1717
package net.sargue.time.jsptags;
1818

19-
import javax.servlet.jsp.JspTagException;
20-
import java.time.ZoneId;
21-
import java.time.ZoneOffset;
22-
import java.util.Locale;
19+
import java.time.Instant;
20+
import java.time.temporal.TemporalAccessor;
21+
import java.time.temporal.TemporalQuery;
2322

2423
/**
2524
* <p>
26-
* A handler for &lt;parseDate&gt; that supports rtexprvalue-based attributes.
25+
* A handler for &lt;parseInstant&gt; that supports rtexprvalue-based attributes.
2726
* </p>
2827
*
2928
* @author Jan Luehe
3029
* @author Jim Newsham
3130
* @author Sergi Baila
3231
*/
3332

34-
@SuppressWarnings("UnusedDeclaration")
35-
public class ParseInstantTag extends ParseInstantSupport {
36-
37-
/**
38-
* Sets the value attribute.
39-
*
40-
* @param value the value
41-
*/
42-
public void setValue(String value) throws JspTagException {
43-
this.value = value;
44-
this.valueSpecified = true;
45-
}
46-
47-
/**
48-
* Sets the style attribute.
49-
*
50-
* @param style the style
51-
*/
52-
public void setStyle(String style) throws JspTagException {
53-
this.style = style;
54-
}
55-
56-
/**
57-
* Sets the pattern attribute.
58-
*
59-
* @param pattern the pattern
60-
*/
61-
public void setPattern(String pattern) throws JspTagException {
62-
this.pattern = pattern;
33+
public class ParseInstantTag extends ParseSupport {
34+
@Override
35+
protected TemporalQuery<TemporalAccessor> temporalQuery() {
36+
return Instant::from;
6337
}
64-
65-
/**
66-
* Sets the zone attribute.
67-
*
68-
* @param dtz the zone
69-
*/
70-
public void setZoneId(Object dtz) throws JspTagException {
71-
if (dtz == null || dtz instanceof String
72-
&& ((String) dtz).length() == 0) {
73-
this.zoneId = null;
74-
} else if (dtz instanceof ZoneId) {
75-
this.zoneId = (ZoneId) dtz;
76-
} else if (dtz instanceof String) {
77-
try {
78-
this.zoneId = ZoneId.of((String) dtz);
79-
} catch (IllegalArgumentException iae) {
80-
this.zoneId = ZoneOffset.UTC;
81-
}
82-
} else
83-
throw new JspTagException("Can only accept ZoneId or String objects.");
84-
}
85-
86-
/**
87-
* Sets the style attribute.
88-
*
89-
* @param loc the locale
90-
*/
91-
public void setLocale(Object loc) throws JspTagException {
92-
if (loc == null
93-
|| (loc instanceof String && ((String) loc).length() == 0)) {
94-
this.locale = null;
95-
} else if (loc instanceof Locale) {
96-
this.locale = (Locale) loc;
97-
} else if (loc instanceof String) {
98-
locale = Util.parseLocale((String) loc);
99-
} else
100-
throw new JspTagException("Can only accept Locale or String objects.");
101-
}
102-
10338
}

0 commit comments

Comments
 (0)