mach 2003/12/10 14:45:19
Modified: lib/Apache/AxKit Tag: axkit-pipeline-2 Provider.pm Log: get_strref only called once on providers. Added get_xml_info method. Avoiding calling get_styles when not needed. Will be moving get_styles from here soon. Revision Changes Path No revision No revision 1.18.2.1 +145 -83 xml-axkit/lib/Apache/AxKit/Provider.pm Index: Provider.pm =================================================================== RCS file: /home/cvs/xml-axkit/lib/Apache/AxKit/Provider.pm,v retrieving revision 1.18 retrieving revision 1.18.2.1 diff -u -r1.18 -r1.18.2.1 --- Provider.pm 3 Oct 2003 18:38:38 -0000 1.18 +++ Provider.pm 10 Dec 2003 22:45:19 -0000 1.18.2.1 @@ -5,6 +5,7 @@ use Apache::AxKit::Exception; use Apache::Constants qw(OK DECLINED); +use AxKit; # use vars qw/$COUNT/; @@ -130,6 +131,7 @@ ); } + sub get_dom { my $self = shift; require Apache::AxKit::LibXMLSupport; @@ -142,21 +144,33 @@ $XML::LibXML::read_cb, $XML::LibXML::close_cb); Apache::AxKit::LibXMLSupport->reset($parser); + my $r = $self->apache_request(); + my $xml_doc; - eval { - my $fh = $self->get_fh(); - AxKit::Debug(8, "Provider::get_dom/parse_fh($fh, ", $self->{apache}->uri(), ")"); - $xml_doc = $parser->parse_fh($fh, $self->{apache}->uri()); - }; - if ($@) { - use bytes; - my $xmlstring = ${$self->get_strref()}; - AxKit::Debug(8, "Provider::get_dom/parse_string($xmlstring, ", $self->{apache}->uri(), ")"); - $xml_doc = $parser->parse_string($xmlstring, $self->{apache}->uri()) || + my $str; + if($str = $r->pnotes('xml_string')) { + use bytes; + AxKit::Debug(8, "Provider::get_dom/parse_string($str, ", $self->{apache}->uri(), ")"); + $xml_doc = $parser->parse_string($str, $self->{apache}->uri()) || throw Apache::AxKit::Exception::Error( -text => "XML::LibXML->parse_string returned nothing!" ); - } + } else { + eval { + my $fh = $self->get_fh(); + AxKit::Debug(8, "Provider::get_dom/parse_fh($fh, ", $self->{apache}->uri(), ")"); + $xml_doc = $parser->parse_fh($fh, $self->{apache}->uri()); + }; + if ($@) { + use bytes; + my $xmlstring = ${$self->get_strref()}; + AxKit::Debug(8, "Provider::get_dom/parse_string($xmlstring, ", $self->{apache}->uri(), ")"); + $xml_doc = $parser->parse_string($xmlstring, $self->{apache}->uri()) || + throw Apache::AxKit::Exception::Error( + -text => "XML::LibXML->parse_string returned nothing!" + ); + } + } return $xml_doc; } @@ -228,9 +242,7 @@ undef $pref_style; } - my $xml_styles = []; - my $vals = []; - + my @styles; my $key = $self->key(); # need to extract the following from the XML file: @@ -238,68 +250,32 @@ # DTD filename # Root element name (including namespace) # use three element array @$vals + if( $AxKit::Cfg->IfAxAddFooProcessors($media, $pref_style) || ! $AxKit::Cfg->IgnoreStylePI()) { + + my ($doctype, $dtd, $root, $xml_styles) = $self->get_xml_info( $media, $pref_style ); + + foreach my $style (@$xml_styles) { + $style->{title} ||= '#default'; + } + + # Let GetMatchingProcessors to process the @$styles array + { + local $^W; # suppress "Use of uninitialized value" warnings + AxKit::Debug(4, "Calling GetMatchingProcessors with ($media, $pref_style, $doctype, $dtd, $root)"); + } + @styles = $AxKit::Cfg->GetMatchingProcessors($media, $pref_style, $doctype, $dtd, $root, $xml_styles, $self); + } else{ + { + local $^W; # suppress "Use of uninitialized value" warnings + AxKit::Debug(4, "No AxAdd*Processors detected, bypassing get_styles()"); + AxKit::Debug(4, "Calling GetMatchingProcessors with ($media, $pref_style)."); + } - if (defined &Apache::AxKit::Provider::xs_get_styles_fh) { - AxKit::Debug(2, "using XS get_styles (libxml2)"); - my ($xs_styles, $doctype, $dtd, $root) = - $self->xs_get_styles($media, $pref_style); - @$xml_styles = @$xs_styles unless $AxKit::Cfg->IgnoreStylePI(); - @$vals = ($doctype, $dtd, $root); - } - else { - require XML::Parser; - - AxKit::Debug(4, "get_styles: creating XML::Parser"); - - my $handlers = { - Start => \&parse_start, - Doctype => \&parse_dtd, - $AxKit::Cfg->IgnoreStylePI() ? () : (Proc => \&parse_pi), - }; - - my $xml_parser = XML::Parser->new( - Namespaces => 1, - ErrorContext => 2, - Handlers => $handlers, - ); - - my $to_parse; - eval { - $to_parse = $self->get_fh(); - }; - if ($@) { - $to_parse = ${ $self->get_strref(); }; - } - - AxKit::Debug(4, "get_styles: calling XML::Parser->parse('$key')"); - $xml_parser->parse( - $to_parse, - XMLStyle_preferred => $pref_style, - XMLStyle_media => $media, - XMLStyle_style => $xml_styles, - XMLStyle_vals => $vals, - XMLStyle_style_screen => [], - ); - - AxKit::Debug(4, "get_styles: parse returned successfully"); - } - - foreach my $style (@$xml_styles) { - $style->{title} ||= '#default'; - } + @styles = $AxKit::Cfg->GetMatchingProcessors($media, $pref_style, undef, undef, undef, [], $self); - # Let GetMatchingProcessors to process the @$styles array - { - local $^W; # suppress "Use of uninitialized value" warnings - AxKit::Debug(4, "Calling GetMatchingProcessors with ($media, $pref_style, $vals->[0], $vals->[1], $vals->[2])"); } - my @styles = $AxKit::Cfg->GetMatchingProcessors($media, - $pref_style, @$vals[0 .. 2], $xml_styles, $self); - if ([EMAIL PROTECTED]) { - throw Apache::AxKit::Exception::Declined( - reason => "No styles defined for '$key'" - ); + throw Apache::AxKit::Exception::Declined( reason => "No styles defined for '$key'" ); } # get mime-type => module mapping @@ -331,20 +307,105 @@ return [EMAIL PROTECTED]; } +sub get_xml_info{ + my ($self, $media, $pref_style ) = @_; + + # This is where the sniffing of the xml content happens. + + my $r = $self->apache_request(); + my @ret; + eval{ + my $fh = $self->get_fh(); + my $pos = eval { tell $fh; }; + if($@) { + # fh is not seekable, thus we must slurp file at this point. + undef $@; + local($/) = undef; + my $string = <$fh>; + $r->pnotes('xml_string', $string); + @ret = parse_xml_info( $r, undef, \$string, $media, $pref_style); + } else { + # seekable. + @ret = parse_xml_info( $r, $fh, undef, $media, $pref_style); + seek $fh, 0, $pos; + } + }; + if($@) { + my $str_ref = $self->get_strref(); + $r->pnotes('xml_string', ${$str_ref}); + @ret = parse_xml_info( $r, undef, $str_ref, $media, $pref_style); + } + return @ret; +} + +sub parse_xml_info{ + my ($r, $fh, $str_ref, $media, $pref_style) = @_; + + if ($pref_style eq '#default') { + undef $pref_style; + } + + my $xml_styles = []; + my $vals = []; + my @styles; + + if (defined &Apache::AxKit::Provider::xs_get_styles_fh) { + AxKit::Debug(2, "using XS get_styles (libxml2)"); + my ($xs_styles, $doctype, $dtd, $root) = xs_get_styles($r, $fh, $str_ref, $media, $pref_style); + @$xml_styles = @$xs_styles unless $AxKit::Cfg->IgnoreStylePI(); + @$vals = ($doctype, $dtd, $root); + } + else { + require XML::Parser; + + AxKit::Debug(4, "get_styles: creating XML::Parser"); + + my $handlers = { + Start => \&parse_start, + Doctype => \&parse_dtd, + $AxKit::Cfg->IgnoreStylePI() ? () : (Proc => \&parse_pi), + }; + + my $xml_parser = XML::Parser->new( + Namespaces => 1, + ErrorContext => 2, + Handlers => $handlers, + ); + + my $to_parse = $fh || ${$str_ref}; + #eval { + # $to_parse = $provider->get_fh(); + #}; + #if ($@) { + # $to_parse = ${ $provider->get_strref(); }; + #} + + AxKit::Debug(4, "get_styles: calling XML::Parser->parse()"); + $xml_parser->parse( + $to_parse, + XMLStyle_preferred => $pref_style, + XMLStyle_media => $media, + XMLStyle_style => $xml_styles, + XMLStyle_vals => $vals, + XMLStyle_style_screen => [], + ); + + AxKit::Debug(4, "get_styles: parse returned successfully"); + } + + return (@$vals, $xml_styles); +} + sub xs_get_styles { - my $self = shift; - my ($media, $pref_style) = @_; + my ($r, $fh, $strref, $media, $pref_style) = @_; my $bits; - eval { - my $fh = $self->get_fh(); + if(defined($fh)) { AxKit::Debug(4, "calling xs_get_styles_fh()"); - $bits = xs_get_styles_fh($self->apache_request, $fh); - }; - if ($@) { - my $strref = $self->get_strref(); + $bits = xs_get_styles_fh($r, $fh); + } else { AxKit::Debug(4, "calling xs_get_styles_str()"); - $bits = xs_get_styles_str($self->apache_request, $$strref); + $bits = xs_get_styles_str($r, $$strref); } my @xml_stylesheet = @{$bits->[0]}; @@ -366,7 +427,7 @@ XMLStyle_style_screen => [], XMLStyle_preferred => $pref_style, XMLStyle_media => $media, - }; + }; foreach my $pi (@xml_stylesheet) { parse_pi($e, "xml-stylesheet", $pi); @@ -536,6 +597,7 @@ } } } + 1; __END__