Compare commits

..

No commits in common. "0ca9f5b9f838b20845676a40893a95300a7d3683" and "c1b0e7fcf53a8cdc0119500ca3e067274ec04a25" have entirely different histories.

6 changed files with 72 additions and 39 deletions

View file

@ -20,6 +20,10 @@ package rife.render;
import rife.template.Template; import rife.template.Template;
import rife.template.ValueRenderer; import rife.template.ValueRenderer;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
/** /**
* <p>Abbreviate a template value with ellipses.</p> * <p>Abbreviate a template value with ellipses.</p>
* *
@ -44,9 +48,14 @@ public class Abbreviate implements ValueRenderer {
var max = -1; var max = -1;
var defaultValue = template.getDefaultValue(valueId); var defaultValue = template.getDefaultValue(valueId);
if (defaultValue != null) { if (defaultValue != null) {
var properties = RenderUtils.parsePropertiesString(defaultValue); var properties = new Properties();
mark = properties.getProperty("mark", mark); try {
max = Integer.parseInt(properties.getProperty("max", String.valueOf(max))); properties.load(new StringReader(defaultValue));
mark = properties.getProperty("mark", mark);
max = Integer.parseInt(properties.getProperty("max", String.valueOf(max)));
} catch (IOException | NumberFormatException ignore) {
// do nothing
}
} }
return template.getEncoder().encode( return template.getEncoder().encode(

View file

@ -20,8 +20,11 @@ package rife.render;
import rife.template.Template; import rife.template.Template;
import rife.template.ValueRenderer; import rife.template.ValueRenderer;
import java.io.IOException;
import java.io.StringReader;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.Properties;
/** /**
* <p>Return the current date and time in ISO 8601 format.</p> * <p>Return the current date and time in ISO 8601 format.</p>
@ -45,11 +48,16 @@ public class DateTimeIso implements ValueRenderer {
public String render(Template template, String valueId, String differentiator) { public String render(Template template, String valueId, String differentiator) {
var defaultValue = template.getDefaultValue(valueId); var defaultValue = template.getDefaultValue(valueId);
if (defaultValue != null) { if (defaultValue != null) {
var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId)); var properties = new Properties();
var tz = "tz"; try {
if (properties.containsKey(tz)) { var tz = "tz";
return ZonedDateTime.now().format( properties.load(new StringReader(defaultValue));
RenderUtils.ISO_8601_FORMATTER.withZone(ZoneId.of(properties.getProperty(tz)))); if (properties.containsKey(tz)) {
return ZonedDateTime.now().format(
RenderUtils.ISO_8601_FORMATTER.withZone(ZoneId.of(properties.getProperty(tz))));
}
} catch (IOException ignore) {
// do nothing
} }
} }

View file

@ -20,6 +20,10 @@ package rife.render;
import rife.template.Template; import rife.template.Template;
import rife.template.ValueRenderer; import rife.template.ValueRenderer;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
/** /**
* <p>Masks characters of a template value.</p> * <p>Masks characters of a template value.</p>
* *
@ -44,15 +48,16 @@ public class Mask implements ValueRenderer {
var unmasked = 0; var unmasked = 0;
var fromStart = false; var fromStart = false;
var defaultValue = template.getDefaultValue(valueId); var defaultValue = template.getDefaultValue(valueId);
if (defaultValue != null && !defaultValue.isBlank()) { if (defaultValue != null) {
var properties = RenderUtils.parsePropertiesString(defaultValue); var properties = new Properties();
mask = properties.getProperty("mask", mask);
try { try {
properties.load(new StringReader(defaultValue));
mask = properties.getProperty("mask", mask);
unmasked = Integer.parseInt(properties.getProperty("unmasked", "0")); unmasked = Integer.parseInt(properties.getProperty("unmasked", "0"));
} catch (NumberFormatException ignore) { fromStart = "true".equalsIgnoreCase(properties.getProperty("fromStart", "false"));
} catch (IOException | NumberFormatException ignore) {
// do nothing // do nothing
} }
fromStart = "true".equalsIgnoreCase(properties.getProperty("fromStart", "false"));
} }
return template.getEncoder().encode( return template.getEncoder().encode(
RenderUtils.mask(template.getValueOrAttribute(differentiator), mask, unmasked, fromStart)); RenderUtils.mask(template.getValueOrAttribute(differentiator), mask, unmasked, fromStart));

View file

@ -20,6 +20,10 @@ package rife.render;
import rife.template.Template; import rife.template.Template;
import rife.template.ValueRenderer; import rife.template.ValueRenderer;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
/** /**
* <p>Generates an SVG QR Code for a template value using <a href="https://goqr.me/">goQR.me</a>.</p> * <p>Generates an SVG QR Code for a template value using <a href="https://goqr.me/">goQR.me</a>.</p>
* *
@ -40,8 +44,17 @@ public class QrCode implements ValueRenderer {
*/ */
@Override @Override
public String render(Template template, String valueId, String differentiator) { public String render(Template template, String valueId, String differentiator) {
var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId)); var size = "150x150";
var size = properties.getProperty("size", "150x150"); var defaultValue = template.getDefaultValue(valueId);
if (defaultValue != null) {
var properties = new Properties();
try {
properties.load(new StringReader(defaultValue));
size = properties.getProperty("size", size);
} catch (IOException ignore) {
// do nothing
}
}
return RenderUtils.qrCode(template.getValueOrAttribute(differentiator), size); return RenderUtils.qrCode(template.getValueOrAttribute(differentiator), size);
} }
} }

