| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Common::Model | # | ||
| 3 | # | Stores representation of Document Type for use by Document | # | ||
| 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; | ||
| 13 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use LaTeXML::Global; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 1 | ▶ | use LaTeXML::Common::Object; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 1 | ▶ | use LaTeXML::Common::Error; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | use LaTeXML::Common::Font; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 0 | ▶ | use LaTeXML::Common::XML; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 20 | use LaTeXML::Util::Pathname; | ||
| 21 | use base qw(LaTeXML::Common::Object); | ||
| 22 | |||
| 23 | #********************************************************************** | ||
| 24 | our $LTX_NAMESPACE = "http://dlmf.nist.gov/LaTeXML"; # [CONSTANT] | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my ($class, %options) = @_; | ||
| 28 | my $self = bless { xpath => LaTeXML::Common::XML::XPath->new(), | ||
| 29 | code_namespace_prefixes => {}, code_namespaces => {}, | ||
| 30 | doctype_namespaces => {}, | ||
| 31 | namespace_errors => 0, | ||
| 32 | %options }, $class; | ||
| 33 | $$self{xpath}->registerFunction('match-font', \&LaTeXML::Common::Font::match_font); | ||
| 34 | $self->registerNamespace('xml', $LaTeXML::Common::XML::XML_NS); | ||
| 35 | $self->registerDocumentNamespace('xml', $LaTeXML::Common::XML::XML_NS); | ||
| 36 | return $self; } | ||
| 37 | |||
| 38 | sub setDocType { | ||
| 39 | my ($self, $roottag, $publicid, $systemid) = @_; | ||
| 40 | $$self{schemadata} = ['DTD', $roottag, $publicid, $systemid]; | ||
| 41 | return; } | ||
| 42 | |||
| 43 | sub setRelaxNGSchema { | ||
| 44 | my ($self, $schema) = @_; | ||
| 45 | $$self{schemadata} = ['RelaxNG', $schema]; | ||
| 46 | return; } | ||
| 47 | |||
| 48 | sub loadSchema { | ||
| 49 | my ($self) = @_; | ||
| 50 | return $$self{schema} if $$self{schema_loaded}; | ||
| 51 | my $name; | ||
| 52 | |||
| 53 | if (!$$self{schemadata}) { | ||
| 54 | Warn('expected', '<model>', undef, "No Schema Model has been declared; assuming LaTeXML"); | ||
| 55 | # article ??? or what ? undef gives problems! | ||
| 56 | $self->setRelaxNGSchema("LaTeXML"); | ||
| 57 | $self->registerNamespace(ltx => $LTX_NAMESPACE); | ||
| 58 | $self->registerNamespace(svg => "http://www.w3.org/2000/svg"); | ||
| 59 | $self->registerNamespace(xlink => "http://www.w3.org/1999/xlink"); # Needed for SVG | ||
| 60 | $self->registerNamespace(m => "http://www.w3.org/1998/Math/MathML"); | ||
| 61 | $self->registerNamespace(xhtml => "http://www.w3.org/1999/xhtml"); | ||
| 62 | |||
| 63 | $$self{permissive} = 1; } # Actually, they could have declared all sorts of Tags.... | ||
| 64 | |||
| 65 | my ($type, @data) = @{ $$self{schemadata} }; | ||
| 66 | if ($type eq 'DTD') { | ||
| 67 | my ($roottag, $publicid, $systemid) = @data; | ||
| 68 | require LaTeXML::Common::Model::DTD; | ||
| 69 | $name = $systemid; | ||
| 70 | $$self{schema} = LaTeXML::Common::Model::DTD->new($self, $roottag, $publicid, $systemid); } | ||
| 71 | elsif ($type eq 'RelaxNG') { | ||
| 72 | ($name) = @data; | ||
| 73 | require LaTeXML::Common::Model::RelaxNG; | ||
| 74 | $$self{schema} = LaTeXML::Common::Model::RelaxNG->new($self, $name); } | ||
| 75 | |||
| 76 | if (my $compiled = !$$self{no_compiled} | ||
| 77 | && pathname_find($name, paths => $STATE->lookupValue('SEARCHPATHS'), | ||
| 78 | types => ['model'], installation_subdir => "resources/$type")) { | ||
| 79 | $self->loadCompiledSchema($compiled); } | ||
| 80 | else { | ||
| 81 | $$self{schema}->loadSchema; } | ||
| 82 | $self->loadInternalExtensions; | ||
| 83 | $self->describeModel if $LaTeXML::Common::Model::DEBUG; | ||
| 84 | $$self{schema_loaded} = 1; | ||
| 85 | return $$self{schema}; } | ||
| 86 | |||
| 87 | sub addSchemaDeclaration { | ||
| 88 | my ($self, $document, $tag) = @_; | ||
| 89 | $$self{schema}->addSchemaDeclaration($document, $tag); | ||
| 90 | return; } | ||
| 91 | |||
| 92 | sub loadInternalExtensions { | ||
| 93 | my ($self) = @_; | ||
| 94 | if (!exists $$self{tagprop}{'ltx:_CaptureBlock_'}) { | ||
| 95 | # Synthesize ltx:_CaptureBlock_ to act like the union of ltx:block, ltx:para, | ||
| 96 | $self->synthesizeElement('ltx:_CaptureBlock_', | ||
| 97 | qw(ltx:block ltx:logical-block ltx:sectional-block Caption)); | ||
| 98 | $$self{tagprop}{'ltx:_CaptureBlock_'}{model}{'svg:g'} = 1; | ||
| 99 | $$self{tagprop}{'ltx:_CaptureBlock_'}{model}{'svg:foreignObject'} = 1; | ||
| 100 | } | ||
| 101 | return; } | ||
| 102 | |||
| 103 | # Clone the tagprop's (allowed content & attributes) of @other to $tag | ||
| 104 | sub synthesizeElement { | ||
| 105 | my ($self, $tag, @others) = @_; | ||
| 106 | $$self{tagprop}{$tag} = {} unless $$self{tagprop}{$tag}; | ||
| 107 | my $capture = $$self{tagprop}{$tag}; | ||
| 108 | foreach my $other (@others) { | ||
| 109 | if (my $content = $$self{schemaclass}{$other}) { | ||
| 110 | foreach my $child (keys %$content) { | ||
| 111 | $$capture{model}{$child} = $$content{$child}; } } | ||
| 112 | elsif (my $entry = $$self{tagprop}{$other}) { | ||
| 113 | foreach my $child (keys %{ $$entry{model} }) { | ||
| 114 | $$capture{model}{$child} = $$entry{model}{$child}; } | ||
| 115 | foreach my $attr (keys %{ $$entry{attributes} }) { | ||
| 116 | $$capture{attributes}{$attr} = $$entry{attributes}{$attr}; } } } | ||
| 117 | return; } | ||
| 118 | |||
| 119 | #===================================================================== | ||
| 120 | # Make provision to precompile the schema. | ||
| 121 | sub compileSchema { | ||
| 122 | my ($self) = @_; | ||
| 123 | $$self{no_compiled} = 1; | ||
| 124 | $self->loadSchema; | ||
| 125 | foreach my $prefix (sort keys %{ $$self{document_namespaces} }) { | ||
| 126 | print $prefix. '=' . $$self{document_namespaces}{$prefix} . "\n"; } | ||
| 127 | if (my $defs = $$self{schemaclass}) { | ||
| 128 | foreach my $classname (sort keys %$defs) { | ||
| 129 | print $classname. ':=(' . join(',', sort keys %{ $$self{schemaclass}{$classname} }) . ')' . "\n"; } } | ||
| 130 | foreach my $tag (sort keys %{ $$self{tagprop} }) { | ||
| 131 | next if $tag =~ /^!/; # Ignore top-level negated (only make sense in content model) | ||
| 132 | print $tag | ||
| 133 | . '{' . join(',', sort keys %{ $$self{tagprop}{$tag}{attributes} }) . '}' | ||
| 134 | . '(' . join(',', sort keys %{ $$self{tagprop}{$tag}{model} }) . ')' . "\n"; } | ||
| 135 | return; } | ||
| 136 | |||
| 137 | sub loadCompiledSchema { | ||
| 138 | my ($self, $file) = @_; | ||
| 139 | ProgressSpinup("Loading compiled schema $file"); | ||
| 140 | my $MODEL; | ||
| 141 | open($MODEL, '<', $file) or Fatal('I/O', $file, undef, "Cannot open Compiled Model $file for reading", $!); | ||
| 142 | my $line; | ||
| 143 | while ($line = <$MODEL>) { | ||
| 144 | if ($line =~ /^([^\{]+)\{(.*?)\}\((.*?)\)$/) { | ||
| 145 | my ($tag, $attr, $children) = ($1, $2, $3); | ||
| 146 | $self->addTagAttribute($tag, split(/,/, $attr)); | ||
| 147 | $self->addTagContent($tag, split(/,/, $children)); } | ||
| 148 | |||
| 149 | elsif ($line =~ /^([^:=]+):=\(?([^)]*?)\)?$/) { | ||
| 150 | my ($classname, $elements) = ($1, $2); | ||
| 151 | $self->setSchemaClass($classname, { map { ($_ => 1) } split(/,/, $elements) }); } | ||
| 152 | elsif ($line =~ /^([^=]+)=(.*?)$/) { | ||
| 153 | my ($prefix, $namespace) = ($1, $2); | ||
| 154 | $self->registerDocumentNamespace($prefix, $namespace); } | ||
| 155 | else { | ||
| 156 | Fatal('internal', $file, undef, "Compiled model '$file' is malformatted at \"$line\""); } | ||
| 157 | } | ||
| 158 | close($MODEL); | ||
| 159 | ProgressSpindown("Loading compiled schema $file"); | ||
| 160 | return; } | ||
| 161 | |||
| 162 | #********************************************************************** | ||
| 163 | # Namespaces | ||
| 164 | #********************************************************************** | ||
| 165 | # There are TWO namespace mappings!!! | ||
| 166 | # One for coding, one for the DocType. | ||
| 167 | # | ||
| 168 | # Coding: this namespace mapping associates prefixes to namespace URIs for | ||
| 169 | # use in the latexml code, constructors and such. | ||
| 170 | # This must be a one to one mapping and there are no default namespaces. | ||
| 171 | # Document: this namespace mapping associates prefixes to namespace URIs | ||
| 172 | # as used in the generated document, and will be the | ||
| 173 | # set of prefixes used in the generated output. | ||
| 174 | # This mapping may also use a prefix of "#default" which is for | ||
| 175 | # the unprefixed form of elements (not used for attributes!) | ||
| 176 | sub registerNamespace { | ||
| 177 | my ($self, $codeprefix, $namespace) = @_; | ||
| 178 | if ($namespace) { | ||
| 179 | $$self{code_namespace_prefixes}{$namespace} = $codeprefix; | ||
| 180 | $$self{code_namespaces}{$codeprefix} = $namespace; | ||
| 181 | $$self{xpath}->registerNS($codeprefix, $namespace); } | ||
| 182 | else { | ||
| 183 | my $prev = $$self{code_namespaces}{$codeprefix}; | ||
| 184 | delete $$self{code_namespace_prefixes}{$prev} if $prev; | ||
| 185 | delete $$self{code_namespaces}{$codeprefix}; } | ||
| 186 | return; } | ||
| 187 | |||
| 188 | # In the following: | ||
| 189 | # $forattribute is 1 if the namespace is for an attribute (in which case, there must be a non-empty prefix) | ||
| 190 | # $probe, if non 0, just test for namespace, without creating an entry if missing. | ||
| 191 | # Get the (code) prefix associated with $namespace, | ||
| 192 | # creating a dummy prefix and signalling an error if none has been registered. | ||
| 193 | sub getNamespacePrefix { | ||
| 194 | my ($self, $namespace, $forattribute, $probe) = @_; | ||
| 195 | if ($namespace) { | ||
| 196 | my $codeprefix = $$self{code_namespace_prefixes}{$namespace}; | ||
| 197 | if ((!defined $codeprefix) && !$probe) { | ||
| 198 | my $docprefix = $$self{document_namespace_prefixes}{$namespace}; | ||
| 199 | # if there's a doc prefix and it's NOT already used in code namespace mapping | ||
| 200 | if ($docprefix && !$$self{code_namespaces}{$docprefix}) { | ||
| 201 | $codeprefix = $docprefix; } | ||
| 202 | else { # Else synthesize one | ||
| 203 | $codeprefix = "namespace" . (++$$self{namespace_errors}); } | ||
| 204 | $self->registerNamespace($codeprefix, $namespace); | ||
| 205 | Warn('malformed', $namespace, undef, | ||
| 206 | "No prefix has been registered for namespace '$namespace' (in code)", | ||
| 207 | "Using '$codeprefix' instead"); } | ||
| 208 | return $codeprefix; } } | ||
| 209 | |||
| 210 | sub getNamespace { | ||
| 211 | my ($self, $codeprefix, $probe) = @_; | ||
| 212 | my $ns = $$self{code_namespaces}{$codeprefix}; | ||
| 213 | if ((!defined $ns) && !$probe) { | ||
| 214 | $self->registerNamespace($codeprefix, | ||
| 215 | $ns = "http://example.com/namespace" . (++$$self{namespace_errors})); | ||
| 216 | Error('malformed', $codeprefix, undef, | ||
| 217 | "No namespace has been registered for prefix '$codeprefix' (in code)", | ||
| 218 | "Using '$ns' instead"); } | ||
| 219 | return $ns; } | ||
| 220 | |||
| 221 | sub registerDocumentNamespace { | ||
| 222 | my ($self, $docprefix, $namespace) = @_; | ||
| 223 | $docprefix = '#default' unless defined $docprefix; | ||
| 224 | if ($namespace) { | ||
| 225 | # Since the default namespace url can still ALSO have a prefix associated, | ||
| 226 | # we prepend "DEFAULT#url" when using as a hash key in the prefixes table. | ||
| 227 | my $regnamespace = ($docprefix eq '#default' ? "DEFAULT#" . $namespace : $namespace); | ||
| 228 | $$self{document_namespace_prefixes}{$regnamespace} = $docprefix; | ||
| 229 | $$self{document_namespaces}{$docprefix} = $namespace; } | ||
| 230 | else { | ||
| 231 | my $prev = $$self{document_namespaces}{$docprefix}; | ||
| 232 | delete $$self{document_namespace_prefixes}{$prev} if $prev; | ||
| 233 | delete $$self{document_namespaces}{$docprefix}; } | ||
| 234 | return; } | ||
| 235 | |||
| 236 | sub getDocumentNamespacePrefix { | ||
| 237 | my ($self, $namespace, $forattribute, $probe) = @_; | ||
| 238 | if ($namespace) { | ||
| 239 | # Get the prefix associated with the namespace url, noting that for elements, it might by "#default", | ||
| 240 | # but for attributes would never be. | ||
| 241 | my $docprefix = (!$forattribute && $$self{document_namespace_prefixes}{ "DEFAULT#" . $namespace }) | ||
| 242 | || $$self{document_namespace_prefixes}{$namespace} | ||
| 243 | || (($namespace ne $LTX_NAMESPACE) && $$self{code_namespace_prefixes}{$namespace}); | ||
| 244 | if ((!defined $docprefix) && !$probe) { | ||
| 245 | $self->registerDocumentNamespace($docprefix = "namespace" . (++$$self{namespace_errors}), $namespace); | ||
| 246 | Warn('malformed', $namespace, undef, | ||
| 247 | "No prefix has been registered for namespace '$namespace' (in document)", | ||
| 248 | "Using '$docprefix' instead"); } | ||
| 249 | return (($docprefix || '#default') eq '#default' ? '' : $docprefix); } } | ||
| 250 | |||
| 251 | sub getDocumentNamespace { | ||
| 252 | my ($self, $docprefix, $probe) = @_; | ||
| 253 | $docprefix = '#default' unless defined $docprefix; | ||
| 254 | my $ns = $$self{document_namespaces}{$docprefix}; | ||
| 255 | $ns =~ s/^DEFAULT#// if $ns; # Remove the default hack, if present! | ||
| 256 | if (($docprefix ne '#default') && (!defined $ns) && !$probe) { | ||
| 257 | $self->registerDocumentNamespace($docprefix, | ||
| 258 | $ns = "http://example.com/namespace" . (++$$self{namespace_errors})); | ||
| 259 | Error('malformed', $docprefix, undef, | ||
| 260 | "No namespace has been registered for prefix '$docprefix' (in document)", | ||
| 261 | "Using '$ns' instead"); } | ||
| 262 | return $ns; } | ||
| 263 | |||
| 264 | # Given a Qualified name, possibly prefixed with a namespace prefix, | ||
| 265 | # as defined by the code namespace mapping, | ||
| 266 | # return the NamespaceURI and localname. | ||
| 267 | sub decodeQName { | ||
| 268 | my ($self, $codetag) = @_; | ||
| 269 | if ($codetag =~ /^([^:]+):(.+)$/) { | ||
| 270 | my ($prefix, $localname) = ($1, $2); | ||
| 271 | return (undef, $codetag) if $prefix eq 'xml'; | ||
| 272 | return ($self->getNamespace($prefix), $localname); } | ||
| 273 | else { | ||
| 274 | return (undef, $codetag); } } | ||
| 275 | |||
| 276 | sub encodeQName { | ||
| 277 | my ($self, $ns, $name) = @_; | ||
| 278 | my $codeprefix = $ns && $self->getNamespacePrefix($ns); | ||
| 279 | return ($codeprefix ? "$codeprefix:$name" : $name); } | ||
| 280 | |||
| 281 | # Get the node's qualified name in standard form | ||
| 282 | # Ie. using the registered (code) prefix for that namespace. | ||
| 283 | # NOTE: Reconsider how _Capture_ & _WildCard_ should be integrated!?! | ||
| 284 | sub getNodeQName { | ||
| 285 | my ($self, $node) = @_; | ||
| 286 | my $type = ($node ? $node->nodeType : -1); | ||
| 287 | if ($type == XML_TEXT_NODE) { | ||
| 288 | return '#PCDATA'; } | ||
| 289 | elsif ($type == XML_DOCUMENT_NODE) { | ||
| 290 | return '#Document'; } | ||
| 291 | elsif ($type == XML_COMMENT_NODE) { | ||
| 292 | return '#Comment'; } | ||
| 293 | elsif ($type == XML_PI_NODE) { | ||
| 294 | return '#ProcessingInstruction'; } | ||
| 295 | elsif ($type == XML_DTD_NODE) { | ||
| 296 | return '#DTD'; } | ||
| 297 | elsif ($type == XML_NAMESPACE_DECL) { | ||
| 298 | my $ns = $node->declaredURI; | ||
| 299 | my $prefix = $ns && $self->getNamespacePrefix($ns, 0, 1); | ||
| 300 | return ($prefix ? 'xmlns:' . $prefix : 'xmlns'); } | ||
| 301 | # Need others? | ||
| 302 | elsif (($type != XML_ELEMENT_NODE) && ($type != XML_ATTRIBUTE_NODE)) { | ||
| 303 | Fatal('misdefined', '<caller>', undef, | ||
| 304 | "Should not ask for Qualified Name for node of type $type: " . Stringify($node)); | ||
| 305 | return; } | ||
| 306 | else { | ||
| 307 | my $ns = $node->namespaceURI; | ||
| 308 | my $prefix = $ns && $self->getNamespacePrefix($ns, 0, 1); | ||
| 309 | return ($prefix ? $prefix . ":" . $node->localname : $node->localname); } } | ||
| 310 | |||
| 311 | # Same thing, but using the Document namespace prefixes | ||
| 312 | sub getNodeDocumentQName { | ||
| 313 | my ($self, $node) = @_; | ||
| 314 | my $type = $node->nodeType; | ||
| 315 | if ($type == XML_TEXT_NODE) { | ||
| 316 | return '#PCDATA'; } | ||
| 317 | elsif ($type == XML_DOCUMENT_NODE) { | ||
| 318 | return '#Document'; } | ||
| 319 | elsif ($type == XML_COMMENT_NODE) { | ||
| 320 | return '#Comment'; } | ||
| 321 | elsif ($type == XML_PI_NODE) { | ||
| 322 | return '#ProcessingInstruction'; } | ||
| 323 | elsif ($type == XML_DTD_NODE) { | ||
| 324 | return '#DTD'; } | ||
| 325 | elsif ($type == XML_NAMESPACE_DECL) { | ||
| 326 | my $ns = $node->declaredURI; | ||
| 327 | my $prefix = $ns && $self->getDocumentNamespacePrefix($ns, 0, 1); | ||
| 328 | return ($prefix ? 'xmlns:' . $prefix : 'xmlns'); } | ||
| 329 | # Need others? | ||
| 330 | elsif (($type != XML_ELEMENT_NODE) && ($type != XML_ATTRIBUTE_NODE)) { | ||
| 331 | Fatal('misdefined', '<caller>', undef, | ||
| 332 | "Should not ask for Qualified Name for node of type $type: " . Stringify($node)); | ||
| 333 | return; } | ||
| 334 | else { | ||
| 335 | my $ns = $node->namespaceURI; | ||
| 336 | my $prefix = $ns && $self->getDocumentNamespacePrefix($ns, 0, 1); | ||
| 337 | return ($prefix ? $prefix . ":" . $node->localname : $node->localname); } } | ||
| 338 | |||
| 339 | # Given a Document QName, convert to "code" form | ||
| 340 | # Used to convert a possibly prefixed name from the DTD | ||
| 341 | # (using the DTD's prefixes) | ||
| 342 | # into a prefixed name using the Code's prefixes | ||
| 343 | # NOTE: Used only for DTD | ||
| 344 | sub recodeDocumentQName { | ||
| 345 | my ($self, $docQName) = @_; | ||
| 346 | my ($docprefix, $name) = (undef, $docQName); | ||
| 347 | if ($docQName =~ /^(#PCDATA|#Comment|ANY|#ProcessingInstruction|#Document)$/) { | ||
| 348 | return $docQName; } | ||
| 349 | else { | ||
| 350 | ($docprefix, $name) = ($1, $2) if $docQName =~ /^([^:]+):(.+)/; | ||
| 351 | return $self->encodeQName($self->getDocumentNamespace($docprefix), $name); } } | ||
| 352 | |||
| 353 | # Get an XPath context that knows about our namespace mappings. | ||
| 354 | sub getXPath { | ||
| 355 | my ($self) = @_; | ||
| 356 | return $$self{xpath}; } | ||
| 357 | |||
| 358 | #********************************************************************** | ||
| 359 | # Accessors | ||
| 360 | #********************************************************************** | ||
| 361 | |||
| 362 | sub getTags { | ||
| 363 | my ($self) = @_; | ||
| 364 | return (sort keys %{ $$self{tagprop} }); } | ||
| 365 | |||
| 366 | sub getTagContents { | ||
| 367 | my ($self, $tag) = @_; | ||
| 368 | my $h = $$self{tagprop}{$tag}{model}; | ||
| 369 | return ($h ? sort keys %$h : ()); } | ||
| 370 | |||
| 371 | sub addTagContent { | ||
| 372 | my ($self, $tag, @elements) = @_; | ||
| 373 | $$self{tagprop}{$tag}{model} = {} unless $$self{tagprop}{$tag}{model}; | ||
| 374 | map { $$self{tagprop}{$tag}{model}{$_} = 1 } @elements; | ||
| 375 | return; } | ||
| 376 | |||
| 377 | sub getTagAttributes { | ||
| 378 | my ($self, $tag) = @_; | ||
| 379 | my $h = $$self{tagprop}{$tag}{attributes}; | ||
| 380 | return $h ? sort keys %$h : (); } | ||
| 381 | |||
| 382 | sub addTagAttribute { | ||
| 383 | my ($self, $tag, @attributes) = @_; | ||
| 384 | $$self{tagprop}{$tag}{attributes} = {} unless $$self{tagprop}{$tag}{attributes}; | ||
| 385 | map { $$self{tagprop}{$tag}{attributes}{$_} = 1 } @attributes; | ||
| 386 | return; } | ||
| 387 | |||
| 388 | sub setSchemaClass { | ||
| 389 | my ($self, $classname, $content) = @_; | ||
| 390 | $$self{schemaclass}{$classname} = $content; | ||
| 391 | return; } | ||
| 392 | |||
| 393 | #********************************************************************** | ||
| 394 | # Document Structure Queries | ||
| 395 | #********************************************************************** | ||
| 396 | # NOTE: These are public, but perhaps should be passed | ||
| 397 | # to submodel, in case it can evolve to more precision? | ||
| 398 | # However, it would need more context to do that. | ||
| 399 | # NOTE: That * matches any NON-namespaced name; *:* matches any namespaced name | ||
| 400 | |||
| 401 | # Can an element with (qualified name) $tag contain a $childtag element? | ||
| 402 | sub canContain { | ||
| 403 | my ($self, $tag, $childtag) = @_; | ||
| 404 | $self->loadSchema unless $$self{schema_loaded}; | ||
| 405 | # Handle obvious cases explicitly. | ||
| 406 | return 0 if !$tag || ($tag eq '#PCDATA') || ($tag eq '#Comment'); | ||
| 407 | return 1 if $tag =~ /(.*?:)?_Capture_$/; # with or without namespace prefix | ||
| 408 | return 1 if $tag eq '_WildCard_'; | ||
| 409 | return 1 if $childtag =~ /(.*?:)?_Capture_$/; # anything can contain these | ||
| 410 | return 1 if $childtag =~ /(.*?:)?_CaptureBlock_$/; | ||
| 411 | return 1 if $childtag eq '_WildCard_'; | ||
| 412 | return 1 if $childtag eq '#Comment'; | ||
| 413 | return 1 if $childtag eq '#ProcessingInstruction'; | ||
| 414 | return 1 if $childtag eq '#DTD'; | ||
| 415 | # return 1 if $$self{permissive}; # No DTD? Punt! | ||
| 416 | return 1 if $$self{permissive} && ($tag eq '#Document') && ($childtag ne '#PCDATA'); # No DTD? Punt! | ||
| 417 | # Else query tag properties. | ||
| 418 | my $model = $$self{tagprop}{$tag}{model}; | ||
| 419 | if (!$model && ($tag =~ /^(\w*):/)) { | ||
| 420 | my $xtag = $1 . ':*'; | ||
| 421 | $model = $$self{tagprop}{$xtag}{model}; } | ||
| 422 | my ($chns, $chname) = ($childtag =~ /^([^:]*):(.*)$/ ? ($1, $2) : (undef, $childtag)); | ||
| 423 | # NOTE: Maybe recode this to (carefully) use 0 & 1, and defined, // | ||
| 424 | if ($chns) { | ||
| 425 | return ($$model{$childtag} ? 1 | ||
| 426 | : ($$model{"!$childtag"} ? 0 | ||
| 427 | : ($$model{"$chns:*"} ? 1 | ||
| 428 | : ($$model{"!$chns:*"} ? 0 | ||
| 429 | : ($$model{'!*:*'} ? 0 | ||
| 430 | : ($$model{'*:*'} ? 1 | ||
| 431 | : 0)))))); } | ||
| 432 | else { | ||
| 433 | return ($$model{$childtag} ? 1 | ||
| 434 | : ($$model{"!$childtag"} ? 0 | ||
| 435 | : ($$model{'!*'} ? 0 | ||
| 436 | : ($$model{'*'} ? 1 | ||
| 437 | : 0)))); } } | ||
| 438 | |||
| 439 | # NOTE: Currently the Document class already allows ANY namespaced attributes! | ||
| 440 | # (which is very unmodular, although it does have to arrange for namespace declarations) | ||
| 441 | sub canHaveAttribute { | ||
| 442 | my ($self, $tag, $attrib) = @_; | ||
| 443 | $self->loadSchema unless $$self{schema_loaded}; | ||
| 444 | return 0 if $tag eq '#PCDATA'; | ||
| 445 | return 0 if $tag eq '#Comment'; | ||
| 446 | return 0 if $tag eq '#Document'; | ||
| 447 | return 0 if $tag eq '#ProcessingInstruction'; | ||
| 448 | return 0 if $tag eq '#DTD'; | ||
| 449 | return 1 if $tag =~ /(.*?:)?_Capture_$/; | ||
| 450 | return 1 if $attrib =~ /^_/; | ||
| 451 | return 1 if $$self{permissive}; | ||
| 452 | my ($tagns, $tagname) = ($tag =~ /^(\w+):(\w*)$/ ? ($1, $2) : ('', $tag)); | ||
| 453 | my ($attrns, $attrname) = ($attrib =~ /^(\w+):(\w*)$/ ? ($1, $2) : ('', $attrib)); | ||
| 454 | my $attr = $$self{tagprop}{$tag}{attributes}; | ||
| 455 | # Check: | ||
| 456 | # data for tag: tagns:tagname, tagns:* (or even *:tagname ???) | ||
| 457 | if (!$attr && ($tag =~ /^(\w*):/)) { | ||
| 458 | my $xtag = $1 . ':*'; | ||
| 459 | $attr = $$self{tagprop}{$xtag}{attributes}; } | ||
| 460 | # Test for allowance OR exclusion of an attribute, possibly w/namespace | ||
| 461 | # testing the most specific form first | ||
| 462 | # NOTE: We could be turning !ns:name into 0 and be checking for defined ??? | ||
| 463 | if ($attrns) { | ||
| 464 | return ($$attr{$attrib} ? 1 | ||
| 465 | : ($$attr{"!$attrib"} ? 0 | ||
| 466 | : ($$attr{"$attrns:*"} ? 1 | ||
| 467 | : ($$attr{"!$attrns:*"} ? 0 | ||
| 468 | : ($$attr{'!*:*'} ? 0 | ||
| 469 | : ($$attr{'*:*'} ? 1 | ||
| 470 | : 0)))))); } | ||
| 471 | else { | ||
| 472 | return ($$attr{$attrib} ? 1 | ||
| 473 | : ($$attr{"!$attrib"} ? 0 | ||
| 474 | : ($$attr{'!*'} ? 0 | ||
| 475 | : ($$attr{'*'} ? 1 | ||
| 476 | : 0)))); } } | ||
| 477 | |||
| 478 | sub getSchemaClassNames { | ||
| 479 | my ($self, $classname) = @_; | ||
| 480 | $self->loadSchema unless $$self{schema_loaded}; | ||
| 481 | my $class_data = $$self{schemaclass}{$classname}; | ||
| 482 | return $class_data ? (keys %$class_data) : (); } | ||
| 483 | |||
| 484 | sub isInSchemaClass { | ||
| 485 | my ($self, $classname, $tag) = @_; | ||
| 486 | $self->loadSchema unless $$self{schema_loaded}; | ||
| 487 | $tag = $self->getNodeQName($tag) if ref $tag; # In case tag is a node. | ||
| 488 | my $class = $$self{schemaclass}{$classname}; | ||
| 489 | return $class && $$class{$tag}; } | ||
| 490 | |||
| 491 | sub isKnownTag { | ||
| 492 | my ($self, $name) = @_; | ||
| 493 | return exists($$self{tagprop}{$name}); } | ||
| 494 | |||
| 495 | #********************************************************************** | ||
| 496 | sub describeModel { | ||
| 497 | my ($self) = @_; | ||
| 498 | Debug("Doctype"); | ||
| 499 | foreach my $tag (sort keys %{ $$self{tagprop} }) { | ||
| 500 | if (my $model = $$self{tagprop}{$tag}{model}) { | ||
| 501 | if (keys %$model) { | ||
| 502 | Debug("$tag can contain " . join(', ', sort keys %{ $$self{tagprop}{$tag}{model} })); } } | ||
| 503 | else { | ||
| 504 | Debug("$tag is empty"); } | ||
| 505 | } | ||
| 506 | return; } | ||
| 507 | |||
| 508 | #********************************************************************** | ||
| 509 | 1; | ||
| 510 | |||
| 511 | __END__ | ||