Package com.api.jsonata4java
Class JSONataUtils
- java.lang.Object
-
- com.api.jsonata4java.JSONataUtils
-
- All Implemented Interfaces:
Serializable
public class JSONataUtils extends Object implements Serializable
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intDAY_MSstatic DecimalFormatFMTstatic byte[]HEX_BYTESstatic StringHEX_CHARSstatic intHOUR_MSstatic intMIN_MSstatic RandomSEED_RANDOMIf this class is initialized in a different JVM is started at the same millisecond, its s_r will generate the same sequence of random numbers.static SecureRandomSEED_SECURE_RANDOM
-
Constructor Summary
Constructors Constructor Description JSONataUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static char[]byteToHexChars(byte bIn)Converts a byte into an array of char's containing the hexadecimal digits.static voidcloseTextFile(BufferedReader br)Close a buffered reader opened usingopenTextFile(String)static SequencecreateSequence(com.fasterxml.jackson.databind.JsonNode... arguments)static StringdecodeURIComponent(String s)Decodes the passed UTF-8 String using an algorithm that's compatible with JavaScript'sdecodeURIComponentfunction.static StringencodeURIComponent(String s)Encodes the passed String as UTF-8 using an algorithm that's compatible with JavaScript'sencodeURIComponentfunction.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 booleanisSequence(Object value)static 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 com.fasterxml.jackson.databind.JsonNodeloadJSONFile(String jsonFQFileName)Load the specified JSON file from the fully qualified file name or throw the appropriate exception.static voidmain(String[] args)static BufferedReaderopenTextFile(String fqFilename)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 com.fasterxml.jackson.databind.JsonNodesaveJSONFile(String jsonFileName, com.fasterxml.jackson.databind.JsonNode jsonData)Save the specified JSONObject in serialized form to the specified file or throw the appropriate exception.static Stringsubstr(String str, Integer start, Integer length)
-
-
-
Field Detail
-
FMT
public static final DecimalFormat FMT
-
HEX_BYTES
public static final byte[] HEX_BYTES
-
DAY_MS
public static final int DAY_MS
- See Also:
- Constant Field Values
-
HOUR_MS
public static final int HOUR_MS
- See Also:
- Constant Field Values
-
MIN_MS
public static final int MIN_MS
- See Also:
- Constant Field Values
-
HEX_CHARS
public static final String HEX_CHARS
- See Also:
- Constant Field Values
-
SEED_RANDOM
public static Random SEED_RANDOM
If this class is initialized in a different JVM is started at the same millisecond, its s_r will generate the same sequence of random numbers.
-
SEED_SECURE_RANDOM
public static SecureRandom SEED_SECURE_RANDOM
-
-
Method Detail
-
closeTextFile
public static void closeTextFile(BufferedReader br)
Close a buffered reader opened usingopenTextFile(String)- Parameters:
br- buffered reader to be closed
-
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.
-
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.
-
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- if the file can not be found- See Also:
to close the reader returned by this function
-
prompt
public static String prompt(String strPrompt)
Print the supplied prompt (if not null) and return the trimmed response- Parameters:
strPrompt- the prompt to be displayed- 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- the prompt to be displayedbTrim- whether or not to trim the input- Returns:
- the trimmed response (if so commanded) to the prompt (may be the empty String ("") if nothing entered)
-
saveJSONFile
public static com.fasterxml.jackson.databind.JsonNode saveJSONFile(String jsonFileName, com.fasterxml.jackson.databind.JsonNode 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
-
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
-
loadJSONFile
public static com.fasterxml.jackson.databind.JsonNode 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
-
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.
-
createSequence
public static Sequence createSequence(com.fasterxml.jackson.databind.JsonNode... arguments)
-
isSequence
public static boolean isSequence(Object value)
-
decodeURIComponent
public static String decodeURIComponent(String s) throws UnsupportedEncodingException
Decodes the passed UTF-8 String using an algorithm that's compatible with JavaScript'sdecodeURIComponentfunction. Returnsnullif the String isnull.- Parameters:
s- The UTF-8 encoded String to be decoded- Returns:
- the decoded String
- Throws:
UnsupportedEncodingException
-
encodeURIComponent
public static String encodeURIComponent(String s) throws URISyntaxException
Encodes the passed String as UTF-8 using an algorithm that's compatible with JavaScript'sencodeURIComponentfunction. Returnsnullif the String isnull.- Parameters:
s- The String to be encoded- Returns:
- the encoded String
- Throws:
URISyntaxException
-
substr
public static String substr(String str, Integer start, Integer length)
- Parameters:
str-start- Location at which to begin extracting characters. If a negative number is given, it is treated as strLength - start where strLength is the length of the string. For example, str.substr(-3) is treated as str.substr(str.length - 3)length- The number of characters to extract. If this argument is null, all the characters from start to the end of the string are extracted.- Returns:
- A new string containing the extracted section of the given string. If length is 0 or a negative number, an empty string is returned.
-
-