File Coverage

blib/lib/XML/NamespaceFactory.pm
Criterion Covered Total %
statement 33 33 100.0
branch 5 8 62.5
condition n/a
subroutine 11 11 100.0
pod 0 4 0.0
total 49 56 87.5


line stmt bran cond sub pod time code
1              
2             package XML::NamespaceFactory;
3 1     1   44289 use strict;
  1         3  
  1         61  
4 1     1   9 use Carp;
  1         2  
  1         135  
5              
6             # ABSTRACT: Simple factory objects for SAX namespaced names.
7             our $VERSION = '1.02'; # VERSION
8              
9 1     1   6 use vars qw($VERSION $AUTOLOAD);
  1         17  
  1         124  
10 1         8 use overload '""' => \&toString,
11             'cmp' => \&cmpString,
12 1     1   2095 '%{}' => \&toHash;
  1         1486  
13              
14             sub new {
15 1 50   1 0 82 my $class = ref($_[0]) ? ref(shift) : shift;
16 1         2 my $ns = shift;
17 1 50       4 confess "Parameter \$ns required." unless defined $ns;
18 1         4 return bless \$ns;
19             }
20              
21             sub AUTOLOAD {
22 1     1   16 $AUTOLOAD =~ s/^.*::([^:]+)/$1/;
23 1         4 return "{$_[0]}$AUTOLOAD";
24             }
25              
26             # overloaders
27 4     4 0 68 sub toString { return ${$_[0]}; }
  4         25  
28             sub toHash {
29 1     1 0 7 tie my %h, 'XML::NamespaceFactory::TiedHash', $_[0];
30 1         8 return \%h;
31             }
32             sub cmpString {
33 2     2 0 4 my $ns = shift;
34 2         4 my $cmp = shift;
35 2         3 my $rev = shift;
36 2 100       6 my $res = ( $$ns eq $cmp ) ? 0 : 1;
37 2 50       12 return $rev ? - $res : $res;
38             }
39              
40              
41             package XML::NamespaceFactory::TiedHash;
42              
43             sub TIEHASH {
44 1     1   2 my $class = shift;
45 1         2 my $ns = shift;
46 1         4 return bless [$ns], $class;
47             }
48              
49             sub FETCH {
50 1     1   2 my $self = shift;
51 1         2 my $key = shift;
52 1         7 return "{" . $self->[0] . "}" . $key;
53             }
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             XML::NamespaceFactory - Simple factory objects for SAX namespaced names.
66              
67             =head1 VERSION
68              
69             version 1.02
70              
71             =head1 SYNOPSIS
72              
73             use XML::NamespaceFactory;
74             my $FOO = XML::NamespaceFactory->new('http://foo.org/ns/');
75            
76             print $FOO->title; # {http://foo.org/ns/}title
77             print $FOO->{'bar.baz-toto'}; # {http://foo.org/ns/}bar.baz-toto
78              
79             =head1 DESCRIPTION
80              
81             Simply create a new XML::NamespaceFactory object with the namespace
82             you wish to use as its single parameter. If you wish to use the empty
83             namespace, simply pass in an empty string (but undef will not do).
84              
85             Then, when you want to get a JClark name, call a method on that object
86             the name of which is the local name you wish to have. It'll return the
87             JClark notation for that local name in your namespace.
88              
89             Unfortunately, some local names legal in XML are not legal in Perl. To
90             circumvent this, you can use the hash notation in which you access a key
91             on the object the name of which is the local name you wish to have. This
92             will work just as the method call name but will accept more characters.
93             Note that it does not check that the name you ask for is a valid XML
94             name. This form is more general but slower.
95              
96             If this is not clear, hopefully the SYNOPSIS should help :)
97              
98             =head1 ABSTRACT
99              
100             A number of accessors for namespaces in SAX use the JClark notation,
101             {namespace}local-name. Those are a bit painful to type repeatedly, and
102             somewhat error-prone as hash keys. This module makes life easier.
103              
104             =head1 AUTHORS
105              
106             =over 4
107              
108             =item *
109              
110             Robin Berjon <robin@knowscape.com>
111              
112             =item *
113              
114             Chris Prather <chris@prather.org>
115              
116             =back
117              
118             =head1 COPYRIGHT AND LICENSE
119              
120             This software is copyright (c) 2013 by Robin Berjon.
121              
122             This is free software; you can redistribute it and/or modify it under
123             the same terms as the Perl 5 programming language system itself.
124              
125             =cut