Class MDfromHTMLUtils

    • Constructor Detail

      • MDfromHTMLUtils

        public MDfromHTMLUtils()
    • Method Detail

      • arrayListToListString

        public static String arrayListToListString​(ArrayList<?> list)
        Utility method to create a list of the content of an ArrayList by converting each object to its toString() representation and appending it within a list using the supplied delimiter. If a String is encountered in the ArrayList, it will be quoted (e.g., surrounded by double quote marks). The list itself is enclosed by an opening ('{') and closing ('}') brace. Note: there is no escaping of the characters in a String object, so if it contains a delimiter or a closing brace, you may get unexpected results.
        Parameters:
        list - the array list of objects to be converted to a list string.
        Returns:
        the list comprising an opening brace, then each object in the arraylist converted to a string, followed by the closing brace, with the caveat that string objects encountered in the arraylist are enclosed in a pair of double quotes.
        See Also:
        listStringToArrayList(java.lang.String)
      • byteToHexChars

        public static char[] byteToHexChars​(byte bIn)
        Converts a byte into an array of char's containing the hexadecimal digits. For example, 0x2f would return char[] {'2','F'}
        Parameters:
        bIn - byte to be converted to hexadecimal digits
        Returns:
        array of chars containing the hexadecimal digits for the value of the input byte.
      • checkWordFilter

        public static boolean checkWordFilter​(String word)
        Set up filter if the supplied word contains an @ like in an email address, or starts or ends with a number, or contains http
        Parameters:
        word - value to be tested
        Returns:
        true if the word should be filtered
      • cleanseHTMLTagsFromText

        public static String cleanseHTMLTagsFromText​(String text)
        Removes HTML Tags from the supplied text, replacing them with a space (0x20)
        Parameters:
        text - the string to be cleansed
        Returns:
        the cleansed string
      • cleanURL

        public static String cleanURL​(String url)
        Cleans trailing characters from the supplied URL based on how URL's might be referenced within dialogs (e.g., removes trailing comma, double quote, question mark, or period, as well as newline, and carriage return.
        Parameters:
        url - the URL to be cleansed
        Returns:
        the cleansed URL
      • trimSpaces

        public static String trimSpaces​(String word)
      • cleanWord

        public static String[] cleanWord​(String word)
        Strip off non-word characters from the beginning and end of the supplied word.
        Parameters:
        word - the word to be cleansed
        Returns:
        array of cleansed word parts: [0] prefix removed from word, [1] cleansed word, [2] suffix removed from word
      • closeTextFile

        public static void closeTextFile​(BufferedWriter bw)
        Close a buffered writer flushing its content first. Nothing happens if a null is passed.
        Parameters:
        bw - the buffered writer to be flushed and closed.
      • convertMillisecondsToTimeZone

        public static String convertMillisecondsToTimeZone​(int iMillisecs)
        Convert a number of milliseconds to a formatted String containing a sign, followed by the hours and minutes as in "-0500" or "+0100" which are used for TimeZones.
        Parameters:
        iMillisecs - the number of milliseconds from Greenwich Mean Time.
        Returns:
        a string of the form +/-hhmm as in "-0500" or "+0100"
      • convertTimeZoneToMilliseconds

        public static int convertTimeZoneToMilliseconds​(String strTimeZone)
        Converts a timezone of the format +/-hhmm to milliseconds
        Parameters:
        strTimeZone - timezone offset from Greenwich Mean Time (GMT) for example "-0500" is Eastern Standard Time, "-0400" is Eastern Daylight Time, "+0000" is Greenwich Mean Time, and "+0100" is the offset for Europe/Paris.
        Returns:
        milliseconds from Greenwich Mean Time
      • exceptionTraceToString

        public static String exceptionTraceToString​(Throwable throwableException)
        Captures an exception's stack trace as a string for inclusion in JSON objects
        Parameters:
        throwableException - exception whose stack is to be captured
        Returns:
        String representation containing the exception's stack trace
      • getKey

        public static char[] getKey()
        Returns:
        the key needed for encrypting content
      • getNameFromClass

        public static String getNameFromClass​(Class<?> inClass)
        Transform a fully qualified Class' name into just the name of the class without the leading package. For example, "com.mdfromhtml.core.MDfromHTMLDate" would return just "MDfromHTMLDate"
        Parameters:
        inClass -
        Returns:
        name of the class without leading qualification
      • getUniqueID

        public static String getUniqueID()
        Returns:
        a 40 byte String random number based on invoking the com.ibm.crypto.fips.provider.SecureRandom class.
      • toUTF8Bytes

        public static byte[] toUTF8Bytes​(String string)
        Transform the String to a UTF-8 encoded byte array
        Parameters:
        string -
        Returns:
        byte array of the supplied string
      • hexDecode

        public static byte[] hexDecode​(String strHex)
                                throws InvalidParameterException
        Transform the string of hexadecimal digits into a byte array.
        Parameters:
        strHex - a String containing pairs of hexadecimal digits
        Returns:
        a byte array created by transforming pairs of hexadecimal digits into a byte. For example "7F41" would become byte [] { 0x7f, 0x41}
        Throws:
        InvalidParameterException - thrown if the input string is null or empty, or if it does not contain an even number of hexadecimal digits, or if it contains something other than a hexadecimal digit.
      • hexEncode

        public static String hexEncode​(byte[] bArray)
        Convert the byte array into a String of hexadecimal digits. For example, the bytes[] {0x31,0x0a} would become "310A".
        Parameters:
        bArray - the array of bytes to be converted.
        Returns:
        a String of hexadecimal digits formed by the hexadecimal digit for each nibble of the byte.
      • isEmpty

        public static boolean isEmpty​(String strInput)
        Determine if the String is empty (equals "").
        Parameters:
        strInput - the string to be evaluated.
        Returns:
        true if the strInput compares to MDfromHTMLConstants.EMPTY_String (""). Returns false if strInput is null or not empty.
      • isUndefined

        public static boolean isUndefined​(Class<?> testValue)
        Determine if the pass Class matches the MDfromHTMLConstants.UNDEFINED_Class.
        Parameters:
        testValue -
        Returns:
        whether (true) or not the supplied class is undefined
      • isUndefined

        public static boolean isUndefined​(Object testValue)
        Determine if the passed Object matches any of the MDfromHTMLConstants for UNDEFINED_* values.
        Parameters:
        testValue - value to compare against the appropriate MDfromHTMLConstants for UNDEFINED_* value.
        Returns:
        true if the values are equal (ignoring case), or false if they are not. Note, if passed value is null, true is returned.
      • isUndefined

        public static boolean isUndefined​(String testValue)
        Determine if the passed String is null, or when trimmed, matches the MDfromHTMLConstants.UNDEFINED_String or is empty or is equal to "null" (to support ABLE rules)
        Parameters:
        testValue - value to compare against MDfromHTMLConstants.UNDEFINED_String .
        Returns:
        true if the values are equal (ignoring case), or false if they are not. Note, if passed value is null or an empty string, true is returned.
      • isUndefined

        public static boolean isUndefined​(URI testValue)
        Determine if the passed URI is null, or equals the MDfromHTMLConstants.UNDEFINED_URI
        Parameters:
        testValue - value to compare against MDfromHTMLConstants.UNDEFINED_URI.
        Returns:
        true if the values are equal (ignoring case), or false if they are not. Note, if passed value is null, true is returned.
      • isValidURL

        public static boolean isValidURL​(String url)
        Tests whether the supplied url is valid
        Parameters:
        url - the URL to be tested
        Returns:
        true if the URL references http or https protocol
      • listSourceFiles

        public static List<Path> listSourceFiles​(Path dir,
                                                 String ext)
                                          throws IOException
        Construct and return a sorted list of files in a directory identified by the dir that have extensions matching the ext
        Parameters:
        dir - the path to the directory containing files to be returned in the list
        ext - the file extension (without the leading period) used to filter files in the dir
        Returns:
        sorted list of files in a directory identified by the dir that have extensions matching the ext
        Throws:
        IOException - if there is difficulty accessing the files in the supplied dir
      • listStringToArrayList

        public static ArrayList<Object> listStringToArrayList​(String strList)
        Transform a list of fields contained in a String bounded with opening ('{') and closing ('}') braces, and delimited with one of the delimiters (comma, space, tab, pipe). Fields containing strings are expected to be enclosed in double quotes ('"').
        Parameters:
        strList - the list of fields enclosed in braces.
        Returns:
        an ArrayList of the fields parsed from the supplied list string. Note: if the passed strList is null or empty, an empty ArrayList is returned (e.g., its size() is 0).
        See Also:
        arrayListToListString(java.util.ArrayList<?>)
      • loadJSONArtifact

        public static com.api.json.JSONArtifact loadJSONArtifact​(String jsonFQFileName)
                                                          throws Exception
        Load the specified JSON file from the fully qualified file name or throw the appropriate exception.
        Parameters:
        jsonFQFileName - name of the JSON file to be loaded
        Returns:
        the JSONObject or JSONArray contained in the file, or an empty JSONObject if no object exists
        Throws:
        Exception - If the file can no be located, or if there is a problem reading the file
      • loadJSONFile

        public static com.api.json.JSONObject loadJSONFile​(String jsonFQFileName)
                                                    throws Exception
        Load the specified JSON file from the fully qualified file name or throw the appropriate exception.
        Parameters:
        jsonFQFileName - name of the JSON file to be loaded
        Returns:
        the JSONObject contained in the file, or an empty JSONObject if no object exists
        Throws:
        Exception - If the file can no be located, or if there is a problem reading the file
      • loadJSONArray

        public static com.api.json.JSONArray loadJSONArray​(String jsonFQFileName)
                                                    throws Exception
        Load the JSONArray from the specified JSON file from the fully qualified file name or throw the appropriate exception.
        Parameters:
        jsonFQFileName - name of the JSON file to be loaded
        Returns:
        the JSONArray contained in the file, or an empty JSONArray if no object exists
        Throws:
        Exception - If the file can no be located, or if there is a problem reading the file
      • fromUTF8Bytes

        public static String fromUTF8Bytes​(byte[] bytes)
        Transform the UTF-8 encoded byte array into a String
        Parameters:
        bytes -
        Returns:
        string built from the supplied UTF-8 bytes
      • getMDfromHTMLWebServicesURI

        public static String getMDfromHTMLWebServicesURI()
        Returns:
        URI for MDfromHTMLwebServices
      • getMDfromHTMLServicesProps

        public static Properties getMDfromHTMLServicesProps()
                                                     throws Exception
        Returns:
        the properties file used to identify MDfromHTML general control parameters
        Throws:
        Exception
      • loadMDfromHTMLProperties

        public static Properties loadMDfromHTMLProperties​(String strPropFileName)
                                                   throws Exception
        Load the specified properties file and return the properties object, or null if an error occurs.
        Parameters:
        strPropFileName - name of the property file to be loaded
        Returns:
        the loaded properties object, or an empty properties if an error occurs.
        Throws:
        Exception - if the file can no be located
      • loadTextFile

        public static List<String> loadTextFile​(String fqFilename)
                                         throws Exception
        Reads the lines of a text file into a list of strings and returns that list. If no lines are present (e.g., empty file) then an empty list is returned.
        Parameters:
        fqFilename - fully qualified filename
        Returns:
        list of strings read from the file
        Throws:
        Exception - if the file can not be read.
      • padLeft

        public static String padLeft​(int iIn,
                                     int iWidth,
                                     char cPad)
        Helper method to create strings of the form "000nn".
        Parameters:
        iIn - integer value to be right justified with leading characters in the returned String.
        iWidth - integer value of the width of the returned String.
        cPad - character value to be used to pad the left portion of the returned String to make it as wide as the specified iWidth parameter. For example, calling toLeftPaddedString(iNum,4,'0') would result in "0045" if iNum == 45, or "0004" if iNum == 4.
        Returns:
        String containing the right justified value, padded to the specified with the specified pad character.
      • padLeft

        public static String padLeft​(String strInput,
                                     int iMax,
                                     char cPadChar)
        Creates a new String padded on its left side with the supplied pad character guaranteed to be the supplied length. If the supplied length is less than or equal to the length of the supplied string, the supplied string is returned. If the supplied string is null, a new string is returned filled with the supplied pad character that is as long as the supplied length.
        Parameters:
        strInput -
        iMax -
        cPadChar -
        Returns:
        formatted string with padding
      • padLeftZero

        public static String padLeftZero​(int iValue,
                                         int iMax)
        Creates a new String padded on its left side with zeros ('0') that is guaranteed to be the supplied length. If the supplied length is less than or equal to the length of the supplied string, the supplied string is returned. If the supplied string is null, a new string is returned filled with zeros as long as the supplied length.
        Parameters:
        iValue - the value to be right justified and left padded with zeros in the returned string.
        iMax - the desired maximum length of the String to be returned.
        Returns:
        a string with the value right justified with leading zeros to fill out to the desired maximum length specified. If iMax is less than the number of digits in the value, the returned string will be large enough to represent the entire value with no padding applied.
      • padLeftZero

        public static String padLeftZero​(String strInput,
                                         int iMax)
        Creates a new String padded on its left side with zeros ('0') that is guaranteed to be the supplied length. If the supplied length is less than or equal to the length of the supplied string, the supplied string is returned. If the supplied string is null, a new string is returned filled with zeros as long as the supplied length.
        Parameters:
        strInput - the input string to be right justified and left padded with zeros in the returned string.
        iMax - the desired maximum length of the String to be returned.
        Returns:
        a string with the input string right justified with leading zeros to fill out to the desired maximum length specified. If iMax is less than the length of the input string, the returned string will be input string.
      • padRight

        public static String padRight​(int iIn,
                                      int iWidth,
                                      char cPad)
        Helper method to create strings of the form "nn000".
        Parameters:
        iIn - integer value to be right justified with leading characters in the returned String.
        iWidth - integer value of the width of the returned String.
        cPad - character value to be used to pad the right portion of the returned String to make it as wide as the specified iWidth parameter. For example, calling toRightPaddedString(iNum,4,'0') would result in "4500" if iNum == 45, or "4000" if iNum == 4.
        Returns:
        String containing the right justified value, padded to the specified with the specified pad character.
      • padRight

        public static String padRight​(String strInput,
                                      int iMax,
                                      char cPadChar)
        Creates a new String padded on its right side with the supplied pad character guaranteed to be the supplied length. If the supplied length is less than or equal to the length of the supplied string, the supplied string is returned. If the supplied string is null, a new string is returned filled with the supplied pad character that is as long as the supplied length.
        Parameters:
        strInput -
        iMax -
        cPadChar -
        Returns:
        formatted string with padding
      • padRightZero

        public static String padRightZero​(int iValue,
                                          int iMax)
        Creates a new String padded on its right side with zeros ('0') that is guaranteed to be the supplied length. If the supplied length is less than or equal to the length of the supplied string, the supplied string is returned. If the supplied string is null, a new string is returned filled with zeros as long as the supplied length.
        Parameters:
        iValue - the value to be right justified and right padded with zeros in the returned string.
        iMax - the desired maximum length of the String to be returned.
        Returns:
        a string with the value right justified with leading zeros to fill out to the desired maximum length specified. If iMax is less than the number of digits in the value, the returned string will be large enough to represent the entire value with no padding applied.
      • padRightZero

        public static String padRightZero​(String strInput,
                                          int iMax)
        Creates a new String padded on its right side with zeros ('0') that is guaranteed to be the supplied length. If the supplied length is less than or equal to the length of the supplied string, the supplied string is returned. If the supplied string is null, a new string is returned filled with zeros as long as the supplied length.
        Parameters:
        strInput - the input string to be right justified and right padded with zeros in the returned string.
        iMax - the desired maximum length of the String to be returned.
        Returns:
        a string with the input string right justified with leading zeros to fill out to the desired maximum length specified. If iMax is less than the length of the input string, the returned string will be input string.
      • parseCSV

        public static String[] parseCSV​(String strRecord,
                                        boolean removeEmptyStrings)
        Method to parse the passed String record to extract data values defined by fields delimited by comma (0x2C), or tab (0x09). The fields are maintained as Strings
        Parameters:
        strRecord - A String containing a record to be parsed.
        removeEmptyStrings - True removes empty strings
        Returns:
        An Object[] containing each of the data fields parsed from the record. If the input string is null or empty, an empty Object[] is returned (e.g., new Object[0]).
      • parseFields

        public static Object[] parseFields​(String strRecord)
        Method to parse the passed String record to extract data values defined by fields delimited by space (0x20), comma (0x2C), pipe (0x7C) or tab (0x09). The fields are examined to determine if they contain data able to be transformed into int, double, or Strings, in that order. A list is represented by content enclosed in open/closed braces ('{' and '}') and is preserved as such in a String. Lists may include embedded quotes and delimiters.
        Parameters:
        strRecord - A String containing a record to be parsed.
        Returns:
        An Object[] containing each of the data fields parsed from the record. If the input string is null or empty, an empty Object[] is returned (e.g., new Object[0]).
      • processField

        public static Object processField​(String strField)
        Retrieve the object from the passed String by interpreting the content of the string to guess if it contains an Integer, Double, or String. The guess is based on first checking for a decimal polong ('.') and if it is present, attempting to create a Double, otherwise, attempting to create an Integer. If the attempted creating fails, the content is retained as a String.
        Parameters:
        strField - the String containing the potential Integer or Double value.
        Returns:
        an Integer, Double or String object. If the input string is null or empty, null is returned.
      • prompt

        public static String prompt​(String strPrompt)
        Print the supplied prompt (if not null) and return the trimmed response
        Parameters:
        strPrompt -
        Returns:
        the trimmed response to the prompt (may be the empty String ("") if nothing entered)
      • prompt

        public static String prompt​(String strPrompt,
                                    boolean bTrim)
        Print the supplied prompt (if not null) and return the trimmed response according to the supplied trim control
        Parameters:
        strPrompt -
        bTrim -
        Returns:
        the trimmed response (if so commanded) to the prompt (may be the empty String ("") if nothing entered)
      • readLine

        public static String readLine​(BufferedReader br)
                               throws IOException
        Reads a buffered reader line up to a newline and returns the content read as a String that does not contain the linefeed.
        Parameters:
        br - buffered reader
        Returns:
        String containing the characters read through the terminator character. If the end of file has been reached with nothing available to be returned, then null is returned.
        Throws:
        IOException - if an error occurs while reading the buffered reader.
        See Also:
        readLine(BufferedReader, HashSet)
      • readLine

        public static String readLine​(BufferedReader br,
                                      HashSet<Integer> terminators)
                               throws IOException
        Reads a buffered reader line up to any of the terminator characters (e.g., 0x0a for newline) and returns the content read as a String that does not contain the terminator.
        Parameters:
        br - buffered reader
        terminators - the set of line terminators used to signal return of the next "line" from the buffered reader.
        Returns:
        String containing the characters read through the terminator character. If the end of file has been reached with nothing available to be returned, then null is returned.
        Throws:
        IOException - if an error occurs while reading the buffered reader.
      • removeTag

        public static String removeTag​(String text,
                                       String tagPrefix)
        Removes the tag identified with tagPrefix through its closing >
        Parameters:
        text - string to be cleansed
        tagPrefix - HTML tag prefix used to search for the tag
        Returns:
        the cleansed text
      • saveJSONFile

        public static com.api.json.JSONObject saveJSONFile​(String jsonFileName,
                                                           com.api.json.JSONObject jsonData)
                                                    throws Exception
        Save the specified JSONObject in serialized form to the specified file or throw the appropriate exception.
        Parameters:
        jsonFileName - fully qualified name of the JSON file to be saved
        jsonData - the JSONObject to be saved to a file.
        Returns:
        the jsonData that was saved
        Throws:
        Exception - IOException) if there is a problem writing the file
      • saveTextFile

        public static void saveTextFile​(String textFileName,
                                        String content)
                                 throws Exception
        Save the specified JSONObject in serialized form to the specified file or throw the appropriate exception.
        Parameters:
        textFileName - fully qualified name of the JSON file to be saved
        content - the content to be saved to a file.
        Throws:
        Exception - IOException) if there is a problem writing the file
      • shortenString

        public static String shortenString​(String input,
                                           int maxLen)
        Shortens a long string to show the first maxLen characters and appends "..."
        Parameters:
        input -
        maxLen -
        Returns:
        shortened version of the supplied input truncated at supplied maxLen with ellipses appended
      • unescapeMarkdown

        public static String unescapeMarkdown​(String mdLine)
        Clean up markdown escape sequences for \. and \-
        Parameters:
        mdLine - markdown line to be cleansed
        Returns:
        cleansed markdown line