Added Normalize renderer

This commit is contained in:
Erik C. Thauvin 2023-03-19 00:38:30 -07:00
parent 0ab3e93caa
commit c5cbff291f
7 changed files with 97 additions and 12 deletions

View file

@ -24,6 +24,7 @@ import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.Normalizer;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
@ -121,6 +122,34 @@ public final class RenderUtils {
return "";
}
/**
* Normalizes a String for inclusion in a URL path.
*
* @param src The source String
* @return The normalized String
*/
public static String normalize(String src) {
var sb = new StringBuilder(src.length());
var normalized = Normalizer.normalize(src.trim(), Normalizer.Form.NFD);
boolean space = false;
for (var c : normalized.toCharArray()) {
if (c <= '\u007F') { // ascii only
if (!space && c == ' ') {
space = true;
sb.append('-');
} else {
space = false;
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
sb.append(c);
} else if (c >= 'A' && c <= 'Z') {
sb.append((char) (c + 32)); // lowercase
}
}
}
}
return sb.toString();
}
/**
* Returns the plural form of a word, if count &gt; 1.
*