|
XML::PatAct::ToObjects - An action module for creating Perl objects |
XML::PatAct::ToObjects - An action module for creating Perl objects
use XML::PatAct::ToObjects;
my $patterns = [ PATTERN => [ OPTIONS ],
PATTERN => "PERL-CODE",
... ];
my $matcher = XML::PatAct::ToObjects->new( Patterns => $patterns,
Matcher => $matcher,
CopyId => 1,
CopyAttributes => 1 );
XML::PatAct::ToObjects is a PerlSAX handler for applying pattern-action lists to XML parses or trees. XML::PatAct::ToObjects creates Perl objects of the types and contents of the action items you define.
New XML::PatAct::ToObject instances are creating by calling `new()'. Parameters can be passed as a list of key, value pairs or a hash. `new()' requires the Patterns and Matcher parameters, the rest are optional:
-make) for that element.
Each action can either be a list of options defined below or a string containing a fragment of Perl code. If the action is a string of Perl code then simple then some simple substitutions are made as described further below.
Options that can be used in an action item containing an option-list:
-pcdata may be used with this option.
Contents
field.
HASH' to
simply create an anonyous hash.
-make => 'HASH', -args => 'URL => %{href}'
would copy the `href' attribute in an element to the `URL' field
of the newly created hash.
%{ATTRIBUTE}' notation can be used to
substitute the value of an attribute into the literal value.
XML::Grove::AsString) and store in FIELD. Only valid with
-field or -push-field.
If an action item is a string, that string is treated as a fragment of Perl code. The following simple substitutions are performed on the fragment to provide easy access to the information being converted:
The example pattern-action list below will convert the following XML representing a Database schema:
<schema>
<table>
<name>MyTable</name>
<summary>A short summary</summary>
<description>A long description that may
contain a subset of HTML</description>
<column>
<name>MyColumn1</name>
<summary>A short summary</summary>
<description>A long description</description>
<unique/>
<non-null/>
<default>42</default>
</column>
</table>
</schema>
into Perl objects looking like:
[
{ Name => "MyTable",
Summary => "A short summary",
Description => $grove_object,
Columns => [
{ Name => "MyColumn1",
Summary => "A short summary",
Description => $grove_object,
Unique => 1,
NonNull => 1,
Default => 42
}
]
}
]
Here is a Perl script and pattern-action list that will perform the
conversion using the simple name matching pattern module
XML::PatAct::MatchName. The script accepts a Schema XML file as an
argument ($ARGV[0]) to the script. This script creates a grove as
one of it's objects, so it requires the XML::Grove module.
use XML::Parser::PerlSAX;
use XML::PatAct::MatchName;
use XML::PatAct::ToObjects;
my $patterns = [
'schema' => [ qw{ -holder } ],
'table' => [ qw{ -make Schema::Table } ],
'name' => [ qw{ -field Name -as-string } ],
'summary' => [ qw{ -field Summary -as-string } ],
'description' => [ qw{ -field Description -grove } ],
'column' => [ qw{ -make Schema::Column -push-field Columns } ],
'unique' => [ qw{ -field Unique -value 1 } ],
'non-null' => [ qw{ -field NonNull -value 1 } ],
'default' => [ qw{ -field Default -as-string } ],
];
my $matcher = XML::PatAct::MatchName->new( Patterns => $patterns );
my $handler = XML::PatAct::ToObjects->new( Patterns => $patterns,
Matcher => $matcher);
my $parser = XML::Parser::PerlSAX->new( Handler => $handler );
my $schema = $parser->parse(Source => { SystemId => $ARGV[0] } );
Ken MacLeod, ken@bitsko.slc.ut.us
perl(1), Data::Grove(3)
``Using PatAct Modules'' and ``Creating PatAct Modules'' in libxml-perl.
|
XML::PatAct::ToObjects - An action module for creating Perl objects |