public abstract class AbstractTextLineContentRewriter extends Object implements ContentRewriter
Constructor and Description |
---|
AbstractTextLineContentRewriter() |
Modifier and Type | Method and Description |
---|---|
void |
rewrite(Source source,
Sink sink,
ContentRewritingContext context)
Reads content from the
source , transforms the content and writes to the sink . |
protected abstract String |
rewriteLine(String line,
ContentRewritingContext context)
Translates the given
line and returns the translated string. |
public void rewrite(Source source, Sink sink, ContentRewritingContext context) throws ContentRewritingException, IOException
source
, transforms the content and writes to the sink
.
This method basically gets a Reader
from the source
and reads each text line from the reader. And it invokes rewriteLine(String, ContentRewritingContext)
method
to translate the line and write the translated line to the writer retrieved from the sink
.
If both source
and sink
are all text-based, and if it is possible to translate line by line,
then you can simply create a derived class from this class with implementing
rewriteLine(String, ContentRewritingContext)
method.
rewrite
in interface ContentRewriter
source
- source of contentsink
- target of content rewrittencontext
- content rewriting contextContentRewritingException
IOException
protected abstract String rewriteLine(String line, ContentRewritingContext context) throws ContentRewritingException, IOException
line
and returns the translated string.
For example, suppose the source
contains the following lines:
${message.greeting} Copyright ${copyright.holder} ${copyright.years}
Then, one implementation of this method might want to return "Hello, World!" for the first text line,
and "Copyright Apache Software Foundation 2014" for instance to let the sink
have the following:
Hello, World! Copyright Apache Software Foundation 2014
Even better, for example, if the given context
contains all those variables (i.e, ${copyright.holder} and ${copyright.years}),
then the implementation of this method can resolve those variables dynamically and translates them based on the variables given
by the runtime.
line
- a text line from the source
context
- the content rewriting context given by the runtimeContentRewritingException
IOException
Copyright © 2008–2015 The Apache Software Foundation. All rights reserved.