| 1 | package XML::Validate::Xerces; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use XML::Validate::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use XML::Xerces; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | |||
| 7 | use vars qw($VERSION $CATCH_ERROR @ISA); | ||
| 8 | $VERSION = sprintf"%d.%03d", q$Revision: 1.21 $ =~ /: (\d+)\.(\d+)/; | ||
| 9 | @ISA = qw(XML::Validate::Base); | ||
| 10 | |||
| 11 | # This should happen in the XML::Xerces INIT block, but we expect this module to | ||
| 12 | # be dynamically loaded, so the INIT block probably won't happen. | ||
| 13 | XML::Xerces::XMLPlatformUtils::Initialize(); | ||
| 14 | |||
| 15 | my $VALID_OPTIONS = { | ||
| 16 | strict_validation => 1, | ||
| 17 | base_uri => '', | ||
| 18 | }; | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $class = shift; | ||
| 22 | my %options = @_; | ||
| 23 | my $self = {}; | ||
| 24 | bless ($self, $class); | ||
| 25 | |||
| 26 | $self->clear_errors(); | ||
| 27 | $self->set_options(\%options,$VALID_OPTIONS); | ||
| 28 | |||
| 29 | DUMP("Instantiating XML::Validate::Xerces", $self); | ||
| 30 | |||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub version { | ||
| 35 | return XML::Xerces->VERSION; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub validate { | ||
| 39 | my $self = shift; | ||
| 40 | my ($xml) = @_; | ||
| 41 | TRACE("Validating with Xerces. XML => " . defined($xml) ? $xml : 'undef' ); | ||
| 42 | |||
| 43 | $self->clear_errors(); | ||
| 44 | $self->{DOMParser} = undef; | ||
| 45 | |||
| 46 | die "validate called with no data to validate\n" unless defined $xml and length $xml > 0; | ||
| 47 | |||
| 48 | my $DOMparser = new XML::Xerces::XercesDOMParser; | ||
| 49 | # set various validation arguments based on argument | ||
| 50 | $self->_set_validation($DOMparser, $self->options->{strict_validation}); | ||
| 51 | |||
| 52 | # error handler | ||
| 53 | my $ErrorHandler = XML::Validate::Xerces::ErrorHandler->new($self); | ||
| 54 | $DOMparser->setErrorHandler($ErrorHandler); | ||
| 55 | |||
| 56 | # Use Memory buffer input source to read the XML string | ||
| 57 | my $input = XML::Xerces::MemBufInputSource->new($xml,$self->options->{base_uri}); | ||
| 58 | |||
| 59 | $DOMparser->parse($input); | ||
| 60 | |||
| 61 | if ($self->last_error) { | ||
| 62 | TRACE("Exception found",$self->last_error); | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | |||
| 66 | $self->{DOMParser} = $DOMparser; | ||
| 67 | return 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub last_dom { | ||
| 71 | my $self = shift; | ||
| 72 | return undef unless defined $self->{DOMParser}; | ||
| 73 | return $self->{DOMParser}->getDocument(); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _set_validation { | ||
| 77 | my $self = shift; | ||
| 78 | my $DOMparser = shift; | ||
| 79 | my $strict = shift; | ||
| 80 | |||
| 81 | TRACE("_set_validation called"); | ||
| 82 | |||
| 83 | if ($strict) { | ||
| 84 | TRACE("Using strict validation"); | ||
| 85 | $DOMparser->setValidationScheme("$XML::Xerces::AbstractDOMParser::Val_Auto"); | ||
| 86 | $DOMparser->setIncludeIgnorableWhitespace(0); | ||
| 87 | $DOMparser->setDoSchema(1); | ||
| 88 | $DOMparser->setDoNamespaces(1); | ||
| 89 | $DOMparser->setValidationSchemaFullChecking(1); | ||
| 90 | $DOMparser->setLoadExternalDTD(1); | ||
| 91 | $DOMparser->setExitOnFirstFatalError(1); | ||
| 92 | $DOMparser->setValidationConstraintFatal(1); | ||
| 93 | } else { | ||
| 94 | TRACE("Using no validation"); | ||
| 95 | $DOMparser->setValidationScheme("$XML::Xerces::AbstractDOMParser::Val_Never"); | ||
| 96 | $DOMparser->setDoSchema(0); | ||
| 97 | $DOMparser->setDoNamespaces(0); | ||
| 98 | $DOMparser->setValidationSchemaFullChecking(0); | ||
| 99 | $DOMparser->setLoadExternalDTD(0); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | # Note: Our use of TRACE and DUMP here is a bit weird. We explicitly pass to | ||
| 104 | # the TRACE and DUMP in the superclass (XML::Validate::Base) because we expect | ||
| 105 | # to be dynamically loaded and we assume that the calling class will have dealt | ||
| 106 | # with Base but not this module. (Note that Log::Trace now has some support for | ||
| 107 | # dynamic loading. It doesn't play well with some modules in 5.6.1, but it seems | ||
| 108 | # fine in 5.8. So someday this won't be necessary.) | ||
| 109 | |||
| 110 | sub TRACE { XML::Validate::Base::TRACE(@_) } | ||
| 111 | sub DUMP { XML::Validate::Base::DUMP(@_) } | ||
| 112 | |||
| 113 | 1; | ||
| 114 | |||
| 115 | # Override XML::Xerces errors into warnings we can catch | ||
| 116 | package XML::Validate::Xerces::ErrorHandler; | ||
| 117 | |||
| 118 | use vars '@ISA'; | ||
| 119 | @ISA = qw(XML::Xerces::PerlErrorHandler); | ||
| 120 | |||
| 121 | sub new { | ||
| 122 | my $class = shift; | ||
| 123 | my ($validator) = @_; | ||
| 124 | my $self = { | ||
| 125 | validator => $validator, | ||
| 126 | }; | ||
| 127 | return bless($self,$class) | ||
| 128 | } | ||
| 129 | |||
| 130 | sub warning { | ||
| 131 | my ($self, $exception) = @_; | ||
| 132 | $self->add_error($exception,"Warning"); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub error { | ||
| 136 | my ($self, $exception) = @_; | ||
| 137 | $self->add_error($exception,"Invalid XML"); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub fatal_error { | ||
| 141 | my ($self, $exception) = @_; | ||
| 142 | $self->add_error($exception,"XML error"); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub add_error { | ||
| 146 | my $self = shift; | ||
| 147 | my ($exception,$message_prefix) = @_; | ||
| 148 | my $error = { | ||
| 149 | line => $exception->getLineNumber, | ||
| 150 | column => $exception->getColumnNumber, | ||
| 151 | message => "$message_prefix: " . $exception->getMessage, | ||
| 152 | }; | ||
| 153 | $self->{validator}->add_error($error); | ||
| 154 | } | ||
| 155 | |||
| 156 | 1; | ||
| 157 | |||
| 158 | __END__ | ||