Changeset 1834

Show
Ignore:
Timestamp:
05/26/06 16:56:45
Author:
miyagawa
Message:

added globally_override

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • XML-Liberal/trunk/Changes

    r1829 r1834  
    11Revision history for Perl extension XML::Liberal 
     2 
     30.08  2006-05-26 16:53:00 JST 
     4        - Added globally_override method to override XML::LibXML->new globally 
    25 
    360.07  2006-05-26 
  • XML-Liberal/trunk/MANIFEST

    r1829 r1834  
    1515t/00_compile.t 
    1616t/01_bad.t 
     17t/02_global.t 
    1718t/bad/BAD-ampersand.xml 
    1819t/bad/BAD-atttibute.xml 
  • XML-Liberal/trunk/MANIFEST.bak

    r1829 r1834  
    1515t/00_compile.t 
    1616t/01_bad.t 
     17t/02_global.t 
    1718t/bad/BAD-ampersand.xml 
    1819t/bad/BAD-atttibute.xml 
  • XML-Liberal/trunk/lib/XML/Liberal.pm

    r1829 r1834  
    22 
    33use strict; 
    4 our $VERSION = '0.07'; 
     4our $VERSION = '0.08'; 
    55 
    66use base qw( Class::Accessor ); 
     
    2929        max_fallback => $max_fb, 
    3030    }, $class; 
     31} 
     32 
     33sub globally_override { 
     34    my $class = shift; 
     35    my $driver = shift || 'LibXML'; 
     36 
     37    my $subclass = "XML::Liberal::$driver"; 
     38       $subclass->require or die $@; 
     39 
     40    $subclass->do_globally_override; 
    3141} 
    3242 
     
    8898  my $doc = $parser->parse_string($broken_xml); 
    8999 
     100  # or, override XML::LibXML->new globally 
     101  use XML::LibXML; 
     102  use XML::Liberal; 
     103 
     104  XML::Liberal->globally_override('LibXML'); 
     105 
     106  my $parser = XML::LibXML->new; # isa XML::Liberal 
     107 
    90108=head1 DESCRIPTION 
    91109 
     
    95113B<This module is ALPHA SOFTWARE> and its API and internal class 
    96114layouts etc. are subject to change later. 
     115 
     116=head1 METHODS 
     117 
     118=over 4 
     119 
     120=item new 
     121 
     122  $parser = XML::Liberal->new('LibXML'); 
     123 
     124Creates an XML::Liberal object. Currently accepted driver is only I<LibXML>. 
     125 
     126=item globally_override 
     127 
     128  XML::Liberal->globally_override('LibXML'); 
     129 
     130Override XML::LibXML's new method globally, to create XML::Liberal 
     131object instead of XML::LibXML parser. 
     132 
     133This is considered B<so evil>, but would be useful if you have 
     134existent software/library that uses XML::LibXML inside and change the 
     135behaviour globally to use Liberal parser instead, with a single method 
     136call. 
     137 
     138For example, the following code lets XML::Atom's parser use Liberal 
     139LibXML parser. 
     140 
     141  use URI; 
     142  use XML::Atom::Feed; 
     143  use XML::Liberal; 
     144 
     145  XML::Liberal->globally_override('LibXML'); 
     146 
     147  # XML::Atom calls XML::LibXML->new, which is aliased to Liberal now 
     148  my $feed = XML::Atom::Feed->new(URI->new('http://example.com/atom.xml')); 
     149 
     150=back 
    97151 
    98152=head1 BUGS 
  • XML-Liberal/trunk/lib/XML/Liberal/LibXML.pm

    r1829 r1834  
    77use base qw( XML::Liberal::Driver ); 
    88 
     9our $XML_LibXML_new; 
     10 
     11sub do_globally_override { 
     12    my $class = shift; 
     13 
     14    no warnings 'redefine'; 
     15    unless ($XML_LibXML_new) { 
     16        $XML_LibXML_new = \&XML::LibXML::new; 
     17        *XML::LibXML::new = sub { XML::Liberal->new('LibXML') }; 
     18    } 
     19 
     20    1; 
     21} 
     22 
    923sub init { 
    1024    my $self = shift; 
    1125    $self->SUPER::init(); 
    12     $self->{parser} = XML::LibXML->new; 
     26    $self->{parser} = $XML_LibXML_new 
     27        ? $XML_LibXML_new->('XML::LibXML') : XML::LibXML->new; 
    1328} 
    1429