Class SourceUtil

java.lang.Object
ch.grengine.source.SourceUtil

public class SourceUtil extends Object
Static utility methods around Source.
Since:
1.0
Author:
Alain Stalder, Made in Switzerland.
  • Method Details

    • textsToSourceSet

      public static Set<Source> textsToSourceSet(Collection<String> texts)
      creates a source set from the given collections of script texts, using the default source factory.
      Parameters:
      texts - script texts
      Returns:
      source set
      Since:
      1.0
    • textsToSourceSet

      public static Set<Source> textsToSourceSet(String... texts)
      creates a source set from the given script texts, using the default source factory.
      Parameters:
      texts - script texts
      Returns:
      source set
      Since:
      1.0
    • textsToSourceSet

      public static Set<Source> textsToSourceSet(SourceFactory sourceFactory, Collection<String> texts)
      creates a source set from the given collections of script texts, using the given source factory.
      Parameters:
      sourceFactory - source factory
      texts - script texts
      Returns:
      source set
      Since:
      1.0
    • textsToSourceSet

      public static Set<Source> textsToSourceSet(SourceFactory sourceFactory, String... texts)
      creates a source set from the given script texts, using the given source factory.
      Parameters:
      sourceFactory - source factory
      texts - script texts
      Returns:
      source set
      Since:
      1.0
    • textsToSourceSet

      public static Set<Source> textsToSourceSet(Map<String,String> texts)
      creates a source set from the given map of desired class name to script text, using the default source factory.
      Parameters:
      texts - map of desired class name to script text
      Returns:
      source set
      Since:
      1.0
    • textsToSourceSet

      public static Set<Source> textsToSourceSet(SourceFactory sourceFactory, Map<String,String> texts)
      creates a source set from the given map of desired class name to script text, using the given source factory.
      Parameters:
      sourceFactory - source factory
      texts - map of desired class name to script text
      Returns:
      source set
      Since:
      1.0
    • filesToSourceSet

      public static Set<Source> filesToSourceSet(Collection<File> files)
      creates a source set from the given collections of script files, using the default source factory.
      Parameters:
      files - script files
      Returns:
      source set
      Since:
      1.0
    • filesToSourceSet

      public static Set<Source> filesToSourceSet(File... files)
      creates a source set from the given script files, using the default source factory.
      Parameters:
      files - script files
      Returns:
      source set
      Since:
      1.0
    • filesToSourceSet

      public static Set<Source> filesToSourceSet(SourceFactory sourceFactory, Collection<File> files)
      creates a source set from the given collections of script files, using the given source factory.
      Parameters:
      sourceFactory - source factory
      files - script files
      Returns:
      source set
      Since:
      1.0
    • filesToSourceSet

      public static Set<Source> filesToSourceSet(SourceFactory sourceFactory, File... files)
      creates a source set from the given script files, using the given source factory.
      Parameters:
      sourceFactory - source factory
      files - script files
      Returns:
      source set
      Since:
      1.0
    • urlsToSourceSet

      public static Set<Source> urlsToSourceSet(Collection<URL> urls)
      creates a source set from the given collections of script URLs, using the default source factory.
      Parameters:
      urls - script URLs
      Returns:
      source set
      Since:
      1.0
    • urlsToSourceSet

      public static Set<Source> urlsToSourceSet(URL... urls)
      creates a source set from the given script URLs, using the default source factory.
      Parameters:
      urls - script URLs
      Returns:
      source set
      Since:
      1.0
    • urlsToSourceSet

      public static Set<Source> urlsToSourceSet(SourceFactory sourceFactory, Collection<URL> urls)
      creates a source set from the given collections of script URLs, using the given source factory.
      Parameters:
      sourceFactory - source factory
      urls - script URLs
      Returns:
      source set
      Since:
      1.0
    • urlsToSourceSet

      public static Set<Source> urlsToSourceSet(SourceFactory sourceFactory, URL... urls)
      creates a source set from the given script URLs, using the given source factory.
      Parameters:
      sourceFactory - source factory
      urls - script URLs
      Returns:
      source set
      Since:
      1.0
    • sourceToSourceSet

      public static Set<Source> sourceToSourceSet(Source source)
      converts the given source to a set of source that contains only the given source.
      Parameters:
      source - source
      Returns:
      source set
      Since:
      1.0
    • sourceCollectionToSourceSet

      public static Set<Source> sourceCollectionToSourceSet(Collection<Source> sourceCollection)
      converts the given collection of source to a set of source.
      Parameters:
      sourceCollection - source collection
      Returns:
      source set
      Since:
      1.0
    • sourceArrayToSourceSet

      public static Set<Source> sourceArrayToSourceSet(Source... sourceArray)
      converts the given source varargs or array of source to a set of source.
      Parameters:
      sourceArray - source varargs or array of source
      Returns:
      source set
      Since:
      1.0
    • hash

      public static String hash(String text, String algorithm)
      calculates a cryptographic hash function (message digest).

      The given text is first UTF-8 encoded to bytes, then the given hash is calculated and finally returned as a hex string.

      Parameters:
      text - the text to hash
      algorithm - the hash algorithm to use
      Returns:
      hash hex string
      Throws:
      UnsupportedOperationException - if the given hash algorithm is not available
      Since:
      1.0
    • md5

      public static String md5(String text)
      calculates an MD5 hash.

      The given text is first UTF-8 encoded to bytes, then the MD5 hash is calculated and finally returned as a hex string.

      Parameters:
      text - the text to hash
      Returns:
      MD5 hash text string
      Throws:
      UnsupportedOperationException - if MD5 is not available (which is very unlikely)
      Since:
      1.0
    • getTextStartNoLineBreaks

      public static String getTextStartNoLineBreaks(String text, int maxLen)
      gets the start of the given text with line breaks removed.

      The returned text is at most maxLen characters long and line breaks are converted to "%n". If the text had to be cut, this is indicated by "[..]" at the end of the returned text.

      Parameters:
      text - text
      maxLen - maximal length of the returned text
      Returns:
      start of the given text with line breaks removed
      Throws:
      IllegalArgumentException - if maxLen is less than 10
      Since:
      1.1.1
    • readUrlText

      public static String readUrlText(URL url, String encoding) throws IOException
      reads the content from the given URL using the given character encoding.
      Parameters:
      url - URL
      encoding - encoding, e.g. "UTF-8"
      Returns:
      URL content text
      Throws:
      IOException - if could not read from the URL
      Since:
      1.0
    • toCanonicalOrAbsoluteFile

      public static File toCanonicalOrAbsoluteFile(File file)
      converts the given file to the canonical file, with fallback to the absolute file if getting the canonical file failed.

      The returned file is thus guaranteed to be an absolute file.

      Parameters:
      file - file
      Returns:
      canonical or absolute file
      Since:
      1.0