Package com.mdfromhtml.core
Class MDfromHTMLUtils
- java.lang.Object
-
- com.mdfromhtml.core.MDfromHTMLUtils
-
- All Implemented Interfaces:
Serializable
public class MDfromHTMLUtils extends Object implements Serializable
Utility methods commonly used throughout the MDfromHTML Project- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intiDayMillisecondsstatic intiHourMillisecondsstatic intiMinuteMillisecondsstatic CharsetUTF8_CHARSET
-
Constructor Summary
Constructors Constructor Description MDfromHTMLUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringarrayListToListString(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.static char[]byteToHexChars(byte bIn)Converts a byte into an array of char's containing the hexadecimal digits.static booleancheckWordFilter(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 httpstatic StringcleanseHTMLTagsFromText(String text)Removes HTML Tags from the supplied text, replacing them with a space (0x20)static StringcleanURL(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.static String[]cleanWord(String word)Strip off non-word characters from the beginning and end of the supplied word.static voidcloseTextFile(BufferedReader br)Close a buffered reader opened usingopenTextFile(String)static voidcloseTextFile(BufferedWriter bw)Close a buffered writer flushing its content first.static StringconvertMillisecondsToTimeZone(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.static intconvertTimeZoneToMilliseconds(String strTimeZone)Converts a timezone of the format +/-hhmm to millisecondsstatic StringexceptionTraceToString(Throwable throwableException)Captures an exception's stack trace as a string for inclusion in JSON objectsstatic StringfromUTF8Bytes(byte[] bytes)Transform the UTF-8 encoded byte array into a Stringstatic char[]getKey()static StringgetMDfromHTMLHomeDirectory()static PropertiesgetMDfromHTMLIPCProps()static PropertiesgetMDfromHTMLServicesProps()static StringgetMDfromHTMLWebServicesURI()static StringgetNameFromClass(Class<?> inClass)Transform a fully qualified Class' name into just the name of the class without the leading package.static StringgetUniqueID()static byte[]hexDecode(String strHex)Transform the string of hexadecimal digits into a byte array.static StringhexEncode(byte[] bArray)Convert the byte array into a String of hexadecimal digits.static booleanisEmpty(String strInput)Determine if the String is empty (equals "").static booleanisUndefined(byte testValue)Determine if the passed byte matches theMDfromHTMLConstants.UNDEFINED_byte.static booleanisUndefined(char testValue)Determine if the passed char matches theMDfromHTMLConstants.UNDEFINED_char.static booleanisUndefined(double testValue)Determine if the passed double matches theMDfromHTMLConstants.UNDEFINED_double.static booleanisUndefined(float testValue)Determine if the passed float matches theMDfromHTMLConstants.UNDEFINED_float.static booleanisUndefined(int iTestValue)Determine if the passed int matches theMDfromHTMLConstants.UNDEFINED_int.static booleanisUndefined(long testValue)Determine if the passed long matches theMDfromHTMLConstants.UNDEFINED_long.static booleanisUndefined(short testValue)Determine if the passed short matches theMDfromHTMLConstants.UNDEFINED_short.static booleanisUndefined(Boolean testValue)Determine if the passed Boolean matches theMDfromHTMLConstants.UNDEFINED_Boolean.static booleanisUndefined(Byte testValue)Determine if the passed Byte matches theMDfromHTMLConstants.UNDEFINED_Byte.static booleanisUndefined(Character testValue)Determine if the passed Character matches theMDfromHTMLConstants.UNDEFINED_Character.static booleanisUndefined(Class<?> testValue)Determine if the pass Class matches theMDfromHTMLConstants.UNDEFINED_Class.static booleanisUndefined(Double testValue)Determine if the passed Double matches theMDfromHTMLConstants.UNDEFINED_Double.static booleanisUndefined(Float testValue)Determine if the passed Float matches theMDfromHTMLConstants.UNDEFINED_Float.static booleanisUndefined(Integer testValue)Determine if the passed Integer matches theMDfromHTMLConstants.UNDEFINED_Integer.static booleanisUndefined(Long testValue)Determine if the passed Long matches theMDfromHTMLConstants.UNDEFINED_Long.static booleanisUndefined(Object testValue)Determine if the passed Object matches any of theMDfromHTMLConstantsfor UNDEFINED_* values.static booleanisUndefined(Short testValue)Determine if the passed Short matches theMDfromHTMLConstants.UNDEFINED_Short.static booleanisUndefined(String testValue)Determine if the passed String is null, or when trimmed, matches theMDfromHTMLConstants.UNDEFINED_Stringor is empty or is equal to "null" (to support ABLE rules)static booleanisUndefined(BigInteger testValue)Determine if the passed BigInteger matches theMDfromHTMLConstants.UNDEFINED_BigInteger.static booleanisUndefined(URI testValue)Determine if the passed URI is null, or equals theMDfromHTMLConstants.UNDEFINED_URIstatic booleanisValidURL(String url)Tests whether the supplied url is validstatic List<Path>listSourceFiles(Path dir, String ext)Construct and return a sorted list of files in a directory identified by the dir that have extensions matching the extstatic 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).static com.api.json.JSONArrayloadJSONArray(String jsonFQFileName)Load the JSONArray from the specified JSON file from the fully qualified file name or throw the appropriate exception.static com.api.json.JSONArtifactloadJSONArtifact(String jsonFQFileName)Load the specified JSON file from the fully qualified file name or throw the appropriate exception.static com.api.json.JSONObjectloadJSONFile(String jsonFQFileName)Load the specified JSON file from the fully qualified file name or throw the appropriate exception.static PropertiesloadMDfromHTMLProperties(String strPropFileName)Load the specified properties file and return the properties object, or null if an error occurs.static List<String>loadTextFile(String fqFilename)Reads the lines of a text file into a list of strings and returns that list.static BufferedReaderopenTextFile(String fqFilename)static BufferedWriteropenTextWriterFile(String fqFilename)static StringpadLeft(int iIn, int iWidth, char cPad)Helper method to create strings of the form "000nn".static StringpadLeft(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.static StringpadLeftZero(int iValue, int iMax)Creates a new String padded on its left side with zeros ('0') that is guaranteed to be the supplied length.static StringpadLeftZero(String strInput, int iMax)Creates a new String padded on its left side with zeros ('0') that is guaranteed to be the supplied length.static StringpadRight(int iIn, int iWidth, char cPad)Helper method to create strings of the form "nn000".static StringpadRight(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.static StringpadRightZero(int iValue, int iMax)Creates a new String padded on its right side with zeros ('0') that is guaranteed to be the supplied length.static StringpadRightZero(String strInput, int iMax)Creates a new String padded on its right side with zeros ('0') that is guaranteed to be the supplied length.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).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).static ObjectprocessField(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.static Stringprompt(String strPrompt)Print the supplied prompt (if not null) and return the trimmed responsestatic Stringprompt(String strPrompt, boolean bTrim)Print the supplied prompt (if not null) and return the trimmed response according to the supplied trim controlstatic StringreadLine(BufferedReader br)Reads a buffered reader line up to a newline and returns the content read as a String that does not contain the linefeed.static StringreadLine(BufferedReader br, HashSet<Integer> terminators)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.static StringremoveTag(String text, String tagPrefix)Removes the tag identified with tagPrefix through its closing >static com.api.json.JSONObjectsaveJSONFile(String jsonFileName, com.api.json.JSONObject jsonData)Save the specified JSONObject in serialized form to the specified file or throw the appropriate exception.static voidsaveTextFile(String textFileName, String content)Save the specified JSONObject in serialized form to the specified file or throw the appropriate exception.static StringshortenString(String input, int maxLen)Shortens a long string to show the first maxLen characters and appends "..."static byte[]toUTF8Bytes(String string)Transform the String to a UTF-8 encoded byte arraystatic StringtrimSpaces(String word)static DoubleundefinedForNull(Double DValue)Converts the input Double toMDfromHTMLConstants.UNDEFINED_Doubleiff the input Double is null.static IntegerundefinedForNull(Integer intInput)Converts the input Integer toMDfromHTMLConstants.UNDEFINED_Integeriff the input Integer is null.static StringundefinedForNull(String strValue)Converts the input String toMDfromHTMLConstants.UNDEFINED_Stringiff the input String is null or empty after being trimmed.static DateundefinedForNull(Date date)Converts the input Date toMDfromHTMLConstants.UNDEFINED_Dateiff the input Date is null.static StringunescapeMarkdown(String mdLine)Clean up markdown escape sequences for \.
-
-
-
Field Detail
-
iDayMilliseconds
public static final int iDayMilliseconds
- See Also:
- Constant Field Values
-
iHourMilliseconds
public static final int iHourMilliseconds
- See Also:
- Constant Field Values
-
iMinuteMilliseconds
public static final int iMinuteMilliseconds
- See Also:
- Constant Field Values
-
UTF8_CHARSET
public static final Charset UTF8_CHARSET
-
-
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
-
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(BufferedReader br)
Close a buffered reader opened usingopenTextFile(String)- Parameters:
br-
-
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
-
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
-
getKey
public static char[] getKey()
- Returns:
- the key needed for encrypting content
-
getMDfromHTMLHomeDirectory
public static String getMDfromHTMLHomeDirectory() throws Exception
- Returns:
- the fully qualified path to the
MDfromHTMLConstants.ENV_MDfromHTML_HOMEdirectory as defined by the Environment variable first, or System property second as defined byMDfromHTMLConstants.ENV_MDfromHTML_HOME. If that is not defined, then the user.home directory will be used. There is no trailingFile.separatorappended to the path. - Throws:
Exception
-
getMDfromHTMLIPCProps
public static Properties getMDfromHTMLIPCProps() throws Exception
- Returns:
- the properties file used to define MDfromHTML interprocess communications
- Throws:
Exception- See Also:
loadMDfromHTMLProperties(String),MDfromHTMLConstants.MDfromHTML_IPC_PropertiesFileName
-
getMDfromHTMLServicesProps
public static Properties getMDfromHTMLServicesProps() throws Exception
- Returns:
- the properties file used to identify MDfromHTML general control parameters
- Throws:
Exception
-
getMDfromHTMLWebServicesURI
public static String getMDfromHTMLWebServicesURI()
- Returns:
- URI for MDfromHTMLwebServices
-
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.
-
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(BigInteger testValue)
Determine if the passed BigInteger matches theMDfromHTMLConstants.UNDEFINED_BigInteger.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_BigInteger.- Returns:
- true if the values are equal, false if they are not. Note, if passed value is null, true is returned.
-
isUndefined
public static boolean isUndefined(Boolean testValue)
Determine if the passed Boolean matches theMDfromHTMLConstants.UNDEFINED_Boolean.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_Boolean.- Returns:
- true if the values are equal, false if they are not.
-
isUndefined
public static boolean isUndefined(byte testValue)
Determine if the passed byte matches theMDfromHTMLConstants.UNDEFINED_byte.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_byte.- Returns:
- true if the values are equal, false if they are not.
-
isUndefined
public static boolean isUndefined(Byte testValue)
Determine if the passed Byte matches theMDfromHTMLConstants.UNDEFINED_Byte.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_Byte.- Returns:
- true if the values are equal, false if they are not. Note, if passed value is null, true is returned.
-
isUndefined
public static boolean isUndefined(char testValue)
Determine if the passed char matches theMDfromHTMLConstants.UNDEFINED_char.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_char.- Returns:
- true if the values are equal, false if they are not.
-
isUndefined
public static boolean isUndefined(Character testValue)
Determine if the passed Character matches theMDfromHTMLConstants.UNDEFINED_Character.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_Character.- Returns:
- true if the values are equal, false if they are not. Note, if passed value is null, true is returned.
-
isUndefined
public static boolean isUndefined(Class<?> testValue)
Determine if the pass Class matches theMDfromHTMLConstants.UNDEFINED_Class.- Parameters:
testValue-- Returns:
- whether (true) or not the supplied class is undefined
-
isUndefined
public static boolean isUndefined(double testValue)
Determine if the passed double matches theMDfromHTMLConstants.UNDEFINED_double.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_double.- Returns:
- true if the values are equal, false if they are not.
-
isUndefined
public static boolean isUndefined(Double testValue)
Determine if the passed Double matches theMDfromHTMLConstants.UNDEFINED_Double.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_Double.- Returns:
- true if the values are equal, false if they are not. Note, if passed value is null, true is returned.
-
isUndefined
public static boolean isUndefined(float testValue)
Determine if the passed float matches theMDfromHTMLConstants.UNDEFINED_float.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_float.- Returns:
- true if the values are equal, false if they are not.
-
isUndefined
public static boolean isUndefined(Float testValue)
Determine if the passed Float matches theMDfromHTMLConstants.UNDEFINED_Float.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_Float.- Returns:
- true if the values are equal, false if they are not. Note, if passed value is null, true is returned.
-
isUndefined
public static boolean isUndefined(int iTestValue)
Determine if the passed int matches theMDfromHTMLConstants.UNDEFINED_int.- Parameters:
iTestValue- value to compare againstMDfromHTMLConstants.UNDEFINED_int.- Returns:
- true if the values are equal, false if they are not.
-
isUndefined
public static boolean isUndefined(Integer testValue)
Determine if the passed Integer matches theMDfromHTMLConstants.UNDEFINED_Integer.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_Integer.- Returns:
- true if the values are equal, false if they are not. Note, if passed value is null, true is returned.
-
isUndefined
public static boolean isUndefined(long testValue)
Determine if the passed long matches theMDfromHTMLConstants.UNDEFINED_long.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_long.- Returns:
- true if the values are equal, false if they are not.
-
isUndefined
public static boolean isUndefined(Long testValue)
Determine if the passed Long matches theMDfromHTMLConstants.UNDEFINED_Long.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_Long.- Returns:
- true if the values are equal, false if they are not. Note, if passed value is null, true is returned.
-
isUndefined
public static boolean isUndefined(Object testValue)
Determine if the passed Object matches any of theMDfromHTMLConstantsfor UNDEFINED_* values.- Parameters:
testValue- value to compare against the appropriateMDfromHTMLConstantsfor 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(short testValue)
Determine if the passed short matches theMDfromHTMLConstants.UNDEFINED_short.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_short.- Returns:
- true if the values are equal, false if they are not.
-
isUndefined
public static boolean isUndefined(Short testValue)
Determine if the passed Short matches theMDfromHTMLConstants.UNDEFINED_Short.- Parameters:
testValue- value to compare againstMDfromHTMLConstants.UNDEFINED_Short.- Returns:
- true if the values are equal, 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 theMDfromHTMLConstants.UNDEFINED_Stringor is empty or is equal to "null" (to support ABLE rules)- Parameters:
testValue- value to compare againstMDfromHTMLConstants.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 theMDfromHTMLConstants.UNDEFINED_URI- Parameters:
testValue- value to compare againstMDfromHTMLConstants.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 listext- 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<?>)
-
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
-
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
-
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.
-
openTextFile
public static BufferedReader openTextFile(String fqFilename) throws Exception
- Parameters:
fqFilename- fully qualified name of the text file to be opened- Returns:
- open buffered reader to allow individual lines of a text file to be read
- Throws:
Exception- See Also:
to close the reader returned by this function
-
openTextWriterFile
public static BufferedWriter openTextWriterFile(String fqFilename) throws Exception
- Parameters:
fqFilename- fully qualified name of the text file to be opened for writing. If the file exists, it will be deleted and recreated.- Returns:
- open buffered writer to allow individual lines of a text file to be written
- Throws:
Exception- See Also:
to close the writer returned by this function
-
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 readerterminators- 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 cleansedtagPrefix- 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 savedjsonData- 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 savedcontent- 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
-
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
-
undefinedForNull
public static Date undefinedForNull(Date date)
Converts the input Date toMDfromHTMLConstants.UNDEFINED_Dateiff the input Date is null.- Parameters:
date- Date to be tested against null and converted.- Returns:
MDfromHTMLConstants.UNDEFINED_Dateif the input Date was null, otherwise, the input Date is echoed back.
-
undefinedForNull
public static Double undefinedForNull(Double DValue)
Converts the input Double toMDfromHTMLConstants.UNDEFINED_Doubleiff the input Double is null.- Parameters:
DValue- Double to be tested against null and converted.- Returns:
MDfromHTMLConstants.UNDEFINED_Doubleif the input Double was null, otherwise, the input Double is echoed back.
-
undefinedForNull
public static Integer undefinedForNull(Integer intInput)
Converts the input Integer toMDfromHTMLConstants.UNDEFINED_Integeriff the input Integer is null.- Parameters:
intInput- Integer to be tested against null and converted.- Returns:
MDfromHTMLConstants.UNDEFINED_Integerif the input Integer was null, otherwise, the input Integer is echoed back.
-
undefinedForNull
public static String undefinedForNull(String strValue)
Converts the input String toMDfromHTMLConstants.UNDEFINED_Stringiff the input String is null or empty after being trimmed.- Parameters:
strValue- String to be tested against null or an empty string after being trimmed, and converted to theMDfromHTMLConstants.UNDEFINED_String.- Returns:
MDfromHTMLConstants.UNDEFINED_Stringif the input String was null, otherwise, the input String is echoed back.
-
-