1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME |
3 |
|
|
|
4 |
|
|
SuikaWiki::Markup::XML --- SuikaWiki: Simple well-formed document fragment generator |
5 |
|
|
|
6 |
|
|
=head1 DESCRIPTION |
7 |
|
|
|
8 |
|
|
This module can be used to generate the document fragment of XML, SGML or |
9 |
|
|
other well-formed (in XML meaning) data formats with the object oriented manner. |
10 |
|
|
|
11 |
|
|
This module cannot be used to parse XML (or other marked-up) document (or its fragment) |
12 |
|
|
by itself, nor is compatible with other huge packages such as XML::Parser. The only purpose |
13 |
|
|
of this module is to make it easy for tiny perl scripts to GENERATE well-formed |
14 |
|
|
markup constructures. (SuikaWiki is not "tiny"? Oh, yes, I see:-)) |
15 |
|
|
|
16 |
|
|
=cut |
17 |
|
|
|
18 |
|
|
package SuikaWiki::Markup::XML; |
19 |
|
|
use strict; |
20 |
wakaba |
1.15 |
our $VERSION = do{my @r=(q$Revision: 1.14 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
21 |
wakaba |
1.9 |
use overload '""' => \&outer_xml, |
22 |
wakaba |
1.2 |
fallback => 1; |
23 |
|
|
use Char::Class::XML qw!InXML_NameStartChar InXMLNameChar InXML_NCNameStartChar InXMLNCNameChar!; |
24 |
wakaba |
1.1 |
our %Namespace_URI_to_prefix = ( |
25 |
|
|
'DAV:' => [qw/dav webdav/], |
26 |
|
|
'http://greenbytes.de/2002/rfcedit' => [qw/ed/], |
27 |
|
|
'http://icl.com/saxon' => [qw/saxon/], |
28 |
|
|
'http://members.jcom.home.ne.jp/jintrick/2003/02/site-concept.xml#' => ['', qw/sitemap/], |
29 |
|
|
'http://purl.org/dc/elements/1.1/' => [qw/dc dc11/], |
30 |
|
|
'http://purl.org/rss/1.0/' => ['', qw/rss rss10/], |
31 |
|
|
'http://suika.fam.cx/~wakaba/lang/rfc/translation/' => [qw/ja/], |
32 |
|
|
'http://www.mozilla.org/xbl' => ['', qw/xbl/], |
33 |
wakaba |
1.3 |
'http://www.w3.org/1999/02/22-rdf-syntax-ns#' => [qw/rdf/], |
34 |
wakaba |
1.1 |
'http://www.w3.org/1999/xhtml' => ['', qw/h h1 xhtml xhtml1/], |
35 |
|
|
'http://www.w3.org/1999/xlink' => [qw/l xlink/], |
36 |
|
|
'http://www.w3.org/1999/XSL/Format' => [qw/fo xslfo xsl-fo xsl/], |
37 |
|
|
'http://www.w3.org/1999/XSL/Transform' => [qw/t s xslt xsl/], |
38 |
|
|
'http://www.w3.org/1999/XSL/TransformAlias' => [qw/axslt axsl xslt xsl/], |
39 |
|
|
'http://www.w3.org/2000/01/rdf-schema#' => [qw/rdfs/], |
40 |
|
|
'http://www.w3.org/2000/svg' => ['', qw/s svg/], |
41 |
|
|
'http://www.w3.org/2002/06/hlink' => [qw/h hlink/], |
42 |
|
|
'http://www.w3.org/2002/06/xhtml2' => ['', qw/h h2 xhtml xhtml2/], |
43 |
wakaba |
1.3 |
'http://www.w3.org/2002/07/owl' => [qw/owl/], |
44 |
wakaba |
1.6 |
'http://www.w3.org/2002/xforms/cr' => [qw/f xforms/], |
45 |
wakaba |
1.1 |
'http://www.w3.org/TR/REC-smil' => ['', qw/smil smil1/], |
46 |
wakaba |
1.3 |
'http://www.wapforum.org/2001/wml' => [qw/wap/], |
47 |
wakaba |
1.1 |
'http://xml.apache.org/xalan' => [qw/xalan/], |
48 |
|
|
'mailto:julian.reschke@greenbytes.de?subject=rcf2629.xslt' => [qw/myns/], |
49 |
|
|
'urn:schemas-microsoft-com:vml' => [qw/v vml/], |
50 |
|
|
'urn:schemas-microsoft-com:xslt' => [qw/ms msxsl msxslt/], |
51 |
|
|
'urn:x-suika-fam-cx:markup:ietf:html:3:draft:00' => ['', qw/H HTML HTML3/], |
52 |
wakaba |
1.7 |
'urn:x-suika-fam-cx:markup:ietf:rfc:2629' => ['', qw/rfc rfc2629/], |
53 |
wakaba |
1.1 |
); |
54 |
|
|
my %Cache; |
55 |
wakaba |
1.10 |
our %NS = ( |
56 |
wakaba |
1.7 |
SGML => 'urn:x-suika-fam-cx:markup:sgml:', |
57 |
w |
1.12 |
XML => 'urn:x-suika-fam-cx:markup:xml:', |
58 |
wakaba |
1.10 |
internal_attr_duplicate => 'http://suika.fam.cx/~wakaba/-temp/2003/05/17/invalid-attr#', |
59 |
|
|
internal_invalid_sysid => 'http://system.identifier.invalid/', |
60 |
|
|
internal_ns_invalid => 'http://suika.fam.cx/~wakaba/-temp/2003/05/17/unknown-namespace#', |
61 |
wakaba |
1.8 |
xml => 'http://www.w3.org/XML/1998/namespace', |
62 |
|
|
xmlns => 'http://www.w3.org/2000/xmlns/', |
63 |
wakaba |
1.7 |
); |
64 |
wakaba |
1.1 |
|
65 |
|
|
=head1 METHODS |
66 |
|
|
|
67 |
|
|
=over 4 |
68 |
|
|
|
69 |
|
|
=item $x = SuikaWiki::Markup::XML->new (%options) |
70 |
|
|
|
71 |
|
|
Returns new instance of the module. It is itself a node. |
72 |
|
|
|
73 |
wakaba |
1.4 |
Available options: C<data_type>, C<default_decl>, C<type> (default: C<#element>), C<local_name>, C<namespace_uri> and C<value>. |
74 |
wakaba |
1.1 |
|
75 |
|
|
=cut |
76 |
|
|
|
77 |
|
|
sub new ($;%) { |
78 |
|
|
my $class = shift; |
79 |
|
|
my $self = bless {@_}, $class; |
80 |
|
|
$self->{type} ||= '#element'; |
81 |
wakaba |
1.5 |
if ($self->{qname}) { |
82 |
|
|
($self->{namespace_prefix}, $self->{local_name}) = $self->_ns_parse_qname ($self->{qname}); |
83 |
|
|
$self->{_qname} = $self->{qname}; |
84 |
|
|
} |
85 |
|
|
if (defined $self->{namespace_prefix}) { |
86 |
|
|
$self->{namespace_prefix} .= ':' if $self->{namespace_prefix} |
87 |
|
|
&& substr ($self->{namespace_prefix}, -1) ne ':'; |
88 |
wakaba |
1.10 |
$self->{ns}->{$self->{namespace_prefix}||''} = $self->{namespace_uri} |
89 |
|
|
if defined $self->{namespace_uri}; |
90 |
wakaba |
1.5 |
} |
91 |
wakaba |
1.4 |
for (qw/local_name value/) { |
92 |
wakaba |
1.10 |
if ($self->_is_same_class ($self->{$_})) { |
93 |
wakaba |
1.2 |
$self->{$_}->{parent} = $self; |
94 |
|
|
} |
95 |
|
|
} |
96 |
wakaba |
1.1 |
$self->{node} ||= []; |
97 |
|
|
$self; |
98 |
|
|
} |
99 |
|
|
|
100 |
wakaba |
1.5 |
sub _ns_parse_qname ($$) { |
101 |
wakaba |
1.10 |
my $qname = $_[1]; |
102 |
wakaba |
1.5 |
if ($qname =~ /:/) { |
103 |
|
|
return split /:/, $qname, 2; |
104 |
|
|
} else { |
105 |
|
|
return (undef, $qname); |
106 |
|
|
} |
107 |
|
|
} |
108 |
wakaba |
1.1 |
|
109 |
|
|
=item $x->append_node ($node) |
110 |
|
|
|
111 |
|
|
Appending given node to the object (as the last child). |
112 |
|
|
If the type of given node is C<#fragment>, its all children, not the node |
113 |
|
|
itself, are appended. |
114 |
|
|
|
115 |
|
|
This method returns the appended node unless the type of given node is C<#fragment>. |
116 |
|
|
In such cases, this node (C<$x>) is returned. |
117 |
|
|
|
118 |
wakaba |
1.2 |
Available options: C<node_or_text>. |
119 |
|
|
|
120 |
wakaba |
1.1 |
=cut |
121 |
|
|
|
122 |
wakaba |
1.2 |
sub append_node ($$;%) { |
123 |
wakaba |
1.1 |
my $self = shift; |
124 |
wakaba |
1.2 |
my ($new_node, %o) = @_; |
125 |
|
|
unless (ref $new_node) { |
126 |
|
|
if ($o{node_or_text}) { |
127 |
|
|
return $self->append_text ($new_node); |
128 |
|
|
} else { |
129 |
wakaba |
1.10 |
die "append_node: Invalid node"; |
130 |
wakaba |
1.2 |
} |
131 |
|
|
} |
132 |
wakaba |
1.1 |
if ($new_node->{type} eq '#fragment') { |
133 |
|
|
for (@{$new_node->{node}}) { |
134 |
|
|
push @{$self->{node}}, $_; |
135 |
|
|
$_->{parent} = $self; |
136 |
|
|
} |
137 |
|
|
$self; |
138 |
|
|
} else { |
139 |
|
|
push @{$self->{node}}, $new_node; |
140 |
|
|
$new_node->{parent} = $self; |
141 |
|
|
$new_node; |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
=item $new_node = $x->append_new_node (%options) |
146 |
|
|
|
147 |
|
|
Appending a new node. The new node is returned. |
148 |
|
|
|
149 |
|
|
Available options: C<type>, C<namespace_uri>, C<local_name>, C<value>. |
150 |
|
|
|
151 |
|
|
=cut |
152 |
|
|
|
153 |
|
|
sub append_new_node ($;%) { |
154 |
|
|
my $self = shift; |
155 |
wakaba |
1.10 |
my $new_node = __PACKAGE__->new (@_); |
156 |
wakaba |
1.1 |
push @{$self->{node}}, $new_node; |
157 |
|
|
$new_node->{parent} = $self; |
158 |
|
|
$new_node; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
=item $new_node = $x->append_text ($text) |
162 |
|
|
|
163 |
|
|
Appending given text as a new text node. The new text node is returned. |
164 |
|
|
|
165 |
|
|
=cut |
166 |
|
|
|
167 |
|
|
sub append_text ($$;%) { |
168 |
|
|
my $self = shift; |
169 |
|
|
my $s = shift; |
170 |
wakaba |
1.10 |
$self->append_new_node (type => '#text', value => $s); |
171 |
wakaba |
1.1 |
} |
172 |
|
|
|
173 |
wakaba |
1.2 |
sub append_baretext ($$;%) { |
174 |
|
|
my $self = shift; |
175 |
|
|
my $s = shift; |
176 |
|
|
$self->append_new_node (type => '#xml', value => $s); |
177 |
|
|
} |
178 |
|
|
|
179 |
wakaba |
1.9 |
sub remove_child_node ($$) { |
180 |
|
|
my ($self, $node) = @_; |
181 |
|
|
return unless ref $node; |
182 |
|
|
$node = overload::StrVal ($node); |
183 |
|
|
$self->{node} = [grep { overload::StrVal ($_) ne $node } @{$self->{node}}]; |
184 |
|
|
} |
185 |
|
|
|
186 |
wakaba |
1.1 |
=item $attr_node = $x->get_attribute ($local_name, %options) |
187 |
|
|
|
188 |
|
|
Returns the attribute node whose local-name is C<$local_name>. |
189 |
|
|
|
190 |
|
|
Available options: C<namespace_uri>, C<make_new_node>. |
191 |
|
|
|
192 |
|
|
=cut |
193 |
|
|
|
194 |
|
|
sub get_attribute ($$;%) { |
195 |
wakaba |
1.10 |
my ($self, $name, %o) = @_; |
196 |
wakaba |
1.1 |
for (@{$self->{node}}) { |
197 |
wakaba |
1.10 |
if ($_->{type} eq '#attribute' |
198 |
|
|
&& $_->{local_name} eq $name |
199 |
|
|
&& $o{namespace_uri} eq $_->{namespace_uri}) { |
200 |
wakaba |
1.1 |
return $_; |
201 |
|
|
} |
202 |
|
|
} |
203 |
|
|
## Node is not exist |
204 |
|
|
if ($o{make_new_node}) { |
205 |
wakaba |
1.10 |
return $self->append_new_node (type => '#attribute', local_name => $name, |
206 |
|
|
namespace_uri => $o{namespace_uri}); |
207 |
wakaba |
1.1 |
} else { |
208 |
|
|
return undef; |
209 |
|
|
} |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
=item $attr_node = $x->set_attribute ($local_name => $value, %options) |
213 |
|
|
|
214 |
|
|
Set the value of the attribute. The attribute node is returned. |
215 |
|
|
|
216 |
|
|
Available options: C<namespace_uri>. |
217 |
|
|
|
218 |
|
|
=cut |
219 |
|
|
|
220 |
|
|
sub set_attribute ($$$;%) { |
221 |
wakaba |
1.10 |
my ($self, $name, $val, %o) = @_; |
222 |
|
|
if ({qw/ARRAY 1 HASH 1 CODE 1/}->{ref ($val)}) { |
223 |
wakaba |
1.7 |
## TODO: common error handling |
224 |
|
|
require Carp; |
225 |
|
|
Carp::croak "set_attribute: new attribute value must be string or blessed object"; |
226 |
|
|
} |
227 |
wakaba |
1.1 |
for (@{$self->{node}}) { |
228 |
wakaba |
1.10 |
if ($_->{type} eq '#attribute' |
229 |
|
|
&& $_->{local_name} eq $name |
230 |
|
|
&& $o{namespace_uri} eq $_->{namespace_uri}) { |
231 |
wakaba |
1.1 |
$_->{value} = $val; |
232 |
|
|
$_->{node} = []; |
233 |
|
|
return $_; |
234 |
|
|
} |
235 |
|
|
} |
236 |
wakaba |
1.10 |
return $self->append_new_node (type => '#attribute', local_name => $name, |
237 |
|
|
value => $val, namespace_uri => $o{namespace_uri}); |
238 |
wakaba |
1.1 |
} |
239 |
|
|
|
240 |
|
|
=item \@children = $x->child_nodes |
241 |
|
|
|
242 |
|
|
Returns an array reference to child nodes. |
243 |
|
|
|
244 |
wakaba |
1.4 |
=item $local_name = $x->local_name ([$new_name]) |
245 |
wakaba |
1.1 |
|
246 |
wakaba |
1.4 |
Returns or set the local-name. |
247 |
|
|
|
248 |
|
|
=item $uri = $x->namespace_uri ([$new_uri]) |
249 |
|
|
|
250 |
|
|
Returns or set namespace name (URI) of the element or the attribute |
251 |
wakaba |
1.1 |
|
252 |
wakaba |
1.5 |
=item $uri = $x->namespace_prefix ([$new_prefix]) |
253 |
|
|
|
254 |
|
|
Returns or set namespace prefix of the element or the attribute. |
255 |
|
|
You may give C<$new_prefix> in form either 'foo' or 'foo:'. |
256 |
|
|
To indicate "default" prefix, use '' (length == 0 string). |
257 |
|
|
|
258 |
wakaba |
1.15 |
=item $uri or ($uri, $name) = $x->expanded_name |
259 |
|
|
|
260 |
|
|
Returns expanded name of the node (element or attribute). |
261 |
|
|
In array context, array of namespace name (URI) and local part |
262 |
|
|
is returned; otherwise, a URI which identify name of the node |
263 |
|
|
(in RDF or WebDAV) is returned. |
264 |
|
|
|
265 |
wakaba |
1.1 |
=item $type = $x->node_type |
266 |
|
|
|
267 |
|
|
Returns the node type. |
268 |
|
|
|
269 |
|
|
=item $node = $x->parent_node |
270 |
|
|
|
271 |
|
|
Returns the parent node. If there is no parent node, undef is returned. |
272 |
|
|
|
273 |
|
|
=cut |
274 |
|
|
|
275 |
wakaba |
1.10 |
sub child_nodes ($) { $_[0]->{node} } |
276 |
wakaba |
1.4 |
sub local_name ($;$) { |
277 |
|
|
my ($self, $newname) = @_; |
278 |
wakaba |
1.10 |
$self->{local_name} = $newname if $newname; |
279 |
wakaba |
1.4 |
if (ref $self->{local_name} && $self->{local_name}->{type} eq '#declaration') { |
280 |
|
|
$self->{local_name}->{local_name}; |
281 |
|
|
} else { |
282 |
|
|
$self->{local_name} |
283 |
|
|
} |
284 |
|
|
} |
285 |
wakaba |
1.10 |
sub node_type ($) { $_[0]->{type} } |
286 |
|
|
sub parent_node ($) { $_[0]->{parent} } |
287 |
wakaba |
1.1 |
|
288 |
wakaba |
1.4 |
sub namespace_uri ($;$) { |
289 |
|
|
my ($self, $new_uri) = @_; |
290 |
wakaba |
1.10 |
$self->{namespace_uri} = $new_uri if defined $new_uri; |
291 |
wakaba |
1.4 |
$self->{namespace_uri}; |
292 |
|
|
} |
293 |
wakaba |
1.5 |
sub namespace_prefix ($;$) { |
294 |
|
|
my ($self, $new_pfx) = @_; |
295 |
|
|
if (defined $new_pfx && $self->{namespace_uri}) { |
296 |
|
|
$new_pfx .= ':' if $new_pfx; |
297 |
|
|
$self->{namespace_prefix} = $new_pfx; |
298 |
|
|
$self->{ns}->{$new_pfx} = $self->{namespace_uri}; |
299 |
|
|
} |
300 |
|
|
$self->_get_namespace_prefix ($self->{namespace_uri}); |
301 |
|
|
} |
302 |
wakaba |
1.4 |
|
303 |
wakaba |
1.15 |
sub expanded_name ($) { |
304 |
|
|
my $self = shift; |
305 |
|
|
wantarray ? ($self->{namespace_uri}, $self->{local_name}) |
306 |
|
|
: $self->{namespace_uri} . $self->{local_name}; |
307 |
|
|
} |
308 |
|
|
|
309 |
wakaba |
1.1 |
=item $i = $x->count |
310 |
|
|
|
311 |
|
|
Returns the number of child nodes. |
312 |
|
|
|
313 |
|
|
=cut |
314 |
|
|
|
315 |
|
|
# TODO: support counting by type |
316 |
|
|
sub count ($;@) { |
317 |
wakaba |
1.10 |
(defined $_[0]->{value} ? 1 : 0) + scalar @{$_[0]->{node}}; |
318 |
wakaba |
1.1 |
} |
319 |
|
|
|
320 |
|
|
# $prefix = $x->_get_namespace_prefix ($namespace_uri) |
321 |
|
|
sub _get_namespace_prefix ($$;%) { |
322 |
wakaba |
1.10 |
my ($self, $uri, %o) = @_; |
323 |
wakaba |
1.1 |
if (defined (my $p = $self->_uri_to_prefix ($uri, undef, %o))) { |
324 |
|
|
return $p if $self->_prefix_to_uri ($p) eq $uri; |
325 |
|
|
} if ($Namespace_URI_to_prefix{$uri}) { |
326 |
|
|
for (@{$Namespace_URI_to_prefix{$uri}}) { |
327 |
|
|
my $pfx = $_; $pfx .= ':' if $pfx; |
328 |
|
|
if ($self->_check_namespace_prefix ($pfx) && !$self->_prefix_to_uri ($pfx)) { |
329 |
|
|
return $self->_uri_to_prefix ($uri => $pfx, %o); |
330 |
|
|
} |
331 |
|
|
} |
332 |
|
|
} else { |
333 |
|
|
my ($u_r_i, $pfx) = ($uri); |
334 |
wakaba |
1.4 |
$u_r_i =~ s/[^0-9A-Za-z._-]+/ /g; |
335 |
|
|
my @u_r_i = split / /, $u_r_i; |
336 |
|
|
for (reverse @u_r_i) { |
337 |
|
|
if (s/([A-Za-z][0-9A-Za-z._-]+)//) { |
338 |
|
|
my $p_f_x = $1 . ':'; |
339 |
|
|
next if lc (substr ($p_f_x, 0, 3)) eq 'xml'; |
340 |
|
|
unless ($self->_prefix_to_uri ($p_f_x)) { |
341 |
|
|
$pfx = $p_f_x; |
342 |
|
|
last; |
343 |
|
|
} |
344 |
wakaba |
1.1 |
} |
345 |
|
|
} |
346 |
|
|
if ($pfx) { |
347 |
|
|
return $self->_uri_to_prefix ($uri => $pfx, %o); |
348 |
|
|
} else { |
349 |
|
|
while (1) { |
350 |
|
|
my $pfx = 'ns'.(++$self->{ns}->{-anonymous}).':'; |
351 |
|
|
unless ($self->_prefix_to_uri ($pfx)) { |
352 |
|
|
return $self->_uri_to_prefix ($uri => $pfx, %o); |
353 |
|
|
} |
354 |
|
|
} |
355 |
|
|
} |
356 |
|
|
} |
357 |
|
|
} |
358 |
|
|
|
359 |
wakaba |
1.4 |
sub _set_prefix_to_uri ($$$;%) { |
360 |
|
|
my ($self, $prefix => $uri, %o) = @_; |
361 |
|
|
return undef unless $self->_check_namespace_prefix ($prefix); |
362 |
|
|
$self->{ns}->{$prefix} = $uri; |
363 |
|
|
$self->_prefix_to_uri ($prefix); |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
## TODO: removing ns declare (1.1) support |
367 |
|
|
# $uri or undef = $x->_prefix_to_uri ($prefix) |
368 |
wakaba |
1.1 |
sub _prefix_to_uri ($$;$%) { |
369 |
wakaba |
1.4 |
my ($self, $prefix, %o) = @_; |
370 |
wakaba |
1.1 |
return undef unless $self->_check_namespace_prefix ($prefix); |
371 |
|
|
if (uc (substr $prefix, 0, 3) eq 'XML') { |
372 |
wakaba |
1.8 |
return $NS{xml} if $prefix eq 'xml:'; |
373 |
|
|
return $NS{xmlns} if $prefix eq 'xmlns:'; |
374 |
wakaba |
1.2 |
} |
375 |
|
|
if (defined $self->{ns}->{$prefix}) { |
376 |
wakaba |
1.1 |
$self->{ns}->{$prefix}; |
377 |
|
|
} elsif (ref $self->{parent}) { |
378 |
|
|
shift; # $self |
379 |
|
|
$self->{parent}->_prefix_to_uri (@_); |
380 |
|
|
} else { |
381 |
|
|
undef; |
382 |
|
|
} |
383 |
|
|
} |
384 |
|
|
|
385 |
|
|
# $prefix or undef = $x->_uri_to_prefix ($uri => [$new_prefix], %options) |
386 |
|
|
# use_no_prefix (default: 1): Allow default namespace (no prefix). |
387 |
|
|
sub _uri_to_prefix ($$;$%) { |
388 |
|
|
my ($self, $uri, $new_prefix, %o) = @_; |
389 |
|
|
if (defined $new_prefix && $self->_check_namespace_prefix ($new_prefix)) { |
390 |
|
|
$self->{ns}->{$new_prefix} = $uri; |
391 |
|
|
$new_prefix; |
392 |
|
|
} else { |
393 |
wakaba |
1.8 |
return 'xml:' if $uri eq $NS{xml}; |
394 |
|
|
return 'xmlns:' if $uri eq $NS{xmlns}; |
395 |
wakaba |
1.1 |
for (keys %{$self->{ns}||{}}) { |
396 |
|
|
next if ($_ eq '') && !(!defined $o{use_no_prefix} || $o{use_no_prefix}); |
397 |
|
|
return $_ if $self->{ns}->{$_} eq $uri; |
398 |
|
|
} |
399 |
wakaba |
1.2 |
if (ref ($self->{parent}) && $self->{parent}->{type} ne '#declaration') { |
400 |
wakaba |
1.1 |
shift; # $self |
401 |
|
|
$self->{parent}->_uri_to_prefix (@_); |
402 |
|
|
} else { |
403 |
|
|
undef; |
404 |
|
|
} |
405 |
|
|
} |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
=item $x->define_new_namespace ($prefix => $uri) |
409 |
|
|
|
410 |
|
|
Defines a new XML Namespace. This method is useful for root or section-level |
411 |
|
|
element node. |
412 |
|
|
|
413 |
|
|
Returned value is unspecified in this version of this module. |
414 |
|
|
|
415 |
|
|
=cut |
416 |
|
|
|
417 |
wakaba |
1.4 |
## TODO: structured URI (such as http://&server;/) support |
418 |
wakaba |
1.1 |
sub define_new_namespace ($$$) { |
419 |
|
|
my ($self, $prefix, $uri) = @_; |
420 |
|
|
if ($prefix eq '' || $self->_check_ncname ($prefix)) { |
421 |
|
|
$prefix .= ':' if $prefix && substr ($prefix, -1) ne ':'; |
422 |
wakaba |
1.4 |
$self->_set_prefix_to_uri ($prefix => $uri); |
423 |
wakaba |
1.1 |
} else { |
424 |
|
|
undef; |
425 |
|
|
} |
426 |
|
|
} |
427 |
|
|
|
428 |
wakaba |
1.4 |
=item $uri = $x->defined_namespace_prefix ($prefix) |
429 |
|
|
|
430 |
|
|
Query whether the namespace prefix is defined or not. |
431 |
|
|
If defined, return namespace name (URI). |
432 |
|
|
|
433 |
|
|
=cut |
434 |
|
|
|
435 |
|
|
sub defined_namespace_prefix ($$) { |
436 |
|
|
my ($self, $prefix) = @_; |
437 |
wakaba |
1.10 |
$prefix .= ':' if $prefix; |
438 |
wakaba |
1.4 |
$self->_prefix_to_uri ($prefix); |
439 |
|
|
} |
440 |
|
|
|
441 |
wakaba |
1.1 |
=item $qname = $x->qname |
442 |
|
|
|
443 |
|
|
Returns QName ((namespace-)qualified name) of the element type. |
444 |
|
|
Undef is retuened when the type does not have its QName |
445 |
|
|
(ie. when type is neither C<#element> or C<#attribute>). |
446 |
|
|
|
447 |
|
|
=cut |
448 |
|
|
|
449 |
|
|
sub qname ($) { |
450 |
|
|
my $self = shift; |
451 |
|
|
if ($self->_check_ncname ($self->{local_name})) { |
452 |
|
|
if ($self->{type} eq '#element') { |
453 |
|
|
$self->{_qname} = $self->_get_namespace_prefix ($self->{namespace_uri}) . $self->{local_name} |
454 |
|
|
unless $self->{_qname}; |
455 |
|
|
return $self->{_qname}; |
456 |
|
|
} elsif ($self->{type} eq '#attribute') { |
457 |
|
|
return $self->attribute_name; |
458 |
|
|
} |
459 |
|
|
} |
460 |
|
|
undef; |
461 |
|
|
} |
462 |
|
|
|
463 |
wakaba |
1.9 |
sub merge_external_subset ($) { |
464 |
|
|
my $self = shift; |
465 |
|
|
unless ($self->{type} eq '#declaration' && $self->{namespace_uri} eq $NS{SGML}.'doctype') { |
466 |
|
|
return unless $self->{type} eq '#document' || $self->{type} eq '#fragment'; |
467 |
|
|
for (@{$self->{node}}) { |
468 |
|
|
$_->merge_external_subset; |
469 |
|
|
} |
470 |
|
|
return; |
471 |
|
|
} |
472 |
|
|
my $xsub = $self->get_attribute ('external-subset'); |
473 |
|
|
return unless ref $xsub; |
474 |
|
|
for (@{$xsub->{node}}) { |
475 |
|
|
$_->{parent} = $self; |
476 |
|
|
} |
477 |
|
|
push @{$self->{node}}, @{$xsub->{node}}; |
478 |
|
|
$self->remove_child_node ($xsub); |
479 |
|
|
$self->remove_child_node ($self->get_attribute ('PUBLIC')); |
480 |
|
|
$self->remove_child_node ($self->get_attribute ('SYSTEM')); |
481 |
|
|
$self->remove_marked_section; |
482 |
|
|
} |
483 |
|
|
|
484 |
|
|
sub remove_marked_section ($) { |
485 |
|
|
my $self = shift; |
486 |
|
|
my @node; |
487 |
|
|
for (@{$self->{node}}) { |
488 |
|
|
$_->remove_marked_section; |
489 |
|
|
} |
490 |
|
|
for (@{$self->{node}}) { |
491 |
|
|
if ($_->{type} ne '#section') { |
492 |
|
|
push @node, $_; |
493 |
|
|
} else { |
494 |
|
|
my $status = $_->get_attribute ('status', make_new_node => 1)->inner_text; |
495 |
|
|
if ($status eq 'CDATA') { |
496 |
|
|
$_->{type} = '#text'; |
497 |
|
|
$_->remove_child_node ($_->get_attribute ('status')); |
498 |
|
|
push @node, $_; |
499 |
|
|
} elsif ($status ne 'IGNORE') { # INCLUDE |
500 |
|
|
for my $e (@{$_->{node}}) { |
501 |
|
|
if ($e->{type} ne '#attribute') { |
502 |
|
|
$e->{parent} = $self; |
503 |
|
|
push @node, $e; |
504 |
|
|
} |
505 |
|
|
} |
506 |
|
|
} |
507 |
|
|
} |
508 |
|
|
} |
509 |
|
|
$self->{node} = \@node; |
510 |
|
|
} |
511 |
|
|
|
512 |
wakaba |
1.7 |
sub remove_references ($) { |
513 |
|
|
my $self = shift; |
514 |
|
|
my @node; |
515 |
wakaba |
1.8 |
for (@{$self->{node}}) { |
516 |
|
|
$_->remove_references; |
517 |
|
|
} |
518 |
wakaba |
1.7 |
for (@{$self->{node}}) { |
519 |
|
|
if ($_->{type} ne '#reference' |
520 |
|
|
|| ($self->{type} eq '#declaration' && $_->{namespace_uri} eq $NS{SGML}.'entity')) { |
521 |
|
|
push @node, $_; |
522 |
|
|
} else { |
523 |
|
|
if ($_->{namespace_uri} =~ /char/) { |
524 |
|
|
my $e = ref ($_)->new (type => '#text', value => chr $_->{value}); |
525 |
|
|
$e->{parent} = $self; |
526 |
|
|
push @node, $e; |
527 |
|
|
} elsif ($_->{flag}->{smxp__ref_expanded}) { |
528 |
|
|
for my $e (@{$_->{node}}) { |
529 |
wakaba |
1.8 |
if ($e->{type} ne '#attribute') { |
530 |
|
|
$e->{parent} = $self; |
531 |
|
|
push @node, $e; |
532 |
|
|
} |
533 |
wakaba |
1.7 |
} |
534 |
|
|
} else { ## reference is not expanded |
535 |
|
|
push @node, $_; |
536 |
|
|
} |
537 |
|
|
} |
538 |
wakaba |
1.8 |
$_->{flag}->{smxp__defined_with_param_ref} = 0 |
539 |
w |
1.13 |
if $_->{flag}->{smxp__defined_with_param_ref} |
540 |
|
|
&& !$_->{flag}->{smxp__non_processed_declaration}; |
541 |
wakaba |
1.7 |
} |
542 |
|
|
$self->{node} = \@node; |
543 |
|
|
} |
544 |
|
|
|
545 |
wakaba |
1.8 |
sub resolve_relative_uri ($;$%) { |
546 |
|
|
require URI; |
547 |
|
|
my ($self, $rel, %o) = @_; |
548 |
|
|
my $base = $self->get_attribute ('base', namespace_uri => $NS{xml}); |
549 |
|
|
$base = ref ($base) ? $base->inner_text : undef; |
550 |
|
|
if ($base !~ /^(?:[0-9A-Za-z.+-]|%[0-9A-Fa-f]{2})+:/) { # $base is relative |
551 |
|
|
$base = $self->_resolve_relative_uri_by_parent ($base, \%o); |
552 |
|
|
} |
553 |
|
|
eval q{ ## Catch error such as $base is 'data:,foo' (non hierarchic scheme,...) |
554 |
|
|
return URI->new ($rel)->abs ($base || '.'); ## BUG (or spec) of URI: $base == false |
555 |
|
|
} or return $rel; |
556 |
|
|
} |
557 |
|
|
sub _resolve_relative_uri_by_parent ($$$) { |
558 |
|
|
my ($self, $rel, $o) = @_; |
559 |
|
|
if (ref $self->{parent}) { |
560 |
|
|
if (!$o->{use_references_base_uri} && $self->{parent}->{type} eq '#reference') { |
561 |
|
|
## This case is necessary to work with |
562 |
|
|
## <element> <!-- element can have base URI --> |
563 |
|
|
## text <!-- text cannot have base URI --> |
564 |
|
|
## &ent; <!-- ref's base URI is referred entity's one (in this module) --> |
565 |
|
|
## <!-- expantion of ent --> |
566 |
|
|
## entity's text <!-- text cannot have base URI, so use <element>'s one --> |
567 |
|
|
## <entitys-element/> <!-- element can have base URI, otherwise ENTITY's one --> |
568 |
|
|
## </element> |
569 |
|
|
return $self->{parent}->_resolve_relative_uri_by_parent ($rel, $o); |
570 |
|
|
} else { |
571 |
|
|
return $self->{parent}->resolve_relative_uri ($rel, %$o); |
572 |
|
|
} |
573 |
|
|
} else { |
574 |
|
|
return $rel; |
575 |
|
|
} |
576 |
|
|
} |
577 |
|
|
sub base_uri ($;$) { |
578 |
|
|
my ($self, $new_uri) = @_; |
579 |
|
|
my $base; |
580 |
|
|
if (defined $new_uri) { |
581 |
|
|
$base = $self->set_attribute (base => $new_uri, namespace_uri => $NS{xml}); |
582 |
|
|
} |
583 |
|
|
$base ||= $self->get_attribute ('base', namespace_uri => $NS{xml}); |
584 |
|
|
ref ($base) ? $base->inner_text : undef; |
585 |
|
|
} |
586 |
|
|
|
587 |
wakaba |
1.1 |
=item $tag = $x->start_tag |
588 |
|
|
|
589 |
wakaba |
1.4 |
Returns the start tag (or something that marks the start of something, such as '<!--' |
590 |
wakaba |
1.1 |
for C<#comment> nodes). |
591 |
|
|
|
592 |
|
|
=cut |
593 |
|
|
|
594 |
|
|
sub start_tag ($) { |
595 |
|
|
my $self = shift; |
596 |
|
|
if ($self->{type} eq '#element' && $self->_check_ncname ($self->{local_name})) { |
597 |
|
|
my $r = '<'; |
598 |
|
|
$r .= $self->qname; |
599 |
|
|
for (@{$self->{node}}) { |
600 |
|
|
$r .= ' ' . $_->outer_xml if $_->node_type eq '#attribute'; |
601 |
|
|
} |
602 |
|
|
for my $prefix (grep !/^-/, keys %{$self->{ns}||{}}) { |
603 |
|
|
if ($prefix) { |
604 |
|
|
$r .= ' xmlns:'.substr ($prefix, 0, length ($prefix)-1); |
605 |
|
|
} else { |
606 |
|
|
$r .= ' xmlns'; |
607 |
|
|
} |
608 |
|
|
$r .= '="'.$self->_entitize ($self->{ns}->{$prefix}).'"'; |
609 |
|
|
} |
610 |
|
|
$r .= '>'; |
611 |
|
|
$r; |
612 |
|
|
} elsif ($self->{type} eq '#comment') { |
613 |
wakaba |
1.4 |
'<!--'; |
614 |
|
|
} elsif ($self->{type} eq '#pi' && $self->_check_ncname ($self->{local_name})) { |
615 |
|
|
'<?' . ($self->{local_name}); |
616 |
wakaba |
1.2 |
} elsif ($self->{type} eq '#reference') { |
617 |
wakaba |
1.10 |
if ($self->{namespace_uri} eq $NS{SGML}.'char:ref:hex') { |
618 |
wakaba |
1.2 |
'&#x'; |
619 |
wakaba |
1.10 |
} elsif ($self->{namespace_uri} eq $NS{SGML}.'char:ref') { |
620 |
wakaba |
1.2 |
'&#'; |
621 |
wakaba |
1.4 |
} elsif ($self->_check_ncname ($self->{local_name})) { |
622 |
wakaba |
1.10 |
if ($self->{namespace_uri} eq $NS{SGML}.'entity:parameter') { |
623 |
wakaba |
1.2 |
'%'; |
624 |
|
|
} else { |
625 |
|
|
'&'; |
626 |
|
|
} |
627 |
|
|
} else { # error |
628 |
|
|
''; |
629 |
|
|
} |
630 |
wakaba |
1.4 |
} elsif ($self->{type} eq '#declaration' && $self->{namespace_uri}) { |
631 |
|
|
'<!' . { |
632 |
wakaba |
1.8 |
$NS{SGML}.'attlist' => 'ATTLIST', |
633 |
|
|
$NS{SGML}.'doctype' => 'DOCTYPE', |
634 |
|
|
$NS{SGML}.'element' => 'ELEMENT', |
635 |
|
|
$NS{SGML}.'entity' => 'ENTITY', |
636 |
|
|
$NS{SGML}.'entity:parameter' => 'ENTITY', |
637 |
|
|
$NS{SGML}.'notation' => 'NOTATION', |
638 |
wakaba |
1.10 |
}->{$self->{namespace_uri}} . ' ' . |
639 |
|
|
($self->{namespace_uri} eq $NS{SGML}.'entity:parameter' ? |
640 |
wakaba |
1.7 |
($self->{flag}->{smxp__defined_with_param_ref}?'':'% '):''); |
641 |
wakaba |
1.2 |
} elsif ($self->{type} eq '#section') { |
642 |
wakaba |
1.9 |
'<!['; |
643 |
wakaba |
1.1 |
} else { |
644 |
|
|
''; |
645 |
|
|
} |
646 |
|
|
} |
647 |
|
|
|
648 |
|
|
=item $tag = $x->end_tag |
649 |
|
|
|
650 |
wakaba |
1.4 |
Returns the end tag (or something that marks the end of something, such as '-->' |
651 |
wakaba |
1.1 |
for C<#comment> nodes). |
652 |
|
|
|
653 |
|
|
=cut |
654 |
|
|
|
655 |
|
|
sub end_tag ($) { |
656 |
|
|
my $self = shift; |
657 |
|
|
if ($self->{type} eq '#element' && $self->_check_ncname ($self->{local_name})) { |
658 |
|
|
'</' . $self->qname . '>'; |
659 |
|
|
} elsif ($self->{type} eq '#comment') { |
660 |
wakaba |
1.4 |
'-->'; |
661 |
wakaba |
1.1 |
} elsif ($self->{type} eq '#pi' && $self->_check_ncname ($self->{local_name})) { |
662 |
|
|
'?>'; |
663 |
wakaba |
1.2 |
} elsif ($self->{type} eq '#reference') { |
664 |
|
|
';'; |
665 |
wakaba |
1.4 |
} elsif ($self->{type} eq '#declaration' && $self->{namespace_uri}) { |
666 |
|
|
'>'; |
667 |
wakaba |
1.1 |
} elsif ($self->{type} eq '#declaration' && $self->_check_ncname ($self->{local_name})) { |
668 |
wakaba |
1.4 |
'>'; |
669 |
wakaba |
1.2 |
} elsif ($self->{type} eq '#section') { |
670 |
wakaba |
1.9 |
']]>'; |
671 |
wakaba |
1.1 |
} else { |
672 |
|
|
''; |
673 |
|
|
} |
674 |
|
|
} |
675 |
|
|
|
676 |
|
|
=item $tag = $x->attribute_name |
677 |
|
|
|
678 |
|
|
Returns the attribute name. |
679 |
|
|
|
680 |
|
|
=cut |
681 |
|
|
|
682 |
|
|
sub attribute_name ($) { |
683 |
|
|
my $self = shift; |
684 |
|
|
if ($self->{type} eq '#attribute' && $self->_check_ncname ($self->{local_name})) { |
685 |
|
|
($self->{namespace_uri} ? |
686 |
|
|
(ref $self->{parent} ? $self->{parent} : $self) |
687 |
|
|
->_get_namespace_prefix ($self->{namespace_uri}, use_no_prefix => 0) : '') |
688 |
|
|
.$self->{local_name}; |
689 |
|
|
} else { |
690 |
|
|
''; |
691 |
|
|
} |
692 |
|
|
} |
693 |
|
|
|
694 |
|
|
=item $tag = $x->attribute_value |
695 |
|
|
|
696 |
|
|
Returns the attribute value. |
697 |
|
|
|
698 |
|
|
=cut |
699 |
|
|
|
700 |
wakaba |
1.4 |
sub attribute_value ($;%) { |
701 |
wakaba |
1.10 |
my ($self, %o) = @_; |
702 |
wakaba |
1.1 |
if ($self->{type} eq '#attribute' && $self->_check_ncname ($self->{local_name})) { |
703 |
wakaba |
1.5 |
my $r = '"'; |
704 |
wakaba |
1.10 |
my $isc = $self->_is_same_class ($self->{value}); |
705 |
w |
1.12 |
$r .= $self->_entitize ($self->{value}, keep_wsp => 1) unless $isc; |
706 |
wakaba |
1.10 |
for (($isc?$self->{value}:()), @{$self->{node}}) { |
707 |
|
|
my $nt = $_->{type}; |
708 |
wakaba |
1.4 |
if ($nt eq '#reference' || $nt eq '#xml') { |
709 |
|
|
$r .= $_->outer_xml; |
710 |
|
|
} elsif ($nt ne '#attribute') { |
711 |
w |
1.12 |
$r .= $self->_entitize ($_->inner_text, keep_wsp => 1); |
712 |
wakaba |
1.4 |
} |
713 |
|
|
} |
714 |
|
|
return $r . '"'; |
715 |
|
|
} else { |
716 |
|
|
''; |
717 |
|
|
} |
718 |
|
|
} |
719 |
|
|
|
720 |
|
|
sub entity_value ($;%) { |
721 |
wakaba |
1.10 |
my ($self, %o) = @_; |
722 |
wakaba |
1.4 |
my $_entitize = sub { |
723 |
|
|
my $s = shift; |
724 |
wakaba |
1.7 |
$s =~ s/&/&/g; |
725 |
|
|
$s =~ s/&(\p{InXML_NameStartChar}\p{InXMLNameChar}*);/&$1;/g; |
726 |
w |
1.12 |
$s =~ s/([\x0D%"])/sprintf '&#x%02X;', ord $1/ge; |
727 |
|
|
$s =~ s/([\x00-\x08\x0B\x0C\x0E-\x1F\x7F])/sprintf '&#x%02X;', ord $1/ge; |
728 |
wakaba |
1.4 |
$s; |
729 |
|
|
}; |
730 |
|
|
if ($self->{type} eq '#attribute' && $self->_check_ncname ($self->{local_name})) { |
731 |
|
|
my $r = '"' . &$_entitize ($self->{value}); |
732 |
|
|
for (@{$self->{node}}) { |
733 |
wakaba |
1.10 |
my $nt = $_->{type}; |
734 |
wakaba |
1.4 |
if ($nt eq '#reference' || $nt eq '#xml') { |
735 |
|
|
$r .= $_->outer_xml; |
736 |
|
|
} elsif ($nt ne '#attribute') { |
737 |
|
|
$r .= &$_entitize ($_->inner_text); |
738 |
wakaba |
1.1 |
} |
739 |
|
|
} |
740 |
wakaba |
1.4 |
return $r . '"'; |
741 |
wakaba |
1.1 |
} else { |
742 |
|
|
''; |
743 |
|
|
} |
744 |
|
|
} |
745 |
|
|
|
746 |
wakaba |
1.7 |
## This method should be called only from SuikaWiki::Markup::XML::* family modules, |
747 |
|
|
## since this is NOT a FORMAL interface. |
748 |
|
|
sub _entity_parameter_literal_value ($;%) { |
749 |
|
|
my $self = shift; |
750 |
wakaba |
1.10 |
my $r = ''; |
751 |
|
|
my $isc = $self->_is_same_class ($self->{value}); |
752 |
|
|
$r = $self->{value} unless $isc; |
753 |
|
|
for (($isc?$self->{value}:()), @{$self->{node}}) { |
754 |
|
|
my $nt = $_->{type}; |
755 |
w |
1.12 |
## Bare node and general entity reference node |
756 |
|
|
if ($nt eq '#xml' || ($nt eq '#reference' && $_->{namespace_uri} eq $NS{SGML}.'entity')) { |
757 |
wakaba |
1.10 |
$r .= $_->outer_xml; |
758 |
w |
1.12 |
## Text node and parameter entity reference node |
759 |
wakaba |
1.10 |
} elsif ($nt ne '#attribute') { |
760 |
|
|
$r .= $_->inner_text; |
761 |
wakaba |
1.7 |
} |
762 |
wakaba |
1.10 |
} |
763 |
|
|
$r; |
764 |
wakaba |
1.7 |
} |
765 |
|
|
|
766 |
wakaba |
1.1 |
=item $tag = $x->attribute |
767 |
|
|
|
768 |
|
|
Returns the attribute (name and value pair). |
769 |
|
|
|
770 |
|
|
=cut |
771 |
|
|
|
772 |
|
|
sub attribute ($) { |
773 |
|
|
my $self = shift; |
774 |
|
|
if ($self->{type} eq '#attribute' && $self->_check_ncname ($self->{local_name})) { |
775 |
|
|
$self->attribute_name . '=' . $self->attribute_value; |
776 |
|
|
} else { |
777 |
|
|
''; |
778 |
|
|
} |
779 |
|
|
} |
780 |
|
|
|
781 |
wakaba |
1.2 |
sub external_id ($;%) { |
782 |
|
|
my $self = shift; |
783 |
|
|
my %o = @_; |
784 |
|
|
my ($pubid, $sysid, $ndata); |
785 |
|
|
for (@{$self->{node}}) { |
786 |
wakaba |
1.4 |
if ($_->{type} eq '#attribute' && !$_->{namespace_uri}) { |
787 |
wakaba |
1.2 |
if ($_->{local_name} eq 'PUBLIC') { |
788 |
wakaba |
1.4 |
$pubid = $_->inner_text; |
789 |
wakaba |
1.2 |
} elsif ($_->{local_name} eq 'SYSTEM') { |
790 |
wakaba |
1.4 |
$sysid = $_->inner_text; |
791 |
wakaba |
1.2 |
} elsif ($_->{local_name} eq 'NDATA') { |
792 |
wakaba |
1.4 |
$ndata = $_->inner_text; |
793 |
|
|
undef $ndata unless $self->_check_ncname ($ndata); |
794 |
wakaba |
1.2 |
} |
795 |
|
|
} |
796 |
|
|
} |
797 |
|
|
my $r = ''; |
798 |
wakaba |
1.9 |
if (defined $pubid) { |
799 |
wakaba |
1.4 |
$pubid =~ s|([^\x0A\x0D\x20A-Za-z0-9'()+,./:=?;!*#\@\$_%-])|sprintf '%%%02X', ord $1|ges; |
800 |
wakaba |
1.9 |
$pubid = '"' . $pubid . '"'; |
801 |
wakaba |
1.4 |
} |
802 |
wakaba |
1.9 |
if (defined $sysid) { |
803 |
wakaba |
1.10 |
if (index ($sysid, '"') > -1) { |
804 |
|
|
if (index ($sysid, "'") > -1) { |
805 |
|
|
$sysid =~ s/"/%22/; $sysid = '"' . $sysid . '"'; |
806 |
wakaba |
1.4 |
} else { |
807 |
|
|
$sysid = "'" . $sysid . "'"; |
808 |
|
|
} |
809 |
|
|
} else { |
810 |
|
|
$sysid = '"' . $sysid . '"'; |
811 |
|
|
} |
812 |
|
|
} |
813 |
wakaba |
1.2 |
if ($pubid && $sysid) { |
814 |
wakaba |
1.9 |
$r = 'PUBLIC ' . $pubid . ' ' . $sysid; |
815 |
wakaba |
1.2 |
} elsif ($sysid) { |
816 |
wakaba |
1.4 |
$r = 'SYSTEM ' . $sysid; |
817 |
wakaba |
1.2 |
} elsif ($pubid && $o{allow_pubid_only}) { |
818 |
wakaba |
1.9 |
$r = 'PUBLIC ' . $pubid; |
819 |
wakaba |
1.2 |
} |
820 |
|
|
if ($r && $ndata && $o{use_ndata}) { |
821 |
wakaba |
1.4 |
$r .= ' NDATA ' . $ndata; |
822 |
wakaba |
1.2 |
} |
823 |
|
|
$r; |
824 |
|
|
} |
825 |
|
|
|
826 |
wakaba |
1.1 |
=item $tag = $x->inner_xml |
827 |
|
|
|
828 |
|
|
Returns the content of the node in XML syntax. (In case of the C<#element> nodes, |
829 |
|
|
element content without start- and end-tags is returned.) |
830 |
|
|
|
831 |
|
|
Note that for not all node types the behavior of this method is defined. |
832 |
|
|
For example, returned value of C<#attribute> might be unexpected one |
833 |
|
|
in this version of this module. |
834 |
|
|
|
835 |
|
|
=cut |
836 |
|
|
|
837 |
wakaba |
1.3 |
sub inner_xml ($;%) { |
838 |
wakaba |
1.10 |
my ($self, %o) = @_; |
839 |
wakaba |
1.1 |
my $r = ''; |
840 |
|
|
if ($self->{type} eq '#comment') { |
841 |
|
|
$r = $self->inner_text; |
842 |
|
|
$r =~ s/--/-E/g; |
843 |
|
|
} elsif ($self->{type} eq '#pi') { |
844 |
wakaba |
1.10 |
my $isc = $self->_is_same_class ($self->{value}); |
845 |
|
|
if (!$isc && length ($self->{value})) { |
846 |
wakaba |
1.1 |
$r = ' ' . $self->{value}; |
847 |
wakaba |
1.2 |
$r =~ s/\?>/? >/g; # Same replacement as of the recommendation of XSLT:p.i. |
848 |
wakaba |
1.1 |
} |
849 |
wakaba |
1.10 |
for (($isc?$self->{value}:()), @{$self->{node}}) { |
850 |
wakaba |
1.1 |
if ($_->node_type eq '#attribute') { |
851 |
|
|
$r .= ' ' . $_->attribute; |
852 |
|
|
} else { |
853 |
|
|
my $s = $_->inner_text; |
854 |
wakaba |
1.10 |
if (length $s) { |
855 |
|
|
$s =~ s/\?>/? >/g; |
856 |
|
|
$r .= ' ' . $s; |
857 |
|
|
} |
858 |
wakaba |
1.1 |
} |
859 |
|
|
} |
860 |
wakaba |
1.2 |
} elsif ($self->{type} eq '#reference') { |
861 |
wakaba |
1.10 |
if ($self->{namespace_uri} eq $NS{SGML}.'char:ref:hex') { |
862 |
wakaba |
1.2 |
$r = sprintf '%02X', $self->{value}; |
863 |
wakaba |
1.10 |
} elsif ($self->{namespace_uri} eq $NS{SGML}.'char:ref') { |
864 |
wakaba |
1.2 |
$r = sprintf '%02d', $self->{value}; |
865 |
wakaba |
1.10 |
} elsif (ref ($self->{local_name}) && $self->{local_name}->{type} eq '#declaration') { |
866 |
wakaba |
1.4 |
$r = $self->{local_name}->{local_name}; |
867 |
|
|
} elsif ($self->_check_ncname ($self->{local_name})) { |
868 |
|
|
$r = ($self->{local_name}); |
869 |
wakaba |
1.2 |
} else { # error |
870 |
|
|
$r = ''; |
871 |
|
|
} |
872 |
wakaba |
1.1 |
} elsif ($self->{type} eq '#declaration') { |
873 |
wakaba |
1.8 |
if ($self->{namespace_uri} eq $NS{SGML}.'doctype') { |
874 |
wakaba |
1.4 |
my $root = $self->get_attribute ('qname'); |
875 |
|
|
ref $root ? ($root = $root->inner_text) : (ref $self->{parent} ? (do { |
876 |
|
|
for (@{$self->{parent}->{node}}) { |
877 |
|
|
if ($_->{type} eq '#element') { |
878 |
|
|
$root = $_->qname; |
879 |
|
|
last if $root; |
880 |
|
|
} |
881 |
|
|
} |
882 |
|
|
$root = '#IMPLIED'; |
883 |
|
|
}) : ($root = '#IMPLIED')); ## error! |
884 |
|
|
my ($isub, $xid) = ('', $self->external_id); |
885 |
|
|
for (@{$self->{node}}) { |
886 |
|
|
$isub .= $_->outer_xml if $_->{type} ne '#attribute'; |
887 |
wakaba |
1.2 |
} |
888 |
|
|
if ($xid) { |
889 |
wakaba |
1.4 |
$r = $xid; |
890 |
|
|
if ($isub) { |
891 |
wakaba |
1.7 |
$r .= " [" . $isub . "]"; |
892 |
wakaba |
1.4 |
} |
893 |
|
|
} else { |
894 |
|
|
if ($isub) { |
895 |
wakaba |
1.7 |
$r = "[" . $isub . "]"; |
896 |
wakaba |
1.4 |
} else { |
897 |
|
|
$r = "[]"; |
898 |
|
|
} |
899 |
|
|
} |
900 |
|
|
$r = $root . ' ' . $r; |
901 |
wakaba |
1.9 |
} elsif ($self->{namespace_uri} eq $NS{SGML}.'entity' |
902 |
|
|
|| $self->{namespace_uri} eq $NS{SGML}.'entity:parameter' |
903 |
|
|
|| $self->{namespace_uri} eq $NS{SGML}.'notation') { |
904 |
wakaba |
1.4 |
my %xid_opt; |
905 |
wakaba |
1.7 |
$r = $self->{local_name} . ' ' if !$self->{flag}->{smxp__defined_with_param_ref} |
906 |
|
|
&& $self->_check_ncname ($self->{local_name}); |
907 |
wakaba |
1.8 |
if ($self->{namespace_uri} eq $NS{SGML}.'entity:parameter') { |
908 |
wakaba |
1.4 |
#$r = '% ' . $r; |
909 |
wakaba |
1.8 |
} elsif ($self->{namespace_uri} eq $NS{SGML}.'entity') { |
910 |
wakaba |
1.4 |
$xid_opt{use_ndata} = 1; |
911 |
wakaba |
1.8 |
} elsif ($self->{namespace_uri} eq $NS{SGML}.'notation') { |
912 |
wakaba |
1.4 |
$xid_opt{allow_pubid_only} = 1; |
913 |
|
|
} |
914 |
|
|
|
915 |
wakaba |
1.10 |
my ($v, $xid) = ($self->{value}); |
916 |
|
|
$xid = $self->external_id (%xid_opt) unless $self->{flag}->{smxp__defined_with_param_ref}; |
917 |
wakaba |
1.4 |
if ($xid) { ## External ID |
918 |
wakaba |
1.2 |
$r .= $xid; |
919 |
wakaba |
1.4 |
} else { ## EntityValue |
920 |
|
|
my $entity_value = $self->get_attribute ('value'); |
921 |
wakaba |
1.7 |
undef $entity_value if $self->{flag}->{smxp__defined_with_param_ref}; |
922 |
wakaba |
1.4 |
if ($entity_value) { # <!ENTITY foo "bar"> |
923 |
|
|
$r .= $entity_value->entity_value; |
924 |
|
|
} else { ## Parameter entity reference |
925 |
wakaba |
1.9 |
my $isc = $self->_is_same_class ($self->{value}); |
926 |
|
|
$r .= $self->{value} unless $isc; |
927 |
|
|
for (($isc?$self->{value}:()), @{$self->{node}}) { |
928 |
wakaba |
1.7 |
$r .= $_->outer_xml unless $_->{type} eq '#attribute'; |
929 |
wakaba |
1.4 |
} |
930 |
wakaba |
1.2 |
} |
931 |
wakaba |
1.1 |
} |
932 |
wakaba |
1.8 |
} elsif ($self->{namespace_uri} eq $NS{SGML}.'element') { |
933 |
w |
1.12 |
if (!$self->{flag}->{smxp__defined_with_param_ref}) { |
934 |
|
|
$r = $self->get_attribute ('qname'); |
935 |
|
|
$r = $r->inner_text if $r; |
936 |
|
|
unless ($self->_check_ncname ($r)) { |
937 |
|
|
$r = undef; |
938 |
|
|
} else { |
939 |
|
|
$r .= ' '; |
940 |
|
|
} |
941 |
|
|
|
942 |
|
|
my $cmodel = $self->get_attribute ('content', make_new_node => 1)->inner_text; |
943 |
|
|
if ($cmodel && $cmodel ne 'mixed') { |
944 |
|
|
$r .= $cmodel; |
945 |
|
|
} else { |
946 |
|
|
my $make_cmodel; |
947 |
|
|
$make_cmodel = sub { |
948 |
|
|
my $c = shift; |
949 |
|
|
my @tt; |
950 |
|
|
for (@{$c->child_nodes}) { |
951 |
|
|
if ($_->node_type eq '#element' && $_->namespace_uri eq $NS{SGML}.'element') { |
952 |
|
|
if ($_->local_name eq 'group') { |
953 |
|
|
my $tt = &$make_cmodel ($_); |
954 |
|
|
push @tt, '(' . $tt . ')' |
955 |
|
|
. ($_->get_attribute ('occurence', make_new_node => 1)->inner_text) |
956 |
|
|
if $tt; |
957 |
|
|
} elsif ($_->local_name eq 'element') { |
958 |
w |
1.14 |
push @tt, $_->get_attribute ('qname', make_new_node => 1)->inner_text |
959 |
|
|
. ($_->get_attribute ('occurence', make_new_node => 1)->inner_text); |
960 |
w |
1.12 |
} |
961 |
|
|
} |
962 |
|
|
} |
963 |
|
|
return join scalar ($c->get_attribute ('connector', make_new_node => 1)->inner_text |
964 |
|
|
|| '|'), |
965 |
|
|
grep {$_} @tt; |
966 |
|
|
}; |
967 |
|
|
my $tt; |
968 |
|
|
my $grp_node; |
969 |
|
|
for (@{$self->child_nodes}) { |
970 |
|
|
if ($_->node_type eq '#element' && $_->namespace_uri eq $NS{SGML}.'element' |
971 |
|
|
&& $_->local_name eq 'group') { |
972 |
|
|
$grp_node = $_; |
973 |
|
|
$tt = &$make_cmodel ($grp_node); |
974 |
|
|
last; |
975 |
|
|
} |
976 |
|
|
} |
977 |
|
|
if ($cmodel eq 'mixed') { ## mixed content |
978 |
|
|
if ($tt) { |
979 |
|
|
$r .= '(#PCDATA|' . $tt . ')*'; |
980 |
|
|
} else { |
981 |
|
|
$r .= '(#PCDATA)' |
982 |
w |
1.14 |
. ($grp_node->get_attribute ('occurence', make_new_node => 1)->inner_text eq '*' |
983 |
w |
1.12 |
? '*' : ''); |
984 |
|
|
} |
985 |
|
|
} else { ## element content |
986 |
|
|
if ($tt) { |
987 |
|
|
$r .= '(' . $tt . ')' |
988 |
|
|
. ($grp_node->get_attribute ('occurence', make_new_node => 1)->inner_text); |
989 |
|
|
} else { |
990 |
|
|
$r .= 'EMPTY'; |
991 |
|
|
} |
992 |
|
|
} # mixed or element content |
993 |
|
|
} # content model group |
994 |
|
|
} else { ## Save source doc's description as far as possible |
995 |
|
|
my $isc = $self->_is_same_class ($self->{value}); |
996 |
|
|
$r .= $self->{value} unless $isc; |
997 |
|
|
for (($isc?$self->{value}:()), @{$self->{node}}) { |
998 |
|
|
unless ($_->{type} eq '#attribute' || $_->{type} eq '#element') { |
999 |
|
|
$r .= $_->outer_xml; |
1000 |
|
|
} elsif ($_->{type} eq '#element' && $_->{namespace_uri} eq $NS{SGML}.'group') { |
1001 |
|
|
$r .= $_->outer_xml; |
1002 |
|
|
} |
1003 |
|
|
} |
1004 |
wakaba |
1.2 |
} |
1005 |
wakaba |
1.11 |
} elsif ($self->{namespace_uri} eq $NS{SGML}.'attlist') { |
1006 |
w |
1.12 |
if (!$self->{flag}->{smxp__defined_with_param_ref}) { |
1007 |
|
|
$r = $self->get_attribute ('qname'); |
1008 |
|
|
$r = $r->inner_text if $r; |
1009 |
|
|
unless ($self->_check_ncname ($r)) { |
1010 |
|
|
$r = undef; |
1011 |
|
|
} else { |
1012 |
|
|
$r .= ' '; |
1013 |
|
|
} |
1014 |
|
|
for (@{$self->{node}}) { |
1015 |
|
|
if ($_->{type} eq '#element' && $_->{namespace_uri} eq $NS{XML}.'attlist' |
1016 |
|
|
&& $_->{local_name} eq 'AttDef') { |
1017 |
|
|
$r .= "\n\t" . $_->get_attribute ('qname', make_new_node => 1)->inner_text; |
1018 |
|
|
my $attr_type = $_->get_attribute ('type', make_new_node => 1)->inner_text; |
1019 |
|
|
if ($attr_type ne 'enum') { |
1020 |
|
|
$r .= "\t" . $attr_type; |
1021 |
|
|
} |
1022 |
|
|
if ($attr_type eq 'enum' || $attr_type eq 'NOTATION') { |
1023 |
|
|
my @l; |
1024 |
|
|
for my $item (@{$_->{node}}) { |
1025 |
|
|
if ($item->{type} eq '#element' && $item->{namespace_uri} eq $NS{XML}.'attlist' |
1026 |
|
|
&& $item->{local_name} eq 'enum') { |
1027 |
|
|
push @l, $item->inner_text; |
1028 |
|
|
} |
1029 |
|
|
} |
1030 |
|
|
$r .= "\t(" . join ('|', @l) . ')'; |
1031 |
|
|
} |
1032 |
|
|
## DefaultDecl |
1033 |
|
|
my $deftype = $_->get_attribute ('default_type', make_new_node => 1)->inner_text; |
1034 |
|
|
if ($deftype) { |
1035 |
|
|
$r .= "\t#" . $deftype; |
1036 |
|
|
} |
1037 |
|
|
if (!$deftype || $deftype eq 'FIXED') { |
1038 |
|
|
$r .= "\t" . $_->get_attribute ('default_value', make_new_node => 1) |
1039 |
|
|
->attribute_value; |
1040 |
|
|
} |
1041 |
|
|
} # AttDef |
1042 |
wakaba |
1.2 |
} |
1043 |
w |
1.12 |
} else { ## Save source doc's description as far as possible |
1044 |
|
|
my $isc = $self->_is_same_class ($self->{value}); |
1045 |
|
|
$r .= $self->{value} unless $isc; |
1046 |
|
|
for (($isc?$self->{value}:()), @{$self->{node}}) { |
1047 |
|
|
unless ($_->{type} eq '#attribute' || $_->{type} eq '#element') { |
1048 |
|
|
$r .= $_->outer_xml; |
1049 |
|
|
} elsif ($_->{type} eq '#element' && $_->{namespace_uri} eq $NS{SGML}.'group') { |
1050 |
|
|
$r .= $_->outer_xml; |
1051 |
|
|
} |
1052 |
|
|
} |
1053 |
wakaba |
1.1 |
} |
1054 |
wakaba |
1.2 |
} else { # unknown |
1055 |
wakaba |
1.3 |
for (@{$self->{node}}) { |
1056 |
|
|
$r .= $_->outer_xml; |
1057 |
|
|
} |
1058 |
wakaba |
1.1 |
} |
1059 |
wakaba |
1.9 |
} elsif ($self->{type} eq '#section') { |
1060 |
|
|
my $status = $self->get_attribute ('status', make_new_node => 1)->inner_text; |
1061 |
|
|
if ($status eq 'CDATA') { |
1062 |
|
|
$r = $self->inner_text; |
1063 |
|
|
$r =~ s/]]>/]]>]]<![CDATA[>/g; |
1064 |
|
|
$r = 'CDATA['.$r; |
1065 |
|
|
} else { |
1066 |
|
|
my $sl = $self->get_attribute ('status_list', make_new_node => 1); |
1067 |
|
|
if ($sl->{flag}->{smxp__defined_with_param_ref}) { |
1068 |
|
|
my $isc = $self->_is_same_class ($self->{value}); |
1069 |
|
|
$status = ''; |
1070 |
|
|
$status = $sl->{value} unless $isc; |
1071 |
|
|
for (($isc?$sl->{value}:()), @{$sl->{node}}) { |
1072 |
|
|
$status .= $_->outer_xml unless $_->{type} eq '#attribute'; |
1073 |
|
|
} |
1074 |
|
|
$r = $status.'['.$r; |
1075 |
|
|
} elsif ($status) { |
1076 |
|
|
$r = $status.'['.$r; |
1077 |
|
|
} else { |
1078 |
|
|
## Must be an ignored section |
1079 |
|
|
} |
1080 |
|
|
my $isc = $self->_is_same_class ($self->{value}); |
1081 |
|
|
$r .= join '', map {s/\]\]>/]]>/g; $_} $self->{value} unless $isc; |
1082 |
|
|
for (($isc?$self->{value}:()), @{$self->{node}}) { |
1083 |
|
|
if ($_->{type} eq '#text') { |
1084 |
|
|
$r .= join '', map {s/\]\]>/]]>/g; $_} $_->inner_text; ## Anyway, this is error |
1085 |
|
|
} elsif ($_->{type} ne '#attribute') { |
1086 |
|
|
$r .= $_->outer_xml; |
1087 |
|
|
} |
1088 |
|
|
} |
1089 |
|
|
} |
1090 |
wakaba |
1.1 |
} else { |
1091 |
wakaba |
1.10 |
my $isc = $self->_is_same_class ($self->{value}); |
1092 |
|
|
unless ($isc) { |
1093 |
|
|
if ($self->{type} ne '#xml') { |
1094 |
|
|
$r = $self->_entitize ($self->{value}); |
1095 |
|
|
} else { |
1096 |
|
|
$r = $self->{value}; |
1097 |
|
|
} |
1098 |
wakaba |
1.1 |
} |
1099 |
wakaba |
1.10 |
for (($isc?$self->{value}:()), @{$self->{node}}) { |
1100 |
|
|
my $nt = $_->{type}; |
1101 |
|
|
if (($self->{option}->{indent}) |
1102 |
|
|
&& ($nt eq '#element' || $nt eq '#comment' || $nt eq '#pi' || $nt eq '#declaration')) { |
1103 |
wakaba |
1.1 |
$r .= "\n"; |
1104 |
|
|
} |
1105 |
|
|
$r .= $_->outer_xml unless $_->node_type eq '#attribute'; |
1106 |
|
|
} |
1107 |
|
|
} |
1108 |
|
|
$r; |
1109 |
|
|
} |
1110 |
|
|
|
1111 |
|
|
|
1112 |
|
|
=item $tag = $x->outer_xml |
1113 |
|
|
|
1114 |
|
|
Returns the node in XML syntax. |
1115 |
|
|
|
1116 |
|
|
=cut |
1117 |
|
|
|
1118 |
|
|
sub outer_xml ($) { |
1119 |
|
|
my $self = shift; |
1120 |
|
|
if ($self->{type} eq '#attribute') { |
1121 |
wakaba |
1.10 |
return $self->attribute; |
1122 |
wakaba |
1.1 |
} else { |
1123 |
wakaba |
1.4 |
if ($self->{option}->{indent} && $self->{type} eq '#element') { |
1124 |
wakaba |
1.1 |
my $r = $self->start_tag; |
1125 |
|
|
my $c = $self->inner_xml; |
1126 |
wakaba |
1.10 |
if (!length ($c) && $self->{option}->{use_EmptyElemTag}) { |
1127 |
wakaba |
1.4 |
substr ($r, -1) = ' />'; |
1128 |
|
|
} else { |
1129 |
|
|
if ($c) { |
1130 |
|
|
$c =~ s/\n/\n /g; |
1131 |
|
|
$r .= "\n " . $c . "\n"; |
1132 |
|
|
} |
1133 |
|
|
$r .= $self->end_tag; |
1134 |
wakaba |
1.1 |
} |
1135 |
wakaba |
1.10 |
return $r; |
1136 |
wakaba |
1.1 |
} else { |
1137 |
wakaba |
1.4 |
my $r = $self->start_tag; |
1138 |
|
|
my $c = $self->inner_xml; |
1139 |
wakaba |
1.8 |
if ($self->{type} eq '#element' && !length ($c) && $self->{option}->{use_EmptyElemTag}) { |
1140 |
wakaba |
1.4 |
substr ($r, -1) = ' />'; |
1141 |
|
|
} else { |
1142 |
|
|
$r .= $c . $self->end_tag; |
1143 |
|
|
} |
1144 |
wakaba |
1.10 |
return $r; |
1145 |
wakaba |
1.11 |
#return '{'.$self->{type}.': '.$r.'}'; ## DEBUG: show structure |
1146 |
wakaba |
1.1 |
} |
1147 |
|
|
} |
1148 |
|
|
} |
1149 |
|
|
|
1150 |
|
|
=item $tag = $x->inner_text |
1151 |
|
|
|
1152 |
|
|
Returns the text content of the node. (In many case the returned value is same |
1153 |
|
|
as WinIE DOM C<inner_text ()> function's or XPath C<text()> function's. |
1154 |
|
|
But some classes that inherits this module might implement to return other |
1155 |
|
|
value (eg. to return the value of the alt attribute of html:img element). |
1156 |
|
|
|
1157 |
wakaba |
1.2 |
Available options: C<output_ref_as_is>. |
1158 |
|
|
|
1159 |
wakaba |
1.1 |
=cut |
1160 |
|
|
|
1161 |
wakaba |
1.2 |
sub inner_text ($;%) { |
1162 |
wakaba |
1.1 |
my $self = shift; |
1163 |
wakaba |
1.2 |
my %o = @_; |
1164 |
wakaba |
1.5 |
my $r = ''; |
1165 |
wakaba |
1.10 |
if ($self->{type} eq '#reference' |
1166 |
|
|
&& ($self->{namespace_uri} eq $NS{SGML}.'char:ref' |
1167 |
|
|
|| $self->{namespace_uri} eq $NS{SGML}.'char:ref:hex')) { |
1168 |
|
|
$r = chr $self->{value}; |
1169 |
wakaba |
1.7 |
} elsif ($self->{type} eq '#declaration' |
1170 |
|
|
&& ($self->{namespace_uri} eq $NS{SGML}.'entity' |
1171 |
|
|
|| $self->{namespace_uri} eq $NS{SGML}.'entity:parameter')) { |
1172 |
wakaba |
1.10 |
## TODO: |
1173 |
|
|
$r = $self->set_attribute ('value')->inner_text; |
1174 |
wakaba |
1.7 |
} else { # not #reference nor #declaration(ENTITY) |
1175 |
wakaba |
1.10 |
my $isc = $self->_is_same_class ($self->{value}); |
1176 |
w |
1.14 |
$r = $self->{value} if !$isc && defined $self->{value}; |
1177 |
wakaba |
1.7 |
if ($o{output_ref_as_is}) { ## output as if RCDATA |
1178 |
|
|
$r =~ s/&/&/g; |
1179 |
wakaba |
1.10 |
for my $node (($isc?$self->{value}:()), @{$self->{node}}) { |
1180 |
wakaba |
1.7 |
my $nt = $node->node_type; |
1181 |
|
|
if ($nt eq '#reference' || $nt eq '#xml') { |
1182 |
|
|
$r .= $node->outer_xml; |
1183 |
|
|
} elsif ($nt ne '#attribute') { |
1184 |
|
|
$r .= map {s/&/&/g; $_} $node->inner_text; |
1185 |
|
|
} |
1186 |
|
|
} |
1187 |
|
|
} else { |
1188 |
wakaba |
1.10 |
for (($isc?$self->{value}:()), @{$self->{node}}) { |
1189 |
|
|
$r .= $_->inner_text unless $_->{type} eq '#attribute'; |
1190 |
wakaba |
1.2 |
} |
1191 |
|
|
} |
1192 |
wakaba |
1.1 |
} |
1193 |
|
|
$r; |
1194 |
|
|
} |
1195 |
|
|
|
1196 |
wakaba |
1.9 |
sub _is_same_class ($$) { |
1197 |
|
|
my ($self, $something) = @_; |
1198 |
wakaba |
1.10 |
return 0 if {qw/ARRAY 1 HASH 1 CODE 1 :nonref: 1/}->{ref ($something) || ':nonref:'}; |
1199 |
|
|
eval q{$self->_CLASS_NAME eq $something->_CLASS_NAME} ? 1 : 0; |
1200 |
wakaba |
1.9 |
} |
1201 |
|
|
|
1202 |
|
|
sub root_node ($) { |
1203 |
|
|
my $self = shift; |
1204 |
|
|
if ($self->{type} eq '#document') { |
1205 |
|
|
return $self; |
1206 |
|
|
} elsif (ref $self->{parent}) { |
1207 |
|
|
return $self->{parent}->root_node; |
1208 |
|
|
} else { |
1209 |
|
|
return $self; |
1210 |
|
|
} |
1211 |
wakaba |
1.1 |
} |
1212 |
|
|
|
1213 |
wakaba |
1.7 |
sub _get_entity_manager ($) { |
1214 |
|
|
my $self = shift; |
1215 |
|
|
if ($self->{type} eq '#document') { |
1216 |
|
|
unless ($self->{flag}->{smx__entity_manager}) { |
1217 |
|
|
require SuikaWiki::Markup::XML::EntityManager; |
1218 |
|
|
$self->{flag}->{smx__entity_manager} = SuikaWiki::Markup::XML::EntityManager->new ($self); |
1219 |
|
|
} |
1220 |
|
|
return $self->{flag}->{smx__entity_manager}; |
1221 |
|
|
} elsif (ref $self->{parent}) { |
1222 |
|
|
return $self->{parent}->_get_entity_manager; |
1223 |
|
|
} else { |
1224 |
|
|
unless ($self->{flag}->{smx__entity_manager}) { |
1225 |
|
|
require SuikaWiki::Markup::XML::EntityManager; |
1226 |
|
|
$self->{flag}->{smx__entity_manager} = SuikaWiki::Markup::XML::EntityManager->new ($self); |
1227 |
|
|
} |
1228 |
|
|
return $self->{flag}->{smx__entity_manager}; |
1229 |
|
|
} |
1230 |
|
|
} |
1231 |
|
|
|
1232 |
wakaba |
1.10 |
sub _CLASS_NAME ($) { 'SuikaWiki::Markup::XML' } |
1233 |
wakaba |
1.5 |
|
1234 |
wakaba |
1.1 |
# $s = $x->_entitize ($s) |
1235 |
wakaba |
1.4 |
sub _entitize ($$;%) { |
1236 |
|
|
my ($self, $s, %o) = (shift, shift, @_); |
1237 |
wakaba |
1.1 |
$s =~ s/&/&/g; |
1238 |
|
|
$s =~ s/</</g; |
1239 |
|
|
$s =~ s/>/>/g; |
1240 |
|
|
$s =~ s/"/"/g; |
1241 |
wakaba |
1.7 |
$s =~ s/([\x00-\x08\x0B\x0C\x0E-\x1F])/sprintf '&#%d;', ord $1/ge; |
1242 |
w |
1.12 |
$s =~ s/([\x09\x0A\x0D])/sprintf '&#%d;', ord $1/ge if $o{keep_wsp}; |
1243 |
wakaba |
1.1 |
$s; |
1244 |
|
|
} |
1245 |
|
|
|
1246 |
wakaba |
1.2 |
# 1/0 = $x->_check_name ($s) |
1247 |
|
|
sub _check_name ($$) { |
1248 |
|
|
my $self = shift; |
1249 |
|
|
my $s = shift; |
1250 |
|
|
return $Cache{name}->{$s} if defined $Cache{name}->{$s}; |
1251 |
|
|
if ($s =~ /^\p{InXML_NameStartChar}/ && $s !~ /\P{InXMLNameChar}/) { |
1252 |
|
|
# \p{...}('*'/'+'/'{n,}') does not work... |
1253 |
|
|
$Cache{name}->{$s} = 1; |
1254 |
|
|
1; |
1255 |
|
|
} else { |
1256 |
|
|
$Cache{name}->{$s} = 0; |
1257 |
|
|
0; |
1258 |
|
|
} |
1259 |
|
|
} |
1260 |
wakaba |
1.1 |
# 1/0 = $x->_check_ncname ($s) |
1261 |
|
|
sub _check_ncname ($$) { |
1262 |
|
|
my $self = shift; |
1263 |
|
|
my $s = shift; |
1264 |
|
|
return $Cache{ncname}->{$s} if defined $Cache{ncname}->{$s}; |
1265 |
|
|
if ($s =~ /^\p{InXML_NCNameStartChar}/ && $s !~ /\P{InXMLNCNameChar}/) { |
1266 |
|
|
# \p{...}('*'/'+'/'{n,}') does not work... |
1267 |
|
|
$Cache{ncname}->{$s} = 1; |
1268 |
|
|
1; |
1269 |
|
|
} else { |
1270 |
|
|
$Cache{ncname}->{$s} = 0; |
1271 |
|
|
0; |
1272 |
|
|
} |
1273 |
|
|
} |
1274 |
|
|
|
1275 |
|
|
# 1/0 = $x->_check_namespace_prefix ($s) |
1276 |
|
|
sub _check_namespace_prefix ($$) { |
1277 |
|
|
my $self = shift; |
1278 |
|
|
my $s = shift; |
1279 |
|
|
return 0 unless defined $s; |
1280 |
|
|
return 1 if $s eq ''; |
1281 |
|
|
substr ($s, -1, 1) = '' if substr ($s, -1, 1) eq ':'; |
1282 |
|
|
$self->_check_ncname ($s); |
1283 |
|
|
} |
1284 |
|
|
|
1285 |
wakaba |
1.2 |
## TODO: cleaning $self->{node} before outputing, to ensure nodes not to have |
1286 |
|
|
## multiple parents. |
1287 |
|
|
## TODO: normalize namespace URI (removing non URI chars) |
1288 |
|
|
|
1289 |
|
|
sub flag ($$;$) { |
1290 |
|
|
my ($self, $name, $value) = @_; |
1291 |
|
|
if (defined $value) { |
1292 |
|
|
$self->{flag}->{$name} = $value; |
1293 |
|
|
} |
1294 |
|
|
$self->{flag}->{$name}; |
1295 |
|
|
} |
1296 |
|
|
|
1297 |
wakaba |
1.3 |
sub option ($$;$) { |
1298 |
|
|
my ($self, $name, $value) = @_; |
1299 |
|
|
if (defined $value) { |
1300 |
|
|
$self->{option}->{$name} = $value; |
1301 |
|
|
} |
1302 |
|
|
$self->{option}->{$name}; |
1303 |
|
|
} |
1304 |
|
|
|
1305 |
wakaba |
1.1 |
=back |
1306 |
|
|
|
1307 |
wakaba |
1.2 |
=head1 NODE TYPES |
1308 |
|
|
|
1309 |
|
|
=over 4 |
1310 |
|
|
|
1311 |
|
|
=item #attribute |
1312 |
|
|
|
1313 |
|
|
Attribute. Its XML representation takes the form of NAME="VALUE". |
1314 |
|
|
|
1315 |
|
|
=item #comment |
1316 |
|
|
|
1317 |
|
|
Comment declarement. <!-- --> |
1318 |
|
|
|
1319 |
|
|
=item #declarement |
1320 |
|
|
|
1321 |
|
|
SGML's declarements, such as SGML, DOCTYPE, ENTITY, etc. |
1322 |
|
|
<!SGML ...>, <!DOCTYPE root []>, <!ENTITY % name "value"> |
1323 |
|
|
|
1324 |
|
|
=item #element |
1325 |
|
|
|
1326 |
|
|
Element. Its XML representation consists of start tag, content and end tag, |
1327 |
|
|
like <TYPE>content</TYPE>. |
1328 |
|
|
|
1329 |
|
|
=item #fragment |
1330 |
|
|
|
1331 |
|
|
Fragment of nodes. It's similar to DOM's fragment node. |
1332 |
|
|
|
1333 |
|
|
=item #pi |
1334 |
|
|
|
1335 |
|
|
Prosessing instruction. <?NAME VALUE?> |
1336 |
|
|
|
1337 |
|
|
=item #reference |
1338 |
|
|
|
1339 |
|
|
Character reference or general or parameter entity reference. |
1340 |
|
|
&#nnnn;, &#xhhhh;, &name;, %name;. |
1341 |
|
|
|
1342 |
|
|
=item #section |
1343 |
|
|
|
1344 |
|
|
Markup section. CDATA, INCLUDE and IGNORE are supported by XML. |
1345 |
|
|
<![%type;[...]]> |
1346 |
|
|
|
1347 |
|
|
=item #text |
1348 |
|
|
|
1349 |
|
|
Text. |
1350 |
|
|
|
1351 |
|
|
=item #xml |
1352 |
|
|
|
1353 |
|
|
Preformatted XML text. |
1354 |
|
|
|
1355 |
|
|
=cut |
1356 |
|
|
|
1357 |
wakaba |
1.4 |
=head1 RESTRICTIONS |
1358 |
|
|
|
1359 |
|
|
=over 4 |
1360 |
|
|
|
1361 |
|
|
=item XML without XML Namespace is not supported. |
1362 |
|
|
|
1363 |
|
|
=item Before default namespace without bounded URI (xmlns="") is outputed, it must be declared. |
1364 |
|
|
|
1365 |
|
|
For example, next code generates invalid (non-well-formed) XML Namespace document. |
1366 |
|
|
|
1367 |
|
|
my $x = SuikaWiki::Markup::XML->new (local_name => 'elementType'); |
1368 |
|
|
print $x # <ns1:elementType xmlns:ns1=""></ns1:elementType> |
1369 |
|
|
|
1370 |
|
|
So you must write like: |
1371 |
|
|
|
1372 |
|
|
my $x = SuikaWiki::Markup::XML->new (local_name => 'elementType'); |
1373 |
|
|
$x->define_new_namespace ('' => ''); |
1374 |
|
|
print $x; # <elementType xmlns=""></elementType> |
1375 |
|
|
|
1376 |
|
|
=back |
1377 |
|
|
|
1378 |
wakaba |
1.1 |
=head1 LICENSE |
1379 |
|
|
|
1380 |
|
|
Copyright 2003 Wakaba <w@suika.fam.cx> |
1381 |
|
|
|
1382 |
|
|
This program is free software; you can redistribute it and/or |
1383 |
|
|
modify it under the same terms as Perl itself. |
1384 |
|
|
|
1385 |
|
|
=cut |
1386 |
|
|
|
1387 |
wakaba |
1.15 |
1; # $Date: 2003/07/16 12:10:22 $ |