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

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