Skip to content

Commit

Permalink
Fixes parsing bug, adds multiple parse tags.
Browse files Browse the repository at this point in the history
Release v1.1.0.
Fixes #1.
Adds multiple parse tags.
  • Loading branch information
sargue committed Oct 29, 2015
1 parent eb8074a commit 6f5a042
Show file tree
Hide file tree
Showing 17 changed files with 685 additions and 132 deletions.
98 changes: 87 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ almost exactly as the tags in the original Joda-Time JSP Tags.
Add the dependency to your project:

#### Gradle
`compile 'net.sargue:java-time-jsptags:1.0.0'`
`compile 'net.sargue:java-time-jsptags:1.1.0'`

#### Maven

```xml
<dependency>
<groupId>net.sargue</groupId>
<artifactId>java-time-jsptags</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
</dependency>
```

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

#### Tags

##### FORMAT
##### &lt;javatime:format&gt;

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

Attributes:

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

##### PARSEINSTANT
##### &lt;javatime:parseInstant&gt;

Example:
```
<javatime:parseInstant value="${dt}" style="MS" />
```

Parses a string into an `Instant` object.
Parses a string into an `java.time.Instant` object.
The `var` and `scope` attributes can be used to set the value of a variable instead of printing the result.
The time zone may be specified using an attribute, an enclosing `<javatime:zoneId/>` tag,
preceding `<javatime:setZoneId/>` tag, or via the `net.sargue.time.zoneId` scoped variable.

Attributes:

| Attribute | Description |
|:----------|:----------------------------------------------------------------------------------------------------------------------------------------------|
| 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. |
| var | The scoped variable to set. |
| scope | The scope of the variable to set. |
| locale | The locale to use for parsing. |
| style | The style to use for parsing (see java.time format documentation for recognized style strings) |
| pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
| zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |

##### &lt;javatime:parseLocalDateTime&gt;

Example:
```
<javatime:parseLocalDateTime value="${dt}" style="MS" />
```

Parses a string into an `java.time.LocalDateTime` object.
The `var` and `scope` attributes can be used to set the value of a variable instead of printing the result.
The time zone may be specified using an attribute, an enclosing `<javatime:zoneId/>` tag,
preceding `<javatime:setZoneId/>` tag, or via the `net.sargue.time.zoneId` scoped variable.
Expand All @@ -95,15 +120,63 @@ Attributes:

| Attribute | Description |
|:----------|:----------------------------------------------------------------------------------------------------------------------------------------------|
| 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. |
| 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. |
| var | The scoped variable to set. |
| scope | The scope of the variable to set. |
| locale | The locale to use for parsing. |
| style | The style to use for parsing (see java.time format documentation for recognized style strings) |
| pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
| zoneId | The time zone to use for parsing. |
| zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |

##### ZONEID
##### &lt;javatime:parseLocalDate&gt;

Example:
```
<javatime:parseLocalDate value="28/10/2015" pattern="dd/MM/yyyy" />
```

Parses a string into an `java.time.LocalDate` object.
The `var` and `scope` attributes can be used to set the value of a variable instead of printing the result.
The time zone may be specified using an attribute, an enclosing `<javatime:zoneId/>` tag,
preceding `<javatime:setZoneId/>` tag, or via the `net.sargue.time.zoneId` scoped variable.

Attributes:

| Attribute | Description |
|:----------|:----------------------------------------------------------------------------------------------------------------------------------------------|
| 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. |
| var | The scoped variable to set. |
| scope | The scope of the variable to set. |
| locale | The locale to use for parsing. |
| style | The style to use for parsing (see java.time format documentation for recognized style strings) |
| pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
| zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |

##### &lt;javatime:parseLocalTime&gt;

Example:
```
<javatime:parseLocalTime value="10:43" pattern="HH:mm" />
```

Parses a string into an `java.time.LocalTime` object.
The `var` and `scope` attributes can be used to set the value of a variable instead of printing the result.
The time zone may be specified using an attribute, an enclosing `<javatime:zoneId/>` tag,
preceding `<javatime:setZoneId/>` tag, or via the `net.sargue.time.zoneId` scoped variable.

Attributes:

| Attribute | Description |
|:----------|:----------------------------------------------------------------------------------------------------------------------------------------------|
| 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. |
| var | The scoped variable to set. |
| scope | The scope of the variable to set. |
| locale | The locale to use for parsing. |
| style | The style to use for parsing (see java.time format documentation for recognized style strings) |
| pattern | The pattern to use for parsing (see java.time format documentation for recognized pattern strings) |
| zoneId | The time zone to use for parsing. See comment above for fallback and defaults. |

##### &lt;javatime:zoneId&gt;

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

##### SETZONEID
##### &lt;javatime:setZoneId&gt;

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

### Changelog

##### v1.1.0
Fixed issue #1, added more parse tags.

##### v1.0.0
Some tests added. Minor refactorings and no functionality changed.
Some documentation. Moved to gradle build. Preparing to publish to Maven Central.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'signing'

group = 'net.sargue'
archivesBaseName = 'java-time-jsptags'
version = '1.0.0'
version = '1.1.0'

