| 1 | package Bio::Phylo::NeXML::Meta::XMLLiteral; | ||
| 2 | use strict; | ||
| 3 | use Bio::Phylo::Util::CONSTANT qw'_META_ /looks_like/'; | ||
| 4 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 5 | { | ||
| 6 | my $TYPE_CONSTANT = _META_; | ||
| 7 | my $CONTAINER_CONSTANT = $TYPE_CONSTANT; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Bio::Phylo::NeXML::Meta::XMLLiteral - Annotation value adaptor, no direct usage | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | # no direct usage | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | No direct usage, is used internally by L<Bio::Phylo::NeXML::Meta> to | ||
| 20 | wrap objects into a common adaptor class for serialization to | ||
| 21 | XML. | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | =head2 CONSTRUCTOR | ||
| 26 | |||
| 27 | =over | ||
| 28 | |||
| 29 | =item new() | ||
| 30 | |||
| 31 | Type : Constructor | ||
| 32 | Title : new | ||
| 33 | Usage : my $lit = Bio::Phylo::NeXML::Meta::XMLLiteral->new($obj); | ||
| 34 | Function: Initializes a Bio::Phylo::NeXML::Meta::XMLLiteral object. | ||
| 35 | Returns : A Bio::Phylo::NeXML::Meta::XMLLiteral object. | ||
| 36 | Args : An object (or array ref of objects) to wrap, | ||
| 37 | either a 'RDF::Core::Model' (or subclass), | ||
| 38 | an 'XML::XMLWriter' or (subclass) or any | ||
| 39 | of the following serialization methods | ||
| 40 | used for duck-typing one of the following classes: | ||
| 41 | Bio::Phylo => to_xml, | ||
| 42 | XML::DOM, XML::GDOME, XML::LibXML => toString, | ||
| 43 | XML::Twig => sprint, | ||
| 44 | XML::DOM2 => xmlify, | ||
| 45 | XML::DOMBacked => as_xml, | ||
| 46 | XML::Handler => dump_tree, | ||
| 47 | XML::Element => as_XML | ||
| 48 | XML::API => _as_string, | ||
| 49 | XML::Code => code | ||
| 50 | |||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my ( $class, $obj ) = @_; | ||
| 56 | return bless \$obj, $class; | ||
| 57 | } | ||
| 58 | |||
| 59 | =back | ||
| 60 | |||
| 61 | =head2 SERIALIZERS | ||
| 62 | |||
| 63 | =over | ||
| 64 | |||
| 65 | =item to_xml() | ||
| 66 | |||
| 67 | Serializes invocant to xml. | ||
| 68 | |||
| 69 | Type : Serializer | ||
| 70 | Title : to_xml | ||
| 71 | Usage : my $xml = $obj->to_xml; | ||
| 72 | Function: Turns the invocant object (and its descendants) into an XML string. | ||
| 73 | Returns : SCALAR | ||
| 74 | Args : NONE | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub to_xml { | ||
| 79 | my $self = shift; | ||
| 80 | my $objs = $$self; | ||
| 81 | my @objs = ref($objs) eq 'ARRAY' ? @{$objs} : ($objs); | ||
| 82 | my $xml = ''; | ||
| 83 | for my $obj (@objs) { | ||
| 84 | |||
| 85 | # for RDF::Core::Model objects | ||
| 86 | if ( looks_like_instance( $obj, 'RDF::Core::Model' ) ) { | ||
| 87 | eval { | ||
| 88 | require RDF::Core::Model::Serializer; | ||
| 89 | my $serialized_model = ''; | ||
| 90 | my $serializer = RDF::Core::Model::Serializer->new( | ||
| 91 | 'Model' => $obj, | ||
| 92 | 'Output' => \$serialized_model, | ||
| 93 | ); | ||
| 94 | $xml .= $serialized_model; | ||
| 95 | }; | ||
| 96 | if ($@) { | ||
| 97 | throw 'API' => $@; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | # for XML::XMLWriter object | ||
| 102 | elsif ( looks_like_instance( $obj, 'XML::XMLWriter' ) ) { | ||
| 103 | $xml .= $obj->get; | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | |||
| 107 | # duck-typing | ||
| 108 | # Bio::Phylo => to_xml, | ||
| 109 | # XML::DOM,XML::GDOME,XML::LibXML => toString, | ||
| 110 | # XML::Twig => sprint | ||
| 111 | # XML::DOM2 => xmlify, | ||
| 112 | # XML::DOMBacked => as_xml, | ||
| 113 | # XML::Handler => dump_tree, | ||
| 114 | # XML::Element => as_XML | ||
| 115 | # XML::API => _as_string, | ||
| 116 | # XML::Code => code | ||
| 117 | my @methods = | ||
| 118 | qw(to_xml toString sprint _as_string code xmlify as_xml dump_tree as_XML); | ||
| 119 | SERIALIZER: for my $method (@methods) { | ||
| 120 | if ( looks_like_implementor( $obj, $method ) ) { | ||
| 121 | $xml .= $obj->$method; | ||
| 122 | last SERIALIZER; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | return $xml; | ||
| 128 | } | ||
| 129 | |||
| 130 | =back | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | # podinherit_insert_token | ||
| 135 | |||
| 136 | =head1 SEE ALSO | ||
| 137 | |||
| 138 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 139 | for any user or developer questions and discussions. | ||
| 140 | |||
| 141 | =over | ||
| 142 | |||
| 143 | =item L<Bio::Phylo::NeXML::Meta> | ||
| 144 | |||
| 145 | =item L<Bio::Phylo::Manual> | ||
| 146 | |||
| 147 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =head1 CITATION | ||
| 152 | |||
| 153 | If you use Bio::Phylo in published research, please cite it: | ||
| 154 | |||
| 155 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 156 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 157 | I<BMC Bioinformatics> B<12>:63. | ||
| 158 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub _type { $TYPE_CONSTANT } | ||
| 163 | sub _container { $CONTAINER_CONSTANT } | ||
| 164 | sub _cleanup { } | ||
| 165 | } | ||
| 166 | 1; |