| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | Bio::Phylo::NeXML::DOM::Document::Libxml - XML DOM document mappings to the | ||
| 5 | C<XML::LibXML> package | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | Don't use directly; use Bio::Phylo::NeXML::DOM->new( -format => 'libxml' ) instead. | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This module provides mappings the methods specified in the | ||
| 14 | L<Bio::Phylo::NeXML::DOM::Document> abstract class to the C<XML::LibXML::Document> | ||
| 15 | package. | ||
| 16 | |||
| 17 | =head1 AUTHOR | ||
| 18 | |||
| 19 | Mark A. Jensen ( maj -at- fortinbras -dot- us ) | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | package Bio::Phylo::NeXML::DOM::Document::Libxml; | ||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 27 | use Bio::Phylo::Util::Dependency 'XML::LibXML'; | ||
| 28 | use Bio::Phylo::Util::CONSTANT 'looks_like_instance'; | ||
| 29 | use Bio::Phylo::NeXML::DOM::Element::Libxml (); # for blessing | ||
| 30 | use base qw'Bio::Phylo::NeXML::DOM::Document XML::LibXML::Document'; | ||
| 31 | |||
| 32 | =head2 Constructor | ||
| 33 | |||
| 34 | =over | ||
| 35 | |||
| 36 | =item new() | ||
| 37 | |||
| 38 | Type : Constructor | ||
| 39 | Title : new | ||
| 40 | Usage : $doc = Bio::Phylo::NeXML::DOM::Document->new(@args) | ||
| 41 | Function: Create a Document object using the underlying package | ||
| 42 | Returns : Document object or undef on fail | ||
| 43 | Args : Package-specific arguments | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my ( $class, @args ) = @_; | ||
| 49 | my $self = XML::LibXML::Document->new(@args); | ||
| 50 | bless $self, $class; | ||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | =item parse_document() | ||
| 55 | |||
| 56 | Type : Factory method | ||
| 57 | Title : parse_document | ||
| 58 | Usage : $doc = $dom->parse_document($text) | ||
| 59 | Function: Create a new XML DOM document from XML text | ||
| 60 | Returns : DOM document | ||
| 61 | Args : An XML String | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub parse_document { | ||
| 66 | my ( $class, $text ) = @_; | ||
| 67 | my $dom = XML::LibXML->load_xml($text); | ||
| 68 | bless $dom, $class; | ||
| 69 | return $dom; | ||
| 70 | } | ||
| 71 | |||
| 72 | =back | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | =head2 Document property accessors/mutators | ||
| 77 | |||
| 78 | =over | ||
| 79 | |||
| 80 | =item set_encoding() | ||
| 81 | |||
| 82 | Type : Mutator | ||
| 83 | Title : set_encoding | ||
| 84 | Usage : $doc->set_encoding($enc) | ||
| 85 | Function: Set encoding for document | ||
| 86 | Returns : True on success | ||
| 87 | Args : Encoding descriptor as string | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub set_encoding { | ||
| 92 | return shift->setEncoding(shift); | ||
| 93 | } | ||
| 94 | |||
| 95 | =item get_encoding() | ||
| 96 | |||
| 97 | Type : Accessor | ||
| 98 | Title : get_encoding | ||
| 99 | Usage : $doc->get_encoding() | ||
| 100 | Function: Get encoding for document | ||
| 101 | Returns : Encoding descriptor as string | ||
| 102 | Args : none | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub get_encoding { | ||
| 107 | return shift->encoding; | ||
| 108 | } | ||
| 109 | |||
| 110 | =item set_root() | ||
| 111 | |||
| 112 | Type : Mutator | ||
| 113 | Title : set_root | ||
| 114 | Usage : $doc->set_root($elt) | ||
| 115 | Function: Set the document's root element | ||
| 116 | Returns : True on success | ||
| 117 | Args : Element object | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub set_root { | ||
| 122 | my ( $self, $root ) = @_; | ||
| 123 | if ( looks_like_instance $root, 'XML::LibXML::Element' ) { | ||
| 124 | $self->setDocumentElement($root); | ||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | throw 'ObjectMismatch' => "Argument is not an XML::LibXML::Element"; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | =item get_root() | ||
| 133 | |||
| 134 | Type : Accessor | ||
| 135 | Title : get_root | ||
| 136 | Usage : $doc->get_root() | ||
| 137 | Function: Get the document's root element | ||
| 138 | Returns : Element object or undef if DNE | ||
| 139 | Args : none | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub get_root { | ||
| 144 | return shift->documentElement; | ||
| 145 | } | ||
| 146 | |||
| 147 | =back | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | =head2 Document element accessors | ||
| 152 | |||
| 153 | =over | ||
| 154 | |||
| 155 | =item get_element_by_id() | ||
| 156 | |||
| 157 | Type : Accessor | ||
| 158 | Title : get_element_by_id | ||
| 159 | Usage : $doc->get_element_by_id($id) | ||
| 160 | Function: Get element having id $id | ||
| 161 | Returns : Element object or undef if DNE | ||
| 162 | Args : id designator as string | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | # the XML::LibXML::Document::get_element_by_id() retrieves only | ||
| 167 | # via @xml:id attributes in a general XML file. This is a kludge | ||
| 168 | # using an XPath expression to find an unqualified id attribute | ||
| 169 | # that matches. | ||
| 170 | sub get_element_by_id { | ||
| 171 | my ( $self, $id ) = @_; | ||
| 172 | unless ($id) { | ||
| 173 | throw 'BadArgs' => "Argument 'id' required"; | ||
| 174 | } | ||
| 175 | my $xp = "//*[\@id = '$id']"; | ||
| 176 | my $e = $self->get_root->find($xp); | ||
| 177 | return unless $e; # don't return undef explicitly, do it this way | ||
| 178 | $e = $e->shift; | ||
| 179 | return bless $e, 'Bio::Phylo::NeXML::DOM::Element::Libxml'; | ||
| 180 | } | ||
| 181 | |||
| 182 | =item get_elements_by_tagname() | ||
| 183 | |||
| 184 | Type : Accessor | ||
| 185 | Title : get_elements_by_tagname | ||
| 186 | Usage : $elt->get_elements_by_tagname($tagname) | ||
| 187 | Function: Get array of elements having given tag name | ||
| 188 | Returns : Array of elements or undef if no match | ||
| 189 | Args : tag name as string | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub get_elements_by_tagname { | ||
| 194 | my ( $self, $tagname, @args ) = @_; | ||
| 195 | my @a = $self->getElementsByTagName($tagname); | ||
| 196 | bless( $_, 'Bio::Phylo::NeXML::DOM::Element::Libxml' ) for (@a); | ||
| 197 | return @a; | ||
| 198 | } | ||
| 199 | |||
| 200 | =back | ||
| 201 | |||
| 202 | =head2 Output methods | ||
| 203 | |||
| 204 | =over | ||
| 205 | |||
| 206 | =item to_xml() | ||
| 207 | |||
| 208 | Type : Serializer | ||
| 209 | Title : to_xml | ||
| 210 | Usage : $doc->to_xml | ||
| 211 | Function: Create XML string from document | ||
| 212 | Returns : XML string | ||
| 213 | Args : Formatting arguments as allowed by underlying package | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub to_xml { | ||
| 218 | my ( $self, @args ) = @_; | ||
| 219 | return $self->toString(@args); | ||
| 220 | } | ||
| 221 | |||
| 222 | =back | ||
| 223 | |||
| 224 | =head1 CITATION | ||
| 225 | |||
| 226 | If you use Bio::Phylo in published research, please cite it: | ||
| 227 | |||
| 228 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 229 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 230 | I<BMC Bioinformatics> B<12>:63. | ||
| 231 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | 1; |