Class Utils


  • public final class Utils
    extends Object
    Utility functions for DSpace.
    Version:
    $Revision$
    Author:
    Peter Breton
    • Method Detail

      • getMD5

        public static String getMD5​(String data)
        Return an MD5 checksum for data in hex format.
        Parameters:
        data - The data to checksum.
        Returns:
        MD5 checksum for the data in hex format.
      • getMD5

        public static String getMD5​(byte[] data)
        Return an MD5 checksum for data in hex format.
        Parameters:
        data - The data to checksum.
        Returns:
        MD5 checksum for the data in hex format.
      • getMD5Bytes

        public static byte[] getMD5Bytes​(byte[] data)
        Return an MD5 checksum for data as a byte array.
        Parameters:
        data - The data to checksum.
        Returns:
        MD5 checksum for the data as a byte array.
      • toHex

        public static String toHex​(byte[] data)
        Return a hex representation of the byte array
        Parameters:
        data - The data to transform.
        Returns:
        A hex representation of the data.
      • generateKey

        public static String generateKey()
        Generate a unique key. The key is a long (length 38 to 40) sequence of digits.
        Returns:
        A unique key as a long sequence of base-10 digits.
      • generateHexKey

        public static String generateHexKey()
        Generate a unique key. The key is a 32-character long sequence of hex digits.
        Returns:
        A unique key as a long sequence of hex digits.
      • generateBytesKey

        public static byte[] generateBytesKey()
        Generate a unique key as a byte array.
        Returns:
        A unique key as a byte array.
      • copy

        public static void copy​(InputStream input,
                                OutputStream output)
                         throws IOException
        Copy stream-data from source to destination. This method does not buffer, flush or close the streams, as to do so would require making non-portable assumptions about the streams' origin and further use. If you wish to perform a buffered copy, use bufferedCopy(java.io.InputStream, java.io.OutputStream).
        Parameters:
        input - The InputStream to obtain data from.
        output - The OutputStream to copy data to.
        Throws:
        IOException - if IO error
      • addEntities

        public static String addEntities​(String value)
        Replace characters that could be interpreted as HTML codes with symbolic references (entities). This function should be called before displaying any metadata fields that could contain the characters "<", ">", "&", "'", and double quotation marks. This will effectively disable HTML links in metadata.
        Parameters:
        value - the metadata value to be scrubbed for display
        Returns:
        the passed-in string, with html special characters replaced with entities.
      • parseDuration

        public static long parseDuration​(String duration)
                                  throws ParseException
        Utility method to parse durations defined as \d+[smhdwy] (seconds, minutes, hours, days, weeks, years)
        Parameters:
        duration - specified duration
        Returns:
        number of milliseconds equivalent to duration.
        Throws:
        ParseException - if the duration is of incorrect format
      • parseISO8601Date

        public static Date parseISO8601Date​(String s)
        Translates timestamp from an ISO 8601-standard format, which is commonly used in XML and RDF documents. This method is synchronized because it depends on a non-reentrant static DateFormat (more efficient than creating a new one each call).
        Parameters:
        s - the input string
        Returns:
        Date object, or null if there is a problem translating.
      • formatISO8601Date

        public static String formatISO8601Date​(Date d)
        Convert a Date to String in the ISO 8601 standard format. The RFC822 timezone is almost right, still need to insert ":". This method is synchronized because it depends on a non-reentrant static DateFormat (more efficient than creating a new one each call).
        Parameters:
        d - the input Date
        Returns:
        String containing formatted date.
      • tokenize

        public static String[] tokenize​(String metadata)
        Utility method to extract schema, element, qualifier from the metadata field key Keep in mind that this method try to auto discover the common separator used in DSpace ("_" or ".") Return an array of token with size 3 which contains: schema = tokens[0]; element = tokens[1]; qualifier = tokens[2]; //it can be empty string
        Parameters:
        metadata - (the field in the form dc.title or dc_title)
        Returns:
        array of tokens
      • standardize

        public static String standardize​(String schema,
                                         String element,
                                         String qualifier,
                                         String separator)
        Make the metadata field key using the separator.
        Parameters:
        schema -
        element -
        qualifier -
        separator - (DSpace common separator are "_" or ".")
        Returns:
        metadata field key
      • getBaseUrl

        public static String getBaseUrl​(String urlString)
        Retrieve the baseurl from a given URL string
        Parameters:
        urlString - URL string
        Returns:
        baseurl (without any context path) or null (if URL was invalid)
      • getHostName

        public static String getHostName​(String uriString)
        Retrieve the hostname from a given URI string
        Parameters:
        uriString - URI string
        Returns:
        hostname (without any www.) or null (if URI was invalid)
      • interpolateConfigsInString

        public static String interpolateConfigsInString​(String string)
        Replaces configuration placeholders within a String with the corresponding value from DSpace's Configuration Service.

        For example, given a String like "My DSpace is installed at ${dspace.dir}", this method will replace "${dspace.dir}" with the configured value of that property.

        Parameters:
        string - source string
        Returns:
        string with any placeholders replaced with configured values.