public class OutTextPreparer
extends java.lang.Object
JZtxtcmd
only for text preparation of one or a few lines.
static OutTextPreparer otxMyText = new OutTextPreparer("otxMyText", UserClass.class, "arg1, arg2", "A simple text with newline: \n" + "<&arg1>: a variable value" + "<&arg1.operation(arg2)>: Invocation of any operation in arguments resulting in an text" + "<:call:otxSubText:arg1>: call of any sub text maybe with args" + "<:exec:operation:arg1> : call of any operation in the given reflection class maybe with args" + "");Instead a given pattern in Java code it is possible to read the pattern from a file.
<:args:var, var2>
written on start of the script defines variable for the script.
<:CHARS>: CHARS
may be special chars, they will be outputted like given, for example
<:<&var>>
produces the text <&var>
for generate a OutTextPreparer-Script itself.
<:<&><&var>>
produces the text <&CONTENT>
whereby CONTENT
is the content of var
.
<:if
etc. cannot be used for common texts, it is used as control statement:
<if:condition>conditional Text<elsif:condition>other Text<else>else-Text<.if>
<for:variable:container>text for any element <&variable> in loop <.for>
<: >
skips over whitespaces till next text, does not output the whitespaces,
able to use for example to write a simple sequence in more as one line for better readability of the script.
<: > <: >
A second <: >
after skipped whitespaces produces one space
because it is not recognized as 'skip again' but as special character, here a space.
## Comment till end of line
it is important for texts from a textual file.
<:args:var, var2><: > <: >anyText after one space as indention <tag attrib="<&var>" >any xml output</tag> <:<&var>>It is proper for XML output generation. The last constructs a text containing "<&var>", it is to produce a OutTextPreparer pattern with OutTextPreparer itself. A "<: >" or "<:+>" skips over whiteSpaces, especially line feed, but immediately following "<: >" inserts one space.
OutTextPreparer(String, Class, String, String)
:
<:exec:operation>
is searched
createArgumentDataObj()
. It is possible
to build this instance on construction in the ctor of the users organization class. Then this instance is reused
(save calculation time for allocation and garbage), possible in single thread usage. public MyWriterClass() { dataMyText = otxMyText.getArgumentData(this);If multithreading should be used (speed up generation time) to built output texts with the same instance of this, it is possible. This class is readonly and can be used without any restriction in multithreading, after it is contsructed. Any thread need its own instance of the
OutTextPreparer.DataTextPreparer
instance to work independent.
try { wr = new BufferedWriter(new FileWriter(toFile)); dataMyText.setArgument("arg1", arg1Value); dataMyText.setArgument("arg2", arg2Value); otxMyText.exec(wr, dataMyText); } catch(IOException exc) {....The argument value are set. It is possible to use
OutTextPreparer.DataTextPreparer.setArgument(int, Object)
too
if the order of arguments is known and definitely. It is faster because the position of the argument
should not be searched (uses binary search, fast too). The it is executed. The output text is written
in the given Appendable
, it may be a StringBuilder
too.
<:exec:operation:arg>
::arg
is optional. The operation
will be searched in the given reflection class (ctor argument).
The operation is stored as Method
for usage, so this operation is fast.
The optional argument is used from given arguments of call, it should be match to the operations argument type.
If the operation is not found, an exception with a detailed error message is thrown on ctor.
If the operations argument is not proper, an exception is thrown on exec(Appendable, DataTextPreparer)
.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
OutTextPreparer.Argument |
(package private) static class |
OutTextPreparer.CallCmd |
(package private) static class |
OutTextPreparer.Cmd |
(package private) static class |
OutTextPreparer.CmdString |
static class |
OutTextPreparer.DataTextPreparer
Instances of this class holds the data for one OutTextPreparer instance but maybe for all invocations.
|
(package private) static class |
OutTextPreparer.DebugCmd |
(package private) static class |
OutTextPreparer.ECmd |
(package private) static class |
OutTextPreparer.ExecCmd |
(package private) static class |
OutTextPreparer.ForCmd |
(package private) static class |
OutTextPreparer.IfCmd |
(package private) static class |
OutTextPreparer.SetCmd |
Modifier and Type | Field and Description |
---|---|
java.lang.Class<?> |
clazzPattern |
private java.util.List<OutTextPreparer.Cmd> |
cmds |
(package private) int |
ctCall |
private java.util.List<java.lang.String> |
listArgs
Via script given arguments for the outText.
|
private java.util.Map<java.lang.String,DataAccess.IntegerIx> |
nameVariables
All argument variables and internal variables sorted.
|
java.lang.String |
pattern
The source of the generation script, argument of
parse(Class, String) only for debug. |
java.lang.String |
sIdent
Name of the generation script used for debug and comparison with data.
|
static java.lang.String |
version
Version, history and license.
|
Constructor and Description |
---|
OutTextPreparer(java.lang.String ident,
java.lang.Class<?> reflData,
java.util.List<java.lang.String> variables,
java.lang.String pattern)
Instantiates for a given pattern.
|
OutTextPreparer(java.lang.String ident,
java.lang.Class<?> reflData,
java.lang.String pattern)
Instantiates for a given pattern.
|
OutTextPreparer(java.lang.String ident,
java.lang.Class<?> reflData,
java.lang.String variables,
java.lang.String pattern)
Constructs the text generation control data for the specific pattern.
|
Modifier and Type | Method and Description |
---|---|
private OutTextPreparer.Cmd |
addCmd(java.lang.String src,
int from,
int to,
OutTextPreparer.ECmd ecmd,
java.lang.String sDatapath,
java.lang.Class<?> data)
Explore the sDatapath and adds the proper Cmd
|
private OutTextPreparer.Cmd |
addCmdSp(java.lang.String src,
int from,
int to,
OutTextPreparer.ECmd ecmd,
StringPartScan sDatapath,
java.lang.Class<?> data)
Explore the sDatapath and adds the proper Cmd
|
private void |
addError(java.lang.String sError) |
OutTextPreparer.DataTextPreparer |
createArgumentDataObj()
Returns an proper instance for argument data for a
exec(Appendable, DataTextPreparer) run. |
(package private) void |
debug() |
void |
exec(java.lang.Appendable wr,
OutTextPreparer.DataTextPreparer args)
Executes preparation of a pattern with given data.
|
private void |
execCall(java.lang.Appendable wr,
OutTextPreparer.CallCmd cmd,
OutTextPreparer.DataTextPreparer args,
OutTextPreparer callVar)
Executes a call
|
private void |
execFor(java.lang.Appendable wr,
OutTextPreparer.ForCmd cmd,
int ixCmd,
java.lang.Object container,
OutTextPreparer.DataTextPreparer args)
Executes a for loop
|
private int |
execIf(java.lang.Appendable wr,
OutTextPreparer.IfCmd ifcmd,
int ixCmd,
java.lang.Object data,
OutTextPreparer.DataTextPreparer args)
Executes a if branch
|
private void |
execSub(java.lang.Appendable wr,
OutTextPreparer.DataTextPreparer args,
int ixStart,
int ixEndExcl)
Executes preparation for a range of cmd for internal control structures
|
OutTextPreparer.DataTextPreparer |
getArgumentData(java.lang.Object execInstance)
Creates the argument data and presets the given execInstance to
OutTextPreparer.DataTextPreparer.setExecObj(Object) |
private void |
parse(java.lang.Class<?> reflData,
java.lang.String pattern)
Parse the pattern.
|
private void |
parseArgs(StringPartScan sp) |
private void |
parseCall(java.lang.String src,
int pos0,
int pos1,
StringPartScan sp,
java.lang.Class<?> reflData) |
private void |
parseExec(java.lang.String src,
int pos0,
int pos1,
StringPartScan sp,
java.lang.Class<?> reflData) |
private void |
parseIf(java.lang.String pattern,
int pos0,
int pos1,
OutTextPreparer.ECmd ecmd,
StringPartScan sp,
java.lang.Class<?> reflData) |
private void |
parseVariables(java.lang.String variables) |
static java.util.Map<java.lang.String,java.lang.String> |
readTemplate(java.io.InputStream inp,
java.lang.String lineStart)
Reads a given template which may contain the pattern for several OutTextPreparer.
|
private void |
setVariables(java.util.List<java.lang.String> listArgs) |
java.lang.String |
toString() |
public static final java.lang.String version
readTemplate(InputStream, String)
to support texts from file. Used firstly for org.vishia.java2Vhdl.Java2Vhdl
CalculatorExpr.Operand#Operand(StringPartScan, Map, Class, boolean)
yet not complete.
private java.util.Map<java.lang.String,DataAccess.IntegerIx> nameVariables
public final java.lang.String pattern
parse(Class, String)
only for debug.public final java.lang.Class<?> clazzPattern
private java.util.List<java.lang.String> listArgs
int ctCall
private java.util.List<OutTextPreparer.Cmd> cmds
public final java.lang.String sIdent
public OutTextPreparer(java.lang.String ident, java.lang.Class<?> reflData, java.lang.String pattern)
OutTextPreparer(String, Class, String, String)
The variables are gotten from the pattern. For this case the order of variable depends on the order in the pattern.
It is essential for access to data via index or via name. ...TODO see wherepublic OutTextPreparer(java.lang.String ident, java.lang.Class<?> reflData, java.lang.String variables, java.lang.String pattern)
ident
- Any identification not used for the generated text.reflData
- If the access via reflection should be done, null possiblevariables
- One variable or list of identifier separated with comma, whiteSpaces possible.pattern
- The pattern in string given form.
This pattern will be parsed and divided in parts for a fast text generation.never
- because the instantiation is possible especially for static variables.
On faulty pattern the prepared cmd for output contains the error message.public OutTextPreparer(java.lang.String ident, java.lang.Class<?> reflData, java.util.List<java.lang.String> variables, java.lang.String pattern)
OutTextPreparer(String, Class, String, String)
variables
- Identifier given as list, parsing is not necessary.
Able to use if the variable idents are anyway given in a list.public static java.util.Map<java.lang.String,java.lang.String> readTemplate(java.io.InputStream inp, java.lang.String lineStart) throws java.io.IOException
=== patternName content <&withVariables> more lines === nextPatternName etc.
inp
- The open input to read, can also a resource in jar, then use Class.getResourceAsStream(String)
to openlineStart
- String which marks a new pattern segment, for the exmaple it should be "=== "java.io.IOException
private void parseVariables(java.lang.String variables)
private void setVariables(java.util.List<java.lang.String> listArgs)
private void parse(java.lang.Class<?> reflData, java.lang.String pattern)
ident
- data instance to get data in the construction phase via reflection.
It is used for identifier in the pattern which is not found in the variables (parameter 'variables' of ctor).
It can be null.java.text.ParseException
private void parseArgs(StringPartScan sp)
private void parseIf(java.lang.String pattern, int pos0, int pos1, OutTextPreparer.ECmd ecmd, StringPartScan sp, java.lang.Class<?> reflData)
private void parseExec(java.lang.String src, int pos0, int pos1, StringPartScan sp, java.lang.Class<?> reflData)
private void parseCall(java.lang.String src, int pos0, int pos1, StringPartScan sp, java.lang.Class<?> reflData)
private void addError(java.lang.String sError)
private OutTextPreparer.Cmd addCmd(java.lang.String src, int from, int to, OutTextPreparer.ECmd ecmd, java.lang.String sDatapath, java.lang.Class<?> data)
src
- The pattern to get text contentsfrom
- start position for text contentto
- end position for text content, if <= from then no text content is stored (both = 0)ecmd
- The cmd kindsDatapath
- null or a textual data path. It will be split to CalculatorExpr.Operand.ixValue
and CalculatorExpr.Operand.dataAccess
private OutTextPreparer.Cmd addCmdSp(java.lang.String src, int from, int to, OutTextPreparer.ECmd ecmd, StringPartScan sDatapath, java.lang.Class<?> data)
src
- The pattern to get text contentsfrom
- start position for text contentto
- end position for text content, if <= from then no text content is stored (both = 0)ecmd
- The cmd kindsDatapath
- null or a textual data path. It will be split to CalculatorExpr.Operand.ixValue
and CalculatorExpr.Operand.dataAccess
public OutTextPreparer.DataTextPreparer createArgumentDataObj()
exec(Appendable, DataTextPreparer)
run.
The arguments should be filled using OutTextPreparer.DataTextPreparer.setArgument(String, Object)
by name
or OutTextPreparer.DataTextPreparer.setArgument(int, Object)
by index if the order of arguments are known.
The argument instance can be reused in the same thread by filling arguments newly for subsequently usage.
If the exec(Appendable, DataTextPreparer)
is concurrently executed (multiple threads,
multicore processing), then any thread should have its own data.public OutTextPreparer.DataTextPreparer getArgumentData(java.lang.Object execInstance)
OutTextPreparer.DataTextPreparer.setExecObj(Object)
execInstance
- It should be an instance of the given reflection class on ctor.public void exec(java.lang.Appendable wr, OutTextPreparer.DataTextPreparer args) throws java.io.IOException
wr
- The output channelargs
- for preparation.
The value instance should be gotten with createArgumentDataObj()
proper to the instance of this, because the order of arguments should be match.
It is internally tested.java.lang.Exception
java.io.IOException
private void execSub(java.lang.Appendable wr, OutTextPreparer.DataTextPreparer args, int ixStart, int ixEndExcl) throws java.io.IOException
wr
- The output channelargs
- for preparation.ixStart
- from this cmd in cmds
java.io.IOException
private int execIf(java.lang.Appendable wr, OutTextPreparer.IfCmd ifcmd, int ixCmd, java.lang.Object data, OutTextPreparer.DataTextPreparer args) throws java.io.IOException
wr
- the output channelcmd
- The ForCmdixCmd
- the index of the cmd in cmds
container
- The container argumentargs
- actual args of the calling leveljava.lang.Exception
java.io.IOException
private void execFor(java.lang.Appendable wr, OutTextPreparer.ForCmd cmd, int ixCmd, java.lang.Object container, OutTextPreparer.DataTextPreparer args) throws java.io.IOException
wr
- the output channelcmd
- The ForCmdixCmd
- the index of the cmd in cmds
container
- The container argumentargs
- actual args of the calling leveljava.lang.Exception
java.io.IOException
private void execCall(java.lang.Appendable wr, OutTextPreparer.CallCmd cmd, OutTextPreparer.DataTextPreparer args, OutTextPreparer callVar) throws java.io.IOException
wr
- the output channelcmd
- The CallCmdargs
- actual args of the calling levelcallVar
- The OutTextPreparer which is called here.java.lang.Exception
java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object
void debug()