File Coverage

blib/lib/XML/Jing.pm
Criterion Covered Total %
statement 21 23 91.3
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 29 31 93.5


line stmt bran cond sub pod time code
1             #
2             # This file is part of XML-Jing
3             #
4             # This software is copyright (c) 2013 by BYU Translation Research Group.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package XML::Jing;
10             # ABSTRACT: Validate XML files using an RNG schema using the Jing tool
11 3     3   3694 use strict;
  3         8  
  3         122  
12 3     3   17 use warnings;
  3         5  
  3         83  
13 3     3   4918 use Path::Tiny;
  3         59613  
  3         283  
14 3     3   3460 use File::ShareDir 'dist_dir';
  3         25990  
  3         3495  
15 3     3   340 use Carp;
  3         9  
  3         1099  
16             our $VERSION = '0.04'; # VERSION
17              
18              
19             #add the Jing jar to the system classpath
20             BEGIN{
21 3     3   15550 use Env::Path;
  3         13019  
  3         23  
22 3     3   253 my $classpath = Env::Path->CLASSPATH;
23 3         148 $classpath->Append(path(dist_dir('XML-Jing'),'jing.jar'));
24             }
25              
26             require Inline;
27             Inline->import(
28             Java => path(dist_dir('XML-Jing'),'RNGValidator.java'),
29             STUDY => ['RNGValidator'],
30             );
31 3     3   6276 use Inline::Java qw(caught);
  0            
  0            
32              
33              
34             sub new {
35             my ($class, $rng_path, $compact) = @_;
36             unless (-e $rng_path){
37             croak "File doesn't exist: $rng_path";
38             }
39             my $self = bless {}, $class;
40              
41             #read in the RNG file, catching any errors
42             eval {
43             $self->{validator} = XML::Jing::RNGValidator->new("$rng_path", $compact)
44             };
45             if ($@){
46             if (caught("org.xml.sax.SAXParseException")){
47             my $error = 'Error reading RNG file: ' . $@->getMessage();
48             #undef $@ so that the Inline::Java object is released (in case someone catches the croak)
49             undef $@;
50             croak $error;
51             }else{
52             # It wasn't a Java exception after all...
53             my $error = $@;
54             undef $@;
55             croak $error ;
56             }
57             }
58             return $self;
59             }
60              
61              
62             sub validate {
63             my ($self, $xml_path) = @_;
64             unless (-e $xml_path){
65             croak "File doesn't exist: $xml_path";
66             }
67              
68             #validate the file, catching any errors
69             my $errors;
70             eval {
71             $errors = $self->{validator}->validate("$xml_path")
72             };
73             if($@){
74             if (caught("java.io.FileNotFoundException")){
75             my $error = 'Error reading file: ' . $@->getMessage();
76             #undef $@ so that the Inline::Java object is released (in case someone catches the croak)
77             undef $@;
78             croak $error;
79             }else{
80             warn 'croaking!';
81             my $error = $@;
82             undef $@;
83             croak $error;
84             }
85             }
86             return $errors;
87             }
88              
89             1;
90              
91             __END__