Class Util


  • public class Util
    extends Object
    Implements several stateless utility methods.
    Author:
    tjquinn
    • Constructor Detail

      • Util

        public Util()
        Creates a new instance of Util
    • Method Detail

      • replaceTokens

        public static String replaceTokens​(String s,
                                           Properties values)
        Searches for placeholders of the form ${token-name} in the input String, retrieves the property with name token-name from the Properties object, and (if found) replaces the token in the input string with the property value.
        Parameters:
        s - String possibly containing tokens
        values - Properties object containing name/value pairs for substitution
        Returns:
        the original string with tokens substituted using their values from the Properties object
      • writeTextToTempFile

        public static File writeTextToTempFile​(String content,
                                               String prefix,
                                               String suffix,
                                               boolean retainFile)
                                        throws IOException,
                                               FileNotFoundException
        Writes the provided text to a temporary file marked for deletion on exit.
        Parameters:
        content - the content to be written
        prefix - prefix for the temp file, conforming to the File.createTempFile requirements
        suffix - suffix for the temp file
        retainFile - whether to keep the file
        Returns:
        File object for the newly-created temp file
        Throws:
        IOException - for any errors writing the temporary file
        FileNotFoundException - if the temp file cannot be opened for any reason
      • writeTextToTempFile

        public static File writeTextToTempFile​(String content,
                                               String prefix,
                                               String suffix)
                                        throws IOException,
                                               FileNotFoundException
        Writes the provided text to a temporary file marked for deletion on exit.
        Parameters:
        content - the content to be written
        prefix - for the temp file, conforming to the File.createTempFile requirements
        suffix - for the temp file
        Returns:
        File object for the newly-created temp file
        Throws:
        IOException - for any errors writing the temporary file
        FileNotFoundException - if the temp file cannot be opened for any reason
      • locateClass

        public static URL locateClass​(Class target)
        Finds the jar file or directory that contains the current class and returns its URI.
        Parameters:
        target - the class, the containing jar file or directory of which is of interest
        Returns:
        URL to the containing jar file or directory
      • loadResource

        public static String loadResource​(Class contextClass,
                                          String resourcePath)
                                   throws IOException
        Retrieves a resource as a String.

        This method does not save the template in a cache. Use the instance method getTemplate for that purpose.

        Parameters:
        contextClass - a class, the class loader of which should be used for searching for the template
        resourcePath - the path of the resource to load, relative to the contextClass
        Returns:
        the resource's contents
        Throws:
        IOException - if the resource is not found or in case of error while loading it