| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | Bio::Phylo::NeXML::DOM::Document::Twig - XML DOM document mappings to the | ||
| 5 | C<XML::Twig> package | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | Don't use directly; use Bio::Phylo::NeXML::DOM->new( -format => 'twig' ) 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 | ||
| 15 | C<XML::Twig> 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::Twig; | ||
| 24 | use strict; | ||
| 25 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 26 | use Bio::Phylo::Util::Dependency 'XML::Twig'; | ||
| 27 | use Bio::Phylo::NeXML::DOM::Document; | ||
| 28 | use Bio::Phylo::Util::CONSTANT 'looks_like_instance'; | ||
| 29 | use base qw'Bio::Phylo::NeXML::DOM::Document XML::Twig'; | ||
| 30 | |||
| 31 | =head2 Constructor | ||
| 32 | |||
| 33 | =over | ||
| 34 | |||
| 35 | =item new() | ||
| 36 | |||
| 37 | Type : Constructor | ||
| 38 | Title : new | ||
| 39 | Usage : $doc = Bio::Phylo::NeXML::DOM::Document->new(@args) | ||
| 40 | Function: Create a Document object using the underlying package | ||
| 41 | Returns : Document object or undef on fail | ||
| 42 | Args : Package-specific arguments | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my ( $class, @args ) = @_; | ||
| 48 | my $self = XML::Twig->new(); | ||
| 49 | $self->set_encoding(); | ||
| 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 $self = XML::Twig->new(); | ||
| 68 | $self->parse($text); | ||
| 69 | bless $self, $class; | ||
| 70 | return $self; | ||
| 71 | } | ||
| 72 | |||
| 73 | =back | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | =head2 Document property accessors/mutators | ||
| 78 | |||
| 79 | =over | ||
| 80 | |||
| 81 | =item set_encoding() | ||
| 82 | |||
| 83 | Type : Mutator | ||
| 84 | Title : set_encoding | ||
| 85 | Usage : $doc->set_encoding($enc) | ||
| 86 | Function: Set encoding for document | ||
| 87 | Returns : True on success | ||
| 88 | Args : Encoding descriptor as string | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub set_encoding { | ||
| 93 | my ( $self, $encoding, @args ) = @_; | ||
| 94 | $self->set_encoding($encoding); | ||
| 95 | return 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | =item get_encoding() | ||
| 99 | |||
| 100 | Type : Accessor | ||
| 101 | Title : get_encoding | ||
| 102 | Usage : $doc->get_encoding() | ||
| 103 | Function: Get encoding for document | ||
| 104 | Returns : Encoding descriptor as string | ||
| 105 | Args : none | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub get_encoding { | ||
| 110 | return shift->encoding; | ||
| 111 | } | ||
| 112 | |||
| 113 | =item set_root() | ||
| 114 | |||
| 115 | Type : Mutator | ||
| 116 | Title : set_root | ||
| 117 | Usage : $doc->set_root($elt) | ||
| 118 | Function: Set the document's root element | ||
| 119 | Returns : True on success | ||
| 120 | Args : Element object | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub set_root { | ||
| 125 | my ( $self, $root ) = @_; | ||
| 126 | if ( looks_like_instance $root, 'XML::Twig::Elt' ) { | ||
| 127 | XML::Twig::set_root( $self, $root ); | ||
| 128 | |||
| 129 | # manage ids | ||
| 130 | for ( $root->descendants_or_self ) { | ||
| 131 | ${ $self->{twig_id_list} }{ $_->att('id') } = $_ if $_->att('id'); | ||
| 132 | } | ||
| 133 | return 1; | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | throw 'ObjectMisMatch' => 'Argument is not an XML::Twig::Elt'; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | =item get_root() | ||
| 141 | |||
| 142 | Type : Accessor | ||
| 143 | Title : get_root | ||
| 144 | Usage : $doc->get_root() | ||
| 145 | Function: Get the document's root element | ||
| 146 | Returns : Element object or undef if DNE | ||
| 147 | Args : none | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub get_root { | ||
| 152 | return shift->root; | ||
| 153 | } | ||
| 154 | |||
| 155 | =back | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | =head2 Document element accessors | ||
| 160 | |||
| 161 | =over | ||
| 162 | |||
| 163 | =item get_element_by_id() | ||
| 164 | |||
| 165 | Type : Accessor | ||
| 166 | Title : get_element_by_id | ||
| 167 | Usage : $doc->get_element_by_id($id) | ||
| 168 | Function: Get element having id $id | ||
| 169 | Returns : Element object or undef if DNE | ||
| 170 | Args : id designator as string | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub get_element_by_id { | ||
| 175 | return shift->elt_id(shift); | ||
| 176 | } | ||
| 177 | |||
| 178 | =item get_elements_by_tagname() | ||
| 179 | |||
| 180 | Type : Accessor | ||
| 181 | Title : get_elements_by_tagname | ||
| 182 | Usage : $elt->get_elements_by_tagname($tagname) | ||
| 183 | Function: Get array of elements having given tag name | ||
| 184 | Returns : Array of elements or undef if no match | ||
| 185 | Args : tag name as string | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub get_elements_by_tagname { | ||
| 190 | my ( $self, $tagname, @args ) = @_; | ||
| 191 | return $self->get_root->get_elements_by_tagname($tagname); | ||
| 192 | } | ||
| 193 | |||
| 194 | =back | ||
| 195 | |||
| 196 | =head2 Output methods | ||
| 197 | |||
| 198 | =over | ||
| 199 | |||
| 200 | =item to_xml() | ||
| 201 | |||
| 202 | Type : Serializer | ||
| 203 | Title : to_xml | ||
| 204 | Usage : $doc->to_xml | ||
| 205 | Function: Create XML string from document | ||
| 206 | Returns : XML string | ||
| 207 | Args : Formatting arguments as allowed by underlying package | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub to_xml { | ||
| 212 | my ( $self, @args ) = @_; | ||
| 213 | return $self->sprint(@args); | ||
| 214 | } | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | =head1 CITATION | ||
| 219 | |||
| 220 | If you use Bio::Phylo in published research, please cite it: | ||
| 221 | |||
| 222 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 223 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 224 | I<BMC Bioinformatics> B<12>:63. | ||
| 225 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | 1; |