Class NameUtils


  • public class NameUtils
    extends Object
    Utilities for checking and converting names to forms safely usable in different circumstances.

    Has accreeted a ragbag of other string bashing utilities.

    Author:
    Dave Reynolds
    • Constructor Detail

      • NameUtils

        public NameUtils()
    • Method Detail

      • safeName

        public static String safeName​(String name)
        Convert an arbitrary name to name safe to use as a file name or URL path slug. Not reversible.
      • safeVarName

        public static String safeVarName​(String name)
        Convert an arbitrary name, such as a CSV column name to something usable as a variable name in an expression as well as a path segment, normalizing the case.
      • uriFor

        public static String uriFor​(String name,
                                    String serverURI,
                                    String type)
        Convert an arbitrary name to a URI which can be used in metadata descriptions of the named object. If the name already looks like a URI then use that. If not then mint a URI based on the address of the server and the given name of the "type" of resource. This will look like:
          {intendedBaseURL}/modal/{type}/{safe-version-of-name}
         
      • isURI

        public static boolean isURI​(String name)
        Test if the given name is a legal absolute URI.
      • idFromURI

        public static String idFromURI​(String uri)
        Encode a resource URI into a unique label which can be used as a URI segment into a REST API. Not reversible.
      • encodeSafeName

        public static String encodeSafeName​(String name)
        Convert a name to a safe name in a reversible fashion by (very) conservative percent-encoding of the UTF-8 version.
      • decodeSafeName

        public static String decodeSafeName​(String name)
        Decode an encoded safe name
      • newName

        public static String newName​(String type)
        Generate a random name for something of the given type, result will currently look like: {type}-{uuid}
      • newURI

        public static String newURI​(String type,
                                    String serverURI)
        Generate a random URI for something of the given type, result will currently look like: {server}/modal/{type}/{uuid}
      • asInt

        public static int asInt​(Object param,
                                int defaultint)
        Extract an integer from a parameter object, with default
      • normalizeFilepath

        public static String normalizeFilepath​(String path)
        Normalize a relative file path to unix syntax. I.e. on widows will replace "\" by "/"
      • removeFilePrefix

        public static String removeFilePrefix​(String f)
        Strip any leading "file:" off a URL string
      • lastSegment

        public static String lastSegment​(String f)
        Find the last path segment in a URL or unix path.
      • stripLastSlash

        public static String stripLastSlash​(String u)
        Normalize a URI by stripping any final "/"
      • ensureLastSlash

        public static String ensureLastSlash​(String u)
        Normalize a URI by ensuring there is a final "/"
      • removeExtension

        public static String removeExtension​(String f)
        Remove a trailing .xxx extension from a file name
      • extension

        public static String extension​(String f)
        Find the .xxx extension of a file name
      • splitAfterLast

        public static String splitAfterLast​(String filename,
                                            String at)
        Return segment of a string after the last occurrence of "at"
      • splitBeforeLast

        public static String splitBeforeLast​(String filen,
                                             String at)
        Return segment of a string before the last occurrence of "at"
      • formatDuration

        public static String formatDuration​(long duration)
        Format a duration (measured in ms) as human readable
      • escape

        public static String escape​(String value,
                                    char esc)
        Escape a sensitive character by a "\" if it is not already so escaped.