Moved un/capitlize to RenderUtils
This commit is contained in:
parent
c55c8d803d
commit
9ff60a6f6b
5 changed files with 57 additions and 31 deletions
|
@ -19,7 +19,6 @@ package rife.render;
|
||||||
|
|
||||||
import rife.template.Template;
|
import rife.template.Template;
|
||||||
import rife.template.ValueRenderer;
|
import rife.template.ValueRenderer;
|
||||||
import rife.tools.Localization;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Capitalizes a template value.</p>
|
* <p>Capitalizes a template value.</p>
|
||||||
|
@ -41,10 +40,6 @@ public class Capitalize implements ValueRenderer {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String render(Template template, String valueId, String differentiator) {
|
public String render(Template template, String valueId, String differentiator) {
|
||||||
var value = template.getValueOrAttribute(differentiator);
|
return RenderUtils.capitalize(template.getValueOrAttribute(differentiator));
|
||||||
if (value == null || value.isBlank()) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
return value.substring(0, 1).toUpperCase(Localization.getLocale()) + value.substring(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,6 +59,11 @@ public final class RenderUtils {
|
||||||
*/
|
*/
|
||||||
public static final DateTimeFormatter RFC_2822_FORMATTER =
|
public static final DateTimeFormatter RFC_2822_FORMATTER =
|
||||||
DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss zzz").withLocale(Localization.getLocale());
|
DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss zzz").withLocale(Localization.getLocale());
|
||||||
|
/**
|
||||||
|
* Year formatter.
|
||||||
|
*/
|
||||||
|
static public final DateTimeFormatter YEAR_FORMATTER =
|
||||||
|
DateTimeFormatter.ofPattern("yyyy").withLocale(Localization.getLocale());
|
||||||
private static final String DEFAULT_USER_AGENT =
|
private static final String DEFAULT_USER_AGENT =
|
||||||
"Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0";
|
"Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0";
|
||||||
|
|
||||||
|
@ -101,6 +106,19 @@ public final class RenderUtils {
|
||||||
return String.format("@%03d", beats);
|
return String.format("@%03d", beats);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Capitalizes a String.
|
||||||
|
*
|
||||||
|
* @param src the source String
|
||||||
|
* @return the capitalized String
|
||||||
|
*/
|
||||||
|
public static String capitalize(String src) {
|
||||||
|
if (src == null || src.isBlank()) {
|
||||||
|
return src;
|
||||||
|
}
|
||||||
|
return src.substring(0, 1).toUpperCase(Localization.getLocale()) + src.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encodes a String to JavaScript/ECMAScript.
|
* Encodes a String to JavaScript/ECMAScript.
|
||||||
*
|
*
|
||||||
|
@ -402,6 +420,19 @@ public final class RenderUtils {
|
||||||
return new String(buff, 0, offset);
|
return new String(buff, 0, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Un-capitalizes a String.
|
||||||
|
*
|
||||||
|
* @param src the source String
|
||||||
|
* @return the capitalized String
|
||||||
|
*/
|
||||||
|
public static String uncapitalize(String src) {
|
||||||
|
if (src == null || src.isBlank()) {
|
||||||
|
return src;
|
||||||
|
}
|
||||||
|
return src.substring(0, 1).toLowerCase(Localization.getLocale()) + src.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the formatted server uptime.
|
* Returns the formatted server uptime.
|
||||||
*
|
*
|
||||||
|
|
|
@ -19,7 +19,6 @@ package rife.render;
|
||||||
|
|
||||||
import rife.template.Template;
|
import rife.template.Template;
|
||||||
import rife.template.ValueRenderer;
|
import rife.template.ValueRenderer;
|
||||||
import rife.tools.Localization;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Un-capitalizes a template value.</p>
|
* <p>Un-capitalizes a template value.</p>
|
||||||
|
@ -41,10 +40,6 @@ public class Uncapitalize implements ValueRenderer {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String render(Template template, String valueId, String differentiator) {
|
public String render(Template template, String valueId, String differentiator) {
|
||||||
var value = template.getValueOrAttribute(differentiator);
|
return RenderUtils.uncapitalize(template.getValueOrAttribute(differentiator));
|
||||||
if (value == null || value.isBlank()) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
return value.substring(0, 1).toLowerCase(Localization.getLocale()) + value.substring(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,10 +19,8 @@ package rife.render;
|
||||||
|
|
||||||
import rife.template.Template;
|
import rife.template.Template;
|
||||||
import rife.template.ValueRenderer;
|
import rife.template.ValueRenderer;
|
||||||
import rife.tools.Localization;
|
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Renders the current year.</p>
|
* <p>Renders the current year.</p>
|
||||||
|
@ -39,17 +37,12 @@ import java.time.format.DateTimeFormatter;
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public class Year implements ValueRenderer {
|
public class Year implements ValueRenderer {
|
||||||
/**
|
|
||||||
* Year formatter.
|
|
||||||
*/
|
|
||||||
static public final DateTimeFormatter yearFormatter =
|
|
||||||
DateTimeFormatter.ofPattern("yyyy").withLocale(Localization.getLocale());
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String render(Template template, String valueId, String differentiator) {
|
public String render(Template template, String valueId, String differentiator) {
|
||||||
return ZonedDateTime.now().format(yearFormatter);
|
return ZonedDateTime.now().format(RenderUtils.YEAR_FORMATTER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,18 @@ class TestRenderUtils {
|
||||||
.isEqualTo(TestCase.SAMPLE_TEXT);
|
.isEqualTo(TestCase.SAMPLE_TEXT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCapitalize() {
|
||||||
|
assertThat(RenderUtils.capitalize("a")).isEqualTo("A");
|
||||||
|
assertThat(RenderUtils.capitalize("")).as("empty").isEqualTo("");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHtmlEntities() {
|
||||||
|
assertThat(RenderUtils.htmlEntities(SAMPLE_GERMAN))
|
||||||
|
.isEqualTo("Möchten Sie ein paar Äpfel?");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMask() {
|
void testMask() {
|
||||||
var foo = "4342256562440179";
|
var foo = "4342256562440179";
|
||||||
|
@ -54,21 +66,21 @@ class TestRenderUtils {
|
||||||
assertThat(RenderUtils.normalize(SAMPLE_GERMAN)).isEqualTo("mochten-sie-ein-paar-apfel");
|
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
|
@Test
|
||||||
void testRot13() {
|
void testRot13() {
|
||||||
var encoded = "Zöpugra Fvr rva cnne Äcsry?";
|
var encoded = "Zöpugra Fvr rva cnne Äcsry?";
|
||||||
assertThat(RenderUtils.rot13(SAMPLE_GERMAN)).as("encode").isEqualTo(encoded);
|
assertThat(RenderUtils.rot13(SAMPLE_GERMAN)).as("encode").isEqualTo(encoded);
|
||||||
assertThat(RenderUtils.rot13(encoded)).as("decode").isEqualTo(SAMPLE_GERMAN);
|
assertThat(RenderUtils.rot13(encoded)).as("decode").isEqualTo(SAMPLE_GERMAN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSwapCase() {
|
||||||
|
assertThat(RenderUtils.swapCase(SAMPLE_GERMAN)).isEqualTo("mÖCHTEN sIE EIN PAAR äPFEL?");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUcapitalize() {
|
||||||
|
assertThat(RenderUtils.uncapitalize("A")).isEqualTo("a");
|
||||||
|
assertThat(RenderUtils.uncapitalize("")).as("empty").isEqualTo("");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue