File Coverage

blib/lib/WebService/Cmis/Property.pm
Criterion Covered Total %
statement 12 109 11.0
branch 0 22 0.0
condition 0 8 0.0
subroutine 4 30 13.3
pod 26 26 100.0
total 42 195 21.5


line stmt bran cond sub pod time code
1             package WebService::Cmis::Property;
2              
3             =head1 NAME
4              
5             WebService::Cmis::Property
6              
7             Representation of a property of a cmis object
8              
9             =head1 SYNOPSIS
10              
11             =head1 DESCRIPTION
12              
13             =cut
14              
15 1     1   9999 use strict;
  1         4  
  1         51  
16 1     1   7 use warnings;
  1         2  
  1         39  
17 1     1   5 use WebService::Cmis qw(:namespaces);
  1         2  
  1         255  
18 1     1   7 use Error qw(:try);
  1         3  
  1         8  
19              
20             =head1 METHODS
21              
22             =over 4
23              
24             =item new(%params)
25              
26             =cut
27              
28             sub new {
29 0     0 1   my $class = shift;
30 0           my %params = @_;
31              
32             #print STDERR "params: ".join(', ', map($_."=".$params{$_}, keys %params))."\n";
33              
34             # shortcut id -> propertyDefinitionId
35 0 0 0       if (defined $params{id} && !defined $params{propertyDefinitionId}) {
36 0           $params{propertyDefinitionId} = delete $params{id};
37             }
38              
39 0           my $this = bless( \%params, $class);
40 0 0         throw Error::Simple("no id for property") unless defined $this->{propertyDefinitionId};
41              
42 0           return $this;
43             }
44              
45             =item toString
46              
47             string representation of this property
48              
49             =cut
50              
51             sub toString {
52 0     0 1   my $this = shift;
53              
54 0   0       my $val = $this->{value} || '';
55 0 0         $val = join(', ', @$val) if ref($val);
56 0           return $this->getId."=".($val);
57             }
58              
59             =item toXml($xmlDoc)
60              
61             returns an XML::LibXml::Node representing this property.
62             $xmlDoc is the document to allocate the xml node for.
63              
64             =cut
65              
66             sub toXml {
67 0     0 1   my ($this, $xmlDoc) = @_;
68              
69 0           my $propElement = $xmlDoc->createElementNS(CMIS_NS, $this->getNodeName);
70              
71 0           foreach my $key (sort keys %$this) {
72 0 0         next if $key eq 'value';
73 0           my $val = $this->{$key};
74 0           $propElement->setAttribute($key, $val);
75             }
76              
77             # add cmis:value
78 0           my $value = $this->getValue;
79 0 0         if (ref($value) eq 'ARRAY') {
80 0           foreach my $val (@$value) {
81 0           my $valElement = $propElement->addNewChild(CMIS_NS, 'cmis:value');
82 0           $valElement->addChild($xmlDoc->createTextNode($this->unparse($val)));
83             }
84             } else {
85 0           my $valElement = $propElement->addNewChild(CMIS_NS, 'cmis:value');
86 0           $valElement->addChild($xmlDoc->createTextNode($this->unparse($value)));
87             }
88              
89 0           return $propElement;
90             }
91              
92             =item getId
93              
94             getter for propertyDefinitionId
95              
96             =cut
97              
98             sub getId {
99 0     0 1   return $_[0]->{propertyDefinitionId};
100             }
101              
102             =item getNodeName
103              
104             returns the xml node name for this property
105              
106             =cut
107              
108             sub getNodeName {
109 0     0 1   my $this = shift;
110              
111 0           my $class = ref($this);
112 0           $class =~ s/^.*:://;
113              
114 0           return "cmis:property".$class;
115             }
116              
117             =item getValue
118              
119             getter for property value
120              
121             =cut
122              
123             sub getValue {
124 0     0 1   return $_[0]->{value};
125             }
126              
127             =item setValue($cmisValue) -> $perlValue
128              
129             setter for property value, converting it to the specific perl representation
130              
131             =cut
132              
133             sub setValue {
134 0     0 1   my ($this, $value) = @_;
135 0           return $this->{value} = $this->parse($value);
136             }
137              
138             =item parse($cmisValue) -> $perlValue
139              
140             parses a cmis value into its perl representation.
141              
142             =cut
143              
144             sub parse {
145             # my ($this, $value) = @_;
146              
147             # no conversion by default
148 0     0 1   return $_[1];
149             }
150              
151             =item unparse($perlValue) $cmisValue
152              
153             converts a perl representation back to a format understood by cmis
154              
155             =cut
156              
157             sub unparse {
158 0     0 1   my ($this, $value) = @_;
159              
160 0 0 0       $value = $this->{value} if ref($this) && ! defined $value;
161              
162 0 0         return 'none' unless defined $value;
163 0           return $value;
164             }
165              
166             =item load
167              
168             static helper utility to create a property object loading it from its xml
169             representation
170              
171             =cut
172              
173             sub load {
174 0     0 1   my $xmlDoc = shift;
175              
176 0           my $nodeName = $xmlDoc->nodeName;
177 0           $nodeName =~ s/^cmis:property//g;
178 0           my $class = "WebService::Cmis::Property::".$nodeName;
179              
180             # untaint
181 0           $class =~ /^(.*)/s;
182 0           $class = $1;
183              
184 0           eval "use $class";
185 0 0         if ($@) {
186 0           throw Error::Simple($@);
187             }
188              
189             # get attributes
190 0           my %attrs = ();
191 0           foreach my $attr (@{$xmlDoc->attributes->nodes}) {
  0            
192             #print STDERR $attr->nodeName."=".$attr->value."\n";
193 0           my $key = $attr->nodeName;
194 0           my $val = $attr->value;
195 0           $attrs{$key} = $val;
196 0 0         $attrs{propertyDefinitionId} = $val if $key eq 'id'; # alias
197             }
198              
199 0           my $property = $class->new(%attrs);
200              
201             # get value
202 0           my $value;
203 0           my @childNodes = $xmlDoc->nonBlankChildNodes;
204 0 0         if (scalar(@childNodes) > 1) {
205 0           push @{$value}, $_->string_value foreach @childNodes;
  0            
206             } else {
207 0           my $node = shift @childNodes;
208 0 0         if ($node) {
209 0           $value = $node->string_value;
210             }
211             }
212              
213 0           $property->setValue($value);
214              
215 0           return $property;
216             }
217              
218             =item newBoolean(%params) -> $propertyBoolean
219              
220             static helper utility to create a property boolean.
221              
222             =cut
223              
224             sub newBoolean {
225 0     0 1   require WebService::Cmis::Property::Boolean;
226 0           return new WebService::Cmis::Property::Boolean(@_);
227             }
228              
229             =item newDateTime(%params) -> $propertyDateTime
230              
231             static helper utility to create a property string.
232              
233             =cut
234              
235             sub newDateTime {
236 0     0 1   require WebService::Cmis::Property::DateTime;
237 0           return new WebService::Cmis::Property::DateTime(@_);
238             }
239              
240             =item newDecimal(%params) -> $propertyDecimal
241              
242             static helper utility to create a property string.
243              
244             =cut
245              
246             sub newDecimal {
247 0     0 1   require WebService::Cmis::Property::Decimal;
248 0           return new WebService::Cmis::Property::Decimal(@_);
249             }
250              
251             =item newId(%params) -> $propertyId
252              
253             static helper utility to create a property string.
254              
255             =cut
256              
257             sub newId {
258 0     0 1   require WebService::Cmis::Property::Id;
259 0           return new WebService::Cmis::Property::Id(@_);
260             }
261              
262             =item newInteger(%params) -> $propertyInteger
263              
264             static helper utility to create a property string.
265              
266             =cut
267              
268             sub newInteger {
269 0     0 1   require WebService::Cmis::Property::Integer;
270 0           return new WebService::Cmis::Property::Integer(@_);
271             }
272              
273             =item newString(%params) -> $propertyString
274              
275             static helper utility to create a property string.
276              
277             =cut
278              
279             sub newString {
280 0     0 1   require WebService::Cmis::Property::String;
281 0           return new WebService::Cmis::Property::String(@_);
282             }
283              
284             =item parseDateTime($string) -> $epochSeconds
285              
286             helper utility to parse an iso date into epoch seconds
287              
288             =cut
289              
290             sub parseDateTime {
291 0     0 1   require WebService::Cmis::Property::DateTime;
292 0           return WebService::Cmis::Property::DateTime->parse(@_);
293             }
294              
295             =item formatDateTime($epochSeconds) -> $isoDate
296              
297             helper utility to format epoch seconds to iso date
298              
299             =cut
300              
301             sub formatDateTime {
302 0     0 1   require WebService::Cmis::Property::DateTime;
303 0           return WebService::Cmis::Property::DateTime->unparse(@_);
304             }
305              
306             =item parseBoolean($string) -> $boolean
307              
308             helper utility to parse a boolean
309              
310             =cut
311              
312             sub parseBoolean {
313 0     0 1   require WebService::Cmis::Property::Boolean;
314 0           return WebService::Cmis::Property::Boolean->parse(@_);
315             }
316              
317             =item formatBoolean($boolean) -> $string
318              
319             helper utility to format a boolean
320              
321             =cut
322              
323             sub formatBoolean {
324 0     0 1   require WebService::Cmis::Property::Boolean;
325 0           return WebService::Cmis::Property::Boolean->unparse(@_);
326             }
327              
328             =item parseId($string) -> $boolean
329              
330             helper utility to parse a cmis:id
331              
332             =cut
333              
334             sub parseId {
335 0     0 1   require WebService::Cmis::Property::Id;
336 0           return WebService::Cmis::Property::Id->parse(@_);
337             }
338              
339             =item formatId($id) -> $string
340              
341             helper utility to format an id
342              
343             =cut
344              
345             sub formatId {
346 0     0 1   require WebService::Cmis::Property::Id;
347 0           return WebService::Cmis::Property::Id->unparse(@_);
348             }
349              
350             =item parseInteger($string) -> $integer
351              
352             helper utility to parse an integer
353              
354             =cut
355              
356             sub parseInteger {
357 0     0 1   require WebService::Cmis::Property::Integer;
358 0           return WebService::Cmis::Property::Integer->parse(@_);
359             }
360              
361             =item formatInteger($int) -> $string
362              
363             helper utility to format an integer
364              
365             =cut
366              
367             sub formatInteger {
368 0     0 1   require WebService::Cmis::Property::Integer;
369 0           return WebService::Cmis::Property::Integer->unparse(@_);
370             }
371              
372             =item parseDecimal($string) -> $decimal
373              
374             helper utility to parse a decimal
375              
376             =cut
377              
378             sub parseDecimal {
379 0     0 1   require WebService::Cmis::Property::Decimal;
380 0           return WebService::Cmis::Property::Decimal->parse(@_);
381             }
382              
383             =item formatDecimal($decimal) -> $string
384              
385             helper utility to format a decimal
386              
387             =cut
388              
389             sub formatDecimal {
390 0     0 1   require WebService::Cmis::Property::Decimal;
391 0           return WebService::Cmis::Property::Decimal->unparse(@_);
392             }
393              
394             =back
395              
396             =head1 COPYRIGHT AND LICENSE
397              
398             Copyright 2012-2013 Michael Daum
399              
400             This module is free software; you can redistribute it and/or modify it under
401             the same terms as Perl itself. See F.
402              
403             =cut
404              
405             1;