lib/MOSES/MOBY/Data/Object.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 137 CC 14 SCAR 33.3
CC
14
Cov
0%
_add_XML_element
56.0
toXML
42.0
_attr_prop
12.0
CRAP 27.9
1 #-----------------------------------------------------------------
2 # MOSES::MOBY::Data::Object
3 # Author: Edward Kawas <edward.kawas@gmail.com>,
4 # Martin Senger <martin.senger@gmail.com>
5 # For copyright and disclaimer see below.
6 #
7 # $Id: Object.pm,v 1.5 2010/12/08 16:14:13 kawas Exp $
8 #-----------------------------------------------------------------
9  
10 package MOSES::MOBY::Data::Object;
11 use base ("MOSES::MOBY::Base");
12 use MOSES::MOBY::Tags;
13 use strict;
14  
15 # add versioning to this module
16 use vars qw /$VERSION/;
17 $VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/;
18  
19 #-----------------------------------------------------------------
20 # load all modules needed for my attributes
21 #-----------------------------------------------------------------
22 use MOSES::MOBY::Data::Xref;
23 use MOSES::MOBY::Data::ProvisionInformation;
24  
25  
26 =head1 NAME
27  
28 MOSES::MOBY::Data::Object - A base Moby data type
29  
30 =head1 SYNOPSIS
31  
32 use MOSES::MOBY::Data::Object;
33  
34 # create a Moby object with a namespace of NCBI_gi and id 545454
35 my $data = MOSES::MOBY::Data::Object->new (namespace=>"NCBI_gi", id=>"545454");
36
37 # set/get an article name for this data object
38 $data->name ('myObject');
39 print $data->name;
40
41 # set/get an id for this data object
42 $data->id ('myID');
43 print $data->id;
44
45 # check if this data object is a primitive type
46 print "a primitive" if $data->primitive;
47 print "not a primitive" if not $data->primitive;
48
49 # set an array of cross references
50 my $xref = new MOSES::MOBY::Data::Xref;
51 $xref->description ('He is looking at you, kid...');
52 $data->xrefs ($xref);
53 # set more cross referneces for this data object
54 my $xref1 = ...
55 my $xref2 = ...
56 my $xref3 = ...
57 $data->xrefs ($xref1, $xref2, $xref3);
58 # or:
59 $data->xrefs ([$xref1, $xref2, $xref3]);
60  
61 # add cross references
62 my $xref4 = ...
63 my $xref5 = ...
64 $data->add_xrefs ($xref4, $xref5);
65 # or:
66 $data->add_xrefs ([$xref4, $xref5]);
67  
68 # finally, get cross references back
69 foreach $xref (@{ $data->xrefs }) {
70 print $xref->toString;
71 }
72  
73 # get a formatted string representation of this data object
74 print $data->toString;
75
76 # retrieve an XML::LibXML::Element representing the data object
77 $xml = $data->toXML();
78 print $xml->toString;
79
80 =head1 DESCRIPTION
81  
82 An object representing a Moby object, a Moby base data type for all
83 other Moby data types.
84  
85 =head1 AUTHORS
86  
87 Edward Kawas (edward.kawas [at] gmail [dot] com)
88 Martin Senger (martin.senger [at] gmail [dot] com)
89  
90 =cut
91  
92 #-----------------------------------------------------------------
93 # A list of allowed attribute names. See MOSES::MOBY::Base for details.
94 #-----------------------------------------------------------------
95  
96 =head1 ACCESSIBLE ATTRIBUTES
97  
98 Details are in L<MOSES::MOBY::Base>. Here just a list of them:
99  
100 =over
101  
102 =item B<namespace>
103  
104 =item B<id>
105  
106 =item B<name>
107  
108 An article name for this datatype. Note that the article name depends
109 on the context where this object is used.
110  
111 =item B<provision>
112  
113 A I<provision information>. A scalar of type
114 C<MOSES::MOBY::Data::ProvisionInformation>.
115  
116 =item B<xrefs>
117  
118 Cross-references. Can be a scalar (of type
119 C<MOSES::MOBY::Data::Xref>, an array, or a array
120 reference.
121  
122 =item B<primitive>
123  
124 A boolean property indicating if this data type is a primitive Moby
125 type or not.
126  
127 =back
128  
129 =cut
130  
131 {
132 my %_allowed =
133 (
134 id => undef,
135 namespace => undef,
136 mobyname => undef,
137 provision => {type => 'MOSES::MOBY::Data::ProvisionInformation'},
138 xrefs => {type => 'MOSES::MOBY::Data::Xref', is_array => 1},
139 primitive => {type => MOSES::MOBY::Base->BOOLEAN},
140 original_memberName => undef,
141 );
142  
143 sub _accessible {
144 my ($self, $attr) = @_;
145 exists $_allowed{$attr} or $self->SUPER::_accessible ($attr);
146 }
147 sub _attr_prop {
148 my ($self, $attr_name, $prop_name) = @_;
149 my $attr = $_allowed {$attr_name};
150 return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
151 return $self->SUPER::_attr_prop ($attr_name, $prop_name);
152 }
153 }
154  
155 #-----------------------------------------------------------------
156  
157 =head1 SUBROUTINES
158  
159 =cut
160  
161 #-----------------------------------------------------------------
162 # init
163 #-----------------------------------------------------------------
164 sub init {
165 my ($self) = shift;
166 $self->SUPER::init();
167 $self->id ('');
168 $self->namespace ('');
169 $self->xrefs ([]);
170 $self->primitive ('no');
171 }
172  
173 #-----------------------------------------------------------------
174 # toXML
175 #-----------------------------------------------------------------
176  
177 =head2 toXML
178  
179 Return an XML::LibXML::Element representing this data object. An
180 optional attribute is an articleName that should be given to this
181 object (in its XML representation).
182  
183 =cut
184  
185 sub toXML {
186 my ($self, $articleName) = @_;
187 $self->increaseXMLCounter;
188  
189 my $moby_name = $self->mobyname;
190 unless ($moby_name) { # backup plan
191 $moby_name = ref $self;
192 $moby_name =~ s/.*:://;
193 }
194  
195 my $root = XML::LibXML::Element->new ($moby_name);
196 $root->setNamespace (MOBY_XML_NS, MOBY_XML_NS_PREFIX);
197  
198 $root->setAttributeNS (MOBY_XML_NS, OBJ_ID, $self->id);
199 $root->setAttributeNS (MOBY_XML_NS, OBJ_NAMESPACE, $self->namespace);
200 $root->setAttributeNS (MOBY_XML_NS, ARTICLENAME, $articleName)
201 if $articleName;
202  
203 $root->appendChild ($self->provision->toXML)
204 if $self->provision;
205
206 # cross-references
207 if ($self->xrefs and @{ $self->xrefs } > 0) {
208 my $crElem = $root->addNewChild (MOBY_XML_NS, CROSSREFERENCE);
209 $crElem->setNamespace (MOBY_XML_NS, MOBY_XML_NS_PREFIX);
210 foreach my $xref (@{ $self->xrefs }) {
211 $crElem->appendChild ($xref->toXML);
212 }
213 }
214  
215 # iterate over all members
216 my ($key, $value);
217 while (($key, $value) = each %$self) {
218 if (ref ($value) eq 'ARRAY') {
219 foreach my $elem (@{ $value }) {
220 $self->_add_XML_element ($key, $elem, $root);
221 }
222 } else {
223 $self->_add_XML_element ($key, $value, $root);
224 }
225 }
226  
227 # return it (TBD: cleaning namespaces happens several times...)
228 return $self->closeXML ($root);
229 }
230  
231 # create an XML element, add it to the $root; ignore some priviledged
232 # names (such as 'xrefs'), and most (except 'value' for primitive
233 # types) of the names whose values are not object references
234  
235 sub _add_XML_element {
236 my ($self, $name, $value, $root) = @_;
237 my (%special_names) =
238 (xrefs => 1,
239 provision => 1,
240 );
241 if ($name eq 'value' and $self->primitive) {
242 if (defined $value) {
243 if ($self->{cdata}) { # don't use: $self->cdata because not everybody has it
244 $root->appendChild (XML::LibXML::CDATASection->new ($self->_express_value ($value)));
245 } else {
246 $root->appendText ($self->_express_value ($value));
247 }
248 }
249 return;
250 }
251 if (ref ($value)) {
252 if ($special_names{$name}) {
253 return;
254 }
255 my $xmlElem = $value->toXML ($name);
256 $root->appendChild ($xmlElem) if $xmlElem;
257 }
258 }
259  
260 # return the same value as given (but others may override it - eg,
261 # Boolean changes here 1 to 'true'
262  
263 sub _express_value {
264 shift;
265 shift;
266 }
267  
268  
269 1;
270 __END__