public class PrintfFormatter extends java.lang.Object implements Formatter
implements a Formatter
which uses a format string to
print parts from a TextStore
into a
StringBuilder
. An additional
java.util.Map
may supply additional material to
populate the output string.
The format string contains ordinary text and control codes
introduced by the percent (%
) character. The control
codes refer to either pieces of text from TextStore
or
to objects of the internal Map
as follows:
%
i,
where i is an integer,
retrieves the i'th part of the TextStore
and
writes it to the output string. As described elsewhere, negative values
access parts relative to the last part. For example
"%-1"
requests the last part.
%
i(
s,e)
is an extension to the the above. It refers to a substring of part
i, where s is the start index and s is
the end index. As usual, the end index is the index just after the
last character taken. If either index is negative, the value denotes the
position resulting from adding the value to the length of the
part.
For example %3(1,-1)
denotes part three of the
TextStore
with first and last character stripped
off. With %2(-3,0)
the last three characters of part
two are returned. As you can see, e=0 denotes the end of
the string, but s=0 denotes the start.
%(
i,
j)
requests all parts k with
i≤k<j. Like above, i and
j may be negative to denote an index relative to the end
of the list.
%(i,j,sep)(s,e) is a combination of the two above mentioned formats. The second parenthesised part is optional. The format requests a range of parts (i to j) while at the same time extracting only the substring denoted by s and e from each part. In addition, when printing the substrings to the output, they will be separated by sep.
If sep shall contain a closing parentheses, it must be escaped with a backslash. All other backslash characters are kept as such. Note that in a constant string you write in source code, the backslash must be doubled, because the compiler will take one away.
Example: "<%(-2,0,/> <)(0,-1)/>"
applied to
a TextStore
which contains "rock", "zock" and "nock"
will print "<zoc/> <noc/>".
%(
key)
retrieves the value
stored for the given key from the
java.lang.Map
passed into format()
. If
the Map
given is null
, a
NullPointerException
will be thrown. If, however, the
key does not exist in the map, the empty string is
silently inserted.
Example: "PMID: %(pmid)"
will result in the text
"PMID: 1234"
provided the value 1234
was
stored before in this object's Map
for the key
"pmid"
.
%l
i is replaced by the length of
the iith part of the TextStore
. Again,
i may be negative.%n
is replaced by the number of parts
extracted by the TextStore
. %%
denotes a percent sign on its own.\
c denotes the character c.Note that nowwhere in the above formats separating blank space is allowed.
Constructor and Description |
---|
PrintfFormatter(java.lang.CharSequence format) |
PrintfFormatter(java.lang.CharSequence format,
int start) |
Modifier and Type | Method and Description |
---|---|
void |
format(java.lang.StringBuilder yytext,
TextStore ts,
java.util.Map<java.lang.Object,java.lang.Object> m)
arranges (some of) the pieces of text found in
st
or m in an implementation dependend manner and
appends them to out . |
java.lang.String |
toString() |
public PrintfFormatter(java.lang.CharSequence format) throws ReSyntaxException
ReSyntaxException
public PrintfFormatter(java.lang.CharSequence format, int start) throws ReSyntaxException
ReSyntaxException
public void format(java.lang.StringBuilder yytext, TextStore ts, java.util.Map<java.lang.Object,java.lang.Object> m) throws CallbackException
Formatter
arranges (some of) the pieces of text found in st
or m
in an implementation dependend manner and
appends them to out
. Depending on the
implementation, both, st
and m
or even
both may be allowed to be null
.
Postcondition: This method may not change the content of
dest
other than by appending to it.
format
in interface Formatter
CallbackException
public java.lang.String toString()
toString
in class java.lang.Object