The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::Class::LibXMLdoc - Create an adjunct "[column]Doc" accessor of a column's data which is automatically parsed into a LibXML documentElement (beta-software).

VERSION

0.05

SYNOPSIS

 package My::DB::thingy;

 __PACKAGE__->load_components(qw/ PK::Auto LibXMLdoc Core /);

 __PACKAGE__->table('thingy');

 __PACKAGE__->add_columns(qw/ id title body created owner whatever /);
 __PACKAGE__->set_primary_key('id');

 __PACKAGE__->libXMLdoc_columns(qw/ body /);

 package main;
 use My::DB;

 my $schema = My::DB->connect(...);
 my $thingy = $schema->resultset("thingy")->find(153);
 print $thingy->bodyDoc->toString, "\n\n";
 print $thingy->bodyDoc->textContent, "\n";

DESCRIPTION

This DBIx::Class component does not alter your data in any way. It takes column names to get the value from the column, parse it into XML with LibXML and make the documentElement object available via an autogenerated accessor named by affixing the column with "Doc."

The XML parsing is on demand so it doesn't waste time doing it to data you don't use or by doing it more than once to data that is unchanged.

A wrapper XML tag for the mini-document is auto-generated from the table + column name. So-

 my $xhmlt = <<";
<p>
Ain't no doubt Jesus see us<br/>
Acting foolishly on American Bandstand
</p>

 my $thingy = $schema->resultset("thingy")
               ->create({ title => "Gullah",
                          body  => $xhtml });

 my $root = $thingy->bodyDoc;

 print $root->toString, $/;

 # gives us ----------------
 <thingybody><p>
 Ain't no doubt Jesus see us<br/>
 Acting foolishly on American Bandstand
 </p></thingybody>

The returned item, $root above, is the doc->documentElement of a XML::LibXML::Document. It returns the documentElement instead of the document object itself because the document object is less frequently/directly useful and in the cases you might want it, e.g. to modify the document with new nodes, you can still get it with ownerDocument. E.g.-

 my $doc = $root->ownerDocument;
 my $title = $doc->createElement("h1");
 my $text = $doc->createTextNode($thingy->title);
 $title->appendChild($text);

 $root->insertBefore($title, $root->firstChild);

 print $root->ownerDocument->toString, $/;

 # NOW gives us (spacing added) ------
 <?xml version="1.0" encoding="utf-8"?>
 <doc table="thingy" column="body" version="0.05">
 <h1>Gullah</h1>
 <p>
 Ain't no doubt Jesus see us<br/>
 Acting foolishly on American Bandstand
 </p>
 </doc>

The encoding, as utf-8 above, is only set if the UTF8Columns component is also being used on the column. I believe this means load order matters. I.e. it should be-

 __PACKAGE__->load_components(qw/ UTF8Columns LibXMLdoc Core /);

When you're using both.

METHODS

libXMLdoc_columns

Use libXMLdoc_columns to set the columns you want available. If the columns contain anything which isn't valid XML, an exception will be thrown.

libXMLdoc_parser_settings

This is a hash ref of methods and their arguments which are passed to the XML::LibXML parser when it is created.

The only pair passed by default is line_numbers => 1. Which is added to the parser like so-

  $parser->line_numbers(1)

You can set any method => argument pairs you like. See what is possible in the XML::LibXML::Parser docs. Any mistaken method names or illegal arguments will cause an error. It is mostly included so you can do the following if you know your content is junk; since parsing errors throw exceptions.

 __PACKAGE__->libXMLdoc_parser_settings({ recover => 1,
                                          recover_silently => 1 });

TO DO

There are basically no live tests right now. This is very bad but Test::DBIC was tough to get going and I haven't had time to fix it or roll something minimal like it. Since the code's been running in production without problems I've been slow off the blocks. I'll try to remedy that soon.

Allow a switch for parse_html...?

BUGS AND LIMITATIONS

This is no longer brand new and it's been used quite robustly in production since November of 2006. There are no known bugs. I love good feedback and bug reports.

Please report any bugs or feature requests to bug-dbix-class-libxmldoc@rt.cpan.org, or through the web interface at http://rt.cpan.org/Public/Dist/Display.html?Name=DBIx-Class-LibXMLdoc.

SEE ALSO

XML::LibXML::Document, XML::LibXML::Node, XML::LibXML::Element, XML::LibXML::Text, and XML::LibXML::Attr.

HTML::Entities and DBIx::Class.

AUTHOR

Ashley Pond V <ashley@cpan.org>.

LICENSE AND COPYRIGHT

Copyright (c) 2008, Ashley Pond V <ashley@cpan.org>.

This module is free software; you can redistribute it and modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

Because this software is licensed free of charge, there is no warranty for the software, to the extent permitted by applicable law. Except when otherwise stated in writing the copyright holders and other parties provide the software "as is" without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the software is with you. Should the software prove defective, you assume the cost of all necessary servicing, repair, or correction.

In no event unless required by applicable law or agreed to in writing will any copyright holder, or any other party who may modify or redistribute the software as permitted by the above license, be liable to you for damages, including any general, special, incidental, or consequential damages arising out of the use or inability to use the software (including but not limited to loss of data or data being rendered inaccurate or losses sustained by you or third parties or a failure of the software to operate with any other software), even if such holder or other party has been advised of the possibility of such damages.