| 1 | package RDF::RDFa::Template::SimpleQuery; | ||
| 2 | |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | RDF::RDFa::Template::SimpleQuery - Module to run a RAT Template Query very easily | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | This module intends to be simple interface to do most of the tasks | ||
| 13 | that most users of RDF::RDFa::Template would want to do. | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | my ($f) = File::Util->new(); | ||
| 18 | my ($rat) = $f->load_file('dbpedia-comment/input.xhtml'); | ||
| 19 | my $query = RDF::RDFa::Template::SimpleQuery->new(rat => $rat, | ||
| 20 | filename => 'dbpedia-comment/input.ttl', | ||
| 21 | syntax => 'turtle'); | ||
| 22 | $query->execute; | ||
| 23 | my $output = $query->rdfa_xhtml; | ||
| 24 | $output->toStringC14N; | ||
| 25 | |||
| 26 | or | ||
| 27 | |||
| 28 | my ($f) = File::Util->new(); | ||
| 29 | my ($rat) = $f->load_file('dbpedia-comment/input.xhtml'); | ||
| 30 | my ($rdf) = $f->load_file('dbpedia-comment/input.ttl'); | ||
| 31 | my $rdfparser = RDF::Trine::Parser->new( 'turtle' ); | ||
| 32 | my $storage = RDF::Trine::Store::Memory->temporary_store; | ||
| 33 | my $model = RDF::Trine::Model->new($storage); | ||
| 34 | $rdfparser->parse_into_model ( "http://example.org/", $rdf, $model ); | ||
| 35 | my $query = RDF::RDFa::Template::SimpleQuery->new(rat => $rat, model => $model); | ||
| 36 | $query->execute; | ||
| 37 | my $output = $query->rdfa_xhtml; | ||
| 38 | $output->toStringC14N; | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | 2 | ▶ | use RDF::RDFa::Template::Unit; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | 2 | ▶ | use RDF::RDFa::Template::Document; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | 2 | ▶ | use RDF::RDFa::Template::SAXFilter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 47 | 2 | ▶ | use RDF::Trine::Model; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 48 | 2 | ▶ | use RDF::Trine::Statement; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 49 | 2 | ▶ | use RDF::Trine::Node::Variable; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 50 | 2 | ▶ | use RDF::Trine::Pattern; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 51 | 0 | ▶ | use RDF::RDFa::Parser; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 52 | use RDF::Query::Client; | ||
| 53 | use XML::LibXML; | ||
| 54 | use XML::LibXML::SAX::Parser; | ||
| 55 | use XML::LibXML::SAX::Builder; | ||
| 56 | use File::Util; | ||
| 57 | |||
| 58 | use Data::Dumper; | ||
| 59 | use Carp; | ||
| 60 | |||
| 61 | =head2 new | ||
| 62 | |||
| 63 | The constructor. This takes a number of named arguments. | ||
| 64 | |||
| 65 | =over | ||
| 66 | |||
| 67 | =item C<rat> | ||
| 68 | |||
| 69 | The RDFa Template XHTML text to be parsed. | ||
| 70 | |||
| 71 | =item C<baseuri> | ||
| 72 | |||
| 73 | The base URI to resolve any relative URIs. This is optional, | ||
| 74 | "http://example.org/" is used if this is not given. | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | The following arguments are all used to give the module the data it | ||
| 79 | shall query to fill the variables. | ||
| 80 | |||
| 81 | =over | ||
| 82 | |||
| 83 | =item C<model> | ||
| 84 | |||
| 85 | May be an L<RDF::Trine::Model> object with the data to be queried. If | ||
| 86 | given, it takes presedence over any other arguments. | ||
| 87 | |||
| 88 | =item C<filename> | ||
| 89 | |||
| 90 | The full path of a file containing some RDF to be queried. | ||
| 91 | |||
| 92 | =item C<rdf> | ||
| 93 | |||
| 94 | A string with some RDF to be queried. | ||
| 95 | |||
| 96 | |||
| 97 | =item C<syntax> | ||
| 98 | |||
| 99 | A string giving the syntax to be parsed. Syntaxes supported by | ||
| 100 | L<RDF::Trine::Parser> are supported, e.g. C<turtle>, C<rdfxml>, | ||
| 101 | etc. This parameter is required if C<filename> or C<rdf> is used. | ||
| 102 | |||
| 103 | =back | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | |||
| 108 | sub new { | ||
| 109 | my ($class, %args) = @_; | ||
| 110 | my $self; | ||
| 111 | $self->{RAT} = $args{rat}; | ||
| 112 | if ($args{model}) { | ||
| 113 | if ($args{model}->isa('RDF::Trine::Model')) { | ||
| 114 | # We got a model | ||
| 115 | $self->{MODEL} = $args{model}; | ||
| 116 | bless ($self, $class); | ||
| 117 | return $self; | ||
| 118 | } else { | ||
| 119 | croak 'model argument is not a RDF::Trine::Model'; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | if ($args{filename}) { | ||
| 124 | if (-f $args{filename}) { | ||
| 125 | # We have a file | ||
| 126 | my($f) = File::Util->new(); | ||
| 127 | my ($rdf) = $f->load_file($args{filename}); | ||
| 128 | $self->{MODEL} = _init_model($args{syntax}, $rdf, $args{baseuri}); | ||
| 129 | } else { | ||
| 130 | croak "Cannot open $args{filename}"; | ||
| 131 | } | ||
| 132 | } elsif ($args{rdf}) { | ||
| 133 | $self->{MODEL} = _init_model($args{syntax}, $args{rdf}, $args{baseuri}); | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | bless ($self, $class); | ||
| 140 | return $self; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub _init_model { | ||
| 144 | my ($syntax, $rdf, $baseuri) = @_; | ||
| 145 | my $rdfparser = RDF::Trine::Parser->new( $syntax ); | ||
| 146 | my $storage = RDF::Trine::Store::Memory->temporary_store; | ||
| 147 | my $model = RDF::Trine::Model->new($storage); | ||
| 148 | $baseuri ||= "http://example.org/"; | ||
| 149 | $rdfparser->parse_into_model ($baseuri, $rdf, $model); | ||
| 150 | return $model; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | =head2 execute | ||
| 156 | |||
| 157 | Now, really run it. This does the heavylifting, and therefore you | ||
| 158 | should run this method manually in your application to control when it | ||
| 159 | does happen. Returns the number of queries generated. | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | sub execute { | ||
| 164 | my $self = shift; | ||
| 165 | my $parser = RDF::RDFa::Parser->new($self->{RAT}, 'http://example.org/foo/', | ||
| 166 | { | ||
| 167 | use_rtnlx => 1, | ||
| 168 | graph => 1, | ||
| 169 | graph_type => 'about', | ||
| 170 | graph_attr => '{http://example.org/graph#}graph', | ||
| 171 | }); | ||
| 172 | $parser->consume; | ||
| 173 | $self->{DOC} = RDF::RDFa::Template::Document->new($parser); | ||
| 174 | $self->{DOC}->extract; | ||
| 175 | |||
| 176 | my $return = 0; | ||
| 177 | # die Dumper($doc->units); | ||
| 178 | foreach my $unit ($self->{DOC}->units) { | ||
| 179 | my $query = 'SELECT * WHERE { ' . $unit->pattern->as_sparql . ' }'; | ||
| 180 | my $model = $self->{MODEL}; | ||
| 181 | my $client; | ||
| 182 | if ($unit->endpoint) { | ||
| 183 | $client = RDF::Query::Client->new($query); | ||
| 184 | $model = $unit->endpoint; | ||
| 185 | } elsif ($self->{MODEL} && ($self->{MODEL}->isa('RDF::Trine::Model'))) { | ||
| 186 | $client = RDF::Query->new($query); | ||
| 187 | } else { | ||
| 188 | croak "Need either an endpoint or an RDF::Trine::Model"; | ||
| 189 | } | ||
| 190 | my $iterator = $client->execute( $model ); | ||
| 191 | |||
| 192 | $unit->results($iterator); | ||
| 193 | $return++; | ||
| 194 | } | ||
| 195 | return $return; | ||
| 196 | } | ||
| 197 | |||
| 198 | =head2 rdfa_xhtml | ||
| 199 | |||
| 200 | Once the query has been executed (see above), this method will return | ||
| 201 | a L<XML::LibXML::Document> containing the RDFa document with the | ||
| 202 | variables inserted. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | |||
| 207 | sub rdfa_xhtml { | ||
| 208 | my $self = shift; | ||
| 209 | my $builder = XML::LibXML::SAX::Builder->new(); | ||
| 210 | |||
| 211 | my $sax = RDF::RDFa::Template::SAXFilter->new(Handler => $builder, Doc => $self->{DOC}); | ||
| 212 | my $generator = XML::LibXML::SAX::Parser->new(Handler => $sax); | ||
| 213 | |||
| 214 | my $orig = $self->{DOC}->dom; | ||
| 215 | |||
| 216 | $generator->generate($orig); | ||
| 217 | |||
| 218 | # TODO: # Get the doctypes and stuff | ||
| 219 | # my $xpc = XML::LibXML::XPathContext->new($orig); | ||
| 220 | # $xpc->registerNs('rat', $self->{DOC}->{RATURI}); | ||
| 221 | # $xpc->registerNs('xhtml', 'http://www.w3.org/1999/xhtml'); | ||
| 222 | # my ($system_id) = $xpc->findnodes('/xhtml:html/@rat:doctype-system') || 'http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd'; | ||
| 223 | # my ($public_id) = $xpc->findnodes('/xhtml:html/@rat:doctype-public') || '-//W3C//DTD XHTML+RDFa 1.0//EN'; | ||
| 224 | # my $dtd = XML::LibXML::Dtd->new($public_id, $system_id); # Downloads the DTD, it seems | ||
| 225 | |||
| 226 | |||
| 227 | my $output = $builder->result; | ||
| 228 | # $output->setExternalSubset($dtd); | ||
| 229 | |||
| 230 | return $output; | ||
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | =head1 AUTHOR | ||
| 237 | |||
| 238 | Kjetil Kjernsmo, C<< <kjetilk at cpan.org> >> | ||
| 239 | |||
| 240 | |||
| 241 | =head1 COPYRIGHT & LICENSE | ||
| 242 | |||
| 243 | Copyright 2010 Kjetil Kjernsmo. | ||
| 244 | |||
| 245 | This program is free software; you can redistribute it and/or modify it | ||
| 246 | under the same terms as Perl itself. | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | 1; | ||