XUL/RDF.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 209 CC 23 SCAR 29.2
CC
23
Cov
0%
new
56.0
as_xml
20.0
_rdf2xml
20.0
CRAP 18.5
1 package POE::XUL::RDF;
2 # $Id$
3 # Copyright Philip Gwyn 2008-2010. All rights reserved.
4  
5 use strict;
6 use warnings;
7  
8 use URI;
9 use HTML::Entities qw( encode_entities_numeric );
10  
11 our $VERSION = '0.0600';
12  
13 ##############################################################
14 sub new
15 {
16 my( $package, %init ) = @_;
17 my $self = bless { data=>[] }, $package;
18  
19 # XXX: baseref from current server
20 $self->baseref( $init{baseref} ) if $init{baseref};
21 $init{baserdf} ||= 'rdf';
22 $self->baserdf( $init{baserdf} ) if $init{baserdf};
23 $self->NS( $init{NS} ) if $init{NS};
24 $self->data( $init{data} ) if $init{data};
25 if( exists $init{dataref} ) {
26 $self->dataref( $init{dataref} );
27 }
28 elsif( ($self->{data}[0]||'') eq 'RDF:about' ) {
29 $self->dataref( $self->{data}[1] );
30 }
31  
32 return $self;
33 }
34  
35 ##############################################################
36 sub fragment
37 {
38 my( $self, $name ) = @_;
39 my $ret = URI->new( $self->{baserdf} );
40 $ret->fragment( $name );
41 return $ret;
42 }
43  
44 sub rdf_fragment
45 {
46 my( $self, $name ) = @_;
47 my $ret = $self->fragment( $name );
48 return "rdf:$ret";
49 }
50  
51 ##############################################################
52 sub data
53 {
54 my( $self, $data ) = @_;
55 return $self->{data} if 1==@_;
56 $self->{data} = $data;
57 }
58  
59 sub NS
60 {
61 my( $self, $NS ) = @_;
62 return $self->{NS} if 1==@_;
63 $self->{NS} = $NS;
64 }
65  
66 sub baseref
67 {
68 my( $self, $baseref ) = @_;
69 return $self->{baseref} if 1==@_;
70 $self->{baseref} = URI->new( $baseref );
71 }
72  
73 sub baserdf
74 {
75 my( $self, $baserdf ) = @_;
76 return $self->{baserdf} if 1==@_;
77 if( $self->{baseref} ) {
78 $self->{baserdf} = URI->new_abs( $baserdf, $self->{baseref} );
79 }
80 else {
81 $self->{baserdf} = URI->new( $baserdf );
82 }
83 $self->{baserdf}->fragment( '' );
84 return $self->{baserdf};
85 }
86  
87 sub dataref
88 {
89 my( $self, $dataref ) = @_;
90 return $self->{dataref} if 1==@_;
91 if( $self->{baseref} ) {
92 $self->{dataref} = URI->new_abs( $dataref, $self->{baseref} );
93 }
94 else {
95 $self->{dataref} = URI->new( $dataref );
96 }
97 return $self->{dataref};
98 }
99  
100  
101 ##############################################################
102 sub mime_type
103 {
104 return 'application/rdf+xml';
105 }
106  
107 ##############################################################
108 ## Linear search for the true row that was selected.
109 sub index_of
110 {
111 my( $self, $col, $primary ) = @_;
112 my $offset=0;
113 for( my $q=0; $q <= $#{$self->{data}}; $q++ ) {
114 if( not ref $self->{data}[$q] ) {
115 $offset+=2;
116 $q+=2;
117 }
118 next unless exists $self->{data}[$q]{$col} and
119 $self->{data}[$q]{$col} eq $primary;
120 return $q-$offset;
121 }
122 return -1;
123 }
124  
125 ##############################################################
126 sub as_xml
127 {
128 my( $self, $data ) = @_;
129  
130 $data ||= $self->{data};
131 my @Seq = ( 'RDF:Seq', {}, [] );
132  
133 my @RDF = [ 'RDF:RDF', { 'xmlns:RDF' => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
134 "xmlns:$self->{NS}" => $self->{baserdf}
135 },
136 [ \@Seq ]
137 ];
138  
139 if( $data->[0] eq 'RDF:about' ) {
140 $Seq[1]{about} = URI->new_abs( $data->[1], $self->{baseref} );
141 $data = [ @{ $data }[ 2..$#$data ] ];
142 }
143  
144 foreach my $row ( @$data ) {
145 push @{ $Seq[2] }, [ 'RDF:li', {},
146 [[ 'RDF:Description', {}, [] ]]
147 ];
148 my $li = $Seq[2][-1][2][0][2];
149 my $att = $Seq[2][-1][2][0][1];
150 while( my( $k, $v ) = each %$row ) {
151 if( $k =~ /^RDF:(\w+)$/ ) {
152 my $a = $1;
153 if( $a eq 'about' ) {
154 $att->{$a} = URI->new_abs( $v, $self->{baseref} );
155 }
156 else {
157 $att->{$a} = $v;
158 }
159 }
160 else {
161 push @$li, [ "$self->{NS}:$k", {}, [ $v ] ];
162 }
163 }
164 }
165 # use Data::Dumper;
166 # warn Dumper \@RDF;
167  
168 return $self->_rdf2xml( [@RDF], '' );
169 }
170  
171 ##############################################################
172 sub _rdf2xml
173 {
174 my( $self, $rdf, $prefix ) = @_;
175  
176 my @ret;
177 foreach my $el ( @$rdf ) {
178 if( ref $el ) {
179 push @ret, "$prefix<$el->[0]".$self->_att2xml( $el->[1] );
180 if( $el->[2] and @{ $el->[2] } ) {
181 $ret[-1] .= ">";
182 if( ref $el->[2][0] ) {
183 push @ret, $self->_rdf2xml( $el->[2], "$prefix " );
184 push @ret, "$prefix</$el->[0]>";
185 }
186 else {
187 $ret[-1] .= "$el->[2][0]</$el->[0]>";
188 }
189 }
190 else {
191 $ret[-1] .= "/>";
192 }
193 }
194 else {
195 push @ret, $el;
196 }
197 }
198  
199 return join "\n", @ret;
200 }
201  
202 sub _att2xml
203 {
204 my( $self, $att ) = @_;
205 return '' unless keys %$att;
206 return join ' ', '', map {
207 join '',
208 encode_entities_numeric( $_, "\x00-\x1f<>&\'\x80-\xff" ),
209 '="',
210 encode_entities_numeric( $att->{$_}, "\x00-\x1f<>&\'\x80-\xff" ),
211 '"'
212 } keys %$att;
213 }
214  
215 1;
216  
217 __END__