XML-Namespace-0.02/0000755000175000017500000000000010302356054013327 5ustar abwabw00000000000000XML-Namespace-0.02/t/0000755000175000017500000000000010302356054013572 5ustar abwabw00000000000000XML-Namespace-0.02/t/namespace.t0000644000175000017500000000443510302354225015717 0ustar abwabw00000000000000#!/usr/bin/perl -w # -*- perl -*- # # Test the XML::Namespace module. # # Written by Andy Wardley # # This is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # use strict; use warnings; use lib qw( ./lib ../lib ); use XML::Namespace; use Test::More tests => 10; my $xsd = 'http://www.w3.org/2001/XMLSchema#'; my $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; #------------------------------------------------------------------------ # test XML::Namespace object #------------------------------------------------------------------------ my $ns = XML::Namespace->new($xsd); is( $ns->uri, "$xsd", 'got xsd uri' ); is( $ns->uri('integer'), "${xsd}integer", 'got xsd integer via uri()' ); is( $ns->integer, "${xsd}integer", 'got xsd integer via integer()' ); #------------------------------------------------------------------------ # check we get an error if no URI parameter passed #------------------------------------------------------------------------ eval { my $dud = XML::Namespace->new(); }; ok( $@, 'missing URI parameter error'); #------------------------------------------------------------------------ # test overloaded methods #------------------------------------------------------------------------ my $ns1 = XML::Namespace->new($xsd); my $ns2 = XML::Namespace->new($xsd); is( "$ns1", "$xsd", 'got xsd uri through stringification' ); ok( $ns1 eq $ns2, 'compared namespaces' ); #------------------------------------------------------------------------ # test the import method #------------------------------------------------------------------------ use XML::Namespace xsd => 'http://www.w3.org/2001/XMLSchema#', rdf => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' ; is( xsd->uri('integer'), "${xsd}integer", 'got xsd integer via uri()' ); is( xsd->integer, "${xsd}integer", 'got xsd integer' ); is( rdf->type, "${rdf}type", 'got rdf type' ); #------------------------------------------------------------------------ # test the import method directly #------------------------------------------------------------------------ use XML::Namespace; XML::Namespace->import( foo => 'http://myfoo.com/' ); is( foo()->hello, 'http://myfoo.com/hello', 'hello foo' ); XML-Namespace-0.02/lib/0000755000175000017500000000000010302356054014075 5ustar abwabw00000000000000XML-Namespace-0.02/lib/XML/0000755000175000017500000000000010302356054014535 5ustar abwabw00000000000000XML-Namespace-0.02/lib/XML/Namespace.pm0000644000175000017500000002115610302355777017007 0ustar abwabw00000000000000#============================================================= -*-perl-*- # # XML::Namespace # # Simple support for XML Namespaces. # # Written by Andy Wardley # # This is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # $Id: Namespace.pm,v 1.2 2005/08/22 14:04:04 abw Exp $ # #======================================================================== package XML::Namespace; use base 'Exporter'; use strict; use warnings; our $VERSION = 0.02; our $AUTOLOAD; our @EXPORT_OK; use overload '""' => \&uri, fallback => 1; #------------------------------------------------------------------------ # import(@symbols) # # Method called by Exporter base class when the module is loaded via # a C statement. Any arguments provided are passed # to the import() method as @symbols. These should be pairs of # (xml_namespace => uri) arguments. The method constructs an XML::Namespace # object for each pair, and a closure subroutine with the same name as # the XML namespace, which simply returns the object. This is then exported # to the caller's package namespace. #------------------------------------------------------------------------ sub import { my $class = shift; my @symbols = @_; my (@imports, $planned); while (@symbols) { no strict 'refs'; my $ns = shift @symbols; my $uri = shift @symbols || die "no URI provided for namespace $ns in 'use $class' statement"; my $obj = $class->new($uri); *{"$class\::$ns"} = sub { return $obj }; push(@imports, $ns); push(@EXPORT_OK, $ns); } $class->export_to_level(1, $class, @imports) if @imports } #------------------------------------------------------------------------ # new($uri) # # A simple object constructed as a reference to the URI passed as an # argument. #------------------------------------------------------------------------ sub new { my $class = shift; my $uri = shift || die "no URI parameter provided for $class new() method"; bless \$uri, $class; } #------------------------------------------------------------------------ # uri() # uri($path) # # Returns the URI for the namespace object, with an optional path # argument added to the end of it. #------------------------------------------------------------------------ sub uri { my $self = shift; my $path = shift || ''; return "$$self$path"; } #------------------------------------------------------------------------ # AUTOLOAD # # Catches all method calls (expect import(), new() and uri(), obviously) # and delegates them to $self->uri() to resolve. #------------------------------------------------------------------------ sub AUTOLOAD { my $self = shift; my $path = $AUTOLOAD; $path =~ s/^.*:://; return if $path eq 'DESTROY'; return $self->uri($path); } 1; __END__ =head1 NAME XML::Namespace - Simple support for XML Namespaces =head1 SYNOPSIS Example 1: using XML::Namespace objects use XML::Namespace; my $xsd = XML::Namespace->new('http://www.w3.org/2001/XMLSchema#'); # explicit access via the uri() method print $xsd->uri(); # http://www.w3.org/2001/XMLSchema# print $xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer # implicit access through AUTOLOAD method print $xsd->integer; # http://www.w3.org/2001/XMLSchema#integer Example 2: importing XML::Namespace objects use XML::Namespace xsd => 'http://www.w3.org/2001/XMLSchema#', rdf => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; # xsd and rdf are imported subroutines that return # XML::Namespace objects which can be used as above print xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer print xsd->integer; # http://www.w3.org/2001/XMLSchema#integer =head1 DESCRIPTION This module implements a simple object for representing XML Namespaces in Perl. It provides little more than some syntactic sugar for your Perl programs, saving you the bother of typing lots of long-winded URIs. It was inspired by the Class::RDF::NS module distributed as part of Class::RDF. =head2 Using XML::Namespace Objects First load the XML::Namespace module. use XML::Namespace; Then create an XML::Namespace object. my $xsd = XML::Namespace->new('http://www.w3.org/2001/XMLSchema#'); Then use the uri() method to return an absolute URI from a relative path. print $xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer Alternately, use the AUTOLOAD method to map method calls to the uri() method. print $xsd->integer; # http://www.w3.org/2001/XMLSchema#integer =head2 Importing XML::Namespace Objects When you C the XML::Namespace module, you can specify a list of namespace definitions. use XML::Namespace xsd => 'http://www.w3.org/2001/XMLSchema#', rdf => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; This defines the C and C subroutines and exports them into the calling package. The subroutines simply return XML::Namespace objects initialised with the relevant namespace URIs. print xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer print xsd->integer; # http://www.w3.org/2001/XMLSchema#integer =head2 Overloaded Stringification Method The XML::Namespace module overloads the stringification operator to return the namespace URI. my $xsd = XML::Namespace->new('http://www.w3.org/2001/XMLSchema#'); print $xsd; # http://www.w3.org/2001/XMLSchema# =head1 METHODS =head2 new($uri) Constructor method which creates a new XML::Namespace object. It expects a single argument denoting the URI that the namespace is to represent. use XML::Namespace; my $xsd = XML::Namespace->new('http://www.w3.org/2001/XMLSchema#'); =head2 uri($path) When called without arguments, this method returns the URI of the namespace object, as defined by the argument passed to the new() constructor method. $xsd->uri(); # http://www.w3.org/2001/XMLSchema# An argument can be passed to indicate a path relative to the namespace URI. The method returns a simple concatenation of the namespace URI and the relative path argument. $xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer =head2 import($name,$uri,$name,$uri,...) This method is provided to work with the Exporter mechanism. It expects a list of C<($name, $uri)> pairs as arguments. It creates XML::Namespace objects and accessor subroutines that are then exported to the caller's package. Although not intended for manual invocation, there's nothing to stop you from doing it. use XML::Namespace; XML::Namespace->import( xsd => 'http://www.w3.org/2001/XMLSchema#' ); xsd()->integer; # http://www.w3.org/2001/XMLSchema#integer Note that the parentheses are required when accessing this subroutine. xsd()->integer; # Good xsd->integer; # Bad Unlike those that are defined automatically by the Importer, Perl doesn't know anything about these subroutines at compile time. Without the parentheses, Perl will think you're trying to call the C method on an unknown C package and you'll see an error like: Can't locate object method "integer" via package "xsd" That's why it's better to define your namespaces when you load the XML::Namespace module. use XML::Namespace xsd => 'http://www.w3.org/2001/XMLSchema#'; xsd->integer; # Good =head2 AUTOLOAD The module defines an AUTOLOAD method that maps all other method calls to the uri() method. Thus, the following return the same value. $xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer $xsd->integer; # http://www.w3.org/2001/XMLSchema#integer =head1 AUTHOR Andy Wardley Emailto:abw@cpan.orgE =head1 VERSION This is version 0.02 of XML::Namespace. =head1 COPYRIGHT Copyright (C) 2005 Andy Wardley. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO The L module, distributed as part of L, provided the inspiration for the module. XML::Namespace essentially does the same thing, albeit in a slightly different way. It's also available as a stand-alone module for use in places unrelated to RDF. The L module also implements similar functionality to L, but instead uses the JClark notation (e.g. "{http://foo.org/ns/}title"). =cut # Local Variables: # mode: perl # perl-indent-level: 4 # indent-tabs-mode: nil # End: # # vim: expandtab shiftwidth=4: XML-Namespace-0.02/README0000644000175000017500000000327710302356037014221 0ustar abwabw00000000000000NAME XML::Namespace - Simple support for XML Namespaces SYNOPSIS Example 1: using XML::Namespace objects use XML::Namespace; my $xsd = XML::Namespace->new('http://www.w3.org/2001/XMLSchema#'); # explicit access via the uri() method print $xsd->uri(); # http://www.w3.org/2001/XMLSchema# print $xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer # implicit access through AUTOLOAD method print $xsd->integer; # http://www.w3.org/2001/XMLSchema#integer Example 2: importing XML::Namespace objects use XML::Namespace xsd => 'http://www.w3.org/2001/XMLSchema#', rdf => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; # xsd and rdf are imported subroutines that return # XML::Namespace objects which can be used as above print xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer print xsd->integer; # http://www.w3.org/2001/XMLSchema#integer DESCRIPTION This module implements a simple object for representing XML Namespaces in Perl. It provides little more than some syntactic sugar for your Perl programs, saving you the bother of typing lots of long-winded URIs. It was inspired by the Class::RDF::NS module distributed as part of Class::RDF. For further details, please see the documentation accompanying the module. AUTHOR Andy Wardley VERSION This is version 0.02 of XML::Namespace. COPYRIGHT Copyright (C) 2005 Andy Wardley. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. XML-Namespace-0.02/Makefile.PL0000644000175000017500000000067410302354275015313 0ustar abwabw00000000000000#!/usr/bin/perl -w # -*- perl -*- use strict; use ExtUtils::MakeMaker; my %opts = ( 'NAME' => 'XML::Namespace', 'VERSION_FROM' => 'lib/XML/Namespace.pm', 'PMLIBDIRS' => [ 'lib' ], ); if ($ExtUtils::MakeMaker::VERSION >= 5.43) { $opts{ AUTHOR } = 'Andy Wardley '; $opts{ ABSTRACT } = 'Simple support for XML Namespaces', } WriteMakefile( %opts ); XML-Namespace-0.02/META.yml0000644000175000017500000000046610302356054014606 0ustar abwabw00000000000000# http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: XML-Namespace version: 0.02 version_from: lib/XML/Namespace.pm installdirs: site requires: distribution_type: module generated_by: ExtUtils::MakeMaker version 6.17 XML-Namespace-0.02/MANIFEST0000644000175000017500000000051010302354330014447 0ustar abwabw00000000000000MANIFEST This list of files README The README file Makefile.PL Perl script to generate Makefile lib/XML/Namespace.pm The XML::Namespace module itself t/namespace.t A test script for the XML::Namespace module META.yml Module meta-data (added by MakeMaker)