| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Common::XML::RelaxNG | # | ||
| 3 | # | wrapper for XML::LibXML | # | ||
| 4 | # |=====================================================================| # | ||
| 5 | # | Part of LaTeXML: | # | ||
| 6 | # | Public domain software, produced as part of work done by the | # | ||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||
| 8 | # |---------------------------------------------------------------------| # | ||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||
| 12 | package LaTeXML::Common::XML::RelaxNG; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use XML::LibXML; | ||
| 16 | use LaTeXML::Util::Pathname; | ||
| 17 | |||
| 18 | # Note: XML::LibXML::RelaxNG->new(...) takes | ||
| 19 | # location=>$filename_or_url; | ||
| 20 | # string=>$schemastring | ||
| 21 | # DOM=>$doc | ||
| 22 | |||
| 23 | # options: nocatalogs, searchpaths | ||
| 24 | |||
| 25 | # Create a Wrapper for a RelaxNG, | ||
| 26 | # containing the XML document representing the schema | ||
| 27 | # defering converting it to an actual RelaxNG object. | ||
| 28 | sub new { | ||
| 29 | my ($class, $name, %options) = @_; | ||
| 30 | LaTeXML::Common::XML::initialize_catalogs(); | ||
| 31 | my $xmlparser = LaTeXML::Common::XML::Parser->new(); | ||
| 32 | my $schemadoc; | ||
| 33 | $name .= ".rng" unless $name =~ /\.rng$/; | ||
| 34 | # First, try to load directly, in case it's found via libxml's catalogs... | ||
| 35 | # But be careful calling C library; its failures are harder to trap w/eval | ||
| 36 | if (!$options{nocatalogs}) { | ||
| 37 | $schemadoc = eval { | ||
| 38 | local $LaTeXML::IGNORE_ERRORS = 1; | ||
| 39 | no warnings 'all'; | ||
| 40 | $xmlparser->parseFile($name); }; } | ||
| 41 | |||
| 42 | if (!$schemadoc) { | ||
| 43 | if (my $path = pathname_find($name, paths => $options{searchpaths} || ['.'], | ||
| 44 | types => ['rng'], # Eventually, rnc? | ||
| 45 | installation_subdir => 'resources/RelaxNG')) { | ||
| 46 | # Hopefully, just a file, not a URL? | ||
| 47 | $schemadoc = $xmlparser->parseFile($path); } | ||
| 48 | else { | ||
| 49 | return; # ??? | ||
| 50 | } } | ||
| 51 | return bless { schemadoc => $schemadoc }, $class; } | ||
| 52 | |||
| 53 | sub validate { | ||
| 54 | my ($self, $document) = @_; | ||
| 55 | # Lazy conversion of the Schema's XML doc into an actual RelaxNG object. | ||
| 56 | if (!$$self{schema} && $$self{schemadoc}) { | ||
| 57 | $$self{schema} = XML::LibXML::RelaxNG->new(DOM => $$self{schemadoc}); } | ||
| 58 | return $$self{schema}->validate($document); } | ||
| 59 | |||
| 60 | # This returns the root element of the XML document representing the schema! | ||
| 61 | sub documentElement { | ||
| 62 | my ($self) = @_; | ||
| 63 | return $$self{schemadoc}->documentElement; } | ||
| 64 | |||
| 65 | sub URI { | ||
| 66 | my ($self) = @_; | ||
| 67 | return $$self{schemadoc}->URI; } | ||
| 68 | |||
| 69 | #====================================================================== | ||
| 70 | 1; |