Package rife.render

Class RenderUtils

java.lang.Object
rife.render.RenderUtils

@NullMarked public final class RenderUtils extends Object
Collection of utility-type methods commonly used by the renderers.
Since:
1.0
Author:
Erik C. Thauvin
  • Field Details

    • ENCODING_PROPERTY

      public static final String ENCODING_PROPERTY
      The encoding property.
      See Also:
    • ISO_8601_DATE_FORMATTER

      public static final DateTimeFormatter ISO_8601_DATE_FORMATTER
      ISO 8601 date formatter.
      See Also:
    • ISO_8601_FORMATTER

      public static final DateTimeFormatter ISO_8601_FORMATTER
      ISO 8601 date and time formatter.
      See Also:
    • ISO_8601_TIME_FORMATTER

      public static final DateTimeFormatter ISO_8601_TIME_FORMATTER
      ISO 8601 time formatter.
      See Also:
    • ISO_8601_YEAR_FORMATTER

      public static final DateTimeFormatter ISO_8601_YEAR_FORMATTER
      ISO 8601 Year formatter.
      See Also:
    • RFC_2822_FORMATTER

      public static final DateTimeFormatter RFC_2822_FORMATTER
      RFC 2822 date and time formatter.
      See Also:
  • Method Details

    • abbreviate

      public static String abbreviate(String src, int max, String marker)
      Abbreviates a String to the given length using a replacement marker.
      Parameters:
      src - the source String
      max - the maximum length of the resulting String
      marker - the String used as a replacement marker
      Returns:
      the abbreviated String
      Throws:
      NullPointerException - if the src or marker is null
    • beatTime

      public static String beatTime(ZonedDateTime zonedDateTime)
      Returns the Swatch Internet (.beat) Time for the give date-time.
      Parameters:
      zonedDateTime - the date and time
      Returns:
      the .beat time. (eg.: @248)
    • capitalizeWords

      public static String capitalizeWords(String src)
      Returns a String with the first letter of each word capitalized and the remaining letters lowercased (i.e., title-cased).

      Note: this method lowercases all non-initial characters within each word. For example, "myHTML" becomes "Myhtml".

      Parameters:
      src - the source String
      Returns:
      the title-cased String
      Throws:
      NullPointerException - if the src is null
    • encode

      public static String encode(String src, Properties properties)

      Encodes the source String to the specified encoding.

      The supported encodings are:

      • html
      • js
      • json
      • unicode
      • url
      • xml
      Parameters:
      src - the source String to encode
      properties - the properties containing the encoding property.
      Returns:
      the encoded String
      Throws:
      NullPointerException - if the src is null
    • encodeJs

      public static String encodeJs(String src)
      Encodes a String to JavaScript/ECMAScript.
      Parameters:
      src - the source String
      Returns:
      the encoded String
      Throws:
      NullPointerException - if the src is null
    • fetchUrl

      public static String fetchUrl(String url, String defaultContent)
      Fetches the content (body) of a URL.
      Parameters:
      url - the URL String
      defaultContent - the default content to return if none fetched
      Returns:
      the URL content, or empty
      Throws:
      NullPointerException - if the URL or default content is null
    • formatCreditCard

      public static String formatCreditCard(String src)

      Returns the last 4 digits a credit card number.

      • The number must satisfy the Luhn algorithm
      • Non-digits are stripped from the number
      Parameters:
      src - the credit card number
      Returns:
      the last 4 digits of the credit card number, or empty if blank or invalid
      Throws:
      NullPointerException - if src is null
    • htmlEntities

      public static String htmlEntities(String src)
      Converts a text String to HTML decimal entities.
      Parameters:
      src - the String to convert
      Returns:
      the converted String
      Throws:
      NullPointerException - if the src is null
    • mask

      public static String mask(String src, String mask, int unmasked, boolean fromStart)
      Masks characters in a String.

      If unmasked is negative or zero, the entire string is masked. If unmasked is greater than or equal to the total character count, the entire string is returned unmasked.

      Parameters:
      src - the source String
      mask - the String to mask characters with
      unmasked - the number of characters to leave unmasked (negative treated as 0)
      fromStart - to unmask characters from the start of the String
      Returns:
      the masked String
      Throws:
      NullPointerException - if src or mask are null
    • normalize

      public static String normalize(String src)
      Normalizes a String for inclusion in a URL path.
      Parameters:
      src - the source String
      Returns:
      the normalized String
      Throws:
      NullPointerException - if the src is null
    • parsePropertiesString

      public static Properties parsePropertiesString(@Nullable String src)
      Returns a new Properties containing the properties specified in the given String.
      Parameters:
      src - the String containing the properties
      Returns:
      the new Properties
    • plural

      public static String plural(long count, String word, String plural)
      Returns the plural form of a word based on count.

      Uses count != 1 as the pluralization condition, so both 0 and values > 1 return the plural form (e.g., "0 minutes", "2 minutes").

      Parameters:
      count - the count
      word - the singular word
      plural - the plural word
      Returns:
      the singular or plural String
    • qrCode

      public static String qrCode(String src, String size)
      Generates an SVG QR Code from the given String using goQR.me.
      Parameters:
      src - the data String
      size - the QR Code size. (e.g. 150x150)
      Returns:
      the QR code
      Throws:
      NullPointerException - if the src or size is null
    • rot13

      public static String rot13(String src)
      Translates a String to/from ROT13.
      Parameters:
      src - the source String
      Returns:
      the translated String
      Throws:
      NullPointerException - if the src is null
    • shortenUrl

      public static String shortenUrl(String url)

      Shortens a URL using is.gid.

      The URL String must be a valid http or https URL.

      Based on isgd-shorten

      Parameters:
      url - the source URL
      Returns:
      the short URL
      Throws:
      NullPointerException - if the url is null
    • swapCase

      public static String swapCase(String src)
      Swaps the case of a String.
      Parameters:
      src - the String to swap the case of
      Returns:
      the modified String
      Throws:
      NullPointerException - if the src is null
    • uptime

      public static String uptime(long uptime, Properties properties)

      Returns the formatted server uptime.

      The default Properties are:

       year=\ year⧵u0020
       years=\ years⧵u0020
       month=\ month⧵u0020
       months=\ months⧵u0020
       week=\ week⧵u0020
       weeks=\ weeks⧵u0020
       day=\ day⧵u0020
       days=\ days⧵u0020
       hour=\ hour⧵u0020
       hours=\ hours⧵u0020
       minute=\ minute
       minutes=\ minutes
       
      Parameters:
      uptime - the uptime in milliseconds
      properties - the format properties
      Returns:
      the formatted uptime
    • validateCreditCard

      public static boolean validateCreditCard(String cc)
      Validates a credit card number using the Luhn algorithm.
      Parameters:
      cc - the credit card number
      Returns:
      true if the credit card number is valid
      Throws:
      NullPointerException - if the cc is null