Moved date/time formatters to utils class
This commit is contained in:
parent
05c1bd3f2a
commit
5ea5727a4a
15 changed files with 66 additions and 48 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -40,7 +40,6 @@
|
|||
/**/.idea/**/libraries
|
||||
/**/.idea/**/mongoSettings.xml
|
||||
/**/.idea/**/replstate.xml
|
||||
/**/.idea/**/shelf
|
||||
/**/.idea/**/shelf/
|
||||
/**/.idea/**/sqlDataSources.xml
|
||||
/**/.idea/**/tasks.xml
|
||||
|
|
|
@ -13,7 +13,7 @@ plugins {
|
|||
id("com.github.ben-manes.versions") version "0.46.0"
|
||||
}
|
||||
|
||||
val rifeVersion by rootProject.extra { "1.5.0" }
|
||||
val rifeVersion by rootProject.extra { "1.5.2" }
|
||||
|
||||
group = "com.uwyn.rife2"
|
||||
version = "0.9.0-SNAPSHOT"
|
||||
|
|
|
@ -45,7 +45,7 @@ public class Abbreviate implements ValueRenderer {
|
|||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
var mark = "...";
|
||||
var max = 0;
|
||||
var max = -1;
|
||||
if (template.hasDefaultValue(valueId)) {
|
||||
var properties = new Properties();
|
||||
try {
|
||||
|
|
|
@ -42,6 +42,9 @@ public class Capitalize implements ValueRenderer {
|
|||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
var value = template.getValueOrAttribute(differentiator);
|
||||
if (value == null || value.isBlank()) {
|
||||
return value;
|
||||
}
|
||||
return value.substring(0, 1).toUpperCase(Localization.getLocale()) + value.substring(1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,10 +19,8 @@ package rife.render;
|
|||
|
||||
import rife.template.Template;
|
||||
import rife.template.ValueRenderer;
|
||||
import rife.tools.Localization;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* <p>Return the current date in ISO 8601 format.</p>
|
||||
|
@ -39,17 +37,11 @@ import java.time.format.DateTimeFormatter;
|
|||
* @since 1.0
|
||||
*/
|
||||
public class DateIso implements ValueRenderer {
|
||||
/**
|
||||
* ISO 8601 date and time formatter.
|
||||
*/
|
||||
static public final DateTimeFormatter iso8601Formatter =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd").withLocale(Localization.getLocale());
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return ZonedDateTime.now().format(iso8601Formatter);
|
||||
return ZonedDateTime.now().format(RenderUtils.ISO_8601_DATE_FORMATTER);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,13 +19,11 @@ package rife.render;
|
|||
|
||||
import rife.template.Template;
|
||||
import rife.template.ValueRenderer;
|
||||
import rife.tools.Localization;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
|
@ -43,12 +41,6 @@ import java.util.Properties;
|
|||
* @since 1.0
|
||||
*/
|
||||
public class DateTimeIso implements ValueRenderer {
|
||||
/**
|
||||
* ISO 8601 date and time formatter.
|
||||
*/
|
||||
static public final DateTimeFormatter iso8601Formatter =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXXXX").withLocale(Localization.getLocale());
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -61,13 +53,13 @@ public class DateTimeIso implements ValueRenderer {
|
|||
properties.load(new StringReader(template.getDefaultValue(valueId)));
|
||||
if (properties.containsKey(tz)) {
|
||||
return ZonedDateTime.now().format(
|
||||
DateTimeIso.iso8601Formatter.withZone(ZoneId.of(properties.getProperty(tz))));
|
||||
RenderUtils.ISO_8601_FORMATTER.withZone(ZoneId.of(properties.getProperty(tz))));
|
||||
}
|
||||
} catch (IOException ignore) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
return ZonedDateTime.now().format(iso8601Formatter);
|
||||
return ZonedDateTime.now().format(RenderUtils.ISO_8601_FORMATTER);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,10 +19,8 @@ package rife.render;
|
|||
|
||||
import rife.template.Template;
|
||||
import rife.template.ValueRenderer;
|
||||
import rife.tools.Localization;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* <p>Return the current date and time in RFC 2822 format.</p>
|
||||
|
@ -39,17 +37,11 @@ import java.time.format.DateTimeFormatter;
|
|||
* @since 1.0
|
||||
*/
|
||||
public class DateTimeRfc2822 implements ValueRenderer {
|
||||
/**
|
||||
* RFC 2822 date and time formatter.
|
||||
*/
|
||||
static public final DateTimeFormatter rfc2822Formatter =
|
||||
DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss zzz").withLocale(Localization.getLocale());
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return ZonedDateTime.now().format(rfc2822Formatter);
|
||||
return ZonedDateTime.now().format(RenderUtils.RFC_2822_FORMATTER);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,6 +41,10 @@ public class Lowercase implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return template.getValueOrAttribute(differentiator).toLowerCase(Localization.getLocale());
|
||||
var value = template.getValueOrAttribute(differentiator);
|
||||
if (value == null || value.isBlank()) {
|
||||
return value;
|
||||
}
|
||||
return value.toLowerCase(Localization.getLocale());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package rife.render;
|
||||
|
||||
import rife.tools.Localization;
|
||||
import rife.tools.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -26,6 +27,7 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.text.Normalizer;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -37,6 +39,26 @@ import java.util.concurrent.TimeUnit;
|
|||
* @since 1.0
|
||||
*/
|
||||
public final class RenderUtils {
|
||||
/**
|
||||
* ISO 8601 date formatter.
|
||||
*/
|
||||
public static final DateTimeFormatter ISO_8601_DATE_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd").withLocale(Localization.getLocale());
|
||||
/**
|
||||
* ISO 8601 date and time formatter.
|
||||
*/
|
||||
public static final DateTimeFormatter ISO_8601_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXXXX").withLocale(Localization.getLocale());
|
||||
/**
|
||||
* ISO 8601 time formatter.
|
||||
*/
|
||||
public static final DateTimeFormatter ISO_8601_TIME_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("HH:mm:ss").withLocale(Localization.getLocale());
|
||||
/**
|
||||
* RFC 2822 date and time formatter.
|
||||
*/
|
||||
public static final DateTimeFormatter RFC_2822_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss zzz").withLocale(Localization.getLocale());
|
||||
private static final String DEFAULT_USER_AGENT =
|
||||
"Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0";
|
||||
|
||||
|
|
|
@ -19,10 +19,8 @@ package rife.render;
|
|||
|
||||
import rife.template.Template;
|
||||
import rife.template.ValueRenderer;
|
||||
import rife.tools.Localization;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* <p>Return the current time in ISO 8601 format.</p>
|
||||
|
@ -39,17 +37,11 @@ import java.time.format.DateTimeFormatter;
|
|||
* @since 1.0
|
||||
*/
|
||||
public class TimeIso implements ValueRenderer {
|
||||
/**
|
||||
* ISO 8601 time formatter.
|
||||
*/
|
||||
static public final DateTimeFormatter iso8601Formatter =
|
||||
DateTimeFormatter.ofPattern("HH:mm:ss").withLocale(Localization.getLocale());
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return ZonedDateTime.now().format(iso8601Formatter);
|
||||
return ZonedDateTime.now().format(RenderUtils.ISO_8601_TIME_FORMATTER);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,10 @@ public class Trim implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return template.getValueOrAttribute(differentiator).trim();
|
||||
var value = template.getValueOrAttribute(differentiator);
|
||||
if (value == null || value.isEmpty()) {
|
||||
return value;
|
||||
}
|
||||
return value.trim();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,9 @@ public class Uncapitalize implements ValueRenderer {
|
|||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
var value = template.getValueOrAttribute(differentiator);
|
||||
if (value == null || value.isBlank()) {
|
||||
return value;
|
||||
}
|
||||
return value.substring(0, 1).toLowerCase(Localization.getLocale()) + value.substring(1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,6 +41,10 @@ public class Uppercase implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return template.getValueOrAttribute(differentiator).toUpperCase(Localization.getLocale());
|
||||
var value = template.getValueOrAttribute(differentiator);
|
||||
if (value == null || value.isBlank()) {
|
||||
return value;
|
||||
}
|
||||
return value.toUpperCase(Localization.getLocale());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,26 +33,26 @@ class TestDateTime {
|
|||
@Test
|
||||
void testDateIso() {
|
||||
var t = TemplateFactory.HTML.get("dateIso");
|
||||
assertThatCode(() -> DateIso.iso8601Formatter.parse(t.getContent())).doesNotThrowAnyException();
|
||||
assertThatCode(() -> RenderUtils.ISO_8601_DATE_FORMATTER.parse(t.getContent())).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDateTimeIso() {
|
||||
var t = TemplateFactory.HTML.get("dateTimeIso");
|
||||
assertThatCode(() -> DateTimeIso.iso8601Formatter.parse(t.getContent())).doesNotThrowAnyException();
|
||||
assertThatCode(() -> RenderUtils.ISO_8601_FORMATTER.parse(t.getContent())).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDateTimeRfc2822() {
|
||||
var t = TemplateFactory.HTML.get("dateTimeRfc2822");
|
||||
assertThatCode(() -> DateTimeRfc2822.rfc2822Formatter.parse(t.getContent())).doesNotThrowAnyException();
|
||||
assertThatCode(() -> RenderUtils.RFC_2822_FORMATTER.parse(t.getContent())).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDateTimeUtc() {
|
||||
var t = TemplateFactory.HTML.get("dateTimeUtc");
|
||||
var content = t.getContent();
|
||||
assertThatCode(() -> DateTimeIso.iso8601Formatter.parse(content)).doesNotThrowAnyException();
|
||||
assertThatCode(() -> RenderUtils.ISO_8601_FORMATTER.parse(content)).doesNotThrowAnyException();
|
||||
assertThat(content).endsWith("Z");
|
||||
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ class TestDateTime {
|
|||
@Test
|
||||
void testTimeIso() {
|
||||
var t = TemplateFactory.HTML.get("timeIso");
|
||||
assertThatCode(() -> TimeIso.iso8601Formatter.parse(t.getContent())).doesNotThrowAnyException();
|
||||
assertThatCode(() -> RenderUtils.ISO_8601_TIME_FORMATTER.parse(t.getContent())).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -54,6 +54,17 @@ class TestRenderUtils {
|
|||
assertThat(RenderUtils.normalize(SAMPLE_GERMAN)).isEqualTo("mochten-sie-ein-paar-apfel");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSwapCase() {
|
||||
assertThat(RenderUtils.swapCase(SAMPLE_GERMAN)).isEqualTo("mÖCHTEN sIE EIN PAAR äPFEL?");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHtmlEntities() {
|
||||
assertThat(RenderUtils.htmlEntities(SAMPLE_GERMAN))
|
||||
.isEqualTo("Möchten Sie ein paar Äpfel?");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRot13() {
|
||||
var encoded = "Zöpugra Fvr rva cnne Äcsry?";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue