From 3bc761995f4da238f49ef26b745f4fa262a41f4c Mon Sep 17 00:00:00 2001
From: "Erik C. Thauvin"
Date: Wed, 22 Mar 2023 11:55:52 -0700
Subject: [PATCH 01/77] Update README.md
Fixed badges
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index c702b28..777f1f2 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
[](https://opensource.org/licenses/Apache-2.0)
[](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html)
-[](https://github.com/rife2/rife2-template-renderers/releases/latest)
-[](https://maven-badges.herokuapp.com/maven-central/com.uwyn.rife2/rife2-renderers)
+
+
[](https://s01.oss.sonatype.org/content/repositories/snapshots/com/uwyn/rife2/rife2-renderers/)
[](https://github.com/rife2/rife2-template-renderers/actions/workflows/gradle.yml)
[](https://github.com/rife2/rife2-template-renderers/actions/workflows/gradle.yml)
From b554fc7f58dd679c4bada15e7f36bc2895610710 Mon Sep 17 00:00:00 2001
From: "Erik C. Thauvin"
Date: Wed, 22 Mar 2023 12:36:33 -0700
Subject: [PATCH 02/77] Fixed Maven Central badge
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 777f1f2..1fae2ee 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
[](https://opensource.org/licenses/Apache-2.0)
[](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html)
-
-
+[](https://github.com/rife2/rife2-template-renderers/releases/latest)
+[](https://maven-badges.herokuapp.com/maven-central/com.uwyn.rife2/rife2-renderers)
[](https://s01.oss.sonatype.org/content/repositories/snapshots/com/uwyn/rife2/rife2-renderers/)
[](https://github.com/rife2/rife2-template-renderers/actions/workflows/gradle.yml)
[](https://github.com/rife2/rife2-template-renderers/actions/workflows/gradle.yml)
From a5855e6638447abe1a265e43fdffcca123480b60 Mon Sep 17 00:00:00 2001
From: "Erik C. Thauvin"
Date: Wed, 22 Mar 2023 14:34:58 -0700
Subject: [PATCH 03/77] Temporary fix for the Maven Central badge
---
README.md | 2 +-
lib/build.gradle.kts | 2 +-
lib/src/main/java/rife/render/RenderUtils.java | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index 1fae2ee..8b32886 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
[](https://opensource.org/licenses/Apache-2.0)
[](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html)
[](https://github.com/rife2/rife2-template-renderers/releases/latest)
-[](https://maven-badges.herokuapp.com/maven-central/com.uwyn.rife2/rife2-renderers)
+[!](https://central.sonatype.com/artifact/com.uwyn.rife2/rife2-renderers/1.0.0)
[](https://s01.oss.sonatype.org/content/repositories/snapshots/com/uwyn/rife2/rife2-renderers/)
[](https://github.com/rife2/rife2-template-renderers/actions/workflows/gradle.yml)
[](https://github.com/rife2/rife2-template-renderers/actions/workflows/gradle.yml)
diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts
index 43adc01..52e192a 100644
--- a/lib/build.gradle.kts
+++ b/lib/build.gradle.kts
@@ -16,7 +16,7 @@ plugins {
val rifeVersion by rootProject.extra { "1.5.4" }
group = "com.uwyn.rife2"
-version = "1.0.0"
+version = "1.0.1-SNAPSHOT"
repositories {
mavenLocal()
diff --git a/lib/src/main/java/rife/render/RenderUtils.java b/lib/src/main/java/rife/render/RenderUtils.java
index f856408..2fea4a7 100644
--- a/lib/src/main/java/rife/render/RenderUtils.java
+++ b/lib/src/main/java/rife/render/RenderUtils.java
@@ -288,7 +288,7 @@ public final class RenderUtils {
* Normalizes a String for inclusion in a URL path.
*
* @param src The source String
- * @return The normalized String
+ * @return the normalized String
*/
public static String normalize(String src) {
if (src == null || src.isBlank()) {
From f264bb43bc092f200081e44bdf9dd7c96bd8649d Mon Sep 17 00:00:00 2001
From: "Erik C. Thauvin"
Date: Wed, 22 Mar 2023 15:05:39 -0700
Subject: [PATCH 04/77] Minor cleanup
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 8b32886..c8d532f 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
[](https://opensource.org/licenses/Apache-2.0)
[](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html)
[](https://github.com/rife2/rife2-template-renderers/releases/latest)
-[!](https://central.sonatype.com/artifact/com.uwyn.rife2/rife2-renderers/1.0.0)
+[](https://central.sonatype.com/artifact/com.uwyn.rife2/rife2-renderers/1.0.0)
[](https://s01.oss.sonatype.org/content/repositories/snapshots/com/uwyn/rife2/rife2-renderers/)
[](https://github.com/rife2/rife2-template-renderers/actions/workflows/gradle.yml)
[](https://github.com/rife2/rife2-template-renderers/actions/workflows/gradle.yml)
From 79cbf76761b28812303ac6a5da57d755aa7d1a41 Mon Sep 17 00:00:00 2001
From: "Erik C. Thauvin"
Date: Thu, 23 Mar 2023 20:37:01 -0700
Subject: [PATCH 05/77] Added validateCreditMethod standalone method
---
README.md | 20 ++---
lib/build.gradle.kts | 2 +-
.../main/java/rife/render/RenderUtils.java | 77 +++++++++++--------
.../java/rife/render/TestRenderUtils.java | 9 +++
4 files changed, 65 insertions(+), 43 deletions(-)
diff --git a/README.md b/README.md
index c8d532f..f608f01 100644
--- a/README.md
+++ b/README.md
@@ -23,16 +23,16 @@ This project provides a collection of useful template renderers.
## Encoding Renderers
-| Renderer | Description |
-|:--------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------|
-| [rife.render.EncodeBase64](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeBase64) | Encodes a template value to Base64 |
-| [rife.render.EncodeHtml](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeHtml) | Encodes a template value to HTML |
-| [rife.render.EncodeHtmlEntities](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeHtmlEntities) | Encodes a template value to HTML decimal entities |
-| [rife.render.EncodeJs](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeJs) | Encodes a template value to JavaScript/ECMAScript |
-| [rife.render.EncodeJson](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeJson) | Encodes a template value to JSON |
-| [rife.render.EncodeUnicode](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeUnicode) | Encodes a template value to Unicode escape codes |
-| [rife.render.EncodeUrl](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeUrl) | URL-encodes a template value |
-| [rife.render.EncodeXml](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeXml) | Encodes a template value to XML |
+| Renderer | Description |
+|:------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------|
+| [rife.render.EncodeBase64](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeBase64) | Encodes a template value to Base64 |
+| [rife.render.EncodeHtml](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeHtml) | Encodes a template value to HTML |
+| [rife.render.EncodeHtmlEntities](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeHtmlEntities) | Encodes a template value to HTML decimal entities |
+| [rife.render.EncodeJs](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeJs) | Encodes a template value to JavaScript/ECMAScript |
+| [rife.render.EncodeJson](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeJson) | Encodes a template value to JSON |
+| [rife.render.EncodeUnicode](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeUnicode) | Encodes a template value to Unicode escape codes |
+| [rife.render.EncodeUrl](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeUrl) | URL-encodes a template value |
+| [rife.render.EncodeXml](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeXml) | Encodes a template value to XML |
## Format Renderers
diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts
index 52e192a..a4035eb 100644
--- a/lib/build.gradle.kts
+++ b/lib/build.gradle.kts
@@ -13,7 +13,7 @@ plugins {
id("com.github.ben-manes.versions") version "0.46.0"
}
-val rifeVersion by rootProject.extra { "1.5.4" }
+val rifeVersion by rootProject.extra { "1.5.5" }
group = "com.uwyn.rife2"
version = "1.0.1-SNAPSHOT"
diff --git a/lib/src/main/java/rife/render/RenderUtils.java b/lib/src/main/java/rife/render/RenderUtils.java
index 2fea4a7..e9fbe3c 100644
--- a/lib/src/main/java/rife/render/RenderUtils.java
+++ b/lib/src/main/java/rife/render/RenderUtils.java
@@ -192,38 +192,13 @@ public final class RenderUtils {
return src;
}
- try {
- var cc = src.replaceAll("[^0-9]", "");
+ var cc = src.replaceAll("[^0-9]", "");
- var len = cc.length();
- if (len >= 4) {
- // Luhn algorithm
- var sum = 0;
- boolean isSecond = false;
- int digit;
- char c;
- for (int i = len - 1; i >= 0; i--) {
- c = cc.charAt(i);
- if (c >= '0' && c <= '9') {
- digit = cc.charAt(i) - '0';
- if (isSecond) {
- digit = digit * 2;
- }
- sum += digit / 10;
- sum += digit % 10;
- }
-
- isSecond = !isSecond;
- }
- if (sum % 10 == 0) {
- return cc.substring(len - 4);
- }
- }
- } catch (NumberFormatException ignore) {
- // do nothing
+ if (validateCreditCard(cc)) {
+ return cc.substring(cc.length() - 4);
+ } else {
+ return "";
}
-
- return "";
}
/**
@@ -344,8 +319,8 @@ public final class RenderUtils {
return src;
}
return fetchUrl(String.format("https://api.qrserver.com/v1/create-qr-code/?format=svg&size=%s&data=%s",
- StringUtils.encodeUrl(size),
- StringUtils.encodeUrl(src.trim())),
+ StringUtils.encodeUrl(size),
+ StringUtils.encodeUrl(src.trim())),
src);
}
@@ -520,4 +495,42 @@ public final class RenderUtils {
return sb.toString();
}
+
+ /**
+ * Validates a credit card number using the Luhn algorithm.
+ *
+ * @param cc the credit card number
+ * @return {@code trude} if the credit card number is valid
+ */
+ public static boolean validateCreditCard(String cc) {
+ try {
+ var len = cc.length();
+ if (len >= 8 && len <= 19) {
+ // Luhn algorithm
+ var sum = 0;
+ boolean second = false;
+ int digit;
+ char c;
+ for (int i = len - 1; i >= 0; i--) {
+ c = cc.charAt(i);
+ if (c >= '0' && c <= '9') {
+ digit = cc.charAt(i) - '0';
+ if (second) {
+ digit = digit * 2;
+ }
+ sum += digit / 10;
+ sum += digit % 10;
+
+ second = !second;
+ }
+ }
+ if (sum % 10 == 0) {
+ return true;
+ }
+ }
+ } catch (NumberFormatException ignore) {
+ // do nothing
+ }
+ return false;
+ }
}
\ No newline at end of file
diff --git a/lib/src/test/java/rife/render/TestRenderUtils.java b/lib/src/test/java/rife/render/TestRenderUtils.java
index 1ef7762..f0f55c5 100644
--- a/lib/src/test/java/rife/render/TestRenderUtils.java
+++ b/lib/src/test/java/rife/render/TestRenderUtils.java
@@ -83,4 +83,13 @@ class TestRenderUtils {
assertThat(RenderUtils.uncapitalize("A")).isEqualTo("a");
assertThat(RenderUtils.uncapitalize("")).as("empty").isEqualTo("");
}
+
+ @Test
+ void testValidateCreditCard() {
+ assertThat(RenderUtils.validateCreditCard("4505 4672 3366 6430")).as("visa").isTrue();
+ assertThat(RenderUtils.validateCreditCard("5189-5923-3915-0425")).as("mastercard").isTrue();
+ assertThat(RenderUtils.validateCreditCard("3433634926643302")).as("amex").isTrue();
+ assertThat(RenderUtils.validateCreditCard("6011 1076-8252 0629")).as("discover").isTrue();
+ assertThat(RenderUtils.validateCreditCard("0123456789012345")).as("invalid").isFalse();
+ }
}
From 538a8d35aed57bbc2204c472d7188a9ea0401971 Mon Sep 17 00:00:00 2001
From: "Erik C. Thauvin"
Date: Fri, 24 Mar 2023 09:22:30 -0700
Subject: [PATCH 06/77] Switched to use StringUtils for un/captitalize
---
lib/src/main/java/rife/render/Capitalize.java | 3 +-
.../main/java/rife/render/RenderUtils.java | 40 ++++---------------
.../main/java/rife/render/Uncapitalize.java | 3 +-
.../java/rife/render/TestRenderUtils.java | 12 ------
4 files changed, 11 insertions(+), 47 deletions(-)
diff --git a/lib/src/main/java/rife/render/Capitalize.java b/lib/src/main/java/rife/render/Capitalize.java
index 9e9bd91..edce803 100644
--- a/lib/src/main/java/rife/render/Capitalize.java
+++ b/lib/src/main/java/rife/render/Capitalize.java
@@ -19,6 +19,7 @@ package rife.render;
import rife.template.Template;
import rife.template.ValueRenderer;
+import rife.tools.StringUtils;
/**
*
Capitalizes a template value.
@@ -40,6 +41,6 @@ public class Capitalize implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return RenderUtils.capitalize(template.getValueOrAttribute(differentiator));
+ return StringUtils.capitalize(template.getValueOrAttribute(differentiator));
}
}
diff --git a/lib/src/main/java/rife/render/RenderUtils.java b/lib/src/main/java/rife/render/RenderUtils.java
index e9fbe3c..f46f1a2 100644
--- a/lib/src/main/java/rife/render/RenderUtils.java
+++ b/lib/src/main/java/rife/render/RenderUtils.java
@@ -106,7 +106,7 @@ public final class RenderUtils {
/**
* Returns the Swatch Internet (.beat) Time for the give date-time.
*
- * @param zonedDateTime the date and time.
+ * @param zonedDateTime the date and time
* @return the .beat time. (eg.: {@code @248})
*/
public static String beatTime(ZonedDateTime zonedDateTime) {
@@ -116,19 +116,6 @@ public final class RenderUtils {
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.
*
@@ -161,8 +148,8 @@ public final class RenderUtils {
* Fetches the content (body) of a URL.
*
* @param url the URL sSng.
- * @param defaultContent the default content to return if none fetched.
- * @return the url content, or empty.
+ * @param defaultContent the default content to return if none fetched
+ * @return the url content, or empty
*/
public static String fetchUrl(String url, String defaultContent) {
try {
@@ -232,7 +219,7 @@ public final class RenderUtils {
/**
* Masks characters in a String.
*
- * @param src the source String.
+ * @param src the source String
* @param mask the String to mask characters with
* @param unmasked the number of characters to leave unmasked
* @param fromStart to unmask characters from the start of the String
@@ -262,7 +249,7 @@ public final class RenderUtils {
/**
* Normalizes a String for inclusion in a URL path.
*
- * @param src The source String
+ * @param src the source String
* @return the normalized String
*/
public static String normalize(String src) {
@@ -413,19 +400,6 @@ public final class RenderUtils {
return new String(buff, 0, offset);
}
- /**
- * Uncapitalizes a String.
- *
- * @param src the source String
- * @return the uncapitalized 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.
*
@@ -448,7 +422,7 @@ public final class RenderUtils {
*
* @param uptime the uptime in milliseconds
* @param properties the format properties
- * @return the formatted uptime.
+ * @return the formatted uptime
*/
@SuppressWarnings("UnnecessaryUnicodeEscape")
public static String uptime(long uptime, Properties properties) {
@@ -500,7 +474,7 @@ public final class RenderUtils {
* Validates a credit card number using the Luhn algorithm.
*
* @param cc the credit card number
- * @return {@code trude} if the credit card number is valid
+ * @return {@code true} if the credit card number is valid
*/
public static boolean validateCreditCard(String cc) {
try {
diff --git a/lib/src/main/java/rife/render/Uncapitalize.java b/lib/src/main/java/rife/render/Uncapitalize.java
index 1e670a7..a4ecd24 100644
--- a/lib/src/main/java/rife/render/Uncapitalize.java
+++ b/lib/src/main/java/rife/render/Uncapitalize.java
@@ -19,6 +19,7 @@ package rife.render;
import rife.template.Template;
import rife.template.ValueRenderer;
+import rife.tools.StringUtils;
/**
*
Un-capitalizes a template value.
@@ -40,6 +41,6 @@ public class Uncapitalize implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return RenderUtils.uncapitalize(template.getValueOrAttribute(differentiator));
+ return StringUtils.uncapitalize(template.getValueOrAttribute(differentiator));
}
}
diff --git a/lib/src/test/java/rife/render/TestRenderUtils.java b/lib/src/test/java/rife/render/TestRenderUtils.java
index f0f55c5..bed7b86 100644
--- a/lib/src/test/java/rife/render/TestRenderUtils.java
+++ b/lib/src/test/java/rife/render/TestRenderUtils.java
@@ -35,12 +35,6 @@ class TestRenderUtils {
.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))
@@ -78,12 +72,6 @@ class TestRenderUtils {
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("");
- }
-
@Test
void testValidateCreditCard() {
assertThat(RenderUtils.validateCreditCard("4505 4672 3366 6430")).as("visa").isTrue();
From 9738ecb0babcec0c4affd43ca97925f2a49bd618 Mon Sep 17 00:00:00 2001
From: "Erik C. Thauvin"
Date: Sat, 25 Mar 2023 15:46:05 -0700
Subject: [PATCH 07/77] Improved the renderers encoding by using the template
encoding or encoding specified in a property
---
lib/build.gradle.kts | 2 +-
lib/src/main/java/rife/render/Abbreviate.java | 6 +-
lib/src/main/java/rife/render/BeatTime.java | 2 +-
lib/src/main/java/rife/render/Capitalize.java | 2 +-
lib/src/main/java/rife/render/DateIso.java | 2 +-
.../main/java/rife/render/DateTimeIso.java | 2 +-
.../java/rife/render/DateTimeRfc2822.java | 2 +-
.../main/java/rife/render/EncodeBase64.java | 6 +-
lib/src/main/java/rife/render/EncodeJs.java | 3 +-
lib/src/main/java/rife/render/EncodeJson.java | 3 +-
.../main/java/rife/render/EncodeUnicode.java | 3 +-
lib/src/main/java/rife/render/EncodeUrl.java | 3 +-
.../java/rife/render/FormatCreditCard.java | 2 +-
lib/src/main/java/rife/render/Lowercase.java | 2 +-
lib/src/main/java/rife/render/Mask.java | 3 +-
lib/src/main/java/rife/render/Normalize.java | 2 +-
.../main/java/rife/render/RenderUtils.java | 138 ++++++++++++++----
lib/src/main/java/rife/render/Rot13.java | 2 +-
lib/src/main/java/rife/render/ShortenUrl.java | 2 +-
lib/src/main/java/rife/render/SwapCase.java | 2 +-
lib/src/main/java/rife/render/TimeIso.java | 2 +-
lib/src/main/java/rife/render/Trim.java | 2 +-
.../main/java/rife/render/Uncapitalize.java | 2 +-
lib/src/main/java/rife/render/Uppercase.java | 2 +-
lib/src/main/java/rife/render/Uptime.java | 7 +-
lib/src/main/java/rife/render/Year.java | 2 +-
lib/src/test/java/rife/render/TestEncode.java | 23 +++
lib/src/test/java/rife/render/TestFormat.java | 7 +-
.../java/rife/render/TestRenderUtils.java | 17 +++
.../resources/templates/encodeBase64.html | 1 +
.../test/resources/templates/encodeJs.html | 1 +
.../test/resources/templates/encodeJson.html | 1 +
.../resources/templates/encodeUnicode.html | 1 +
.../templates/encodeUrlwithUnicode.html | 1 +
34 files changed, 197 insertions(+), 61 deletions(-)
create mode 100644 lib/src/test/resources/templates/encodeBase64.html
create mode 100644 lib/src/test/resources/templates/encodeJs.html
create mode 100644 lib/src/test/resources/templates/encodeJson.html
create mode 100644 lib/src/test/resources/templates/encodeUnicode.html
create mode 100644 lib/src/test/resources/templates/encodeUrlwithUnicode.html
diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts
index a4035eb..b53d9dc 100644
--- a/lib/build.gradle.kts
+++ b/lib/build.gradle.kts
@@ -13,7 +13,7 @@ plugins {
id("com.github.ben-manes.versions") version "0.46.0"
}
-val rifeVersion by rootProject.extra { "1.5.5" }
+val rifeVersion by rootProject.extra { "1.5.6" }
group = "com.uwyn.rife2"
version = "1.0.1-SNAPSHOT"
diff --git a/lib/src/main/java/rife/render/Abbreviate.java b/lib/src/main/java/rife/render/Abbreviate.java
index cfe679a..6944aa4 100644
--- a/lib/src/main/java/rife/render/Abbreviate.java
+++ b/lib/src/main/java/rife/render/Abbreviate.java
@@ -57,6 +57,8 @@ public class Abbreviate implements ValueRenderer {
// do nothing
}
}
- return RenderUtils.abbreviate(template.getValueOrAttribute(differentiator), max, mark);
+
+ return template.getEncoder().encode(
+ RenderUtils.abbreviate(template.getValueOrAttribute(differentiator), max, mark));
}
-}
+}
\ No newline at end of file
diff --git a/lib/src/main/java/rife/render/BeatTime.java b/lib/src/main/java/rife/render/BeatTime.java
index e1eaf44..dd4cfad 100644
--- a/lib/src/main/java/rife/render/BeatTime.java
+++ b/lib/src/main/java/rife/render/BeatTime.java
@@ -42,6 +42,6 @@ public class BeatTime implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return RenderUtils.beatTime(ZonedDateTime.now());
+ return template.getEncoder().encode(RenderUtils.beatTime(ZonedDateTime.now()));
}
}
\ No newline at end of file
diff --git a/lib/src/main/java/rife/render/Capitalize.java b/lib/src/main/java/rife/render/Capitalize.java
index edce803..dc4c126 100644
--- a/lib/src/main/java/rife/render/Capitalize.java
+++ b/lib/src/main/java/rife/render/Capitalize.java
@@ -41,6 +41,6 @@ public class Capitalize implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return StringUtils.capitalize(template.getValueOrAttribute(differentiator));
+ return template.getEncoder().encode(StringUtils.capitalize(template.getValueOrAttribute(differentiator)));
}
}
diff --git a/lib/src/main/java/rife/render/DateIso.java b/lib/src/main/java/rife/render/DateIso.java
index b261268..3fe35c6 100644
--- a/lib/src/main/java/rife/render/DateIso.java
+++ b/lib/src/main/java/rife/render/DateIso.java
@@ -42,6 +42,6 @@ public class DateIso implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return ZonedDateTime.now().format(RenderUtils.ISO_8601_DATE_FORMATTER);
+ return template.getEncoder().encode(ZonedDateTime.now().format(RenderUtils.ISO_8601_DATE_FORMATTER));
}
}
diff --git a/lib/src/main/java/rife/render/DateTimeIso.java b/lib/src/main/java/rife/render/DateTimeIso.java
index 683abb7..50ac866 100644
--- a/lib/src/main/java/rife/render/DateTimeIso.java
+++ b/lib/src/main/java/rife/render/DateTimeIso.java
@@ -61,6 +61,6 @@ public class DateTimeIso implements ValueRenderer {
}
}
- return ZonedDateTime.now().format(RenderUtils.ISO_8601_FORMATTER);
+ return template.getEncoder().encode(ZonedDateTime.now().format(RenderUtils.ISO_8601_FORMATTER));
}
}
diff --git a/lib/src/main/java/rife/render/DateTimeRfc2822.java b/lib/src/main/java/rife/render/DateTimeRfc2822.java
index 5058b70..a420201 100644
--- a/lib/src/main/java/rife/render/DateTimeRfc2822.java
+++ b/lib/src/main/java/rife/render/DateTimeRfc2822.java
@@ -42,6 +42,6 @@ public class DateTimeRfc2822 implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return ZonedDateTime.now().format(RenderUtils.RFC_2822_FORMATTER);
+ return template.getEncoder().encode(ZonedDateTime.now().format(RenderUtils.RFC_2822_FORMATTER));
}
}
diff --git a/lib/src/main/java/rife/render/EncodeBase64.java b/lib/src/main/java/rife/render/EncodeBase64.java
index c5bd854..fd2e62a 100644
--- a/lib/src/main/java/rife/render/EncodeBase64.java
+++ b/lib/src/main/java/rife/render/EncodeBase64.java
@@ -44,7 +44,9 @@ public class EncodeBase64 implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return StringUtils.encodeBase64(template.getValueOrAttribute(differentiator)
- .getBytes(StandardCharsets.UTF_8));
+ var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId));
+ return RenderUtils.encode(
+ StringUtils.encodeBase64(template.getValueOrAttribute(differentiator).getBytes(StandardCharsets.UTF_8)),
+ properties);
}
}
diff --git a/lib/src/main/java/rife/render/EncodeJs.java b/lib/src/main/java/rife/render/EncodeJs.java
index caafb18..1122356 100644
--- a/lib/src/main/java/rife/render/EncodeJs.java
+++ b/lib/src/main/java/rife/render/EncodeJs.java
@@ -42,6 +42,7 @@ public class EncodeJs implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return RenderUtils.encodeJs(template.getValueOrAttribute(differentiator));
+ var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId));
+ return RenderUtils.encode(RenderUtils.encodeJs(template.getValueOrAttribute(differentiator)), properties);
}
}
diff --git a/lib/src/main/java/rife/render/EncodeJson.java b/lib/src/main/java/rife/render/EncodeJson.java
index 60916f6..7356650 100644
--- a/lib/src/main/java/rife/render/EncodeJson.java
+++ b/lib/src/main/java/rife/render/EncodeJson.java
@@ -42,6 +42,7 @@ public class EncodeJson implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return StringUtils.encodeJson(template.getValueOrAttribute(differentiator));
+ var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId));
+ return RenderUtils.encode(StringUtils.encodeJson(template.getValueOrAttribute(differentiator)), properties);
}
}
diff --git a/lib/src/main/java/rife/render/EncodeUnicode.java b/lib/src/main/java/rife/render/EncodeUnicode.java
index 1d67bf1..60166a1 100644
--- a/lib/src/main/java/rife/render/EncodeUnicode.java
+++ b/lib/src/main/java/rife/render/EncodeUnicode.java
@@ -42,6 +42,7 @@ public class EncodeUnicode implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return StringUtils.encodeUnicode(template.getValueOrAttribute(differentiator));
+ var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId));
+ return RenderUtils.encode(StringUtils.encodeUnicode(template.getValueOrAttribute(differentiator)), properties);
}
}
diff --git a/lib/src/main/java/rife/render/EncodeUrl.java b/lib/src/main/java/rife/render/EncodeUrl.java
index f6201d5..6692636 100644
--- a/lib/src/main/java/rife/render/EncodeUrl.java
+++ b/lib/src/main/java/rife/render/EncodeUrl.java
@@ -42,6 +42,7 @@ public class EncodeUrl implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return StringUtils.encodeUrl(template.getValueOrAttribute(differentiator));
+ var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId));
+ return RenderUtils.encode(StringUtils.encodeUrl(template.getValueOrAttribute(differentiator)), properties);
}
}
diff --git a/lib/src/main/java/rife/render/FormatCreditCard.java b/lib/src/main/java/rife/render/FormatCreditCard.java
index 840d66e..f2025eb 100644
--- a/lib/src/main/java/rife/render/FormatCreditCard.java
+++ b/lib/src/main/java/rife/render/FormatCreditCard.java
@@ -40,6 +40,6 @@ public class FormatCreditCard implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return RenderUtils.formatCreditCard(template.getValueOrAttribute(differentiator));
+ return template.getEncoder().encode(RenderUtils.formatCreditCard(template.getValueOrAttribute(differentiator)));
}
}
diff --git a/lib/src/main/java/rife/render/Lowercase.java b/lib/src/main/java/rife/render/Lowercase.java
index d966142..aa7858c 100644
--- a/lib/src/main/java/rife/render/Lowercase.java
+++ b/lib/src/main/java/rife/render/Lowercase.java
@@ -45,6 +45,6 @@ public class Lowercase implements ValueRenderer {
if (value == null || value.isBlank()) {
return value;
}
- return value.toLowerCase(Localization.getLocale());
+ return template.getEncoder().encode(value.toLowerCase(Localization.getLocale()));
}
}
diff --git a/lib/src/main/java/rife/render/Mask.java b/lib/src/main/java/rife/render/Mask.java
index 897b415..a9c1e22 100644
--- a/lib/src/main/java/rife/render/Mask.java
+++ b/lib/src/main/java/rife/render/Mask.java
@@ -59,6 +59,7 @@ public class Mask implements ValueRenderer {
// do nothing
}
}
- return RenderUtils.mask(template.getValueOrAttribute(differentiator), mask, unmasked, fromStart);
+ return template.getEncoder().encode(
+ RenderUtils.mask(template.getValueOrAttribute(differentiator), mask, unmasked, fromStart));
}
}
diff --git a/lib/src/main/java/rife/render/Normalize.java b/lib/src/main/java/rife/render/Normalize.java
index 636c8cb..c3e66a2 100644
--- a/lib/src/main/java/rife/render/Normalize.java
+++ b/lib/src/main/java/rife/render/Normalize.java
@@ -40,6 +40,6 @@ public class Normalize implements ValueRenderer {
*/
@Override
public String render(Template template, String valueId, String differentiator) {
- return RenderUtils.normalize(template.getValueOrAttribute(differentiator));
+ return template.getEncoder().encode(RenderUtils.normalize(template.getValueOrAttribute(differentiator)));
}
}
diff --git a/lib/src/main/java/rife/render/RenderUtils.java b/lib/src/main/java/rife/render/RenderUtils.java
index f46f1a2..139d17f 100644
--- a/lib/src/main/java/rife/render/RenderUtils.java
+++ b/lib/src/main/java/rife/render/RenderUtils.java
@@ -21,6 +21,7 @@ import rife.tools.Localization;
import rife.tools.StringUtils;
import java.io.IOException;
+import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@@ -39,6 +40,11 @@ import java.util.concurrent.TimeUnit;
* @since 1.0
*/
public final class RenderUtils {
+ /**
+ * The encoding property.
+ */
+ public static final String ENCODING_PROPERTY = "encoding";
+
/**
* ISO 8601 date formatter.
*
@@ -82,12 +88,12 @@ public final class RenderUtils {
}
/**
- * Abbreviates a String to the given length using a replacement marker.
+ * Abbreviates a {@code String} to the given length using a replacement marker.
*
- * @param src the source String
- * @param max the maximum length of the resulting String
- * @param marker the String used as a replacement marker
- * @return the abbreviated String
+ * @param src the source {@code String}
+ * @param max the maximum length of the resulting {@code String}
+ * @param marker the {@code String} used as a replacement marker
+ * @return the abbreviated {@code String}
*/
public static String abbreviate(String src, int max, String marker) {
if (src == null || src.isBlank() || marker == null) {
@@ -117,10 +123,59 @@ public final class RenderUtils {
}
/**
- * Encodes a String to JavaScript/ECMAScript.
+ *
Encodes the source {@code String} to the specified encoding.
*
- * @param src the source String
- * @return the encoded String
+ *
The supported encodings are:
+ *
+ *
+ *
{@code html}
+ *
{@code js}
+ *
{@code json}
+ *
{@code unicode}
+ *
{@code url}
+ *
{@code xml}
+ *
+ *
+ * @param src the source {@code String} to encode
+ * @param properties the properties containing the {@link #ENCODING_PROPERTY encoding property}.
+ * @return the encoded {@code String}
+ */
+ public static String encode(String src, Properties properties) {
+ if (src == null || src.isBlank() || properties.isEmpty()) {
+ return src;
+ }
+
+ var encoding = properties.getProperty(ENCODING_PROPERTY, "");
+ switch (encoding) {
+ case "html" -> {
+ return StringUtils.encodeHtml(src);
+ }
+ case "js" -> {
+ return RenderUtils.encodeJs(src);
+ }
+ case "json" -> {
+ return StringUtils.encodeJson(src);
+ }
+ case "unicode" -> {
+ return StringUtils.encodeUnicode(src);
+ }
+ case "url" -> {
+ return StringUtils.encodeUrl(src);
+ }
+ case "xml" -> {
+ return StringUtils.encodeXml(src);
+ }
+ default -> {
+ return src;
+ }
+ }
+ }
+
+ /**
+ * Encodes a {@code String} to JavaScript/ECMAScript.
+ *
+ * @param src the source {@code String}
+ * @return the encoded {@code String}
*/
public static String encodeJs(String src) {
if (src == null || src.isBlank()) {
@@ -147,7 +202,7 @@ public final class RenderUtils {
/**
* Fetches the content (body) of a URL.
*
- * @param url the URL sSng.
+ * @param url the URL {@code String}
* @param defaultContent the default content to return if none fetched
* @return the url content, or empty
*/
@@ -168,8 +223,12 @@ public final class RenderUtils {
}
/**
- * Returns the last 4 digits a credit card number. The number must satisfy the Luhn algorithm.
- * Non-digits are stripped from the number.
+ * Returns the last 4 digits a credit card number.
+ *
+ *
+ *
The number must satisfy the Luhn algorithm
+ *
Non-digits are stripped from the number
+ *
*
* @param src the credit card number
* @return the last 4 digits of the credit card number or empty
@@ -189,10 +248,10 @@ public final class RenderUtils {
}
/**
- * Converts a text String to HTML decimal entities.
+ * Converts a text {@code String} to HTML decimal entities.
*
- * @param src the String to convert
- * @return the converted String
+ * @param src the {@code String} to convert
+ * @return the converted {@code String}
*/
@SuppressWarnings("PMD.AvoidReassigningLoopVariables")
public static String htmlEntities(String src) {
@@ -219,11 +278,11 @@ public final class RenderUtils {
/**
* Masks characters in a String.
*
- * @param src the source String
- * @param mask the String to mask characters with
+ * @param src the source {@code String}
+ * @param mask the {@code String} to mask characters with
* @param unmasked the number of characters to leave unmasked
- * @param fromStart to unmask characters from the start of the String
- * @return the masked String
+ * @param fromStart to unmask characters from the start of the {@code String}
+ * @return the masked {@code String}
*/
public static String mask(String src, String mask, int unmasked, boolean fromStart) {
if (src == null || src.isEmpty()) {
@@ -247,10 +306,10 @@ public final class RenderUtils {
}
/**
- * Normalizes a String for inclusion in a URL path.
+ * Normalizes a {@code String} for inclusion in a URL path.
*
- * @param src the source String
- * @return the normalized String
+ * @param src the source {@code String}
+ * @return the normalized {@code String}
*/
public static String normalize(String src) {
if (src == null || src.isBlank()) {
@@ -278,13 +337,31 @@ public final class RenderUtils {
return sb.toString();
}
+ /**
+ * Returns a new {@code Properties} containing the properties specified in the given {$String}.
+ *
+ * @param src the {@code} String containing the properties
+ * @return the new {@code Properties}
+ */
+ public static Properties parsePropertiesString(String src) {
+ var properties = new Properties();
+ if (src != null && !src.isBlank()) {
+ try {
+ properties.load(new StringReader(src));
+ } catch (IOException ignore) {
+ // ignore
+ }
+ }
+ return properties;
+ }
+
/**
* Returns the plural form of a word, if count > 1.
*
* @param count the count
* @param word the singular word
* @param plural the plural word
- * @return the singular or plural String
+ * @return the singular or plural {@code String}
*/
public static String plural(final long count, final String word, final String plural) {
if (count > 1) {
@@ -295,9 +372,9 @@ public final class RenderUtils {
}
/**
- * Generates an SVG QR Code from the given String using goQR.me.
+ * Generates an SVG QR Code from the given {@code String} using goQR.me.
*
- * @param src the data String
+ * @param src the data {@code String}
* @param size the QR Code size. (e.g. {@code 150x150})
* @return the QR code
*/
@@ -312,10 +389,10 @@ public final class RenderUtils {
}
/**
- * Translates a String to/from ROT13.
+ * Translates a {@code String} to/from ROT13.
*
- * @param src the source String
- * @return the translated String
+ * @param src the source {@code String}
+ * @return the translated {@code String}
*/
public static String rot13(String src) {
if (src == null || src.isBlank()) {
@@ -353,7 +430,7 @@ public final class RenderUtils {
/**
*