| 1 | |||||||||||
| 2 | package XML::XPathExt; | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use vars qw($VERSION $hasXSLT $hasXPCtx); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | $VERSION = '0.01_01'; | |||||||||
| 7 | |||||||||||
| 8 | 0 | ▶ | eval 'use XML::LibXSLT; $hasXSLT = 1;'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||
| 9 | 0 | ▶ | eval 'use XML::XPathContext; $hasXPCtx = 1;'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=3 SCAR=15.0
| |||||||||||
| 12 | 1 | ▶ | if ($hasXSLT) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 13 | 0 | my $pkg = caller; | |||||||||
| 14 | 1 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 0 | for my $prms (@{$pkg . '::EXTENSIONS'}) { | |||||||||
| 16 | 0 | XML::LibXSLT->register_function(@$prms); | |||||||||
| 17 | } | ||||||||||
| 18 | 0 | return 1; | |||||||||
| 19 | } | ||||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | sub registerOnContext { | ||||||||
|
Subroutine
registerOnContext: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 23 | 0 | ▶ | die "This method require XML::XPathContext" unless $hasXPCtx; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | my $class = shift; | |||||||||
| 25 | 0 | my $ctx = shift; | |||||||||
| 26 | 0 | for my $prms (@{$class . '::EXTENSIONS'}) { | |||||||||
| 27 | 0 | $ctx->registerFunctionNS(@$prms); | |||||||||
| 28 | } | ||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 1 | 1; | |||||||||
| 32 | |||||||||||
| 33 | 1 | 1; | |||||||||
| 34 | |||||||||||
| 35 | =pod | ||||||||||
| 36 | |||||||||||
| 37 | =head1 NAME | ||||||||||
| 38 | |||||||||||
| 39 | XML::XPathExt - Common XPath extension framework | ||||||||||
| 40 | |||||||||||
| 41 | =head1 SYNOPSIS | ||||||||||
| 42 | |||||||||||
| 43 | package XML::XPathExt::MyCoolExtension; | ||||||||||
| 44 | use base 'XML::XPathExt'; | ||||||||||
| 45 | |||||||||||
| 46 | my $ns = "http://perl-xml-rocks.org/java-xml-sucks/"; | ||||||||||
| 47 | our @EXTENSIONS = ( [ $ns, 'func1', \&my_func1 ], | ||||||||||
| 48 | [ $ns, 'func2', \&my_func2 ], | ||||||||||
| 49 | [ $ns, 'func3', \&my_func3 ],); | ||||||||||
| 50 | |||||||||||
| 51 | # your functions... | ||||||||||
| 52 | |||||||||||
| 53 | =head1 WARNING, HEY, WARNING, YES YOU | ||||||||||
| 54 | |||||||||||
| 55 | THIS IS EVEN BEFORE AN ALPHA, IT IS A PROOF OF CONCEPT THAT HAS NOT BEEN | ||||||||||
| 56 | TESTED. | ||||||||||
| 57 | |||||||||||
| 58 | SURGEON GENERAL WARNING: SMOKING PRE-ALPHA MODULES IN PRODUCTION MAY | ||||||||||
| 59 | ENSURE JOB SECURITY OF CONSULTANTS. | ||||||||||
| 60 | |||||||||||
| 61 | USING THIS YOU ARE IN THE DARK, YOU MAY BE EATEN BY A GRUE. | ||||||||||
| 62 | |||||||||||
| 63 | =head1 DESCRIPTION | ||||||||||
| 64 | |||||||||||
| 65 | This is a simple module, the goal of which is to help make XPath extensions | ||||||||||
| 66 | consistent in such a way that they work with both XML::LibXSLT and XML::XPathContext. | ||||||||||
| 67 | |||||||||||
| 68 | I would very much like to support other modules, but that will require more work | ||||||||||
| 69 | (mostly around having factory methods that do the right thing to convert to the | ||||||||||
| 70 | right objects, which is simple enough but long -- patches welcome). | ||||||||||
| 71 | |||||||||||
| 72 | Your modules implementing XPath extensions should inherit from this class. It will | ||||||||||
| 73 | do two things for them: if c<XML::LibXSLT> is present, when your class is loaded its | ||||||||||
| 74 | extensions will be registered automatically; and you will inherit a C<registerOnContext> | ||||||||||
| 75 | method that when called with an C<XML::XPathContext> context object will register all | ||||||||||
| 76 | your extensions on it (it can't be done automatically as for C<XML::LibXSLT> because | ||||||||||
| 77 | and instance of the class is required). | ||||||||||
| 78 | |||||||||||
| 79 | There's a special variable that should exist and be publically available in your | ||||||||||
| 80 | package called C<@EXTENSIONS>. It is an array containing arrayrefs. The synopsis | ||||||||||
| 81 | should be pretty clear (hopefully) but in case it is not, each of those arrayrefs | ||||||||||
| 82 | contains three items: the namespace URI, the name of the extension function, and | ||||||||||
| 83 | a reference to its Perl implementation. Behaviour of extension functions with no | ||||||||||
| 84 | defined namespace is not guaranteed and even if it works for you it may very well | ||||||||||
| 85 | blow up in other cases. So it is quite a bad idea to not use a namespace. | ||||||||||
| 86 | |||||||||||
| 87 | =head1 GRATUITOUS PONTIFICATING | ||||||||||
| 88 | |||||||||||
| 89 | It is recommended that your extension modules be under the C<XML::XPathExt::*> hierarchy, | ||||||||||
| 90 | though of course if you have good reasons to put them elsewhere you are totally free | ||||||||||
| 91 | to do as you wish. | ||||||||||
| 92 | |||||||||||
| 93 | It is also B<STRONGLY> recommended that the namespace you choose for your extensions | ||||||||||
| 94 | be an C<http:> URI, and not something much harder to retrieve such as a URN. The reason | ||||||||||
| 95 | for this is that in the close future (as of this writing, in August 2003) it is likely | ||||||||||
| 96 | that the W3C will publish a Note on RDDL indicating documents to be put at the end of | ||||||||||
| 97 | a namespace URI. Interesting things that could be useful for this module could be | ||||||||||
| 98 | discovered through such a mechanism, using the C<XML::RDDL> module. | ||||||||||
| 99 | |||||||||||
| 100 | Also, if you wish to make your namespace URIs easy to remember for your users, I suggest | ||||||||||
| 101 | you use the form C<http://xmlns.perl.org/xpath/MyExtension/> where C<MyExtension> matches | ||||||||||
| 102 | the one in C<XML::XPathExt::MyExtension>. The C<xmlns.perl.org> has already been registered | ||||||||||
| 103 | with the perl.org admin (thanks!) and should RDDL be published then a corresponding server | ||||||||||
| 104 | will be made available to the community shortly thereafter to simply RDDL publishing. | ||||||||||
| 105 | |||||||||||
| 106 | =head1 AUTHOR | ||||||||||
| 107 | |||||||||||
| 108 | Robin Berjon, E<lt>robin.berjon@expway.frE<gt> | ||||||||||
| 109 | |||||||||||
| 110 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 111 | |||||||||||
| 112 | Copyright 2003 by Robin Berjon | ||||||||||
| 113 | |||||||||||
| 114 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 115 | it under the same terms as Perl itself. | ||||||||||
| 116 | |||||||||||
| 117 | =cut | ||||||||||