| 1 |
wakaba |
1.1 |
|
| 2 |
|
|
=head1 NAME |
| 3 |
|
|
|
| 4 |
|
|
SuikaWiki::Markup::XML::EntityManager --- SuikaWiki XML: Entity manager |
| 5 |
|
|
|
| 6 |
|
|
=head1 DESCRIPTION |
| 7 |
|
|
|
| 8 |
|
|
This module is part of SuikaWiki XML support. |
| 9 |
|
|
|
| 10 |
|
|
=cut |
| 11 |
|
|
|
| 12 |
|
|
package SuikaWiki::Markup::XML::EntityManager; |
| 13 |
|
|
use strict; |
| 14 |
|
|
our $VERSION = do{my @r=(q$Revision: 1.1 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
| 15 |
|
|
|
| 16 |
|
|
my %NS = ( |
| 17 |
|
|
SGML => 'urn:x-suika-fam-cx:markup:sgml:', |
| 18 |
|
|
); |
| 19 |
|
|
|
| 20 |
|
|
sub new ($$) { |
| 21 |
|
|
my ($class, $yourself) = @_; |
| 22 |
|
|
my $self = bless {node => $yourself}, $class; |
| 23 |
|
|
for (@{$yourself->{node}}) { |
| 24 |
|
|
if ($_->{type} eq '#declaration' && $_->{namespace_uri} eq $NS{SGML}.'doctype') { |
| 25 |
|
|
$self->{doctype} = $_; |
| 26 |
|
|
last; |
| 27 |
|
|
} |
| 28 |
|
|
} |
| 29 |
|
|
$self; |
| 30 |
|
|
} |
| 31 |
|
|
|
| 32 |
|
|
## TODO: is this result cachable? |
| 33 |
|
|
sub get_entity ($$%) { |
| 34 |
|
|
my ($self, $name, %o) = @_; |
| 35 |
|
|
if (ref $name) { |
| 36 |
|
|
$o{namespace_uri} ||= $name->{namespace_uri}; |
| 37 |
|
|
$name = $name->{local_name}; |
| 38 |
|
|
} else { |
| 39 |
|
|
$o{namespace_uri} ||= $NS{SGML}.'entity'; |
| 40 |
|
|
} |
| 41 |
wakaba |
1.2 |
if (!$o{dont_use_predefined_entities} |
| 42 |
|
|
&& $o{namespace_uri} eq $NS{SGML}.'entity') { ## General entity |
| 43 |
|
|
my $predec = { |
| 44 |
|
|
amp => '&', |
| 45 |
|
|
apos => ''', |
| 46 |
|
|
gt => '>', |
| 47 |
|
|
lt => '<', |
| 48 |
|
|
quot => '"', |
| 49 |
|
|
}->{$name}; |
| 50 |
|
|
if ($predec) { |
| 51 |
|
|
for (SuikaWiki::Markup::XML->new (type => '#declaration', |
| 52 |
|
|
namespace_uri => $NS{SGML}.'entity')) { |
| 53 |
|
|
$_->set_attribute ('value')->append_new_node (type => '#xml', value => $predec); |
| 54 |
|
|
return $_; |
| 55 |
|
|
} |
| 56 |
wakaba |
1.1 |
} |
| 57 |
|
|
} |
| 58 |
wakaba |
1.2 |
$self->_get_entity ($name, $self->{doctype}->{node}, \%o); |
| 59 |
wakaba |
1.1 |
} |
| 60 |
|
|
sub _get_entity ($$$$) { |
| 61 |
|
|
my ($self, $name, $nodes, $o) = @_; |
| 62 |
|
|
return undef unless ref $nodes; |
| 63 |
|
|
for (@$nodes) { |
| 64 |
|
|
if ($_->{type} eq '#declaration' && $_->{namespace_uri} eq $o->{namespace_uri} |
| 65 |
|
|
&& $_->{local_name} eq $name) { |
| 66 |
|
|
return $_; |
| 67 |
|
|
} elsif ($_->{type} eq '#reference') { |
| 68 |
|
|
my $e = $self->_get_entity ($name, $_->{node}, $o); |
| 69 |
|
|
return $e if ref $e; |
| 70 |
|
|
} |
| 71 |
|
|
} |
| 72 |
|
|
return undef; |
| 73 |
|
|
} |
| 74 |
|
|
|
| 75 |
wakaba |
1.2 |
# DOM's get*By* |
| 76 |
|
|
sub get_entities ($$%) { |
| 77 |
|
|
my ($self, $l, %o) = @_; |
| 78 |
|
|
$o{namespace_uri} ||= $NS{SGML}.'entity'; |
| 79 |
|
|
$self->_get_entities ($l, $self->{doctype}->{node}, \%o); |
| 80 |
|
|
} |
| 81 |
|
|
sub _get_entities ($$$$) { |
| 82 |
|
|
my ($self, $l, $nodes, $o) = @_; |
| 83 |
|
|
return undef unless ref $nodes; |
| 84 |
|
|
for (@$nodes) { |
| 85 |
|
|
if ($_->{type} eq '#declaration' && $_->{namespace_uri} eq $o->{namespace_uri}) { |
| 86 |
|
|
push @$l, $_; |
| 87 |
|
|
} elsif ($_->{type} eq '#reference') { |
| 88 |
|
|
$self->_get_entities ($l, $_->{node}, $o); |
| 89 |
|
|
} |
| 90 |
|
|
} |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
wakaba |
1.1 |
sub is_standalone_document_1 ($) { |
| 94 |
|
|
my $self = shift; |
| 95 |
|
|
return $self->{node}->{flag}->{smxe__standalone_1} |
| 96 |
|
|
if defined $self->{node}->{flag}->{smxe__standalone_1}; |
| 97 |
|
|
for (@{$self->{node}->{node}}) { |
| 98 |
|
|
if ($_->{type} eq '#pi' && $_->{local_name} eq 'xml') { |
| 99 |
|
|
my $a = $_->get_attribute ('standalone'); |
| 100 |
|
|
if (ref $a) { |
| 101 |
|
|
$self->{node}->{flag}->{smxe__standalone_1} = $a->inner_text eq 'yes' ? 1 : 0; |
| 102 |
|
|
return $self->{node}->{flag}->{smxe__standalone_1}; |
| 103 |
|
|
} |
| 104 |
|
|
last; |
| 105 |
|
|
} |
| 106 |
|
|
} |
| 107 |
|
|
if ($self->{doctype}) { |
| 108 |
|
|
if ($self->{doctype}->external_id) { |
| 109 |
|
|
$self->{node}->{flag}->{smxe__standalone_1} = 0; |
| 110 |
|
|
return $self->{node}->{flag}->{smxe__standalone_1}; |
| 111 |
|
|
} |
| 112 |
|
|
for (@{$self->{doctype}->{node}}) { |
| 113 |
|
|
if ($_->{type} eq '#declaration' && $_->{namespace_uri} eq $NS{SGML}.'entity:parameter') { |
| 114 |
|
|
$self->{node}->{flag}->{smxe__standalone_1} = 0; |
| 115 |
|
|
return $self->{node}->{flag}->{smxe__standalone_1}; |
| 116 |
|
|
} |
| 117 |
|
|
} |
| 118 |
|
|
} |
| 119 |
|
|
$self->{node}->{flag}->{smxe__standalone_1} = 1; |
| 120 |
|
|
return $self->{node}->{flag}->{smxe__standalone_1}; |
| 121 |
|
|
} |
| 122 |
|
|
|
| 123 |
|
|
=head1 LICENSE |
| 124 |
|
|
|
| 125 |
|
|
Copyright 2003 Wakaba <[email protected]> |
| 126 |
|
|
|
| 127 |
|
|
This program is free software; you can redistribute it and/or |
| 128 |
|
|
modify it under the same terms as Perl itself. |
| 129 |
|
|
|
| 130 |
|
|
=cut |
| 131 |
|
|
|
| 132 |
wakaba |
1.2 |
1; # $Date: 2003/06/16 09:58:26 $ |