| 1 | package MOBYXSLT; | ||
| 2 | |||
| 3 | my $TMP_DIR = '/tmp/';#Where your temporary files will be written | ||
| 4 | my $XSLTPROC = '/usr/bin/xsltproc';#Where your xsltproc binary is located | ||
| 5 | my $XSL_SHEET = 'xsl/parseMobyMessage.xsl';#Where your xsltproc style-sheet is located | ||
| 6 | |||
| 7 | use vars qw /$VERSION/; | ||
| 8 | $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/; | ||
| 9 | |||
| 10 | #$Id: MOBYXSLT.pm,v 1.3 2008/09/02 13:14:18 kawas Exp $ | ||
| 11 | |||
| 12 | =pod | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | MOBYXSLT - CommonSubs using XSLT | ||
| 17 | |||
| 18 | =head1 WHY | ||
| 19 | |||
| 20 | Because huge XML message parsing with XML::Dom take too much time. | ||
| 21 | xsltproc is a binary very very efficient to parse huge files. | ||
| 22 | |||
| 23 | |||
| 24 | =head1 TO BE EDITED | ||
| 25 | |||
| 26 | Globals variables are defined in this package: | ||
| 27 | |||
| 28 | my $TMP_DIR = '/tmp/'; #Where your temporary files will be written | ||
| 29 | my $XSLTPROC = '/usr/bin/xsltproc'; #Where your xsltproc binary is located | ||
| 30 | my $XSL_SHEET = './parseMobyMessage.xsl'; #Where your xsltproc style-sheet is located | ||
| 31 | |||
| 32 | |||
| 33 | =head1 SYNOPSIS | ||
| 34 | |||
| 35 | sub MonWebservice | ||
| 36 | { | ||
| 37 | |||
| 38 | my ($caller, $message) = (@_); | ||
| 39 | |||
| 40 | my $moby_response; | ||
| 41 | |||
| 42 | my $service_name = 'MonWebservice'; | ||
| 43 | |||
| 44 | #Message Parsing | ||
| 45 | my ($service_notes,$ra_queries) = MOBYXSLT::getInputs($message); #Message Parsing | ||
| 46 | |||
| 47 | foreach my $query (@{$ra_queries}) | ||
| 48 | { | ||
| 49 | my $query_id = MOBYXSLT::getInputID($query);#Retrieve Query ID | ||
| 50 | my @a_input_articles = MOBYXSLT::getArticles($query);#Retrieve articles | ||
| 51 | |||
| 52 | my ($fasta_sequences, $fasta_namespace, $fasta_id) = ('','',''); | ||
| 53 | |||
| 54 | foreach my $input_article (@a_input_articles) | ||
| 55 | { | ||
| 56 | my ($article_name, $article) = @{$input_article}; | ||
| 57 | |||
| 58 | if (MOBYXSLT::isSimpleArticle($article)) | ||
| 59 | { | ||
| 60 | my $object_type = MOBYXSLT::getObjectType($article); | ||
| 61 | |||
| 62 | if (IsTheCorrectType($object_type)) | ||
| 63 | { | ||
| 64 | $fasta_sequences = MOBYXSLT::getObjectContent($article); | ||
| 65 | $fasta_namespace = MOBYXSLT::getObjectNamespace($article); | ||
| 66 | $fasta_id = MOBYXSLT::getObjectId($article); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | elsif (MOBYXSLT::isCollectionArticle($article)) | ||
| 70 | { | ||
| 71 | } | ||
| 72 | |||
| 73 | elsif (MOBYXSLT::isSecondaryArticle($article)) | ||
| 74 | { | ||
| 75 | my ($param_name,$param_value) = MOBYXSLT::getParameter($article);#Retrieve parameters | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | ###### | ||
| 80 | #What you want to do with your data | ||
| 81 | ###### | ||
| 82 | |||
| 83 | |||
| 84 | my $cmd ="..."; | ||
| 85 | |||
| 86 | system("$cmd"); | ||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | ######### | ||
| 92 | #Send result | ||
| 93 | ######### | ||
| 94 | |||
| 95 | $moby_response .= MOBYXSLT::simpleResponse("<$output_object_type1>$out_data</$output_object_type1>", $output_article_name1, $query_id); | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | return SOAP::Data->type( | ||
| 100 | 'base64' => (MOBYXSLT::responseHeader(-authority => $auth_uri, -note => "Documentation about $service_name at $url_doc")) | ||
| 101 | . $moby_response | ||
| 102 | . MOBYXSLT::responseFooter()); | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | =head1 GLOBALS | ||
| 107 | |||
| 108 | my $TMP_DIR = '/tmp/'; #Where your temporary files will be written | ||
| 109 | my $XSLTPROC = '/usr/bin/xsltproc'; #Where your xsltproc binary is located | ||
| 110 | my $XSL_SHEET = './parseMobyMessage.xsl'; #Where your xsltproc style-sheet is located | ||
| 111 | |||
| 112 | |||
| 113 | =head1 DESCRIPTION | ||
| 114 | |||
| 115 | Note: many functions have same names as those from MOBY::CommonSubs | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | |||
| 120 | use strict; | ||
| 121 | use Carp; | ||
| 122 | |||
| 123 | =head2 function getInputs | ||
| 124 | |||
| 125 | Title : getInputs | ||
| 126 | Usage : my ($servicenotes, $ra_queries) = getInputs($moby_message) | ||
| 127 | Prerequisite : | ||
| 128 | Function : Parse Moby message and build Perl structures to access | ||
| 129 | for each query to their articles and objects. | ||
| 130 | Returns : $servicenotes: Notes returned by service provider | ||
| 131 | $ra_queries: ARRAYREF of all queries analysed in MOBY message | ||
| 132 | Args : $moby_message: MOBY XML message | ||
| 133 | Globals : $XSLTPROC: /path/to/xsltproc binary | ||
| 134 | $XSL_SHEET: XSL Sheet for MobyMessage Parsing | ||
| 135 | $TMP_DIR: /where | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub getInputs | ||
| 140 | { | ||
| 141 | my ($moby_message) = (@_); | ||
| 142 | |||
| 143 | my $tmp_file = 'MOBYXSLT' . $$ . $^T; | ||
| 144 | my $header_with_ns = "<moby:MOBY xmlns:moby='http://www.biomoby.org/moby' "; | ||
| 145 | |||
| 146 | $moby_message =~ s/xmlns:moby/xmlns:moby2/; | ||
| 147 | |||
| 148 | $moby_message =~ s/<moby:MOBY/$header_with_ns/; | ||
| 149 | |||
| 150 | open(TMP, ">$TMP_DIR$tmp_file") || confess("$! :$TMP_DIR$tmp_file"); | ||
| 151 | print TMP $moby_message; | ||
| 152 | close TMP; | ||
| 153 | |||
| 154 | my $parsed_message = `$XSLTPROC $XSL_SHEET $TMP_DIR$tmp_file`; | ||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | my $servicenotes = ''; | ||
| 159 | my $ra_exceptions = (); | ||
| 160 | my @a_queries = (); | ||
| 161 | |||
| 162 | my $servicenotes_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES#'; | ||
| 163 | $parsed_message =~ s/\n/__nl__/g; | ||
| 164 | if ($parsed_message =~ /$servicenotes_tag(.*)$servicenotes_tag/) | ||
| 165 | { | ||
| 166 | my $notes = $1; | ||
| 167 | |||
| 168 | ($servicenotes,$ra_exceptions) = _AnalyseServiceNotes($notes); | ||
| 169 | #($servicenotes) = ($parsed_message =~ /$servicenotes_tag(.+)$servicenotes_tag/); | ||
| 170 | } | ||
| 171 | $parsed_message =~ s/__nl__/\n/g; | ||
| 172 | |||
| 173 | my $mobydata_tag = '#XSL_LIPM_MOBYPARSER_DATA_START#'; | ||
| 174 | my ($header, @a_mobydata_blocs) = split($mobydata_tag, $parsed_message); | ||
| 175 | |||
| 176 | my $query_count = 0; | ||
| 177 | |||
| 178 | foreach my $mobydata_bloc (@a_mobydata_blocs) | ||
| 179 | { | ||
| 180 | |||
| 181 | my $queryid_tag = '#XSL_LIPM_MOBYPARSER_QUERYID#'; | ||
| 182 | my ($queryid) = ($mobydata_bloc =~ /$queryid_tag(.+)$queryid_tag/); | ||
| 183 | |||
| 184 | my $article_start_tag = '#XSL_LIPM_MOBYPARSER_ARTICLE_START#'; | ||
| 185 | my ($header_article, @a_article_blocs) = split($article_start_tag, $mobydata_bloc); | ||
| 186 | |||
| 187 | my @a_input_articles = (); | ||
| 188 | |||
| 189 | foreach my $article_bloc (@a_article_blocs) | ||
| 190 | { | ||
| 191 | my $articlename_tag = '#XSL_LIPM_MOBYPARSER_ARTICLENAME#'; | ||
| 192 | my ($articlename) = ($article_bloc =~ /$articlename_tag(.+)$articlename_tag/); | ||
| 193 | |||
| 194 | my $articletype_tag = '#XSL_LIPM_MOBYPARSER_ARTICLETYPE#'; | ||
| 195 | my ($articletype) = ($article_bloc =~ /$articletype_tag(.+)$articletype_tag/); | ||
| 196 | $articletype =~ s/^moby://; | ||
| 197 | |||
| 198 | my $simple_start_tag = '#XSL_LIPM_MOBYPARSER_SIMPLE_START#'; | ||
| 199 | |||
| 200 | my $article_objects = ''; | ||
| 201 | if (_IsCollection($articletype)) | ||
| 202 | { | ||
| 203 | my ($header_collec, @a_simple_blocs) = split($simple_start_tag, $article_bloc); | ||
| 204 | my @a_simple_objects = (); | ||
| 205 | foreach my $simple_bloc (@a_simple_blocs) | ||
| 206 | { | ||
| 207 | my $rh_simple = _AnalyseSimple($simple_bloc); | ||
| 208 | push(@a_simple_objects, $rh_simple); | ||
| 209 | } | ||
| 210 | $article_objects = \@a_simple_objects; | ||
| 211 | } | ||
| 212 | elsif (_IsSimple($articletype)) | ||
| 213 | { | ||
| 214 | my ($header_collec, $simple_bloc) = split($simple_start_tag, $article_bloc); | ||
| 215 | $article_objects = _AnalyseSimple($simple_bloc); | ||
| 216 | } | ||
| 217 | elsif (_IsSecondary($articletype)) | ||
| 218 | { | ||
| 219 | |||
| 220 | my $secondary_start = '#XSL_LIPM_MOBYPARSER_SECONDARY_START#'; | ||
| 221 | my $secondary_end = '#XSL_LIPM_MOBYPARSER_SECONDARY_END#'; | ||
| 222 | my $secondary_sep = '#XSL_LIPM_MOBYPARSER_SECONDARY_SEP#'; | ||
| 223 | my (@a_param) = ($article_bloc =~ /$secondary_start(.+)$secondary_sep(.+)$secondary_end/); | ||
| 224 | $article_objects = \@a_param; | ||
| 225 | } | ||
| 226 | |||
| 227 | my %h_input_article = ( | ||
| 228 | 'article_type' => $articletype, | ||
| 229 | 'article_name' => $articlename, | ||
| 230 | 'article_objects' => $article_objects | ||
| 231 | ); | ||
| 232 | |||
| 233 | push(@a_input_articles, \%h_input_article); | ||
| 234 | |||
| 235 | } | ||
| 236 | |||
| 237 | my %h_query = ( | ||
| 238 | 'query_id' => $queryid, | ||
| 239 | 'query_articles' => \@a_input_articles | ||
| 240 | ); | ||
| 241 | |||
| 242 | push(@a_queries, \%h_query); | ||
| 243 | |||
| 244 | } | ||
| 245 | |||
| 246 | unlink("$TMP_DIR$tmp_file"); | ||
| 247 | return ($servicenotes, \@a_queries, $ra_exceptions); | ||
| 248 | } | ||
| 249 | |||
| 250 | =head2 function getInputID | ||
| 251 | |||
| 252 | Title : getInputID | ||
| 253 | Usage : my $query_id =getInputID($rh_query); | ||
| 254 | Prerequisite : | ||
| 255 | Function : Return query_id of a query from getInputs | ||
| 256 | Returns : $query_id | ||
| 257 | Args : $rh_query: query HASHREF structure from getInputs | ||
| 258 | Globals : none | ||
| 259 | |||
| 260 | =cut | ||
| 261 | |||
| 262 | sub getInputID | ||
| 263 | { | ||
| 264 | my $rh_query = shift(); | ||
| 265 | return $rh_query->{'query_id'}; | ||
| 266 | } | ||
| 267 | |||
| 268 | =head2 function getArticles | ||
| 269 | |||
| 270 | Title : getArticles | ||
| 271 | Usage : my @a_input_articles =getArticles($rh_query); | ||
| 272 | Prerequisite : | ||
| 273 | Function : For a query from getInputs, retrieve list of articles | ||
| 274 | represented by a ARRAYREF corresponding to REF(articleName, articlePerlStructure) | ||
| 275 | Returns : @a_input_articles: ARRAY of articles ARRAYREF | ||
| 276 | Args : $rh_query: query HASHREF structure from getInputs | ||
| 277 | Globals : none | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | sub getArticles | ||
| 282 | { | ||
| 283 | my $rh_query = shift(); | ||
| 284 | my @a_input_articles = (); | ||
| 285 | |||
| 286 | foreach my $rh_input_article (@{$rh_query->{'query_articles'}}) | ||
| 287 | { | ||
| 288 | my @a_input_article = ($rh_input_article->{'article_name'}, $rh_input_article); | ||
| 289 | push(@a_input_articles, \@a_input_article); | ||
| 290 | } | ||
| 291 | return (@a_input_articles); | ||
| 292 | } | ||
| 293 | |||
| 294 | =head2 function getCollectedSimples | ||
| 295 | |||
| 296 | Title : getCollectedSimples | ||
| 297 | Usage : my @a_simple_articles =getCollectedSimples($rh_collection_article); | ||
| 298 | Prerequisite : | ||
| 299 | Function : For a collection query from getArticles, retrieve list of | ||
| 300 | simple articles | ||
| 301 | Returns : @a_simple_articles: ARRAY of articles HASHREF | ||
| 302 | Args : $rh_collection_article: collection article HASHREF structure from getArticles | ||
| 303 | Globals : none | ||
| 304 | |||
| 305 | =cut | ||
| 306 | |||
| 307 | sub getCollectedSimples | ||
| 308 | { | ||
| 309 | my $rh_collection_article = shift(); | ||
| 310 | return @{$rh_collection_article->{'article_objects'}}; | ||
| 311 | } | ||
| 312 | |||
| 313 | =head2 function getCrossReferences | ||
| 314 | |||
| 315 | Title : getCrossReferences | ||
| 316 | Usage : my @a_crossreferences =getCrossReferences($rh_simple_article); | ||
| 317 | Prerequisite : | ||
| 318 | Function : Takes a simple article structure (from getArticles or getCollectedSimples) | ||
| 319 | and retrieve the list of crossreferences HASHREF | ||
| 320 | Returns : @a_crossreferences: ARRAY of crossreferences HASHREF | ||
| 321 | Args : $rh_simple_article: simple article HASHREF structure from getArticles or getCollectedSimples | ||
| 322 | Globals : none | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | sub getCrossReferences | ||
| 327 | { | ||
| 328 | my $rh_simple_article = shift(); | ||
| 329 | |||
| 330 | if (defined $rh_simple_article->{'article_objects'}) | ||
| 331 | { | ||
| 332 | if ($rh_simple_article->{'article_objects'}->{'object_crossreference'} ne '') | ||
| 333 | { | ||
| 334 | return (@{$rh_simple_article->{'article_objects'}->{'object_crossreference'}}); | ||
| 335 | } | ||
| 336 | else | ||
| 337 | { | ||
| 338 | return (); | ||
| 339 | } | ||
| 340 | } | ||
| 341 | else | ||
| 342 | { | ||
| 343 | if ($rh_simple_article->{'object_crossreference'} ne '') | ||
| 344 | { | ||
| 345 | return @{$rh_simple_article->{'object_crossreference'}}; | ||
| 346 | } | ||
| 347 | else | ||
| 348 | { | ||
| 349 | return (); | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | } | ||
| 354 | |||
| 355 | =head2 function getProvisionInformation | ||
| 356 | |||
| 357 | Title : getProvisionInformation | ||
| 358 | Usage : my @a_pib =getProvisionInformation($rh_simple_article); | ||
| 359 | Prerequisite : | ||
| 360 | Function : Takes a simple article structure (from getArticles or getCollectedSimples) | ||
| 361 | and retrieve the list of Provision Information HASHREF | ||
| 362 | Returns : @a_pib: ARRAY of provisionInformation HASHREF | ||
| 363 | Args : $rh_simple_article: simple article HASHREF structure from getArticles or getCollectedSimples | ||
| 364 | Globals : none | ||
| 365 | |||
| 366 | =cut | ||
| 367 | |||
| 368 | sub getProvisionInformation | ||
| 369 | { | ||
| 370 | my $rh_simple_article = shift(); | ||
| 371 | |||
| 372 | if (defined $rh_simple_article->{'article_objects'}) | ||
| 373 | { | ||
| 374 | if ($rh_simple_article->{'article_objects'}->{'object_pib'} ne '') | ||
| 375 | { | ||
| 376 | return (@{$rh_simple_article->{'article_objects'}->{'object_pib'}}); | ||
| 377 | } | ||
| 378 | else | ||
| 379 | { | ||
| 380 | return (); | ||
| 381 | } | ||
| 382 | } | ||
| 383 | else | ||
| 384 | { | ||
| 385 | if ($rh_simple_article->{'object_pib'} ne '') | ||
| 386 | { | ||
| 387 | return @{$rh_simple_article->{'object_pib'}}; | ||
| 388 | } | ||
| 389 | else | ||
| 390 | { | ||
| 391 | return (); | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | } | ||
| 396 | |||
| 397 | =head2 function getObjectHasaElements | ||
| 398 | |||
| 399 | Title : getObjectHasaElements | ||
| 400 | Usage : my @a_hasa_elements =getObjectHasaElements($rh_simple_article); | ||
| 401 | Prerequisite : | ||
| 402 | Function : Takes a simple article structure (from getArticles or getCollectedSimples) | ||
| 403 | and retrieve the list of "HASA" element HASHREF | ||
| 404 | Returns : @a_hasa_elements: ARRAY of "HASA" element HASHREF | ||
| 405 | Args : $rh_object: simple article HASHREF structure from getArticles or getCollectedSimples | ||
| 406 | Globals : none | ||
| 407 | |||
| 408 | =cut | ||
| 409 | |||
| 410 | sub getObjectHasaElements | ||
| 411 | { | ||
| 412 | my $rh_simple_article = shift(); | ||
| 413 | |||
| 414 | if (defined $rh_simple_article->{'article_objects'}) | ||
| 415 | { | ||
| 416 | if ($rh_simple_article->{'article_objects'}->{'object_hasa'} ne '') | ||
| 417 | { | ||
| 418 | return (@{$rh_simple_article->{'article_objects'}->{'object_hasa'}}); | ||
| 419 | } | ||
| 420 | else | ||
| 421 | { | ||
| 422 | return (); | ||
| 423 | } | ||
| 424 | } | ||
| 425 | else | ||
| 426 | { | ||
| 427 | if ($rh_simple_article->{'object_hasa'} ne '') | ||
| 428 | { | ||
| 429 | return @{$rh_simple_article->{'object_hasa'}}; | ||
| 430 | } | ||
| 431 | else | ||
| 432 | { | ||
| 433 | return (); | ||
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 437 | # if ($rh_object->{'object_hasa'} ne '') | ||
| 438 | # { | ||
| 439 | # return (@{$rh_object->{'object_hasa'}}); | ||
| 440 | # } | ||
| 441 | # else | ||
| 442 | # { | ||
| 443 | # return (); | ||
| 444 | # } | ||
| 445 | } | ||
| 446 | |||
| 447 | =head2 function getObjectType | ||
| 448 | |||
| 449 | Title : getObjectType | ||
| 450 | Usage : my $object_type =getObjectType($rh_object); | ||
| 451 | Prerequisite : | ||
| 452 | Function : Returns object MOBY class/type | ||
| 453 | Returns : $object_type: object MOBY class/type | ||
| 454 | Args : $rh_object: simple article (object) HASHREF structure from getArticles,getCollectedSimples or getObjectHasaElements | ||
| 455 | Globals : none | ||
| 456 | |||
| 457 | =cut | ||
| 458 | |||
| 459 | sub getObjectType | ||
| 460 | { | ||
| 461 | my $rh_object = shift(); | ||
| 462 | if (defined $rh_object->{'article_objects'}) | ||
| 463 | { | ||
| 464 | return ($rh_object->{'article_objects'}->{'object_type'}); | ||
| 465 | } | ||
| 466 | else | ||
| 467 | { | ||
| 468 | return $rh_object->{'object_type'}; | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | =head2 function getObjectName | ||
| 473 | |||
| 474 | Title : getObjectName | ||
| 475 | Usage : my $object_name =getObjectName($rh_object); | ||
| 476 | Prerequisite : | ||
| 477 | Function : Returns object moby:articleName | ||
| 478 | Returns : $object_name: moby:articleName | ||
| 479 | Args : $rh_object: simple article (object) HASHREF structure from getArticles,getCollectedSimples or getObjectHasaElements | ||
| 480 | Globals : none | ||
| 481 | |||
| 482 | =cut | ||
| 483 | |||
| 484 | sub getObjectName | ||
| 485 | { | ||
| 486 | my $rh_object = shift(); | ||
| 487 | if (defined $rh_object->{'article_objects'}) | ||
| 488 | { | ||
| 489 | return ($rh_object->{'article_objects'}->{'object_name'}); | ||
| 490 | } | ||
| 491 | else | ||
| 492 | { | ||
| 493 | return $rh_object->{'object_name'}; | ||
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | =head2 function getObjectNamespace | ||
| 498 | |||
| 499 | Title : getObjectNamespace | ||
| 500 | Usage : my $object_namespace =getObjectNamespace($rh_object); | ||
| 501 | Prerequisite : | ||
| 502 | Function : Returns object moby:namespace | ||
| 503 | Returns : $object_name: moby:namespace | ||
| 504 | Args : $rh_object: simple article (object) HASHREF structure from getArticles,getCollectedSimples or getObjectHasaElements | ||
| 505 | Globals : none | ||
| 506 | |||
| 507 | =cut | ||
| 508 | |||
| 509 | sub getObjectNamespace | ||
| 510 | { | ||
| 511 | my $rh_object = shift(); | ||
| 512 | if (defined $rh_object->{'article_objects'}) | ||
| 513 | { | ||
| 514 | return ($rh_object->{'article_objects'}->{'object_namespace'}); | ||
| 515 | } | ||
| 516 | else | ||
| 517 | { | ||
| 518 | return $rh_object->{'object_namespace'}; | ||
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 | =head2 function getObjectContent | ||
| 523 | |||
| 524 | Title : getObjectContent | ||
| 525 | Usage : my $object_content =getObjectContent($rh_object); | ||
| 526 | Prerequisite : | ||
| 527 | Function : Returns object content (using HTML::Entities::decode) | ||
| 528 | Warning: this content could contain emptylines if | ||
| 529 | your objects contains Crossreferences or Hasa Elements ... | ||
| 530 | Returns : $object_content: object content (decoded using HTML::Entities::decode) | ||
| 531 | Args : $rh_object: simple article (object) HASHREF structure from getArticles,getCollectedSimples or getObjectHasaElements | ||
| 532 | Globals : none | ||
| 533 | |||
| 534 | =cut | ||
| 535 | |||
| 536 | sub getObjectContent | ||
| 537 | { | ||
| 538 | use HTML::Entities (); | ||
| 539 | my $rh_object = shift(); | ||
| 540 | my $encoded_content = ''; | ||
| 541 | if (defined $rh_object->{'article_objects'}) | ||
| 542 | { | ||
| 543 | $encoded_content = $rh_object->{'article_objects'}->{'object_content'}; | ||
| 544 | } | ||
| 545 | else | ||
| 546 | { | ||
| 547 | $encoded_content = $rh_object->{'object_content'}; | ||
| 548 | } | ||
| 549 | my $decoded_object = HTML::Entities::decode($encoded_content); | ||
| 550 | return ($decoded_object); | ||
| 551 | } | ||
| 552 | |||
| 553 | =head2 function getObjectXML | ||
| 554 | |||
| 555 | Title : getObjectXML | ||
| 556 | Usage : my $object_xml =getObjectXML($rh_object); | ||
| 557 | Prerequisite : | ||
| 558 | Function : Returns full object moby:xml string | ||
| 559 | Returns : $object_xml: object moby:xml string | ||
| 560 | Args : $rh_object: simple article (object) HASHREF structure from getArticles,getCollectedSimples or getObjectHasaElements | ||
| 561 | Globals : none | ||
| 562 | |||
| 563 | =cut | ||
| 564 | |||
| 565 | sub getObjectXML | ||
| 566 | { | ||
| 567 | my $rh_object = shift(); | ||
| 568 | if (defined $rh_object->{'article_objects'}) | ||
| 569 | { | ||
| 570 | return ($rh_object->{'article_objects'}->{'object_xml'}); | ||
| 571 | } | ||
| 572 | else | ||
| 573 | { | ||
| 574 | return $rh_object->{'object_xml'}; | ||
| 575 | } | ||
| 576 | |||
| 577 | } | ||
| 578 | |||
| 579 | =head2 function getObjectId | ||
| 580 | |||
| 581 | Title : getObjectId | ||
| 582 | Usage : my $object_id =getObjectId($rh_object); | ||
| 583 | Prerequisite : | ||
| 584 | Function : Returns object moby:id | ||
| 585 | Returns : $object_id: moby:id | ||
| 586 | Args : $rh_object: simple article (object) HASHREF structure from getArticles,getCollectedSimples or getObjectHasaElements | ||
| 587 | Globals : none | ||
| 588 | |||
| 589 | =cut | ||
| 590 | |||
| 591 | sub getObjectId | ||
| 592 | { | ||
| 593 | my $rh_object = shift(); | ||
| 594 | |||
| 595 | if (defined $rh_object->{'article_objects'}) | ||
| 596 | { | ||
| 597 | return ($rh_object->{'article_objects'}->{'object_id'}); | ||
| 598 | } | ||
| 599 | else | ||
| 600 | { | ||
| 601 | return $rh_object->{'object_id'}; | ||
| 602 | } | ||
| 603 | } | ||
| 604 | |||
| 605 | =head2 function getParameter | ||
| 606 | |||
| 607 | Title : getParameter | ||
| 608 | Usage : my ($parameter_name,$parameter_value) =getParameter($rh_article); | ||
| 609 | Prerequisite : | ||
| 610 | Function : Returns parameter name an value for a Secondary aricle | ||
| 611 | Returns : $parameter_name | ||
| 612 | $parameter_value | ||
| 613 | Args : $rh_article: secondary article HASHREF structure from getArticles | ||
| 614 | Globals : none | ||
| 615 | |||
| 616 | =cut | ||
| 617 | |||
| 618 | sub getParameter | ||
| 619 | { | ||
| 620 | my $rh_article = shift(); | ||
| 621 | if (_IsSecondary($rh_article->{'article_type'})) | ||
| 622 | { | ||
| 623 | return (@{$rh_article->{'article_objects'}}); | ||
| 624 | } | ||
| 625 | |||
| 626 | return; | ||
| 627 | } | ||
| 628 | |||
| 629 | =head2 function getNodeContentWithArticle | ||
| 630 | |||
| 631 | Title : getNodeContentWithArticle | ||
| 632 | Usage : my $content = getNodeContentWithArticle($rh_query, $article_type, $article_name) | ||
| 633 | Prerequisite : | ||
| 634 | Function : inside a mobyData bloc (structured in $rh_query), | ||
| 635 | look for an article of a defined type (Simple, Collection or Parameter). | ||
| 636 | Foreach matching article, search for an object named $article_name. | ||
| 637 | If found, return its content. | ||
| 638 | Returns : $content: content of article requested | ||
| 639 | Args : $rh_query: query HASHREF structure from getInputs | ||
| 640 | $article_type: 'Simple/Collection/Parameter' | ||
| 641 | $article_name: attribute moby:articleName | ||
| 642 | Globals : | ||
| 643 | |||
| 644 | =cut | ||
| 645 | |||
| 646 | sub getNodeContentWithArticle | ||
| 647 | { | ||
| 648 | my ($rh_query, $article_type, $article_name) = (@_); | ||
| 649 | |||
| 650 | foreach my $rh_article (@{$rh_query->{'query_articles'}}) | ||
| 651 | { | ||
| 652 | if ( (_IsSecondary($article_type)) | ||
| 653 | && ($rh_article->{'article_type'} =~ /^$article_type$/i) | ||
| 654 | && ($article_name eq $rh_article->{'article_name'})) | ||
| 655 | { | ||
| 656 | my ($article_name, $article_value) = @{$rh_article->{'article_objects'}}; | ||
| 657 | return $article_value; | ||
| 658 | } | ||
| 659 | elsif (_IsSimple($article_type)) | ||
| 660 | { | ||
| 661 | if ($rh_article->{'article_type'} =~ /^$article_type$/i) | ||
| 662 | { | ||
| 663 | |||
| 664 | if ($rh_article->{'article_name'} eq $article_name) | ||
| 665 | { | ||
| 666 | return $rh_article->{'article_objects'}->{'object_content'}; | ||
| 667 | } | ||
| 668 | elsif ($rh_article->{'article_objects'}->{'object_hasa'} ne '') | ||
| 669 | { | ||
| 670 | foreach my $rh_object (@{$rh_article->{'article_objects'}->{'object_hasa'}}) | ||
| 671 | { | ||
| 672 | if ($rh_object->{'object_name'} eq $article_name) | ||
| 673 | { | ||
| 674 | return $rh_object->{'object_content'}; | ||
| 675 | } | ||
| 676 | } | ||
| 677 | } | ||
| 678 | } | ||
| 679 | } | ||
| 680 | elsif (_IsCollection($article_type)) | ||
| 681 | { | ||
| 682 | if ($rh_article->{'article_type'} =~ /^$article_type$/i) | ||
| 683 | { | ||
| 684 | if ($rh_article->{'article_name'} eq $article_name) | ||
| 685 | { | ||
| 686 | my $content = ''; | ||
| 687 | foreach my $rh_object (@{$rh_article->{'article_objects'}}) | ||
| 688 | { | ||
| 689 | $content .= $rh_object->{'object_content'}; | ||
| 690 | } | ||
| 691 | return $content; | ||
| 692 | } | ||
| 693 | else | ||
| 694 | { | ||
| 695 | foreach my $rh_object (@{$rh_article->{'article_objects'}}) | ||
| 696 | { | ||
| 697 | if ($rh_object->{'object_name'} eq $article_name) | ||
| 698 | { | ||
| 699 | return $rh_object->{'object_content'}; | ||
| 700 | } | ||
| 701 | } | ||
| 702 | } | ||
| 703 | |||
| 704 | } | ||
| 705 | } | ||
| 706 | } | ||
| 707 | |||
| 708 | return; | ||
| 709 | } | ||
| 710 | |||
| 711 | =head2 function isSimpleArticle | ||
| 712 | |||
| 713 | Title : isSimpleArticle | ||
| 714 | Usage : isSimpleArticle($rh_article) | ||
| 715 | Prerequisite : | ||
| 716 | Function : Test if an article is a moby:Simple | ||
| 717 | Returns : $response: BOOLEAN | ||
| 718 | Args : $rh_article: article HASHREF structure from getArticles | ||
| 719 | Globals : none | ||
| 720 | |||
| 721 | =cut | ||
| 722 | |||
| 723 | sub isSimpleArticle | ||
| 724 | { | ||
| 725 | my $rh_article = shift(); | ||
| 726 | my $response = _IsSimple($rh_article->{article_type}); | ||
| 727 | return $response; | ||
| 728 | } | ||
| 729 | |||
| 730 | =head2 function isCollectionArticle | ||
| 731 | |||
| 732 | Title : isCollectionArticle | ||
| 733 | Usage : isCollectionArticle($rh_article) | ||
| 734 | Prerequisite : | ||
| 735 | Function : Test if an article is a moby:Collection | ||
| 736 | Returns : $response: BOOLEAN | ||
| 737 | Args : $rh_article: article HASHREF structure from getArticles | ||
| 738 | Globals : none | ||
| 739 | |||
| 740 | =cut | ||
| 741 | |||
| 742 | sub isCollectionArticle | ||
| 743 | { | ||
| 744 | my $rh_article = shift(); | ||
| 745 | my $response = _IsCollection($rh_article->{article_type}); | ||
| 746 | return $response; | ||
| 747 | } | ||
| 748 | |||
| 749 | =head2 function isSecondaryArticle | ||
| 750 | |||
| 751 | Title : isSecondaryArticle | ||
| 752 | Usage : isSecondaryArticle($rh_article) | ||
| 753 | Prerequisite : | ||
| 754 | Function : Test if articleType is moby:Parameter (secondary article) | ||
| 755 | Returns : $response: BOOLEAN | ||
| 756 | Args : $rh_article: article HASHREF structure from getArticles | ||
| 757 | Globals : none | ||
| 758 | |||
| 759 | =cut | ||
| 760 | |||
| 761 | sub isSecondaryArticle | ||
| 762 | { | ||
| 763 | my $rh_article = shift(); | ||
| 764 | my $response = _IsSecondary($rh_article->{article_type}); | ||
| 765 | return $response; | ||
| 766 | } | ||
| 767 | |||
| 768 | =head2 function _AnalyseSimple | ||
| 769 | |||
| 770 | Title : _AnalyseSimple | ||
| 771 | Usage : _AnalyseSimple($simple_bloc) | ||
| 772 | Prerequisite : | ||
| 773 | Function : Analyse a "Simple Bloc" from XSL transformation parsing | ||
| 774 | Build a $rh_simple_article structure with fields: | ||
| 775 | 'object_name' => moby:articleName | ||
| 776 | 'object_type' => moby:Class | ||
| 777 | 'object_namespace' => moby:namespace | ||
| 778 | 'object_id' => moby:id | ||
| 779 | 'object_content' => text content of simple article | ||
| 780 | 'object_xml' => full xml content of article | ||
| 781 | 'object_hasa' => ARRAYREF of hasa elements | ||
| 782 | (each one is structured in a same | ||
| 783 | structured hash (recursivity) | ||
| 784 | 'object_crossreference' => ARRAYREF of crossreferences objects | ||
| 785 | (each one is structured in a hash with fields | ||
| 786 | 'type', 'id', 'namespace') | ||
| 787 | |||
| 788 | Returns : $rh_simple: article HASHREF | ||
| 789 | Args : $simple_bloc: from parsing of a "simple" XSLT transformation | ||
| 790 | Globals : none | ||
| 791 | |||
| 792 | =cut | ||
| 793 | |||
| 794 | sub _AnalyseSimple | ||
| 795 | { | ||
| 796 | my $simple_bloc = shift(); | ||
| 797 | my @a_crossref = (); | ||
| 798 | my @a_pib = (); | ||
| 799 | my @a_hasa = (); | ||
| 800 | |||
| 801 | my ($object_type,$object_name,$object_id,$object_namespace) = ('','','',''); | ||
| 802 | my $object_type_tag = '#XSL_LIPM_MOBYPARSER_OBJECTTYPE#'; | ||
| 803 | |||
| 804 | if ($simple_bloc =~ /$object_type_tag(.+)$object_type_tag/) | ||
| 805 | { | ||
| 806 | $object_type = $1; | ||
| 807 | $object_type =~ s/^moby://i; | ||
| 808 | } | ||
| 809 | |||
| 810 | my $object_namespace_tag = '#XSL_LIPM_MOBYPARSER_OBJECTNAMESPACE#'; | ||
| 811 | |||
| 812 | if ($simple_bloc =~ /$object_namespace_tag(.+)$object_namespace_tag/) | ||
| 813 | { | ||
| 814 | $object_namespace = $1; | ||
| 815 | } | ||
| 816 | |||
| 817 | my $object_id_tag = '#XSL_LIPM_MOBYPARSER_OBJECTID#'; | ||
| 818 | |||
| 819 | if ($simple_bloc =~ /$object_id_tag(.+)$object_id_tag/) | ||
| 820 | { | ||
| 821 | $object_id = $1; | ||
| 822 | } | ||
| 823 | |||
| 824 | my $object_name_tag = '#XSL_LIPM_MOBYPARSER_OBJECTNAME#'; | ||
| 825 | |||
| 826 | if ($simple_bloc =~ /$object_name_tag(.+)$object_name_tag/) | ||
| 827 | { | ||
| 828 | $object_name = $1 | ||
| 829 | } | ||
| 830 | |||
| 831 | my $crossref_start_tag = '#XSL_LIPM_MOBYPARSER_CROSSREF_START#'; | ||
| 832 | my $crossref_end_tag = '#XSL_LIPM_MOBYPARSER_CROSSREF_END#'; | ||
| 833 | my $crossref_sep_tag = '#XSL_LIPM_MOBYPARSER_CROSSREF_SEP#'; | ||
| 834 | |||
| 835 | while ($simple_bloc =~ m/$crossref_start_tag(.*)$crossref_sep_tag(.*)$crossref_sep_tag(.*)$crossref_end_tag/g) | ||
| 836 | { | ||
| 837 | my %h_crossref = ('type' => $1, 'id' => $2, 'namespace' => $3); | ||
| 838 | $simple_bloc =~ s/$crossref_start_tag$1$crossref_sep_tag$2$crossref_sep_tag$3$crossref_end_tag//; | ||
| 839 | push(@a_crossref, \%h_crossref); | ||
| 840 | } | ||
| 841 | |||
| 842 | my $ra_crossref = \@a_crossref; | ||
| 843 | if ($#a_crossref < 0) | ||
| 844 | { | ||
| 845 | $ra_crossref = ''; | ||
| 846 | } | ||
| 847 | |||
| 848 | #19/12/2005 | ||
| 849 | #Provision Information Block | ||
| 850 | |||
| 851 | my $pib_start_tag = '#XSL_LIPM_MOBYPARSER_PIB_START#'; | ||
| 852 | my $pib_end_tag = '#XSL_LIPM_MOBYPARSER_PIB_END#'; | ||
| 853 | my $pib_software_start_tag = '#XSL_LIPM_MOBYPARSER_SOFTWARE_START#'; | ||
| 854 | my $pib_software_end_tag = '#XSL_LIPM_MOBYPARSER_SOFTWARE_END#'; | ||
| 855 | my $pib_software_sep_tag = '#XSL_LIPM_MOBYPARSER_SOFTWARE_SEP#'; | ||
| 856 | my $pib_database_start_tag = '#XSL_LIPM_MOBYPARSER_DATABASE_START#'; | ||
| 857 | my $pib_database_end_tag = '#XSL_LIPM_MOBYPARSER_DATABASE_END#'; | ||
| 858 | my $pib_database_sep_tag = '#XSL_LIPM_MOBYPARSER_DATABASE_SEP#'; | ||
| 859 | my $pib_comment_start_tag = '#XSL_LIPM_MOBYPARSER_COMMENT_START#'; | ||
| 860 | my $pib_comment_end_tag = '#XSL_LIPM_MOBYPARSER_COMMENT_END#'; | ||
| 861 | |||
| 862 | |||
| 863 | while ($simple_bloc =~ m/$pib_start_tag(.*)$pib_end_tag/g) | ||
| 864 | { | ||
| 865 | my $provision_block = $1; | ||
| 866 | $simple_bloc =~ s/$pib_start_tag$provision_block$pib_end_tag//; | ||
| 867 | my ($software_name,$software_version,$software_comment) = ('','',''); | ||
| 868 | if ($provision_block =~ /$pib_software_start_tag(.*)$pib_software_end_tag/) | ||
| 869 | { | ||
| 870 | ($software_name,$software_version,$software_comment) = split (/$pib_software_sep_tag/,$1); | ||
| 871 | } | ||
| 872 | my ($database_name,$database_version,$database_comment) = ('','',''); | ||
| 873 | if ($provision_block =~ /$pib_database_start_tag(.*)$pib_database_end_tag/) | ||
| 874 | { | ||
| 875 | ($database_name,$database_version,$database_comment) = split (/$pib_database_sep_tag/,$1); | ||
| 876 | } | ||
| 877 | my ($service_comment) = (''); | ||
| 878 | if ($provision_block =~ /$pib_comment_start_tag(.*)$pib_comment_end_tag/) | ||
| 879 | { | ||
| 880 | ($service_comment) = ($1); | ||
| 881 | } | ||
| 882 | |||
| 883 | my %h_pib = ( | ||
| 884 | 'software_name' => $software_name, | ||
| 885 | 'software_version' => $software_version, | ||
| 886 | 'software_comment' => $software_comment, | ||
| 887 | 'database_name' => $database_name, | ||
| 888 | 'database_version' => $database_version, | ||
| 889 | 'database_comment' => $database_comment, | ||
| 890 | 'service_comment' => $service_comment | ||
| 891 | ); | ||
| 892 | |||
| 893 | open (TMP, ">>/tmp/pib.txt"); | ||
| 894 | print TMP <<END; | ||
| 895 | 'software_name' => $software_name, | ||
| 896 | 'software_version' => $software_version, | ||
| 897 | 'software_comment' => $software_comment, | ||
| 898 | 'database_name' => $database_name, | ||
| 899 | 'database_version' => $database_version, | ||
| 900 | 'database_comment' => $database_comment, | ||
| 901 | 'service_comment' => $service_comment | ||
| 902 | END | ||
| 903 | close TMP; | ||
| 904 | chmod 0777, "/tmp/pib.txt"; | ||
| 905 | |||
| 906 | push(@a_pib, \%h_pib); | ||
| 907 | } | ||
| 908 | |||
| 909 | my $ra_pib = \@a_pib; | ||
| 910 | if ($#a_pib < 0) | ||
| 911 | { | ||
| 912 | $ra_pib = ''; | ||
| 913 | } | ||
| 914 | |||
| 915 | |||
| 916 | |||
| 917 | |||
| 918 | |||
| 919 | my $object_content_tag = '#XSL_LIPM_MOBYPARSER_OBJECTCONTENT#'; | ||
| 920 | my ($before, $object_content, $after) = ('','',''); | ||
| 921 | ($before, $object_content, $after) = split($object_content_tag, $simple_bloc); | ||
| 922 | |||
| 923 | #Sebastien 21/12/2005 | ||
| 924 | # | ||
| 925 | $object_content =~ s/^\s+//g; | ||
| 926 | $object_content =~ s/\s+$//g; | ||
| 927 | # | ||
| 928 | |||
| 929 | my $object_hasa_start_tag = '#XSL_LIPM_MOBYPARSER_OBJECTHASA_START#'; | ||
| 930 | |||
| 931 | if ($simple_bloc =~ /$object_hasa_start_tag/) | ||
| 932 | { | ||
| 933 | my (@a_hasa_blocs) = split($object_hasa_start_tag, $simple_bloc); | ||
| 934 | |||
| 935 | #Sebastien 19/12/2005 | ||
| 936 | #le premier est le pere | ||
| 937 | #shift @a_hasa_blocs; | ||
| 938 | foreach my $hasa_bloc (@a_hasa_blocs) | ||
| 939 | { | ||
| 940 | if ($hasa_bloc ne '') | ||
| 941 | { | ||
| 942 | my $rh_hasa = _AnalyseSimple($hasa_bloc); | ||
| 943 | push(@a_hasa, $rh_hasa); | ||
| 944 | } | ||
| 945 | } | ||
| 946 | } | ||
| 947 | |||
| 948 | my $ra_hasa = \@a_hasa; | ||
| 949 | my $object_xml = ''; | ||
| 950 | |||
| 951 | if ($#a_hasa < 0) | ||
| 952 | { | ||
| 953 | $ra_hasa = ''; | ||
| 954 | $object_xml = | ||
| 955 | "<moby:$object_type moby:id='$object_id' moby:namespace='$object_namespace'>$object_content</moby:$object_type>"; | ||
| 956 | } | ||
| 957 | else | ||
| 958 | { | ||
| 959 | $object_xml = "<moby:$object_type moby:id='$object_id' moby:namespace='$object_namespace'>\n"; | ||
| 960 | foreach my $rh_hasa (@a_hasa) | ||
| 961 | { | ||
| 962 | $object_xml .= $rh_hasa->{'object_content'} . "\n"; | ||
| 963 | } | ||
| 964 | $object_xml .= "</moby:$object_type>"; | ||
| 965 | } | ||
| 966 | |||
| 967 | my %h_simple = ( | ||
| 968 | 'object_name' => $object_name, | ||
| 969 | 'object_type' => $object_type, | ||
| 970 | 'object_namespace' => $object_namespace, | ||
| 971 | 'object_id' => $object_id, | ||
| 972 | 'object_content' => $object_content, | ||
| 973 | 'object_xml' => $object_xml, | ||
| 974 | 'object_crossreference' => $ra_crossref, | ||
| 975 | 'object_pib' => $ra_pib, | ||
| 976 | 'object_hasa' => $ra_hasa | ||
| 977 | ); | ||
| 978 | |||
| 979 | return \%h_simple; | ||
| 980 | } | ||
| 981 | |||
| 982 | =head2 simpleResponse (stolen from MOBY::CommonSubs) | ||
| 983 | |||
| 984 | name : simpleResponse | ||
| 985 | function : wraps a simple article in the appropriate (mobyData) structure | ||
| 986 | usage : $resp .= &simpleResponse($object, 'MyArticleName', $queryID); | ||
| 987 | args : (in order) | ||
| 988 | $object - (optional) a MOBY Object as raw XML | ||
| 989 | $article - (optional) an articeName for this article | ||
| 990 | $query - (optional, but strongly recommended) the queryID value for the | ||
| 991 | mobyData block to which you are responding | ||
| 992 | notes : as required by the API you must return a response for every input. | ||
| 993 | If one of the inputs was invalid, you return a valid (empty) MOBY | ||
| 994 | response by calling &simpleResponse(undef, undef, $queryID) with no arguments. | ||
| 995 | |||
| 996 | =cut | ||
| 997 | |||
| 998 | sub simpleResponse | ||
| 999 | { | ||
| 1000 | my ($data, $articleName, $qID) = @_; # articleName optional | ||
| 1001 | |||
| 1002 | $data ||= ''; # initialize to avoid uninit value errors | ||
| 1003 | $qID ||= ""; | ||
| 1004 | $articleName ||= ""; | ||
| 1005 | if ($articleName) | ||
| 1006 | { | ||
| 1007 | return " | ||
| 1008 | <moby:mobyData moby:queryID='$qID'> | ||
| 1009 | <moby:Simple moby:articleName='$articleName'>$data</moby:Simple> | ||
| 1010 | </moby:mobyData> | ||
| 1011 | "; | ||
| 1012 | } | ||
| 1013 | elsif ($data) | ||
| 1014 | { | ||
| 1015 | return " | ||
| 1016 | <moby:mobyData moby:queryID='$qID'> | ||
| 1017 | <moby:Simple moby:articleName='$articleName'>$data</moby:Simple> | ||
| 1018 | </moby:mobyData> | ||
| 1019 | "; | ||
| 1020 | } | ||
| 1021 | else | ||
| 1022 | { | ||
| 1023 | return " | ||
| 1024 | <moby:mobyData moby:queryID='$qID'/> | ||
| 1025 | "; | ||
| 1026 | } | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | =head2 collectionResponse (stolen from MOBY::CommonSubs) | ||
| 1030 | |||
| 1031 | name : collectionResponse | ||
| 1032 | function : wraps a set of articles in the appropriate mobyData structure | ||
| 1033 | usage : return responseHeader . &collectionResponse(\@objects, 'MyArticleName', $queryID) . responseFooter; | ||
| 1034 | args : (in order) | ||
| 1035 | \@objects - (optional) a listref of MOBY Objects as raw XML | ||
| 1036 | $article - (optional) an articeName for this article | ||
| 1037 | $queryID - (optional, but strongly recommended) the mobyData ID | ||
| 1038 | to which you are responding | ||
| 1039 | notes : as required by the API you must return a response for every input. | ||
| 1040 | If one of the inputs was invalid, you return a valid (empty) MOBY | ||
| 1041 | response by calling &collectionResponse(undef, undef, $queryID). | ||
| 1042 | |||
| 1043 | =cut | ||
| 1044 | |||
| 1045 | sub collectionResponse | ||
| 1046 | { | ||
| 1047 | my ($data, $articleName, $qID) = @_; # articleName optional | ||
| 1048 | my $content = ""; | ||
| 1049 | $data ||= []; | ||
| 1050 | $qID ||= ''; | ||
| 1051 | unless ((ref($data) =~ /array/i) && $data->[0]) | ||
| 1052 | { # we're expecting an arrayref as input data,and it must not be empty | ||
| 1053 | return "<moby:mobyData moby:queryID='$qID'/>"; | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | foreach (@{$data}) | ||
| 1057 | { | ||
| 1058 | if ($_) | ||
| 1059 | { | ||
| 1060 | $content .= " | ||
| 1061 | <moby:Simple>$_</moby:Simple> | ||
| 1062 | "; | ||
| 1063 | } | ||
| 1064 | else | ||
| 1065 | { | ||
| 1066 | $content .= " | ||
| 1067 | <moby:Simple/> | ||
| 1068 | "; | ||
| 1069 | } | ||
| 1070 | } | ||
| 1071 | if ($articleName) | ||
| 1072 | { | ||
| 1073 | return " | ||
| 1074 | <moby:mobyData moby:queryID='$qID'> | ||
| 1075 | <moby:Collection moby:articleName='$articleName'> | ||
| 1076 | $content | ||
| 1077 | </moby:Collection> | ||
| 1078 | </moby:mobyData> | ||
| 1079 | "; | ||
| 1080 | } | ||
| 1081 | else | ||
| 1082 | { | ||
| 1083 | return " | ||
| 1084 | <moby:mobyData moby:queryID='$qID'> | ||
| 1085 | <moby:Collection moby:articleName='$articleName'>$content</moby:Collection> | ||
| 1086 | </moby:mobyData> | ||
| 1087 | "; | ||
| 1088 | } | ||
| 1089 | } | ||
| 1090 | |||
| 1091 | |||
| 1092 | =head2 complexResponse (stolen from MOBY::CommonSubs) | ||
| 1093 | |||
| 1094 | name : complexResponse | ||
| 1095 | function : wraps a set of articles in the one mobyData structure | ||
| 1096 | usage : return responseHeader . &complexResponse(\@a_article_structures, $queryID) . responseFooter; | ||
| 1097 | args : (in order) | ||
| 1098 | \@a_article_structures - (optional) a listref of structured articles | ||
| 1099 | %h_article = ( | ||
| 1100 | article_type => 'collection/simple', | ||
| 1101 | article_content => 'MOBY XML formatted content', | ||
| 1102 | article_name => 'articleName attribut') | ||
| 1103 | $queryID - (optional, but strongly recommended) the mobyData ID | ||
| 1104 | to which you are responding | ||
| 1105 | =cut | ||
| 1106 | |||
| 1107 | |||
| 1108 | sub complexResponse | ||
| 1109 | { | ||
| 1110 | my ($ra_data, $qID) = @_; | ||
| 1111 | |||
| 1112 | $ra_data ||= []; | ||
| 1113 | $qID ||= ''; | ||
| 1114 | unless ((ref($ra_data) =~ /array/i) && $ra_data->[0]) | ||
| 1115 | { # we're expecting an arrayref as input data,and it must not be empty | ||
| 1116 | return "<moby:mobyData moby:queryID='$qID'/>"; | ||
| 1117 | } | ||
| 1118 | my $moby_data_content = ''; | ||
| 1119 | foreach my $rh_data_block (@{$ra_data}) | ||
| 1120 | { | ||
| 1121 | my $article_name = $rh_data_block->{article_name}; | ||
| 1122 | my $article_content = $rh_data_block->{article_content}; | ||
| 1123 | |||
| 1124 | if ($rh_data_block->{article_type} =~ /collection/i) | ||
| 1125 | { | ||
| 1126 | my $collection_content = "<moby:Collection moby:articleName='$article_name'>\n"; | ||
| 1127 | if ((ref($article_content) =~ /array/i) && $article_content->[0]) | ||
| 1128 | { | ||
| 1129 | foreach my $simple_element (@{$article_content}) | ||
| 1130 | { | ||
| 1131 | $collection_content .= "\t<moby:Simple>\n\t$simple_element\n\t</moby:Simple>\n"; | ||
| 1132 | } | ||
| 1133 | } | ||
| 1134 | else | ||
| 1135 | { | ||
| 1136 | $collection_content .= "\t<moby:Simple/>\n"; | ||
| 1137 | } | ||
| 1138 | $collection_content .= "</moby:Collection>\n"; | ||
| 1139 | |||
| 1140 | $moby_data_content .= $collection_content; | ||
| 1141 | |||
| 1142 | } | ||
| 1143 | else | ||
| 1144 | { | ||
| 1145 | my $simple_content = "<moby:Simple moby:articleName='$article_name'>\n\t$article_content\n</moby:Simple>"; | ||
| 1146 | $moby_data_content .= $simple_content; | ||
| 1147 | } | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | return "<moby:mobyData moby:queryID='$qID'>\n\t$moby_data_content\n</moby:mobyData>\n"; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | |||
| 1154 | =head2 responseHeader (stolen from MOBY::CommonSubs) | ||
| 1155 | |||
| 1156 | B<function:> print the XML string of a MOBY response header +/- serviceNotes +/- Exceptions | ||
| 1157 | |||
| 1158 | B<usage:> | ||
| 1159 | |||
| 1160 | responseHeader('illuminae.com') | ||
| 1161 | |||
| 1162 | responseHeader( | ||
| 1163 | -authority => 'illuminae.com', | ||
| 1164 | -note => 'here is some data from the service provider' | ||
| 1165 | -exception=>'an xml encoded exception string') | ||
| 1166 | |||
| 1167 | |||
| 1168 | B<args:> a string representing the service providers authority URI, OR | ||
| 1169 | a set of named arguments with the authority and the service provision | ||
| 1170 | notes which can include already xml encoded exceptions | ||
| 1171 | |||
| 1172 | B< caveat :> | ||
| 1173 | |||
| 1174 | B<notes:> returns everything required up to the response articles themselves. i.e. something like: | ||
| 1175 | |||
| 1176 | <?xml version='1.0' encoding='UTF-8'?> | ||
| 1177 | <moby:MOBY xmlns:moby='http://www.biomoby.org/moby'> | ||
| 1178 | <moby:Response moby:authority='http://www.illuminae.com'> | ||
| 1179 | |||
| 1180 | =cut | ||
| 1181 | |||
| 1182 | sub responseHeader { | ||
| 1183 | use HTML::Entities (); | ||
| 1184 | my ( $auth, $notes, $exception ) = _rearrange( [qw[AUTHORITY NOTE EXCEPTION]], @_ ); | ||
| 1185 | $auth ||= "not_provided"; | ||
| 1186 | $notes ||= ""; | ||
| 1187 | $exception ||=""; | ||
| 1188 | my $xml = | ||
| 1189 | "<?xml version='1.0' encoding='UTF-8'?>" | ||
| 1190 | . "<moby:MOBY xmlns:moby='http://www.biomoby.org/moby' xmlns='http://www.biomoby.org/moby'>" | ||
| 1191 | . "<moby:mobyContent moby:authority='$auth'>"; | ||
| 1192 | if ($exception) { | ||
| 1193 | $xml .= "<moby:serviceNotes>$exception"; | ||
| 1194 | if ( $notes ) { | ||
| 1195 | my $encodednotes = HTML::Entities::encode( $notes ); | ||
| 1196 | $xml .= "<moby:Notes>$encodednotes</moby:Notes>"; | ||
| 1197 | } | ||
| 1198 | $xml .="</moby:serviceNotes>"; | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | elsif ( $notes ) { | ||
| 1202 | my $encodednotes = HTML::Entities::encode( $notes ); | ||
| 1203 | $xml .= "<moby:serviceNotes><moby:Notes>$encodednotes</moby:Notes></moby:serviceNotes>"; | ||
| 1204 | } | ||
| 1205 | return $xml; | ||
| 1206 | } | ||
| 1207 | |||
| 1208 | |||
| 1209 | =head2 encodeException (stolen from MOBY::CommonSubs) | ||
| 1210 | |||
| 1211 | B<function:> wraps a Biomoby Exception with all its parameters into the appropiate MobyData structure | ||
| 1212 | |||
| 1213 | B<usage:> | ||
| 1214 | |||
| 1215 | encodeException( | ||
| 1216 | -refElement => 'refers to the queryID of the offending input mobyData', | ||
| 1217 | -refQueryID => 'refers to the articleName of the offending input Simple or Collection' | ||
| 1218 | -severity=>'error' | ||
| 1219 | -exceptionCode=>'An error code ' | ||
| 1220 | -exceptionMessage=>'a human readable description for the error code') | ||
| 1221 | |||
| 1222 | B<args:>the different arguments required by the mobyException API | ||
| 1223 | severity can be either error, warning or information | ||
| 1224 | valid error codes are decribed on the biomoby website | ||
| 1225 | |||
| 1226 | |||
| 1227 | B<notes:> returns everything required to use for the responseHeader: | ||
| 1228 | |||
| 1229 | <moby:mobyException moby:refElement='input1' moby:refQueryID='1' moby:severity =''> | ||
| 1230 | <moby:exceptionCode>600</moby:exceptionCode> | ||
| 1231 | <moby:exceptionMessage>Unable to execute the service</moby:exceptionMessage> | ||
| 1232 | </moby:mobyException> | ||
| 1233 | |||
| 1234 | =cut | ||
| 1235 | |||
| 1236 | sub encodeException{ | ||
| 1237 | use HTML::Entities (); | ||
| 1238 | my ( $refElement, $refQueryID, $severity, $code, $message ) = _rearrange( [qw[REFELEMENT REFQUERYID SEVERITY EXCEPTIONCODE EXCEPTIONMESSAGE]], @_ ); | ||
| 1239 | $refElement ||= ""; | ||
| 1240 | defined($refQueryID) || ($refQueryID= ""); | ||
| 1241 | $severity ||= ""; | ||
| 1242 | defined($code) || ($code = ""); | ||
| 1243 | $message ||= "not provided"; | ||
| 1244 | my $xml="<moby:mobyException moby:refElement='$refElement' moby:refQueryID='$refQueryID' moby:severity ='$severity'>". | ||
| 1245 | "<moby:exceptionCode>$code</moby:exceptionCode>". | ||
| 1246 | "<moby:exceptionMessage>".HTML::Entities::encode($message)."</moby:exceptionMessage>". | ||
| 1247 | "</moby:mobyException>"; | ||
| 1248 | } | ||
| 1249 | |||
| 1250 | |||
| 1251 | =head2 responseFooter (stolen from MOBY::CommonSubs) | ||
| 1252 | |||
| 1253 | name : responseFooter | ||
| 1254 | function : print the XML string of a MOBY response footer | ||
| 1255 | usage : return responseHeader('illuminae.com') . $DATA . responseFooter; | ||
| 1256 | notes : returns everything required after the response articles themselves | ||
| 1257 | i.e. something like: | ||
| 1258 | |||
| 1259 | </moby:Response> | ||
| 1260 | </moby:MOBY> | ||
| 1261 | |||
| 1262 | =cut | ||
| 1263 | |||
| 1264 | sub responseFooter | ||
| 1265 | { | ||
| 1266 | return "</moby:mobyContent></moby:MOBY>"; | ||
| 1267 | } | ||
| 1268 | |||
| 1269 | =head2 function _IsCollection | ||
| 1270 | |||
| 1271 | Title : _IsCollection | ||
| 1272 | Usage : _IsCollection($article_type) | ||
| 1273 | Prerequisite : | ||
| 1274 | Function : Compares a string to string 'collection' | ||
| 1275 | Remove namespace 'moby:' from parameter string | ||
| 1276 | Case insensitive | ||
| 1277 | Returns : BOOLEAN | ||
| 1278 | Args : $articletype: a string | ||
| 1279 | Globals : none | ||
| 1280 | |||
| 1281 | =cut | ||
| 1282 | |||
| 1283 | sub _IsCollection | ||
| 1284 | { | ||
| 1285 | my $articletype = shift(); | ||
| 1286 | |||
| 1287 | $articletype =~ s/^moby://; | ||
| 1288 | if ($articletype =~ /^collection$/i) | ||
| 1289 | { | ||
| 1290 | return 1; | ||
| 1291 | } | ||
| 1292 | else | ||
| 1293 | { | ||
| 1294 | return 0; | ||
| 1295 | } | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | =head2 function _IsSimple | ||
| 1299 | |||
| 1300 | Title : _IsSimple | ||
| 1301 | Usage : _IsSimple($article_type) | ||
| 1302 | Prerequisite : | ||
| 1303 | Function : Compares a string to string 'simple' | ||
| 1304 | Remove namespace 'moby:' from parameter string | ||
| 1305 | Case insensitive | ||
| 1306 | Returns : BOOLEAN | ||
| 1307 | Args : $articletype: a string | ||
| 1308 | Globals : none | ||
| 1309 | |||
| 1310 | =cut | ||
| 1311 | |||
| 1312 | sub _IsSimple | ||
| 1313 | { | ||
| 1314 | my $articletype = shift(); | ||
| 1315 | |||
| 1316 | $articletype =~ s/^moby://; | ||
| 1317 | if ($articletype =~ /^simple$/i) | ||
| 1318 | { | ||
| 1319 | return 1; | ||
| 1320 | } | ||
| 1321 | else | ||
| 1322 | { | ||
| 1323 | return 0; | ||
| 1324 | } | ||
| 1325 | } | ||
| 1326 | |||
| 1327 | =head2 function _IsSecondary | ||
| 1328 | |||
| 1329 | Title : _IsSecondary | ||
| 1330 | Usage : _IsSecondary($article_type) | ||
| 1331 | Prerequisite : | ||
| 1332 | Function : Compares a string to string 'parameter' | ||
| 1333 | Remove namespace 'moby:' from parameter string | ||
| 1334 | Case insensitive | ||
| 1335 | Returns : BOOLEAN | ||
| 1336 | Args : $articletype: a string | ||
| 1337 | Globals : none | ||
| 1338 | |||
| 1339 | =cut | ||
| 1340 | |||
| 1341 | sub _IsSecondary | ||
| 1342 | { | ||
| 1343 | my $articletype = shift(); | ||
| 1344 | |||
| 1345 | $articletype =~ s/^moby://; | ||
| 1346 | if ($articletype =~ /^parameter$/i) | ||
| 1347 | { | ||
| 1348 | return 1; | ||
| 1349 | } | ||
| 1350 | else | ||
| 1351 | { | ||
| 1352 | return 0; | ||
| 1353 | } | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | =head2 _rearrange (stolen from MOBY::CommonSubs) | ||
| 1357 | |||
| 1358 | |||
| 1359 | =cut | ||
| 1360 | |||
| 1361 | sub _rearrange | ||
| 1362 | { | ||
| 1363 | |||
| 1364 | # my $dummy = shift; | ||
| 1365 | my $order = shift; | ||
| 1366 | |||
| 1367 | return @_ unless (substr($_[0] || '', 0, 1) eq '-'); | ||
| 1368 | push @_, undef unless $#_ % 2; | ||
| 1369 | my %param; | ||
| 1370 | while (@_) | ||
| 1371 | { | ||
| 1372 | (my $key = shift) =~ tr/a-z\055/A-Z/d; #deletes all dashes! | ||
| 1373 | $param{$key} = shift; | ||
| 1374 | } | ||
| 1375 | map {$_ = uc($_)} @$order; # for bug #1343, but is there perf hit here? | ||
| 1376 | return @param{@$order}; | ||
| 1377 | } | ||
| 1378 | |||
| 1379 | |||
| 1380 | =head2 function _AnalyseServiceNotes | ||
| 1381 | |||
| 1382 | Title : _AnalyseServiceNotes | ||
| 1383 | Usage : _AnalyseServiceNotes($simple_bloc) | ||
| 1384 | Prerequisite : | ||
| 1385 | Function : Analyse a "Simple Bloc" from XSL transformation parsing | ||
| 1386 | Build a $rh_simple_article structure with fields: | ||
| 1387 | 'object_name' => moby:articleName | ||
| 1388 | 'object_type' => moby:Class | ||
| 1389 | 'object_namespace' => moby:namespace | ||
| 1390 | 'object_id' => moby:id | ||
| 1391 | 'object_content' => text content of simple article | ||
| 1392 | 'object_xml' => full xml content of article | ||
| 1393 | 'object_hasa' => ARRAYREF of hasa elements | ||
| 1394 | (each one is structured in a same | ||
| 1395 | structured hash (recursivity) | ||
| 1396 | 'object_crossreference' => ARRAYREF of crossreferences objects | ||
| 1397 | (each one is structured in a hash with fields | ||
| 1398 | 'type', 'id', 'namespace') | ||
| 1399 | |||
| 1400 | Returns : $services_notes: article HASHREF | ||
| 1401 | $ra_exceptions: article HASHREF | ||
| 1402 | Args : $service_notes_bloc: from parsing of a "serviceNotes" XSLT transformation | ||
| 1403 | Globals : none | ||
| 1404 | |||
| 1405 | =cut | ||
| 1406 | |||
| 1407 | sub _AnalyseServiceNotes | ||
| 1408 | { | ||
| 1409 | my $service_notes_block = shift(); | ||
| 1410 | my @a_exceptions = (); | ||
| 1411 | my $service_notes = ''; | ||
| 1412 | |||
| 1413 | |||
| 1414 | my $exception_start_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_START#'; | ||
| 1415 | my $exception_end_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_END#'; | ||
| 1416 | my $exception_refelement_start_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_REFELEMENT_START#'; | ||
| 1417 | my $exception_refelement_end_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_REFELEMENT_END#'; | ||
| 1418 | my $exception_refqueryid_start_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_REFQUERYID_START#'; | ||
| 1419 | my $exception_refqueryid_end_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_REFQUERYID_END#'; | ||
| 1420 | my $exception_severity_start_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_SEVERITY_START#'; | ||
| 1421 | my $exception_severity_end_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_SEVERITY_END#'; | ||
| 1422 | my $exception_code_start_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_CODE_START#'; | ||
| 1423 | my $exception_code_end_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_CODE_END#'; | ||
| 1424 | my $exception_message_start_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_MESSAGE_START#'; | ||
| 1425 | my $exception_message_end_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_EXCEPTION_MESSAGE_END#'; | ||
| 1426 | |||
| 1427 | |||
| 1428 | while ($service_notes_block =~ m/$exception_start_tag(.*)$exception_end_tag/g) | ||
| 1429 | { | ||
| 1430 | my $exception_block = $1; | ||
| 1431 | my ($refelement,$refqueryid,$severity,$code,$message) = ('','','','',''); | ||
| 1432 | if ($exception_block =~ /$exception_refelement_start_tag(.*)$exception_refelement_end_tag/) | ||
| 1433 | { | ||
| 1434 | $refelement = $1; | ||
| 1435 | } | ||
| 1436 | if ($exception_block =~ /$exception_refqueryid_start_tag(.*)$exception_refqueryid_end_tag/) | ||
| 1437 | { | ||
| 1438 | $refqueryid = $1; | ||
| 1439 | } | ||
| 1440 | if ($exception_block =~ /$exception_severity_start_tag(.*)$exception_severity_end_tag/) | ||
| 1441 | { | ||
| 1442 | $severity = $1; | ||
| 1443 | } | ||
| 1444 | if ($exception_block =~ /$exception_code_start_tag(.*)$exception_code_end_tag/) | ||
| 1445 | { | ||
| 1446 | $code = $1; | ||
| 1447 | } | ||
| 1448 | if ($exception_block =~ /$exception_message_start_tag(.*)$exception_message_end_tag/) | ||
| 1449 | { | ||
| 1450 | $message = $1; | ||
| 1451 | $message =~ s/__nl__/\n/g; | ||
| 1452 | |||
| 1453 | } | ||
| 1454 | |||
| 1455 | my %h_exception = ( | ||
| 1456 | 'refelement' => $refelement, | ||
| 1457 | 'refqueryid' => $refqueryid, | ||
| 1458 | 'severity' => $severity, | ||
| 1459 | 'code' => $code, | ||
| 1460 | 'message' => $message | ||
| 1461 | ); | ||
| 1462 | |||
| 1463 | |||
| 1464 | push(@a_exceptions, \%h_exception); | ||
| 1465 | } | ||
| 1466 | my $ra_exceptions = \@a_exceptions; | ||
| 1467 | if ($#a_exceptions < 0) | ||
| 1468 | { | ||
| 1469 | $ra_exceptions = ''; | ||
| 1470 | } | ||
| 1471 | |||
| 1472 | |||
| 1473 | my $notes_start_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_NOTES_START#'; | ||
| 1474 | my $notes_end_tag = '#XSL_LIPM_MOBYPARSER_SERVICENOTES_NOTES_END#'; | ||
| 1475 | |||
| 1476 | if ($service_notes_block =~ /$notes_start_tag(.*)$notes_end_tag/) | ||
| 1477 | { | ||
| 1478 | $service_notes = $1; | ||
| 1479 | } | ||
| 1480 | |||
| 1481 | return ($service_notes,$ra_exceptions); | ||
| 1482 | } | ||
| 1483 | |||
| 1484 | |||
| 1485 | 1; |