File Coverage

blib/lib/ODO/Parser/XML.pm
Criterion Covered Total %
statement 45 52 86.5
branch 6 14 42.8
condition 2 6 33.3
subroutine 12 13 92.3
pod 4 4 100.0
total 69 89 77.5


line stmt bran cond sub pod time code
1             #
2             # Copyright (c) 2006 IBM Corporation.
3             #
4             # All rights reserved. This program and the accompanying materials
5             # are made available under the terms of the Eclipse Public License v1.0
6             # which accompanies this distribution, and is available at
7             # http://www.eclipse.org/legal/epl-v10.html
8             #
9             # File: $Source: /var/lib/cvs/ODO/lib/ODO/Parser/XML.pm,v $
10             # Created by: Stephen Evanchik( evanchik@us.ibm.com )
11             # Created on: 10/01/2006
12             # Revision: $Id: XML.pm,v 1.6 2010-02-11 18:27:59 ubuntu Exp $
13             #
14             # Contributors:
15             # IBM Corporation - initial API and implementation
16             #
17             package ODO::Parser::XML;
18              
19 4     4   54616 use strict;
  4         8  
  4         140  
20 4     4   24 use warnings;
  4         6  
  4         105  
21              
22 4     4   525 use ODO::Exception;
  4         10  
  4         169  
23              
24 4     4   3937 use XML::SAX::ParserFactory;
  4         23519  
  4         140  
25              
26 4     4   860 use Module::Load::Conditional qw/can_load/;
  4         26645  
  4         245  
27 4     4   22 use vars qw /$VERSION/;
  4         9  
  4         267  
28             $VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /: (\d+)\.(\d+)/;
29 4     4   63 use base qw/ODO::Parser/;
  4         9  
  4         2638  
30              
31             our $_BACKEND = 'ODO::Parser::XML::Slow';
32              
33              
34 4         36 use Class::Interfaces('ODO::Parser::XML'=>
35             {
36             'isa'=> 'ODO::Parser',
37             'methods'=> [ 'parse_rdf' ],
38             }
39 4     4   40 );
  4         8  
40              
41             =head1 NAME
42              
43             ODO::Parser::XML - Parser for statements serialized to RDF/XML
44              
45             =head1 SYNOPSIS
46              
47             use ODO::Parser::XML;
48            
49             my $statements = ODO::Parser::XML->parse_file('some/path/to/data.rdfxml');
50            
51             my $rdf = ' ... rdf xml here ... ';
52             my $other_statements = ODO::Parser::XML->parse(\$rdf);
53            
54             =head1 DESCRIPTION
55              
56             RDF/XML parser that implements the L interface.
57              
58             =head1 METHODS
59              
60             =over
61              
62             =item parse( )
63              
64             =item select_parser( )
65              
66             This subroutine allows you to select the SAX implementation that is used by this SAX parser. Argument is a scalar string.
67              
68             The following options are available, but are not limited to:
69              
70             =over
71              
72             =item XML::LibXML - not actually a SAX engine, but emits SAX events
73              
74             =item XML::LibXML::SAX - a SAX parser provided by XML::LibXML
75              
76             =item XML::LibXML::SAX::Parser - another SAX parser provided by XML::LibXML; the one used by default. Not sure how different it is from XML::LibXML::SAX
77              
78             =item XML::SAX::PurePerl - pure perl implementation; not very efficient.
79              
80             =back
81              
82             =back
83              
84             =cut
85              
86              
87             sub parse {
88 1     1 1 992 my ($self, $rdf, %parameters) = @_;
89              
90 1 50       6 $self = $self->SUPER::new(%parameters)
91             unless(ref $self);
92 1         4 my ($statements, $imports) = $self->parse_rdf($rdf);
93 0         0 return ($statements, $imports);
94             }
95              
96              
97             =item parse_file( )
98              
99             =cut
100              
101             sub parse_file {
102 3     3 1 49 my ($self, $filename, %parameters) = @_;
103              
104 3 50       96 throw ODO::Exception::File::Missing(error=> "Could not locate file: $filename")
105             unless(-e $filename);
106              
107 3 50       42 $self = $self->SUPER::new(%parameters)
108             unless(ref $self);
109            
110 3         265 open(RDF_FILE, $filename);
111 3         23 my ($statements, $imports) = $self->parse_rdf(\*RDF_FILE);
112 0         0 close(RDF_FILE);
113            
114 0         0 return ($statements, $imports);
115             }
116              
117              
118             sub init {
119 4     4 1 204 my ($self, $config) = @_;
120 4   33     31 my $parser_backend = $config->{'backend'} || $_BACKEND;
121 4         7 my $sax_parser = $config->{'sax_parser'};
122 4         37 delete $config->{'backend'};
123 4         13 delete $config->{'sax_parser'};
124            
125             # set the sax parser
126 4 50       16 $self->select_parser($sax_parser) if defined $sax_parser;
127            
128 4         27 my $backend_loaded = can_load( modules => {$parser_backend=> undef } );
129              
130 4 50 33     223 throw ODO::Exception::Module(error=> "Could not load RDF/XML parser: '$parser_backend'\n==> $@")
131             if(!defined($backend_loaded) && UNIVERSAL::can($parser_backend, 'new'));
132              
133 4         11 my $rdf_xml_parser = $parser_backend->new(%{ $config });
  4         65  
134            
135 4         100 return $rdf_xml_parser;
136             }
137              
138             sub select_parser {
139 0     0 1 0 my $self = shift;
140 0         0 my $parser = shift;
141 0 0       0 $parser = 'XML::LibXML::SAX::Parser' unless defined $parser;
142             # warn("parser chosen: $parser");
143 0         0 $XML::SAX::ParserPackage = $parser;
144             }
145              
146              
147              
148              
149              
150             sub import {
151 4     4   51 my ($package, %options) = @_;
152 4 50       3146 $_BACKEND = $options{'backend'}
153             if(exists($options{'backend'}));
154             }
155              
156              
157             =back
158              
159             =head1 COPYRIGHT
160              
161             Copyright (c) 2006 IBM Corporation.
162              
163             All rights reserved. This program and the accompanying materials
164             are made available under the terms of the Eclipse Public License v1.0
165             which accompanies this distribution, and is available at
166             http://www.eclipse.org/legal/epl-v10.html
167              
168             =cut
169              
170             1;
171              
172             __END__