Package com.epimorphics.util
Class NameUtils
- java.lang.Object
-
- com.epimorphics.util.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 Summary
Constructors Constructor Description NameUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intasInt(Object param, int defaultint)Extract an integer from a parameter object, with defaultstatic StringdecodeSafeName(String name)Decode an encoded safe namestatic StringencodeSafeName(String name)Convert a name to a safe name in a reversible fashion by (very) conservative percent-encoding of the UTF-8 version.static StringensureLastSlash(String u)Normalize a URI by ensuring there is a final "/"static Stringescape(String value, char esc)Escape a sensitive character by a "\" if it is not already so escaped.static Stringextension(String f)Find the .xxx extension of a file namestatic StringformatDuration(long duration)Format a duration (measured in ms) as human readablestatic StringidFromURI(String uri)Encode a resource URI into a unique label which can be used as a URI segment into a REST API.static booleanisURI(String name)Test if the given name is a legal absolute URI.static StringlastSegment(String f)Find the last path segment in a URL or unix path.static StringnewName(String type)Generate a random name for something of the given type, result will currently look like: {type}-{uuid}static StringnewURI(String type, String serverURI)Generate a random URI for something of the given type, result will currently look like: {server}/modal/{type}/{uuid}static StringnormalizeFilepath(String path)Normalize a relative file path to unix syntax.static StringremoveExtension(String f)Remove a trailing .xxx extension from a file namestatic StringremoveFilePrefix(String f)Strip any leading "file:" off a URL stringstatic StringsafeName(String name)Convert an arbitrary name to name safe to use as a file name or URL path slug.static StringsafeVarName(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.static StringsplitAfterLast(String filename, String at)Return segment of a string after the last occurrence of "at"static StringsplitBeforeLast(String filen, String at)Return segment of a string before the last occurrence of "at"static StringstripLastSlash(String u)Normalize a URI by stripping any final "/"static StringuriFor(String name, String serverURI, String type)Convert an arbitrary name to a URI which can be used in metadata descriptions of the named object.
-
-
-
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.
-
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
-
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
-
-