def NEXUS_USERNAME = hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ''
def NEXUS_PASSWORD = hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ''
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sargue/time/jsptags/FormatSupport.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 1999-2004 The Apache Software Foundation.
* Modifications, Copyright 2005 Stephen Colebourne, 2014 Sergi Baila
* Modifications, Copyright 2005 Stephen Colebourne, 2014-2015 Sergi Baila
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/net/sargue/time/jsptags/FormatTag.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 1999-2004 The Apache Software Foundation.
* Modifications, Copyright 2005 Stephen Colebourne, 2014 Sergi Baila
* Modifications, Copyright 2005 Stephen Colebourne, 2014-2015 Sergi Baila
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,14 +18,13 @@

import javax.servlet.jsp.JspTagException;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Locale;

/**
* <p>
* A handler for &lt;format&gt; that supports rtexprvalue-based attributes.
* </p>
*
*
* @author Jan Luehe
* @author Jim Newsham
* @author Sergi Baila
Expand All @@ -35,35 +34,36 @@ public class FormatTag extends FormatSupport {

/**
* Sets the value attribute.
*
*
* @param value the value
*/
public void setValue(Object value) throws JspTagException {
public void setValue(Object value) {
this.value = value;
}

/**
* Sets the style attribute.
*
*
* @param style the style
*/
public void setStyle(String style) throws JspTagException {
public void setStyle(String style) {
this.style = style;
}

/**
* Sets the pattern attribute.
*
*
* @param pattern the pattern
*/
public void setPattern(String pattern) throws JspTagException {
public void setPattern(String pattern) {
this.pattern = pattern;
}

/**
* Sets the zone attribute.
*
*
* @param dtz the zone
* @throws JspTagException incorrect zone or dtz parameter
*/
public void setZoneId(Object dtz) throws JspTagException {
if (dtz == null || (dtz instanceof String && ((String) dtz).isEmpty())) {
Expand All @@ -74,19 +74,20 @@ public void setZoneId(Object dtz) throws JspTagException {
try {
this.zoneId = ZoneId.of((String) dtz);
} catch (IllegalArgumentException iae) {
this.zoneId = ZoneOffset.UTC;
throw new JspTagException("Incorrect Zone: " + dtz);
}
} else
throw new JspTagException("Can only accept ZoneId or String objects.");
}

/**
* Sets the style attribute.
*
*
* @param loc the locale
* @throws JspTagException parameter not a Locale or String
*/
public void setLocale(Object loc) throws JspTagException {
if (loc == null || (loc instanceof String && ((String) loc).isEmpty())) {
if (loc == null) {
this.locale = null;
} else if (loc instanceof Locale) {
this.locale = (Locale) loc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 1999-2004 The Apache Software Foundation.
* Modifications, Copyright 2005 Stephen Colebourne, 2014 Sergi Baila
* Modifications, Copyright 2005 Stephen Colebourne, 2014-2015 Sergi Baila
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
83 changes: 9 additions & 74 deletions src/main/java/net/sargue/time/jsptags/ParseInstantTag.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 1999-2004 The Apache Software Foundation.
* Modifications, Copyright 2005 Stephen Colebourne, 2014 Sergi Baila
* Modifications, Copyright 2005 Stephen Colebourne, 2014-2015 Sergi Baila
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,88 +16,23 @@
*/
package net.sargue.time.jsptags;

import javax.servlet.jsp.JspTagException;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Locale;
import java.time.Instant;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQuery;

/**
* <p>
* A handler for &lt;parseDate&gt; that supports rtexprvalue-based attributes.
* A handler for &lt;parseInstant&gt; that supports rtexprvalue-based attributes.
* </p>
*
* @author Jan Luehe
* @author Jim Newsham
* @author Sergi Baila
*/

@SuppressWarnings("UnusedDeclaration")
public class ParseInstantTag extends ParseInstantSupport {

/**
* Sets the value attribute.
*
* @param value the value
*/
public void setValue(String value) throws JspTagException {
this.value = value;
this.valueSpecified = true;
}

/**
* Sets the style attribute.
*
* @param style the style
*/
public void setStyle(String style) throws JspTagException {
this.style = style;
}

/**
* Sets the pattern attribute.
*
* @param pattern the pattern
*/
public void setPattern(String pattern) throws JspTagException {
this.pattern = pattern;
public class ParseInstantTag extends ParseSupport {
@Override
protected TemporalQuery<TemporalAccessor> temporalQuery() {
return Instant::from;
}

/**
* Sets the zone attribute.
*
* @param dtz the zone
*/
public void setZoneId(Object dtz) throws JspTagException {
if (dtz == null || dtz instanceof String
&& ((String) dtz).length() == 0) {
this.zoneId = null;
} else if (dtz instanceof ZoneId) {
this.zoneId = (ZoneId) dtz;
} else if (dtz instanceof String) {
try {
this.zoneId = ZoneId.of((String) dtz);
} catch (IllegalArgumentException iae) {
this.zoneId = ZoneOffset.UTC;
}
} else
throw new JspTagException("Can only accept ZoneId or String objects.");
}

/**
* Sets the style attribute.
*
* @param loc the locale
*/
public void setLocale(Object loc) throws JspTagException {
if (loc == null
|| (loc instanceof String && ((String) loc).length() == 0)) {
this.locale = null;
} else if (loc instanceof Locale) {
this.locale = (Locale) loc;
} else if (loc instanceof String) {
locale = Util.parseLocale((String) loc);
} else
throw new JspTagException("Can only accept Locale or String objects.");
}

}
Loading

0 comments on commit 6f5a042

Please sign in to comment.