MAP | PageMixer Documents > Tutorial > Mixing with PageMixer > 'NOP' filter | << | >> |
This section explains how to parse and render HTML page in PageMixer framework.
Class diagram in this section is shown below:
Classes which you must define are colored, and other are already defined.
Object diagram in this section is shown below:
Processing flow of parsing and renderring of HTML page is shown as below:
Filter
Renderer
instance
Producer
instance
ConsumerContext
instance
Filter
to Renderer
Producer#produce
with
Filter
and ConsumerContext
And sequence diagram after Producer#produce
invocation is
shown below.
In this tutorial, abbreviated class names are used. Complete names are shown below.
Notation | Full name |
---|---|
ConsumerContext | jp.ne.dti.lares.foozy.pagemixer.mixer.ConsumerContext |
Filter | jp.ne.dti.lares.foozy.pagemixer.mixer.Filter |
HashMapConsumerContext | jp.ne.dti.lares.foozy.pagemixer.mixer.HashMapConsumerContext |
LoosePageState | jp.ne.dti.lares.foozy.pagemixer.parser.LoosePageState |
PageParser | jp.ne.dti.lares.foozy.pagemixer.mixer.PageParser |
PageState | jp.ne.dti.lares.foozy.pagemixer.parser.PageState |
Producer | jp.ne.dti.lares.foozy.pagemixer.mixer.Producer |
Renderer | jp.ne.dti.lares.foozy.pagemixer.mixer.Renderer |
Token | jp.ne.dti.lares.foozy.pagemixer.Token |
Notation | Full name |
---|---|
Bootstrap | pagemixer.filter.Bootstrap |
Bootstrap.Default | pagemixer.filter.Bootstrap.Default |
NopFilter | pagemixer.filter.NopFilter |
At first, define filter which does nothing(NOP:No OPeration) but passes token sequence to connected consumer.
public class NopFilter extends Filter { public NopFilter(){ super(); } public void consume(ConsumerContext context, Token token){ getConsumer().consume(context, token); } public void flush(ConsumerContext context){ getConsumer().flush(context); } }
Both "consume
" and "flush
" methods
only invoke same signature method of Consumer
to which it is connected.
NOTE:
In fact,
that kind filter is already defined as "DefaultFilter
",
and you should use it if you need 'NOP' filter.
Renderer
In PageMixer framework,
HTML page is treated as Token
sequence.
But string(or byte-stream) representation must be needed
to show HTML page as result of processing.
"Renderer
" class renders token sequence
on java.io.Writer
specified at construction.
For example,
Renderer
writing on standard-out is created by:
Renderer renderer = new Renderer(new OutputStreamWriter(System.out));
Producer
Once again,
In PageMixer framework,
HTML page is treated as "Token
" sequence.
So you must get "Producer
",
the provider of token sequence.
There is very easy way to create
Producer
which provides token sequence
representing specified HTML page.
It is to use "PageParser
" and
"LoosePageState
".
LoosePageState pageState = new LoosePageState(); PageParser parser = new PageParser(pageState); Producer producer = parser.parse(filename);
Variable "filename
" contains filename of HTML page.
ATTENTION:
"LoosePageState
" is
sample implementation of
"PageState
" interface,
its name "Loose" comes from "Syntactically Loose".
This implementation allows invalid tag structure, 'table' under 'h1' for example, so you should not expect it to tell HTML source invalidity.
ConsumerContext
"ConsumerContext
" is created
for dynamic values and inter-filter communication.
For stand-alone environment,
there is "HashMapConsumerContext
" class
which implements ConsumerContext
interface
with java.util.HashMap
,
and you can create it as below:
HashMapConsumerContext context = new HashMapConsumerContext();
To know how to create this(or what class you should use) is important even though nothing needs this "context" in this section.
Now, everything needed are ready to use. To parse and render HTML page, you should do only below steps.
NopFilter
NopFilter
to Renderer
Producer#produce
with
NopFilter
and ConsumerContext
NopFilter filter = new NopFilter(); // --- (1) filter.connectTo(renderer); // --- (2) producer.produce(context, filter); // --- (3)
In fact,
utility class "Bootstrap
", or its derived classes,
assists each steps described
from "Create Renderer
"
to "Connect and Mix"(this sub section).
You can get rendered HTML page from standard-out by as below.
Bootstrap.Default
"
with filename of HTML page to parse
execute(Filter)
"
with your specific Filter
Execution code is as below (see pagemixer.filter.NopFilter for detail).
try{ Bootstrap bootstrap = new Bootstrap.Default(filename); bootstrap.execute(new NopFilter()); } catch(Exception e){ e.printStackTrace(System.err); }
MAP | PageMixer Documents > Tutorial > Mixing with PageMixer > 'NOP' filter | << | >> |