|
Packit |
d27c7e |
=head1 Creating PatAct Modules
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
This document is targeted towards the module writer creating a new
|
|
Packit |
d27c7e |
pattern or action module or readers who want to understand what is
|
|
Packit |
d27c7e |
going on inside a pattern or action module. If you are only
|
|
Packit |
d27c7e |
interesting in using PatAct modules, please see ``Using PatAct
|
|
Packit |
d27c7e |
Modules.''
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
There are two types of modules involved in processing a pattern-action
|
|
Packit |
d27c7e |
list the pattern module and the action module. Pattern modules are
|
|
Packit |
d27c7e |
created by users and passed to the `new()' method of action modules,
|
|
Packit |
d27c7e |
otherwise all pattern module methods are used only by the action
|
|
Packit |
d27c7e |
module. Action modules are PerlSAX handlers (see PerlSAX.pod in
|
|
Packit |
d27c7e |
libxml-perl). Action modules are responsible for initializing the
|
|
Packit |
d27c7e |
pattern module, receiving PerlSAX events, calling the `match()' method
|
|
Packit |
d27c7e |
in the pattern module for each element, and applying actions for
|
|
Packit |
d27c7e |
matching elements.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
The interface the user uses to call the drivers is described in
|
|
Packit |
d27c7e |
``Using PatAct Modules''.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
In general, the pattern-action modules perform their work on an
|
|
Packit |
d27c7e |
element-by-element basis, but the action modules are called with
|
|
Packit |
d27c7e |
PerlSAX events for all parse events (characters, processing
|
|
Packit |
d27c7e |
instructions, etc.).
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
=head1 Pattern Modules
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
Pattern modules have this interface, where PATTERN is the pattern or
|
|
Packit |
d27c7e |
query implementation:
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
use XML::PatAct::PATTERN;
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
$matcher = XML::PatAct::PATTERN->new(Patterns => $patterns [, OPTIONS]);
|
|
Packit |
d27c7e |
$matcher->initialize($actor);
|
|
Packit |
d27c7e |
$index = $matcher->match($element, $names, $nodes);
|
|
Packit |
d27c7e |
$matcher->finalize();
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
A pattern module instance is created with the pattern list that will
|
|
Packit |
d27c7e |
be used or processing as well as any additional options a pattern
|
|
Packit |
d27c7e |
module may define. `$patterns' is the original array reference passed
|
|
Packit |
d27c7e |
in by the user to the action module, so it is made up of pairs of
|
|
Packit |
d27c7e |
PATTERN => ACTION. The pattern matcher should ignore the ACTION
|
|
Packit |
d27c7e |
items.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
`initialize()' is called before any calls to `match()'. `$actor' is
|
|
Packit |
d27c7e |
the action module that is calling the pattern module. `initialize()'
|
|
Packit |
d27c7e |
is normally called from the `start_document()' PerlSAX event.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
`match()' performs a single matching against the pattern list and
|
|
Packit |
d27c7e |
returns the index of the matching pattern or undef if no pattern
|
|
Packit |
d27c7e |
matches. `$element' is the element to match. `$names' and `$nodes'
|
|
Packit |
d27c7e |
are array references containing the names and nodes (hashes) of this
|
|
Packit |
d27c7e |
element and all parent elements up to the element where processing
|
|
Packit |
d27c7e |
started.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
`finalize()' is called at the end of processing and may be used to
|
|
Packit |
d27c7e |
release state information. `finalize()' is normally called from the
|
|
Packit |
d27c7e |
`end_document()' PerlSAX event.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
Here is a template for creating a pattern module:
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
@include ../lib/XML/PatAct/PatternTempl.pm
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
=head1 Action Modules
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
Action modules are PerlSAX handlers (see PerlSAX.pod in libxml-perl).
|
|
Packit |
d27c7e |
Action modules are responsible for initializing the pattern module,
|
|
Packit |
d27c7e |
receiving PerlSAX events, calling the `match()' method in the pattern
|
|
Packit |
d27c7e |
module for each element, and applying actions for matching elements.
|
|
Packit |
d27c7e |
Action modules must also maintain arrays of element names and element
|
|
Packit |
d27c7e |
nodes to be passed to the `match()' method.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
Here is a template for creating an action module:
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
@include ../lib/XML/PatAct/ActionTempl.pm
|