Class JRT
- Direct Known Subclasses:
SandboxedJRT
Variable access is achieved through the VariableManager interface. The constructor requires a VariableManager instance (which, in this case, is the compiled Jawk class itself).
Main services include:
- File and command output redirection via print(f).
- File and command input redirection via getline.
- Most built-in AWK functions, such as system(), sprintf(), etc.
- Automatic AWK type conversion routines.
- IO management for input rule processing.
- Random number engine management.
- Input field ($0, $1, ...) management.
All static and non-static service methods should be package-private
to the resultant AWK script class rather than public. However,
the resultant script class is not in the org.metricshub.jawk.jrt package
by default, and the user may reassign the resultant script class
to another package. Therefore, all accessed methods are public.
- Author:
- Danny Daglas
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyRS.static voidCalled by AVM/compiled modules to assign local environment variables to an associative array (in this case, to ENVIRON).final voidassignInitialVariables(Map<String, Object> initialVarMap) Assign all -v variables.static booleanCompares two objects.booleanconsumeInput(InputStream input, boolean forGetline, Locale locale) Attempt to consume one line of input.static ObjectReturn an object which is numerically equivalent to one minus a given object.Getter for the fieldinputLine.Getter for the fieldoutputFiles.Getter for the fieldpartitioningReader.booleanstatic ObjectReturn an object which is numerically equivalent to one plus a given object.static booleanisActuallyLong(double d) Determines whether a double value actually represents a long integer within the limits of floating point precision.Attempt to close an open stream, whether it is an input file, output file, input process, or output process.voidjrtCloseAll.booleanjrtConsumeCommandInput.jrtConsumeCommandInputForGetline(String cmdString) Retrieve the next line of output from a command, executing the command if necessary and store it to $0.booleanjrtConsumeFileInput(String filename) jrtConsumeFileInput.jrtConsumeFileInputForGetline(String filename) jrtConsumeFileInputForGetline.jrtGetInputField(int fieldnum) jrtGetInputField.jrtGetInputField(Object fieldnumObj) Retrieve the contents of a particular input field.Retrieve $0.jrtGetPrintStream(String filename, boolean append) Retrieve the PrintStream which writes to a particular file, creating the PrintStream if necessary.voidSplits $0 into $1, $2, etc.jrtSetInputField(Object valueObj, int fieldNum) Stores value_obj into an input field.voidAdjust the current input field list and $0 when NF is updated by the AWK script.jrtSpawnForOutput(String cmd) Retrieve the PrintStream which shuttles data to stdin for a process, executing the process if necessary.Executes the command specified by cmd and waits for termination, returning an Integer object containing the return code.static BSDRandomnewRandom(int seed) newRandom.static StringprepareReplacement(String awkRepl) Transform the sub/gsub replacement string from Awk syntax (with '&') to Java (with '$') so it can be used in Matcher.appendReplacement()static voidprintfNoCatch(PrintStream ps, Locale locale, String fmtArg, Object... arr) printfFunctionNoCatch.static voidprintfNoCatch(Locale locale, String fmtArg, Object... arr) printfFunctionNoCatch.static IntegerreplaceAll(String origValue, String repl, String ere, StringBuffer sb) Replace all occurrences of the regular expression with specified stringstatic IntegerreplaceFirst(String origValue, String repl, String ere, StringBuffer sb) replaceFirst.voidsetInputLine(String inputLine) Setter for the fieldinputLine.voidsetInputLineforEval(InputStream input) Read input from stdin, only once, and just for simple AWK expression evaluationvoidsetStreams(PrintStream ps, PrintStream err) Sets the streams for spawned command output and error.static intSplits the string into parts separated the regular expression fs.static intSplits the string into parts separated by one or more spaces; blank first and last fields are eliminated.static StringsprintfNoCatch(Locale locale, String fmtArg, Object... arr) sprintfFunctionNoCatch.static Stringsubstr.static Stringsubstr.static inttimeSeed()timeSeed.static StringtoAwkString(Object o, String convfmt, Locale locale) Convert Strings, Integers, and Doubles to Strings based on the CONVFMT variable contents.static StringtoAwkStringForOutput(Object o, String ofmt, Locale locale) Convert a String, Integer, or Double to String based on the OFMT variable contents.final booleanConverts an Integer, Double, String, Pattern, or ConditionPair to a boolean.static doubleConvert a String, Integer, or Double to Double.static longConvert a String, Long, or Double to Long.
-
Constructor Details
-
JRT
Create a JRT with a VariableManager- Parameters:
vm- The VariableManager to use with this JRT.
-
-
Method Details
-
setStreams
Sets the streams for spawned command output and error.- Parameters:
ps- PrintStream to send command output toerr- PrintStream to send command error output to
-
assignInitialVariables
Assign all -v variables.- Parameters:
initialVarMap- A map containing all initial variable names and their values.
-
assignEnvironmentVariables
Called by AVM/compiled modules to assign local environment variables to an associative array (in this case, to ENVIRON).- Parameters:
aa- The associative array to populate with environment variables. The module asserts that the associative array is empty prior to population.
-
toAwkString
Convert Strings, Integers, and Doubles to Strings based on the CONVFMT variable contents.- Parameters:
o- Object to convert.convfmt- The contents of the CONVFMT variable.locale- aLocaleobject- Returns:
- A String representation of o.
-
toAwkStringForOutput
Convert a String, Integer, or Double to String based on the OFMT variable contents. Jawk will subsequently use this String for output via print().- Parameters:
o- Object to convert.ofmt- The contents of the OFMT variable.locale- aLocaleobject- Returns:
- A String representation of o.
-
toDouble
Convert a String, Integer, or Double to Double.- Parameters:
o- Object to convert.- Returns:
- the "double" value of o, or 0 if invalid
-
isActuallyLong
public static boolean isActuallyLong(double d) Determines whether a double value actually represents a long integer within the limits of floating point precision.- Parameters:
d- the double value to examine- Returns:
trueifdis effectively an integer
-
toLong
Convert a String, Long, or Double to Long.- Parameters:
o- Object to convert.- Returns:
- the "long" value of o, or 0 if invalid
-
compare2
Compares two objects. Whether to employ less-than, equals, or greater-than checks depends on the mode chosen by the callee. It handles Awk variable rules and type conversion semantics.- Parameters:
o1- The 1st object.o2- the 2nd object.mode-- < 0 - Return true if o1 < o2.
- 0 - Return true if o1 == o2.
- > 0 - Return true if o1 > o2.
- Returns:
- a boolean
-
inc
Return an object which is numerically equivalent to one plus a given object. For Integers and Doubles, this is similar to o+1. For Strings, attempts are made to convert it to a double first. If the String does not represent a valid Double, 1 is returned.- Parameters:
o- The object to increase.- Returns:
- o+1 if o is an Integer or Double object, or if o is a String object and represents a double. Otherwise, 1 is returned. If the return value is an integer, an Integer object is returned. Otherwise, a Double object is returned.
-
dec
Return an object which is numerically equivalent to one minus a given object. For Integers and Doubles, this is similar to o-1. For Strings, attempts are made to convert it to a double first. If the String does not represent a valid Double, -1 is returned.- Parameters:
o- The object to increase.- Returns:
- o-1 if o is an Integer or Double object, or if o is a String object and represents a double. Otherwise, -1 is returned. If the return value is an integer, an Integer object is returned. Otherwise, a Double object is returned.
-
toBoolean
Converts an Integer, Double, String, Pattern, or ConditionPair to a boolean.- Parameters:
o- The object to convert to a boolean.- Returns:
- For the following class types for o:
- Integer - o.intValue() != 0
- Long - o.longValue() != 0
- Double - o.doubleValue() != 0
- String - o.length() > 0
- UninitializedObject - false
- Pattern - $0 ~ o
-
split
Splits the string into parts separated by one or more spaces; blank first and last fields are eliminated. This conforms to the 2-argument version of AWK's split function.- Parameters:
array- The array to populate.string- The string to split.convfmt- Contents of the CONVFMT variable.locale- aLocaleobject- Returns:
- The number of parts resulting from this split operation.
-
split
Splits the string into parts separated the regular expression fs. This conforms to the 3-argument version of AWK's split function.If fs is blank, it behaves similar to the 2-arg version of AWK's split function.
- Parameters:
fs- Field separator regular expression.array- The array to populate.string- The string to split.convfmt- Contents of the CONVFMT variable.locale- aLocaleobject- Returns:
- The number of parts resulting from this split operation.
-
getPartitioningReader
Getter for the field
partitioningReader.- Returns:
- a
PartitioningReaderobject
-
getInputLine
Getter for the field
inputLine.- Returns:
- a
Stringobject
-
setInputLine
Setter for the field
inputLine.- Parameters:
inputLine- aStringobject
-
consumeInput
public boolean consumeInput(InputStream input, boolean forGetline, Locale locale) throws IOException Attempt to consume one line of input. Input may come from standard input or from files/variable assignments supplied on the command line viaARGV. Variable assignment arguments are evaluated lazily when encountered.- Parameters:
input- stream used when consuming from standard inputforGetline-trueif the call is forgetline; whenfalsethe fields of$0are parsed automaticallylocale- locale used for string conversion- Returns:
trueif a line was consumed,falseif no more input is available- Throws:
IOException- upon an IO error
-
setInputLineforEval
Read input from stdin, only once, and just for simple AWK expression evaluation- Parameters:
input- Stdin- Throws:
IOException- if couldn't read stdin (should never happen, as it's based on a String)
-
jrtParseFields
public void jrtParseFields()Splits $0 into $1, $2, etc. Called when an update to $0 has occurred. -
hasInputFields
public boolean hasInputFields()- Returns:
- true if at least one input field has been initialized.
-
jrtSetNF
Adjust the current input field list and $0 when NF is updated by the AWK script. Fields are either truncated or extended with empty values so thatNFtruly reflects the number of fields.- Parameters:
nfObj- New value for NF
-
jrtGetInputField
Retrieve the contents of a particular input field.- Parameters:
fieldnumObj- Object referring to the field number.- Returns:
- Contents of the field.
-
jrtGetInputField
jrtGetInputField.
- Parameters:
fieldnum- a int- Returns:
- a
Objectobject
-
jrtSetInputField
Stores value_obj into an input field.- Parameters:
valueObj- The RHS of the assignment.fieldNum- Object referring to the field number.- Returns:
- A string representation of valueObj.
-
jrtConsumeFileInputForGetline
jrtConsumeFileInputForGetline.
-
jrtConsumeCommandInputForGetline
Retrieve the next line of output from a command, executing the command if necessary and store it to $0.- Parameters:
cmdString- The command to execute.- Returns:
- Integer(1) if successful, Integer(0) if no more input is available, Integer(-1) upon an IO error.
-
jrtGetInputString
Retrieve $0.- Returns:
- The contents of the $0 input field.
-
getOutputFiles
Getter for the field
outputFiles.- Returns:
- a
Mapobject
-
jrtGetPrintStream
Retrieve the PrintStream which writes to a particular file, creating the PrintStream if necessary.- Parameters:
filename- The file which to write the contents of the PrintStream.append- true to append to the file, false to overwrite the file.- Returns:
- a
PrintStreamobject
-
jrtConsumeFileInput
jrtConsumeFileInput.
- Parameters:
filename- aStringobject- Returns:
- a boolean
- Throws:
IOException- if any.
-
jrtConsumeCommandInput
jrtConsumeCommandInput.
- Parameters:
cmd- aStringobject- Returns:
- a boolean
- Throws:
IOException- if any.
-
jrtSpawnForOutput
Retrieve the PrintStream which shuttles data to stdin for a process, executing the process if necessary. Threads are created to shuttle the data to/from the process.- Parameters:
cmd- The command to execute.- Returns:
- The PrintStream which to write to provide input data to the process.
-
jrtClose
Attempt to close an open stream, whether it is an input file, output file, input process, or output process.The specification did not describe AWK behavior when attempting to close streams/processes with the same file/command name. In this case, all open streams with this name are closed.
- Parameters:
filename- The filename/command process to close.- Returns:
- Integer(0) upon a successful close, Integer(-1) otherwise.
-
jrtCloseAll
public void jrtCloseAll()jrtCloseAll.
-
jrtSystem
Executes the command specified by cmd and waits for termination, returning an Integer object containing the return code. stdin to this process is closed while threads are created to shuttle stdout and stderr of the command to stdout/stderr of the calling process.- Parameters:
cmd- The command to execute.- Returns:
- Integer(return_code) of the created process. Integer(-1) is returned on an IO error.
-
sprintfNoCatch
public static String sprintfNoCatch(Locale locale, String fmtArg, Object... arr) throws IllegalFormatException sprintfFunctionNoCatch.
- Parameters:
locale- aLocaleobjectfmtArg- aStringobjectarr- an array ofObjectobjects- Returns:
- a
Stringobject - Throws:
IllegalFormatException- if any.
-
printfNoCatch
printfFunctionNoCatch.
-
printfNoCatch
printfFunctionNoCatch.
- Parameters:
ps- aPrintStreamobjectlocale- aLocaleobjectfmtArg- aStringobjectarr- an array ofObjectobjects
-
prepareReplacement
Transform the sub/gsub replacement string from Awk syntax (with '&') to Java (with '$') so it can be used in Matcher.appendReplacement()Awk and Java don't use the same syntax for regex replace:
- Awk uses & to refer to the matched string
- Java uses $0, $g, or ${name} to refer to the corresponding match groups
- Parameters:
awkRepl- the replace string passed in sub() and gsub()- Returns:
- a string that can be used in Java's Matcher.appendReplacement()
-
replaceFirst
replaceFirst.
- Parameters:
origValue- aStringobjectrepl- aStringobjectere- aStringobjectsb- aStringBufferobject- Returns:
- a
Integerobject
-
replaceAll
Replace all occurrences of the regular expression with specified string- Parameters:
origValue- String where replace is donerepl- Replacement string (with '&' for referring to matching string)ere- Regular expressionsb- StringBuffer we will work on- Returns:
- the number of replacements performed
-
substr
substr.
-
substr
substr.
-
timeSeed
public static int timeSeed()timeSeed.
- Returns:
- a int
-
newRandom
newRandom.
- Parameters:
seed- a int- Returns:
- a
Randomobject
-
applyRS
applyRS.
- Parameters:
rsObj- aObjectobject
-