1 |
wakaba |
1.1 |
package Message::DOM::EntityReference; |
2 |
|
|
use strict; |
3 |
wakaba |
1.6 |
our $VERSION=do{my @r=(q$Revision: 1.5 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
4 |
wakaba |
1.1 |
push our @ISA, 'Message::DOM::Node', 'Message::IF::EntityReference'; |
5 |
|
|
require Message::DOM::Node; |
6 |
|
|
|
7 |
|
|
sub ____new ($$$) { |
8 |
|
|
my $self = shift->SUPER::____new (shift); |
9 |
wakaba |
1.2 |
$$self->{node_name} = $_[0]; |
10 |
wakaba |
1.3 |
$$self->{child_nodes} = []; |
11 |
wakaba |
1.1 |
return $self; |
12 |
|
|
} # ____new |
13 |
|
|
|
14 |
|
|
sub AUTOLOAD { |
15 |
|
|
my $method_name = our $AUTOLOAD; |
16 |
|
|
$method_name =~ s/.*:://; |
17 |
|
|
return if $method_name eq 'DESTROY'; |
18 |
|
|
|
19 |
|
|
if ({ |
20 |
|
|
## Read-only attributes (trivial accessors) |
21 |
wakaba |
1.2 |
node_name => 1, |
22 |
wakaba |
1.1 |
}->{$method_name}) { |
23 |
|
|
no strict 'refs'; |
24 |
|
|
eval qq{ |
25 |
|
|
sub $method_name (\$) { |
26 |
|
|
if (\@_ > 1) { |
27 |
|
|
require Carp; |
28 |
|
|
Carp::croak (qq<Can't modify read-only attribute>); |
29 |
|
|
} |
30 |
|
|
return \${\$_[0]}->{$method_name}; |
31 |
|
|
} |
32 |
|
|
}; |
33 |
|
|
goto &{ $AUTOLOAD }; |
34 |
|
|
} elsif ({ |
35 |
wakaba |
1.4 |
## Read-write attributes (boolean, trivial accessors) |
36 |
|
|
manakai_expanded => 1, |
37 |
|
|
manakai_external => 1, |
38 |
wakaba |
1.1 |
}->{$method_name}) { |
39 |
|
|
no strict 'refs'; |
40 |
|
|
eval qq{ |
41 |
wakaba |
1.4 |
sub $method_name (\$;\$) { |
42 |
wakaba |
1.1 |
if (\@_ > 1) { |
43 |
wakaba |
1.4 |
if (\${\${\$_[0]}->{owner_document}}->{manakai_strict_error_checking} and |
44 |
|
|
\${\$_[0]}->{manakai_read_only}) { |
45 |
|
|
report Message::DOM::DOMException |
46 |
|
|
-object => \$_[0], |
47 |
|
|
-type => 'NO_MODIFICATION_ALLOWED_ERR', |
48 |
|
|
-subtype => 'READ_ONLY_NODE_ERR'; |
49 |
|
|
} |
50 |
|
|
if (\$_[1]) { |
51 |
|
|
\${\$_[0]}->{$method_name} = 1; |
52 |
|
|
} else { |
53 |
|
|
delete \${\$_[0]}->{$method_name}; |
54 |
|
|
} |
55 |
wakaba |
1.1 |
} |
56 |
wakaba |
1.4 |
return \${\$_[0]}->{$method_name}; |
57 |
wakaba |
1.1 |
} |
58 |
|
|
}; |
59 |
|
|
goto &{ $AUTOLOAD }; |
60 |
|
|
} else { |
61 |
|
|
require Carp; |
62 |
|
|
Carp::croak (qq<Can't locate method "$AUTOLOAD">); |
63 |
|
|
} |
64 |
|
|
} # AUTOLOAD |
65 |
|
|
|
66 |
wakaba |
1.4 |
## |Node| attributes |
67 |
|
|
|
68 |
|
|
sub base_uri ($) { |
69 |
|
|
## NOTE: Same as |CharacterData|'s. |
70 |
wakaba |
1.1 |
|
71 |
wakaba |
1.4 |
my $self = $_[0]; |
72 |
|
|
local $Error::Depth = $Error::Depth + 1; |
73 |
|
|
my $pe = $$self->{parent_node}; |
74 |
|
|
while (defined $pe) { |
75 |
|
|
my $nt = $pe->node_type; |
76 |
|
|
if ($nt == 1 or $nt == 2 or $nt == 6 or $nt == 9 or $nt == 11) { |
77 |
|
|
## Element, Attr, Entity, Document, or DocumentFragment |
78 |
|
|
return $pe->base_uri; |
79 |
|
|
} elsif ($nt == 5) { |
80 |
|
|
## EntityReference |
81 |
|
|
return $pe->manakai_entity_base_uri if $pe->manakai_external; |
82 |
|
|
} |
83 |
|
|
$pe = $$pe->{parent_node}; |
84 |
|
|
} |
85 |
|
|
return $pe->base_uri if $pe; |
86 |
|
|
return $$self->{owner_document}->base_uri; |
87 |
|
|
} # base_uri |
88 |
wakaba |
1.2 |
|
89 |
|
|
sub node_name ($); # read-only trivial accessor |
90 |
|
|
|
91 |
wakaba |
1.4 |
sub node_type () { 5 } # ENTITY_REFERENCE_NODE |
92 |
|
|
|
93 |
|
|
## |EntityReference| attributes |
94 |
|
|
|
95 |
|
|
sub manakai_entity_base_uri ($;$) { |
96 |
|
|
my $self = $_[0]; |
97 |
|
|
if (@_ > 1) { |
98 |
|
|
if (${$$self->{owner_document}}->{strict_error_checking}) { |
99 |
|
|
if ($$self->{manakai_read_only}) { |
100 |
|
|
report Message::DOM::DOMException |
101 |
|
|
-object => $self, |
102 |
|
|
-type => 'NO_MODIFICATION_ALLOWED_ERR', |
103 |
|
|
-subtype => 'READ_ONLY_NODE_ERR'; |
104 |
|
|
} |
105 |
|
|
} |
106 |
|
|
if (defined $_[1]) { |
107 |
|
|
$$self->{manakai_entity_base_uri} = ''.$_[1]; |
108 |
|
|
} else { |
109 |
|
|
delete $$self->{manakai_entity_base_uri}; |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
if (defined $$self->{manakai_entity_base_uri}) { |
114 |
|
|
return $$self->{manakai_entity_base_uri}; |
115 |
|
|
} else { |
116 |
|
|
local $Error::Depth = $Error::Depth + 1; |
117 |
|
|
return $self->base_uri; |
118 |
|
|
} |
119 |
|
|
} # manakai_entity_base_uri |
120 |
|
|
|
121 |
|
|
sub manakai_expanded ($;$); |
122 |
wakaba |
1.2 |
|
123 |
wakaba |
1.4 |
sub manakai_external ($;$); |
124 |
wakaba |
1.1 |
|
125 |
|
|
package Message::IF::EntityReference; |
126 |
|
|
|
127 |
|
|
package Message::DOM::Document; |
128 |
|
|
|
129 |
|
|
sub create_entity_reference ($$) { |
130 |
wakaba |
1.5 |
our $CreateEntityReference_OpenEntity; |
131 |
|
|
## TODO: This is Multithread unsafe |
132 |
|
|
|
133 |
|
|
my $self = $_[0]; |
134 |
|
|
my $orig_strict = $self->strict_error_checking; |
135 |
|
|
if ($orig_strict) { |
136 |
|
|
my $xv = $self->xml_version; |
137 |
|
|
if (defined $xv) { |
138 |
|
|
if ($xv eq '1.0' and |
139 |
|
|
$_[1] =~ /\A\p{InXML_NameStartChar10}\p{InXMLNameChar10}*\z/) { |
140 |
|
|
# |
141 |
|
|
} elsif ($xv eq '1.1' and |
142 |
|
|
$_[1] =~ /\A\p{InXMLNameStartChar11}\p{InXMLNameChar11}*\z/) { |
143 |
|
|
# |
144 |
|
|
} else { |
145 |
|
|
report Message::DOM::DOMException |
146 |
|
|
-object => $self, |
147 |
|
|
-type => 'INVALID_CHARACTER_ERR', |
148 |
|
|
-subtype => 'MALFORMED_NAME_ERR'; |
149 |
|
|
} |
150 |
|
|
} |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
my $r = Message::DOM::EntityReference->____new ($self, $_[1]); |
154 |
|
|
|
155 |
|
|
## Expansion |
156 |
|
|
unless ($CreateEntityReference_OpenEntity->{$_[1]}) { |
157 |
|
|
local $CreateEntityReference_OpenEntity->{$_[1]} = 1; |
158 |
|
|
local $Error::Depth = $Error::Depth + 1; |
159 |
|
|
|
160 |
|
|
my $doctype = $self->doctype; |
161 |
|
|
unless ($doctype) { |
162 |
|
|
$r->manakai_set_read_only (1, 1); |
163 |
|
|
return $r; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
my $ent = $doctype->get_general_entity_node ($_[1]); |
167 |
|
|
unless ($ent) { |
168 |
|
|
$r->manakai_set_read_only (1, 1); |
169 |
|
|
return $r; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
$self->strict_error_checking (0); |
173 |
|
|
for my $c (@{$ent->child_nodes}) { |
174 |
|
|
my $clone = $c->clone_node (1); |
175 |
|
|
$r->append_child ($clone); |
176 |
|
|
} |
177 |
|
|
$r->manakai_expanded ($ent->has_replacement_tree); |
178 |
|
|
$self->strict_error_checking ($orig_strict); |
179 |
|
|
} |
180 |
|
|
$r->manakai_set_read_only (1, 1); |
181 |
|
|
return $r; |
182 |
wakaba |
1.1 |
} # create_entity_reference |
183 |
|
|
|
184 |
wakaba |
1.4 |
=head1 LICENSE |
185 |
|
|
|
186 |
|
|
Copyright 2007 Wakaba <w@suika.fam.cx> |
187 |
|
|
|
188 |
|
|
This program is free software; you can redistribute it and/or |
189 |
|
|
modify it under the same terms as Perl itself. |
190 |
|
|
|
191 |
|
|
=cut |
192 |
|
|
|
193 |
wakaba |
1.1 |
1; |
194 |
wakaba |
1.6 |
## $Date: 2007/07/07 11:11:34 $ |