I have previously been using the jPOI to the rescue example (in B4J) to substitute string variables for specific words in a MS-Word (DOC) file and it was performing great. String variables I am using inside the file text are of the format: "<<variable>>" .
Would like to try now to do something similar in B4A, though it seems the jPOI could not be used for some reasons, therefore I thought to use the RTF file format.
Seen a reference to a Java library: "com.tutego.jrtf" that has a class which is able to make variable substitution in an existing RTF file: RtfTemplate Class
Asking for some guidance on how to use this class specifically the .inject method in B4A, so I could open an existing RTF, replace the variables (I could have them as key/value Map) and write the resulting file to another file name.
File.Copy(File.DirAssets,"orgfile.rtf",File.DirInternal,"orgfile.rtf")
Dim rtf As String = File.ReadString(File.DirInternal,"orgfile.rtf")
' replace the placeholder(s) in rtf
File.WriteString(File.DirInternal,"newfile.rtf",rtf)
Thanks Manfred,
The RTF file its kind of big in size, and not very sure I could handled with using regex to cover some 150 different placeholders, that is why I turned to the jRTF lib, hoping it would be simpler to pass a Map with those placeholders, while I could learn some on using external jars.
This is what I currently try with regex, after I read line by line as strings from RTF file,
though honestly I am looking for some help on how to use that external jar - jRtf - RtfTemplate Class specifically.
B4X:
public Sub tryReplace
Dim data As String ' this is the example RTF file read as text
data = "We have <<ADDRESSLINE1>> as buld and <<ADDRESSLINE2>> not ready"
'
Dim matcher1 As Matcher
matcher1 = Regex.Matcher("<<[^>]+>>", data) ' regex pattern for placeholders
Do While matcher1.Find = True
'Log(matcher1.Match)
Select matcher1.Match ' care for specific placeholders
Case "<<ADDRESSLINE1>>"
data = data.Replace("<<ADDRESSLINE1>>", "tutego")
Case "<<ADDRESSLINE2>>"
data = data.Replace("<<ADDRESSLINE2>>", "Sonsbeck")
End Select
Loop
'
Log(data)
End Sub
The regex solution just works ok, no issues.
I am trying now to pass all "placeholders" and their replacement values as a Map.
Would be great if I could optimize it, yes.
Thank you.