| 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 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 4 | ▶ | use Path::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 4 | ▶ | use File::ShareDir 'dist_dir'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 4 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | our $VERSION = '0.04'; # VERSION | ||
| 17 | |||
| 18 | |||
| 19 | #add the Jing jar to the system classpath | ||
| 20 | 4 | ▶ | BEGIN{ |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 4 | ▶ | use Env::Path; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 4 | my $classpath = Env::Path->CLASSPATH; | |
| 23 | 4 | $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 | 0 | ▶ | use Inline::Java qw(caught); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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__ | ||