lib/LaTeXML/Common/Model/DTD.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 151 CC 19 SCAR 48.7
CC
19
Cov
0%
loadSchema
240.0
readDTD
30.0
new
2.0
CRAP 129.9
1 # /=====================================================================\ #
2 # | LaTeXML::Common::Model::DTD | #
3 # | Extract Model information from a DTD | #
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::Model::DTD;
13 use strict;
14 use warnings;
15 use LaTeXML::Util::Pathname;
16 use LaTeXML::Global;
17 use LaTeXML::Common::Error;
18 use LaTeXML::Common::XML;
19 use URI::file;
20  
21 #**********************************************************************
22 # NOTE: Arglist is DTD specific.
23 # Effectively asks for DTD submodel.
24 sub new {
25 my ($class, $model, $roottag, $publicid, $systemid) = @_;
26 my $self = { model => $model, roottag => $roottag, public_id => $publicid, system_id => $systemid };
27 bless $self, $class;
28 return $self; }
29  
30 # Question: if we don't have a doctype, can we rig the queries to
31 # let it build a `reasonable' document?
32  
33 # This is responsible for setting any DocType, and adding any
34 # required namespace declarations to the root element.
35 sub addSchemaDeclaration {
36 my ($self, $document, $tag) = @_;
37 $document->getDocument->createInternalSubset($tag, $$self{public_id}, $$self{system_id});
38 return; }
39  
40 #**********************************************************************
41 # DTD Analysis
42 #**********************************************************************
43 # Uses XML::LibXML to read in the DTD. Then extracts a simplified
44 # model: which elements can appear within each element, ignoring
45 # (for now) the ordering, repeat, etc, of the elements.
46 # From this, and the Tag declarations of autoOpen (that an
47 # element can be opened automatically, if needed) we derive an implicit model.
48 # Thus, if we want to insert an element (or, say #PCDATA) into an
49 # element that doesn't allow it, we may find an implied element
50 # to create & insert, and insert the #PCDATA into it.
51  
52 my $NAME_re = qr/[a-zA-Z0-9\-\_\:]+/; # [CONSTANT]
53  
54 sub loadSchema {
55 my ($self) = @_;
56 $$self{schema_loaded} = 1;
57 ProgressSpinup("Loading DTD " . $$self{public_id} || $$self{system_id});
58 my $model = $$self{model};
59 $model->addTagContent('#Document', $$self{roottag}) if $$self{roottag};
60 # Parse the DTD
61 my $dtd = $self->readDTD;
62 return unless $dtd;
63  
64 ProgressSpinup("Analyzing DTD");
65 # Extract all possible namespace attributes
66 foreach my $node ($dtd->childNodes()) {
67 if ($node->nodeType() == XML_ATTRIBUTE_DECL) {
68 if ($node->toString =~ /^<!ATTLIST\s+($NAME_re)\s+($NAME_re)\s+(.*)>$/) {
69 my ($tag, $attr, $extra) = ($1, $2, $3);
70 if ($attr =~ /^xmlns(:($NAME_re))?$/) {
71 my $prefix = ($1 ? $2 : '#default');
72 my $ns;
73 if ($extra =~ /^CDATA\s+#FIXED\s+(\'|\")(.*)\1\s*$/) {
74 $ns = $2; }
75 # Just record prefix, not element??
76 $model->registerDocumentNamespace($prefix, $ns); } } } }
77  
78 # Extract all possible children for each tag.
79 foreach my $node ($dtd->childNodes()) {
80 if ($node->nodeType() == XML_ELEMENT_DECL) {
81 my $decl = $node->toString();
82 chomp($decl);
83 if ($decl =~ /^<!ELEMENT\s+($NAME_re)\s+(.*)>$/) {
84 my ($tag, $content) = ($1, $2);
85 $content =~ s/[\+\*\?\,\(\)\|]/ /g;
86 $content =~ s/\s+/ /g; $content =~ s/^\s+//; $content =~ s/\s+$//;
87 $model->addTagContent($model->recodeDocumentQName($tag),
88 ($content eq 'EMPTY'
89 ? ()
90 : map { $model->recodeDocumentQName($_) } split(/ /, $content))); }
91 else {
92 Warn('misdefined', $decl, undef, "Can't process DTD declaration '$decl'"); } }
93  
94 elsif ($node->nodeType() == XML_ATTRIBUTE_DECL) {
95 if ($node->toString =~ /^<!ATTLIST\s+($NAME_re)\s+($NAME_re)\s+(.*)>$/) {
96 my ($tag, $attr, $extra) = ($1, $2, $3);
97 if ($attr !~ /^xmlns/) {
98 $model->addTagAttribute($model->recodeDocumentQName($tag),
99 ($attr =~ /:/ ? $model->recodeDocumentQName($attr) : $attr));
100 } } }
101 }
102 ProgressSpindown("Analyzing DTD"); # Done analyzing
103 ProgressSpindown("Loading DTD " . $$self{public_id} || $$self{system_id});
104 return; }
105  
106 sub readDTD {
107 my ($self) = @_;
108 LaTeXML::Common::XML::initialize_catalogs();
109 my $message = "Loading DTD for $$self{public_id} $$self{system_id}";
110 my $how;
111 ProgressSpinup($message);
112 # NOTE: setting XML_DEBUG_CATALOG makes this Fail!!!
113 my $dtd = XML::LibXML::Dtd->new($$self{public_id}, $$self{system_id});
114 if ($dtd) {
115 $how = " via catalog "; }
116 else { # Couldn't find dtd in catalog, try finding the file. (search path?)
117 my $dtdfile = pathname_find($$self{system_id},
118 paths => $STATE->lookupValue('SEARCHPATHS'),
119 installation_subdir => 'resources/DTD');
120 if ($dtdfile) {
121 $dtd = XML::LibXML::Dtd->new($$self{public_id}, URI::file->new($dtdfile));
122 $how = " from $dtdfile ";
123 Error('misdefined', $$self{system_id}, undef,
124 "Parsing of DTD \"$$self{public_id}\" \"$$self{system_id}\" failed")
125 unless $dtd;
126 }
127 else {
128 Error('missing_file', $$self{system_id}, undef,
129 "Can't find DTD \"$$self{public_id}\" \"$$self{system_id}\""); } }
130 ProgressSpindown($message);
131 NoteLog("Read dtd $how") if $how && $dtd;
132 return $dtd; }
133  
134 #======================================================================
135 1;
136  
137 __END__