View file

@ -23,7 +23,6 @@ import rife.tools.StringUtils;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.text.Normalizer; import java.text.Normalizer;
@ -33,8 +32,6 @@ import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField; import java.time.temporal.ChronoField;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* Collection of utility-type methods commonly used by the renderers. * Collection of utility-type methods commonly used by the renderers.
@ -47,6 +44,7 @@ public final class RenderUtils {
* The encoding property. * The encoding property.
*/ */
public static final String ENCODING_PROPERTY = "encoding"; public static final String ENCODING_PROPERTY = "encoding";
/** /**
* ISO 8601 date formatter. * ISO 8601 date formatter.
* *
@ -84,7 +82,6 @@ public final class RenderUtils {
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());
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";
private final static Logger LOGGER = Logger.getLogger(RenderUtils.class.getName());
private RenderUtils() { private RenderUtils() {
// no-op // no-op
@ -211,26 +208,15 @@ public final class RenderUtils {
*/ */
public static String fetchUrl(String url, String defaultContent) { public static String fetchUrl(String url, String defaultContent) {
try { try {
var fetchUrl = new URL(url); var connection = (HttpURLConnection) new URL(url).openConnection();
try { connection.setRequestProperty("User-Agent", DEFAULT_USER_AGENT);
var connection = (HttpURLConnection) fetchUrl.openConnection(); var code = connection.getResponseCode();
connection.setRequestProperty("User-Agent", DEFAULT_USER_AGENT); if (code >= 200 && code <= 399) {
var code = connection.getResponseCode(); try (var inputStream = connection.getInputStream()) {
if (code >= 200 && code <= 399) { return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
try (var inputStream = connection.getInputStream()) {
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}
} else {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning("A " + code + " status code was returned by " + fetchUrl.getHost());
}
}
} catch (IOException ioe) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, "An IO error occurred while connecting to " + fetchUrl.getHost(), ioe);
} }
} }
} catch (MalformedURLException ignore) { } catch (IOException ignore) {
// do nothing // do nothing
} }
return defaultContent; return defaultContent;
@ -352,7 +338,7 @@ public final class RenderUtils {
} }
/** /**
* Returns a new {@code Properties} containing the properties specified in the given {@code String}. * Returns a new {@code Properties} containing the properties specified in the given {$String}.
* *
* @param src the {@code} String containing the properties * @param src the {@code} String containing the properties
* @return the new {@code Properties} * @return the new {@code Properties}

View file

@ -20,7 +20,10 @@ package rife.render;
import rife.template.Template; import rife.template.Template;
import rife.template.ValueRenderer; import rife.template.ValueRenderer;
import java.io.IOException;
import java.io.StringReader;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.util.Properties;
/** /**
* Renders the server uptime. * Renders the server uptime.
@ -42,7 +45,16 @@ public class Uptime implements ValueRenderer {
*/ */
@Override @Override
public String render(Template template, String valueId, String differentiator) { public String render(Template template, String valueId, String differentiator) {
var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId)); var properties = new Properties();
var defaultValue = template.getDefaultValue(valueId);
if (defaultValue != null) {
try {
properties.load(new StringReader(defaultValue));
} catch (IOException ignore) {
// ignore
}
}
String uptime; String uptime;
if (template.hasAttribute(Uptime.class.getName())) { if (template.hasAttribute(Uptime.class.getName())) {
uptime = RenderUtils.uptime((long) template.getAttribute(Uptime.class.getName()), properties); uptime = RenderUtils.uptime((long) template.getAttribute(Uptime.class.getName()), properties);