/[pub]/suikawiki/script/lib/SuikaWiki/Markup/XML.pm
Suika

Contents of /suikawiki/script/lib/SuikaWiki/Markup/XML.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations) (download)
Sat Jul 12 06:12:00 2003 UTC (22 years ago) by w
Branch: MAIN
Changes since 1.11: +127 -44 lines
ELEMENT and ATTLIST declaration support reimplemented

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 w 1.12 our $VERSION = do{my @r=(q$Revision: 1.11 $=~/\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.1 =item $type = $x->node_type
259    
260     Returns the node type.
261    
262     =item $node = $x->parent_node
263    
264     Returns the parent node. If there is no parent node, undef is returned.
265    
266     =cut
267    
268 wakaba 1.10 sub child_nodes ($) { $_[0]->{node} }
269 wakaba 1.4 sub local_name ($;$) {
270     my ($self, $newname) = @_;
271 wakaba 1.10 $self->{local_name} = $newname if $newname;
272 wakaba 1.4 if (ref $self->{local_name} && $self->{local_name}->{type} eq '#declaration') {
273     $self->{local_name}->{local_name};
274     } else {
275     $self->{local_name}
276     }
277     }
278 wakaba 1.10 sub node_type ($) { $_[0]->{type} }
279     sub parent_node ($) { $_[0]->{parent} }
280 wakaba 1.1
281 wakaba 1.4 sub namespace_uri ($;$) {
282     my ($self, $new_uri) = @_;
283 wakaba 1.10 $self->{namespace_uri} = $new_uri if defined $new_uri;
284 wakaba 1.4 $self->{namespace_uri};
285     }
286 wakaba 1.5 sub namespace_prefix ($;$) {
287     my ($self, $new_pfx) = @_;
288     if (defined $new_pfx && $self->{namespace_uri}) {
289     $new_pfx .= ':' if $new_pfx;
290     $self->{namespace_prefix} = $new_pfx;
291     $self->{ns}->{$new_pfx} = $self->{namespace_uri};
292     }
293     $self->_get_namespace_prefix ($self->{namespace_uri});
294     }
295 wakaba 1.4
296 wakaba 1.1 =item $i = $x->count
297    
298     Returns the number of child nodes.
299    
300     =cut
301    
302     # TODO: support counting by type
303     sub count ($;@) {
304 wakaba 1.10 (defined $_[0]->{value} ? 1 : 0) + scalar @{$_[0]->{node}};
305 wakaba 1.1 }
306    
307     # $prefix = $x->_get_namespace_prefix ($namespace_uri)
308     sub _get_namespace_prefix ($$;%) {
309 wakaba 1.10 my ($self, $uri, %o) = @_;
310 wakaba 1.1 if (defined (my $p = $self->_uri_to_prefix ($uri, undef, %o))) {
311     return $p if $self->_prefix_to_uri ($p) eq $uri;
312     } if ($Namespace_URI_to_prefix{$uri}) {
313     for (@{$Namespace_URI_to_prefix{$uri}}) {
314     my $pfx = $_; $pfx .= ':' if $pfx;
315     if ($self->_check_namespace_prefix ($pfx) && !$self->_prefix_to_uri ($pfx)) {
316     return $self->_uri_to_prefix ($uri => $pfx, %o);
317     }
318     }
319     } else {
320     my ($u_r_i, $pfx) = ($uri);
321 wakaba 1.4 $u_r_i =~ s/[^0-9A-Za-z._-]+/ /g;
322     my @u_r_i = split / /, $u_r_i;
323     for (reverse @u_r_i) {
324     if (s/([A-Za-z][0-9A-Za-z._-]+)//) {
325     my $p_f_x = $1 . ':';
326     next if lc (substr ($p_f_x, 0, 3)) eq 'xml';
327     unless ($self->_prefix_to_uri ($p_f_x)) {
328     $pfx = $p_f_x;
329     last;
330     }
331 wakaba 1.1 }
332     }
333     if ($pfx) {
334     return $self->_uri_to_prefix ($uri => $pfx, %o);
335     } else {
336     while (1) {
337     my $pfx = 'ns'.(++$self->{ns}->{-anonymous}).':';
338     unless ($self->_prefix_to_uri ($pfx)) {
339     return $self->_uri_to_prefix ($uri => $pfx, %o);
340     }
341     }
342     }
343     }
344     }
345    
346 wakaba 1.4 sub _set_prefix_to_uri ($$$;%) {
347     my ($self, $prefix => $uri, %o) = @_;
348     return undef unless $self->_check_namespace_prefix ($prefix);
349     $self->{ns}->{$prefix} = $uri;
350     $self->_prefix_to_uri ($prefix);
351     }
352    
353     ## TODO: removing ns declare (1.1) support
354     # $uri or undef = $x->_prefix_to_uri ($prefix)
355 wakaba 1.1 sub _prefix_to_uri ($$;$%) {
356 wakaba 1.4 my ($self, $prefix, %o) = @_;
357 wakaba 1.1 return undef unless $self->_check_namespace_prefix ($prefix);
358     if (uc (substr $prefix, 0, 3) eq 'XML') {
359 wakaba 1.8 return $NS{xml} if $prefix eq 'xml:';
360     return $NS{xmlns} if $prefix eq 'xmlns:';
361 wakaba 1.2 }
362     if (defined $self->{ns}->{$prefix}) {
363 wakaba 1.1 $self->{ns}->{$prefix};
364     } elsif (ref $self->{parent}) {
365     shift; # $self
366     $self->{parent}->_prefix_to_uri (@_);
367     } else {
368     undef;
369     }
370     }
371    
372     # $prefix or undef = $x->_uri_to_prefix ($uri => [$new_prefix], %options)
373     # use_no_prefix (default: 1): Allow default namespace (no prefix).
374     sub _uri_to_prefix ($$;$%) {
375     my ($self, $uri, $new_prefix, %o) = @_;
376     if (defined $new_prefix && $self->_check_namespace_prefix ($new_prefix)) {
377     $self->{ns}->{$new_prefix} = $uri;
378     $new_prefix;
379     } else {
380 wakaba 1.8 return 'xml:' if $uri eq $NS{xml};
381     return 'xmlns:' if $uri eq $NS{xmlns};
382 wakaba 1.1 for (keys %{$self->{ns}||{}}) {
383     next if ($_ eq '') && !(!defined $o{use_no_prefix} || $o{use_no_prefix});
384     return $_ if $self->{ns}->{$_} eq $uri;
385     }
386 wakaba 1.2 if (ref ($self->{parent}) && $self->{parent}->{type} ne '#declaration') {
387 wakaba 1.1 shift; # $self
388     $self->{parent}->_uri_to_prefix (@_);
389     } else {
390     undef;
391     }
392     }
393     }
394    
395     =item $x->define_new_namespace ($prefix => $uri)
396    
397     Defines a new XML Namespace. This method is useful for root or section-level
398     element node.
399    
400     Returned value is unspecified in this version of this module.
401    
402     =cut
403    
404 wakaba 1.4 ## TODO: structured URI (such as http://&server;/) support
405 wakaba 1.1 sub define_new_namespace ($$$) {
406     my ($self, $prefix, $uri) = @_;
407     if ($prefix eq '' || $self->_check_ncname ($prefix)) {
408     $prefix .= ':' if $prefix && substr ($prefix, -1) ne ':';
409 wakaba 1.4 $self->_set_prefix_to_uri ($prefix => $uri);
410 wakaba 1.1 } else {
411     undef;
412     }
413     }
414    
415 wakaba 1.4 =item $uri = $x->defined_namespace_prefix ($prefix)
416    
417     Query whether the namespace prefix is defined or not.
418     If defined, return namespace name (URI).
419    
420     =cut
421    
422     sub defined_namespace_prefix ($$) {
423     my ($self, $prefix) = @_;
424 wakaba 1.10 $prefix .= ':' if $prefix;
425 wakaba 1.4 $self->_prefix_to_uri ($prefix);
426     }
427    
428 wakaba 1.1 =item $qname = $x->qname
429    
430     Returns QName ((namespace-)qualified name) of the element type.
431     Undef is retuened when the type does not have its QName
432     (ie. when type is neither C<#element> or C<#attribute>).
433    
434     =cut
435    
436     sub qname ($) {
437     my $self = shift;
438     if ($self->_check_ncname ($self->{local_name})) {
439     if ($self->{type} eq '#element') {
440     $self->{_qname} = $self->_get_namespace_prefix ($self->{namespace_uri}) . $self->{local_name}
441     unless $self->{_qname};
442     return $self->{_qname};
443     } elsif ($self->{type} eq '#attribute') {
444     return $self->attribute_name;
445     }
446     }
447     undef;
448     }
449    
450 wakaba 1.9 sub merge_external_subset ($) {
451     my $self = shift;
452     unless ($self->{type} eq '#declaration' && $self->{namespace_uri} eq $NS{SGML}.'doctype') {
453     return unless $self->{type} eq '#document' || $self->{type} eq '#fragment';
454     for (@{$self->{node}}) {
455     $_->merge_external_subset;
456     }
457     return;
458     }
459     my $xsub = $self->get_attribute ('external-subset');
460     return unless ref $xsub;
461     for (@{$xsub->{node}}) {
462     $_->{parent} = $self;
463     }
464     push @{$self->{node}}, @{$xsub->{node}};
465     $self->remove_child_node ($xsub);
466     $self->remove_child_node ($self->get_attribute ('PUBLIC'));
467     $self->remove_child_node ($self->get_attribute ('SYSTEM'));
468     $self->remove_marked_section;
469     }
470    
471     sub remove_marked_section ($) {
472     my $self = shift;
473     my @node;
474     for (@{$self->{node}}) {
475     $_->remove_marked_section;
476     }
477     for (@{$self->{node}}) {
478     if ($_->{type} ne '#section') {
479     push @node, $_;
480     } else {
481     my $status = $_->get_attribute ('status', make_new_node => 1)->inner_text;
482     if ($status eq 'CDATA') {
483     $_->{type} = '#text';
484     $_->remove_child_node ($_->get_attribute ('status'));
485     push @node, $_;
486     } elsif ($status ne 'IGNORE') { # INCLUDE
487     for my $e (@{$_->{node}}) {
488     if ($e->{type} ne '#attribute') {
489     $e->{parent} = $self;
490     push @node, $e;
491     }
492     }
493     }
494     }
495     }
496     $self->{node} = \@node;
497     }
498    
499 wakaba 1.7 sub remove_references ($) {
500     my $self = shift;
501     my @node;
502 wakaba 1.8 for (@{$self->{node}}) {
503     $_->remove_references;
504     }
505 wakaba 1.7 for (@{$self->{node}}) {
506     if ($_->{type} ne '#reference'
507     || ($self->{type} eq '#declaration' && $_->{namespace_uri} eq $NS{SGML}.'entity')) {
508     push @node, $_;
509     } else {
510     if ($_->{namespace_uri} =~ /char/) {
511     my $e = ref ($_)->new (type => '#text', value => chr $_->{value});
512     $e->{parent} = $self;
513     push @node, $e;
514     } elsif ($_->{flag}->{smxp__ref_expanded}) {
515     for my $e (@{$_->{node}}) {
516 wakaba 1.8 if ($e->{type} ne '#attribute') {
517     $e->{parent} = $self;
518     push @node, $e;
519     }
520 wakaba 1.7 }
521     } else { ## reference is not expanded
522     push @node, $_;
523     }
524     }
525 wakaba 1.8 $_->{flag}->{smxp__defined_with_param_ref} = 0
526     if $_->{flag}->{smxp__defined_with_param_ref};
527 wakaba 1.7 }
528     $self->{node} = \@node;
529     }
530    
531 wakaba 1.8 sub resolve_relative_uri ($;$%) {
532     require URI;
533     my ($self, $rel, %o) = @_;
534     my $base = $self->get_attribute ('base', namespace_uri => $NS{xml});
535     $base = ref ($base) ? $base->inner_text : undef;
536     if ($base !~ /^(?:[0-9A-Za-z.+-]|%[0-9A-Fa-f]{2})+:/) { # $base is relative
537     $base = $self->_resolve_relative_uri_by_parent ($base, \%o);
538     }
539     eval q{ ## Catch error such as $base is 'data:,foo' (non hierarchic scheme,...)
540     return URI->new ($rel)->abs ($base || '.'); ## BUG (or spec) of URI: $base == false
541     } or return $rel;
542     }
543     sub _resolve_relative_uri_by_parent ($$$) {
544     my ($self, $rel, $o) = @_;
545     if (ref $self->{parent}) {
546     if (!$o->{use_references_base_uri} && $self->{parent}->{type} eq '#reference') {
547     ## This case is necessary to work with
548     ## <element> <!-- element can have base URI -->
549     ## text <!-- text cannot have base URI -->
550     ## &ent; <!-- ref's base URI is referred entity's one (in this module) -->
551     ## <!-- expantion of ent -->
552     ## entity's text <!-- text cannot have base URI, so use <element>'s one -->
553     ## <entitys-element/> <!-- element can have base URI, otherwise ENTITY's one -->
554     ## </element>
555     return $self->{parent}->_resolve_relative_uri_by_parent ($rel, $o);
556     } else {
557     return $self->{parent}->resolve_relative_uri ($rel, %$o);
558     }
559     } else {
560     return $rel;
561     }
562     }
563     sub base_uri ($;$) {
564     my ($self, $new_uri) = @_;
565     my $base;
566     if (defined $new_uri) {
567     $base = $self->set_attribute (base => $new_uri, namespace_uri => $NS{xml});
568     }
569     $base ||= $self->get_attribute ('base', namespace_uri => $NS{xml});
570     ref ($base) ? $base->inner_text : undef;
571     }
572    
573 wakaba 1.1 =item $tag = $x->start_tag
574    
575 wakaba 1.4 Returns the start tag (or something that marks the start of something, such as '<!--'
576 wakaba 1.1 for C<#comment> nodes).
577    
578     =cut
579    
580     sub start_tag ($) {
581     my $self = shift;
582     if ($self->{type} eq '#element' && $self->_check_ncname ($self->{local_name})) {
583     my $r = '<';
584     $r .= $self->qname;
585     for (@{$self->{node}}) {
586     $r .= ' ' . $_->outer_xml if $_->node_type eq '#attribute';
587     }
588     for my $prefix (grep !/^-/, keys %{$self->{ns}||{}}) {
589     if ($prefix) {
590     $r .= ' xmlns:'.substr ($prefix, 0, length ($prefix)-1);
591     } else {
592     $r .= ' xmlns';
593     }
594     $r .= '="'.$self->_entitize ($self->{ns}->{$prefix}).'"';
595     }
596     $r .= '>';
597     $r;
598     } elsif ($self->{type} eq '#comment') {
599 wakaba 1.4 '<!--';
600     } elsif ($self->{type} eq '#pi' && $self->_check_ncname ($self->{local_name})) {
601     '<?' . ($self->{local_name});
602 wakaba 1.2 } elsif ($self->{type} eq '#reference') {
603 wakaba 1.10 if ($self->{namespace_uri} eq $NS{SGML}.'char:ref:hex') {
604 wakaba 1.2 '&#x';
605 wakaba 1.10 } elsif ($self->{namespace_uri} eq $NS{SGML}.'char:ref') {
606 wakaba 1.2 '&#';
607 wakaba 1.4 } elsif ($self->_check_ncname ($self->{local_name})) {
608 wakaba 1.10 if ($self->{namespace_uri} eq $NS{SGML}.'entity:parameter') {
609 wakaba 1.2 '%';
610     } else {
611     '&';
612     }
613     } else { # error
614     '';
615     }
616 wakaba 1.4 } elsif ($self->{type} eq '#declaration' && $self->{namespace_uri}) {
617     '<!' . {
618 wakaba 1.8 $NS{SGML}.'attlist' => 'ATTLIST',
619     $NS{SGML}.'doctype' => 'DOCTYPE',
620     $NS{SGML}.'element' => 'ELEMENT',
621     $NS{SGML}.'entity' => 'ENTITY',
622     $NS{SGML}.'entity:parameter' => 'ENTITY',
623     $NS{SGML}.'notation' => 'NOTATION',
624 wakaba 1.10 }->{$self->{namespace_uri}} . ' ' .
625     ($self->{namespace_uri} eq $NS{SGML}.'entity:parameter' ?
626 wakaba 1.7 ($self->{flag}->{smxp__defined_with_param_ref}?'':'% '):'');
627 wakaba 1.2 } elsif ($self->{type} eq '#section') {
628 wakaba 1.9 '<![';
629 wakaba 1.1 } else {
630     '';
631     }
632     }
633    
634     =item $tag = $x->end_tag
635    
636 wakaba 1.4 Returns the end tag (or something that marks the end of something, such as '-->'
637 wakaba 1.1 for C<#comment> nodes).
638    
639     =cut
640    
641     sub end_tag ($) {
642     my $self = shift;
643     if ($self->{type} eq '#element' && $self->_check_ncname ($self->{local_name})) {
644     '</' . $self->qname . '>';
645     } elsif ($self->{type} eq '#comment') {
646 wakaba 1.4 '-->';
647 wakaba 1.1 } elsif ($self->{type} eq '#pi' && $self->_check_ncname ($self->{local_name})) {
648     '?>';
649 wakaba 1.2 } elsif ($self->{type} eq '#reference') {
650     ';';
651 wakaba 1.4 } elsif ($self->{type} eq '#declaration' && $self->{namespace_uri}) {
652     '>';
653 wakaba 1.1 } elsif ($self->{type} eq '#declaration' && $self->_check_ncname ($self->{local_name})) {
654 wakaba 1.4 '>';
655 wakaba 1.2 } elsif ($self->{type} eq '#section') {
656 wakaba 1.9 ']]>';
657 wakaba 1.1 } else {
658     '';
659     }
660     }
661    
662     =item $tag = $x->attribute_name
663    
664     Returns the attribute name.
665    
666     =cut
667    
668     sub attribute_name ($) {
669     my $self = shift;
670     if ($self->{type} eq '#attribute' && $self->_check_ncname ($self->{local_name})) {
671     ($self->{namespace_uri} ?
672     (ref $self->{parent} ? $self->{parent} : $self)
673     ->_get_namespace_prefix ($self->{namespace_uri}, use_no_prefix => 0) : '')
674     .$self->{local_name};
675     } else {
676     '';
677     }
678     }
679    
680     =item $tag = $x->attribute_value
681    
682     Returns the attribute value.
683    
684     =cut
685    
686 wakaba 1.4 sub attribute_value ($;%) {
687 wakaba 1.10 my ($self, %o) = @_;
688 wakaba 1.1 if ($self->{type} eq '#attribute' && $self->_check_ncname ($self->{local_name})) {
689 wakaba 1.5 my $r = '"';
690 wakaba 1.10 my $isc = $self->_is_same_class ($self->{value});
691 w 1.12 $r .= $self->_entitize ($self->{value}, keep_wsp => 1) unless $isc;
692 wakaba 1.10 for (($isc?$self->{value}:()), @{$self->{node}}) {
693     my $nt = $_->{type};
694 wakaba 1.4 if ($nt eq '#reference' || $nt eq '#xml') {
695     $r .= $_->outer_xml;
696     } elsif ($nt ne '#attribute') {
697 w 1.12 $r .= $self->_entitize ($_->inner_text, keep_wsp => 1);
698 wakaba 1.4 }
699     }
700     return $r . '"';
701     } else {
702     '';
703     }
704     }
705    
706     sub entity_value ($;%) {
707 wakaba 1.10 my ($self, %o) = @_;
708 wakaba 1.4 my $_entitize = sub {
709     my $s = shift;
710 wakaba 1.7 $s =~ s/&/&#x26;/g;
711     $s =~ s/&#x26;(\p{InXML_NameStartChar}\p{InXMLNameChar}*);/&$1;/g;
712 w 1.12 $s =~ s/([\x0D%"])/sprintf '&#x%02X;', ord $1/ge;
713     $s =~ s/([\x00-\x08\x0B\x0C\x0E-\x1F\x7F])/sprintf '&amp;#x%02X;', ord $1/ge;
714 wakaba 1.4 $s;
715     };
716     if ($self->{type} eq '#attribute' && $self->_check_ncname ($self->{local_name})) {
717     my $r = '"' . &$_entitize ($self->{value});
718     for (@{$self->{node}}) {
719 wakaba 1.10 my $nt = $_->{type};
720 wakaba 1.4 if ($nt eq '#reference' || $nt eq '#xml') {
721     $r .= $_->outer_xml;
722     } elsif ($nt ne '#attribute') {
723     $r .= &$_entitize ($_->inner_text);
724 wakaba 1.1 }
725     }
726 wakaba 1.4 return $r . '"';
727 wakaba 1.1 } else {
728     '';
729     }
730     }
731    
732 wakaba 1.7 ## This method should be called only from SuikaWiki::Markup::XML::* family modules,
733     ## since this is NOT a FORMAL interface.
734     sub _entity_parameter_literal_value ($;%) {
735     my $self = shift;
736 wakaba 1.10 my $r = '';
737     my $isc = $self->_is_same_class ($self->{value});
738     $r = $self->{value} unless $isc;
739     for (($isc?$self->{value}:()), @{$self->{node}}) {
740     my $nt = $_->{type};
741 w 1.12 ## Bare node and general entity reference node
742     if ($nt eq '#xml' || ($nt eq '#reference' && $_->{namespace_uri} eq $NS{SGML}.'entity')) {
743 wakaba 1.10 $r .= $_->outer_xml;
744 w 1.12 ## Text node and parameter entity reference node
745 wakaba 1.10 } elsif ($nt ne '#attribute') {
746     $r .= $_->inner_text;
747 wakaba 1.7 }
748 wakaba 1.10 }
749     $r;
750 wakaba 1.7 }
751    
752 wakaba 1.1 =item $tag = $x->attribute
753    
754     Returns the attribute (name and value pair).
755    
756     =cut
757    
758     sub attribute ($) {
759     my $self = shift;
760     if ($self->{type} eq '#attribute' && $self->_check_ncname ($self->{local_name})) {
761     $self->attribute_name . '=' . $self->attribute_value;
762     } else {
763     '';
764     }
765     }
766    
767 wakaba 1.2 sub external_id ($;%) {
768     my $self = shift;
769     my %o = @_;
770     my ($pubid, $sysid, $ndata);
771     for (@{$self->{node}}) {
772 wakaba 1.4 if ($_->{type} eq '#attribute' && !$_->{namespace_uri}) {
773 wakaba 1.2 if ($_->{local_name} eq 'PUBLIC') {
774 wakaba 1.4 $pubid = $_->inner_text;
775 wakaba 1.2 } elsif ($_->{local_name} eq 'SYSTEM') {
776 wakaba 1.4 $sysid = $_->inner_text;
777 wakaba 1.2 } elsif ($_->{local_name} eq 'NDATA') {
778 wakaba 1.4 $ndata = $_->inner_text;
779     undef $ndata unless $self->_check_ncname ($ndata);
780 wakaba 1.2 }
781     }
782     }
783     my $r = '';
784 wakaba 1.9 if (defined $pubid) {
785 wakaba 1.4 $pubid =~ s|([^\x0A\x0D\x20A-Za-z0-9'()+,./:=?;!*#\@\$_%-])|sprintf '%%%02X', ord $1|ges;
786 wakaba 1.9 $pubid = '"' . $pubid . '"';
787 wakaba 1.4 }
788 wakaba 1.9 if (defined $sysid) {
789 wakaba 1.10 if (index ($sysid, '"') > -1) {
790     if (index ($sysid, "'") > -1) {
791     $sysid =~ s/"/%22/; $sysid = '"' . $sysid . '"';
792 wakaba 1.4 } else {
793     $sysid = "'" . $sysid . "'";
794     }
795     } else {
796     $sysid = '"' . $sysid . '"';
797     }
798     }
799 wakaba 1.2 if ($pubid && $sysid) {
800 wakaba 1.9 $r = 'PUBLIC ' . $pubid . ' ' . $sysid;
801 wakaba 1.2 } elsif ($sysid) {
802 wakaba 1.4 $r = 'SYSTEM ' . $sysid;
803 wakaba 1.2 } elsif ($pubid && $o{allow_pubid_only}) {
804 wakaba 1.9 $r = 'PUBLIC ' . $pubid;
805 wakaba 1.2 }
806     if ($r && $ndata && $o{use_ndata}) {
807 wakaba 1.4 $r .= ' NDATA ' . $ndata;
808 wakaba 1.2 }
809     $r;
810     }
811    
812     =item $s = $x->content_spec
813    
814     Generates contentspec of element type declaration (ex. C<(E1 | E2 | E3)>)
815     or AttDef of attribute declaration (ex. C<name CDATA #REQUIRED>).
816    
817     =cut
818    
819     sub content_spec ($) {
820     my $self = shift;
821     if ($self->{type} eq '#element') {
822 wakaba 1.10 my $text = 0;
823     my $contentspec = join ' | ', map {$_->qname} grep {$text = 1 if $_->{type} eq '#text'; $_->{type} eq '#element'} @{$self->{node}};
824 wakaba 1.2 $contentspec = '#PCDATA' . ($contentspec ? ' | ' . $contentspec : '') if $text;
825    
826     return $contentspec ? '(' . $contentspec . ')' : 'EMPTY';
827     } elsif ($self->{type} eq '#attribute') {
828     my $attdef = $self->qname . "\t" . ($self->{data_type} || 'CDATA') . "\t";
829     my $default = $self->{default_decl};
830     $default .= ' ' . $self->attribute_value if $default eq '#FIXED';
831     unless ($default) {
832     $default = defined $self->{value} ? $self->attribute_value : '#IMPLIED';
833     }
834     return $attdef . $default;
835     }
836     }
837    
838 wakaba 1.1 =item $tag = $x->inner_xml
839    
840     Returns the content of the node in XML syntax. (In case of the C<#element> nodes,
841     element content without start- and end-tags is returned.)
842    
843     Note that for not all node types the behavior of this method is defined.
844     For example, returned value of C<#attribute> might be unexpected one
845     in this version of this module.
846    
847     =cut
848    
849 wakaba 1.3 sub inner_xml ($;%) {
850 wakaba 1.10 my ($self, %o) = @_;
851 wakaba 1.1 my $r = '';
852     if ($self->{type} eq '#comment') {
853     $r = $self->inner_text;
854     $r =~ s/--/-&#x45;/g;
855     } elsif ($self->{type} eq '#pi') {
856 wakaba 1.10 my $isc = $self->_is_same_class ($self->{value});
857     if (!$isc && length ($self->{value})) {
858 wakaba 1.1 $r = ' ' . $self->{value};
859 wakaba 1.2 $r =~ s/\?>/? >/g; # Same replacement as of the recommendation of XSLT:p.i.
860 wakaba 1.1 }
861 wakaba 1.10 for (($isc?$self->{value}:()), @{$self->{node}}) {
862 wakaba 1.1 if ($_->node_type eq '#attribute') {
863     $r .= ' ' . $_->attribute;
864     } else {
865     my $s = $_->inner_text;
866 wakaba 1.10 if (length $s) {
867     $s =~ s/\?>/? >/g;
868     $r .= ' ' . $s;
869     }
870 wakaba 1.1 }
871     }
872 wakaba 1.2 } elsif ($self->{type} eq '#reference') {
873 wakaba 1.10 if ($self->{namespace_uri} eq $NS{SGML}.'char:ref:hex') {
874 wakaba 1.2 $r = sprintf '%02X', $self->{value};
875 wakaba 1.10 } elsif ($self->{namespace_uri} eq $NS{SGML}.'char:ref') {
876 wakaba 1.2 $r = sprintf '%02d', $self->{value};
877 wakaba 1.10 } elsif (ref ($self->{local_name}) && $self->{local_name}->{type} eq '#declaration') {
878 wakaba 1.4 $r = $self->{local_name}->{local_name};
879     } elsif ($self->_check_ncname ($self->{local_name})) {
880     $r = ($self->{local_name});
881 wakaba 1.2 } else { # error
882     $r = '';
883     }
884 wakaba 1.1 } elsif ($self->{type} eq '#declaration') {
885 wakaba 1.8 if ($self->{namespace_uri} eq $NS{SGML}.'doctype') {
886 wakaba 1.4 my $root = $self->get_attribute ('qname');
887     ref $root ? ($root = $root->inner_text) : (ref $self->{parent} ? (do {
888     for (@{$self->{parent}->{node}}) {
889     if ($_->{type} eq '#element') {
890     $root = $_->qname;
891     last if $root;
892     }
893     }
894     $root = '#IMPLIED';
895     }) : ($root = '#IMPLIED')); ## error!
896     my ($isub, $xid) = ('', $self->external_id);
897     for (@{$self->{node}}) {
898     $isub .= $_->outer_xml if $_->{type} ne '#attribute';
899 wakaba 1.2 }
900     if ($xid) {
901 wakaba 1.4 $r = $xid;
902     if ($isub) {
903 wakaba 1.7 $r .= " [" . $isub . "]";
904 wakaba 1.4 }
905     } else {
906     if ($isub) {
907 wakaba 1.7 $r = "[" . $isub . "]";
908 wakaba 1.4 } else {
909     $r = "[]";
910     }
911     }
912     $r = $root . ' ' . $r;
913 wakaba 1.9 } elsif ($self->{namespace_uri} eq $NS{SGML}.'entity'
914     || $self->{namespace_uri} eq $NS{SGML}.'entity:parameter'
915     || $self->{namespace_uri} eq $NS{SGML}.'notation') {
916 wakaba 1.4 my %xid_opt;
917 wakaba 1.7 $r = $self->{local_name} . ' ' if !$self->{flag}->{smxp__defined_with_param_ref}
918     && $self->_check_ncname ($self->{local_name});
919 wakaba 1.8 if ($self->{namespace_uri} eq $NS{SGML}.'entity:parameter') {
920 wakaba 1.4 #$r = '% ' . $r;
921 wakaba 1.8 } elsif ($self->{namespace_uri} eq $NS{SGML}.'entity') {
922 wakaba 1.4 $xid_opt{use_ndata} = 1;
923 wakaba 1.8 } elsif ($self->{namespace_uri} eq $NS{SGML}.'notation') {
924 wakaba 1.4 $xid_opt{allow_pubid_only} = 1;
925     }
926    
927 wakaba 1.10 my ($v, $xid) = ($self->{value});
928     $xid = $self->external_id (%xid_opt) unless $self->{flag}->{smxp__defined_with_param_ref};
929 wakaba 1.4 if ($xid) { ## External ID
930 wakaba 1.2 $r .= $xid;
931 wakaba 1.4 } else { ## EntityValue
932     my $entity_value = $self->get_attribute ('value');
933 wakaba 1.7 undef $entity_value if $self->{flag}->{smxp__defined_with_param_ref};
934 wakaba 1.4 if ($entity_value) { # <!ENTITY foo "bar">
935     $r .= $entity_value->entity_value;
936     } else { ## Parameter entity reference
937 wakaba 1.9 my $isc = $self->_is_same_class ($self->{value});
938     $r .= $self->{value} unless $isc;
939     for (($isc?$self->{value}:()), @{$self->{node}}) {
940 wakaba 1.7 $r .= $_->outer_xml unless $_->{type} eq '#attribute';
941 wakaba 1.4 }
942 wakaba 1.2 }
943 wakaba 1.1 }
944 wakaba 1.8 } elsif ($self->{namespace_uri} eq $NS{SGML}.'element') {
945 w 1.12 if (!$self->{flag}->{smxp__defined_with_param_ref}) {
946     $r = $self->get_attribute ('qname');
947     $r = $r->inner_text if $r;
948     unless ($self->_check_ncname ($r)) {
949     $r = undef;
950     } else {
951     $r .= ' ';
952     }
953    
954     my $cmodel = $self->get_attribute ('content', make_new_node => 1)->inner_text;
955     if ($cmodel && $cmodel ne 'mixed') {
956     $r .= $cmodel;
957     } else {
958     my $make_cmodel;
959     $make_cmodel = sub {
960     my $c = shift;
961     my @tt;
962     for (@{$c->child_nodes}) {
963     if ($_->node_type eq '#element' && $_->namespace_uri eq $NS{SGML}.'element') {
964     if ($_->local_name eq 'group') {
965     my $tt = &$make_cmodel ($_);
966     push @tt, '(' . $tt . ')'
967     . ($_->get_attribute ('occurence', make_new_node => 1)->inner_text)
968     if $tt;
969     } elsif ($_->local_name eq 'element') {
970     push @tt, $_->get_attribute ('qname', make_new_node => 1)->inner_text;
971     }
972     }
973     }
974     return join scalar ($c->get_attribute ('connector', make_new_node => 1)->inner_text
975     || '|'),
976     grep {$_} @tt;
977     };
978     my $tt;
979     my $grp_node;
980     for (@{$self->child_nodes}) {
981     if ($_->node_type eq '#element' && $_->namespace_uri eq $NS{SGML}.'element'
982     && $_->local_name eq 'group') {
983     $grp_node = $_;
984     $tt = &$make_cmodel ($grp_node);
985     last;
986     }
987     }
988     if ($cmodel eq 'mixed') { ## mixed content
989     if ($tt) {
990     $r .= '(#PCDATA|' . $tt . ')*';
991     } else {
992     $r .= '(#PCDATA)'
993     . ($grp_node->get_attribute ('connector', make_new_node => 1)->inner_text eq '*'
994     ? '*' : '');
995     }
996     } else { ## element content
997     if ($tt) {
998     $r .= '(' . $tt . ')'
999     . ($grp_node->get_attribute ('occurence', make_new_node => 1)->inner_text);
1000     } else {
1001     $r .= 'EMPTY';
1002     }
1003     } # mixed or element content
1004     } # content model group
1005     } else { ## Save source doc's description as far as possible
1006     my $isc = $self->_is_same_class ($self->{value});
1007     $r .= $self->{value} unless $isc;
1008     for (($isc?$self->{value}:()), @{$self->{node}}) {
1009     unless ($_->{type} eq '#attribute' || $_->{type} eq '#element') {
1010     $r .= $_->outer_xml;
1011     } elsif ($_->{type} eq '#element' && $_->{namespace_uri} eq $NS{SGML}.'group') {
1012     $r .= $_->outer_xml;
1013     }
1014     }
1015 wakaba 1.2 }
1016 wakaba 1.11 } elsif ($self->{namespace_uri} eq $NS{SGML}.'attlist') {
1017 w 1.12 if (!$self->{flag}->{smxp__defined_with_param_ref}) {
1018     $r = $self->get_attribute ('qname');
1019     $r = $r->inner_text if $r;
1020     unless ($self->_check_ncname ($r)) {
1021     $r = undef;
1022     } else {
1023     $r .= ' ';
1024     }
1025     for (@{$self->{node}}) {
1026     if ($_->{type} eq '#element' && $_->{namespace_uri} eq $NS{XML}.'attlist'
1027     && $_->{local_name} eq 'AttDef') {
1028     $r .= "\n\t" . $_->get_attribute ('qname', make_new_node => 1)->inner_text;
1029     my $attr_type = $_->get_attribute ('type', make_new_node => 1)->inner_text;
1030     if ($attr_type ne 'enum') {
1031     $r .= "\t" . $attr_type;
1032     }
1033     if ($attr_type eq 'enum' || $attr_type eq 'NOTATION') {
1034     my @l;
1035     for my $item (@{$_->{node}}) {
1036     if ($item->{type} eq '#element' && $item->{namespace_uri} eq $NS{XML}.'attlist'
1037     && $item->{local_name} eq 'enum') {
1038     push @l, $item->inner_text;
1039     }
1040     }
1041     $r .= "\t(" . join ('|', @l) . ')';
1042     }
1043     ## DefaultDecl
1044     my $deftype = $_->get_attribute ('default_type', make_new_node => 1)->inner_text;
1045     if ($deftype) {
1046     $r .= "\t#" . $deftype;
1047     }
1048     if (!$deftype || $deftype eq 'FIXED') {
1049     $r .= "\t" . $_->get_attribute ('default_value', make_new_node => 1)
1050     ->attribute_value;
1051     }
1052     } # AttDef
1053 wakaba 1.2 }
1054 w 1.12 } else { ## Save source doc's description as far as possible
1055     my $isc = $self->_is_same_class ($self->{value});
1056     $r .= $self->{value} unless $isc;
1057     for (($isc?$self->{value}:()), @{$self->{node}}) {
1058     unless ($_->{type} eq '#attribute' || $_->{type} eq '#element') {
1059     $r .= $_->outer_xml;
1060     } elsif ($_->{type} eq '#element' && $_->{namespace_uri} eq $NS{SGML}.'group') {
1061     $r .= $_->outer_xml;
1062     }
1063     }
1064 wakaba 1.1 }
1065 wakaba 1.2 } else { # unknown
1066 wakaba 1.3 for (@{$self->{node}}) {
1067     $r .= $_->outer_xml;
1068     }
1069 wakaba 1.1 }
1070 wakaba 1.9 } elsif ($self->{type} eq '#section') {
1071     my $status = $self->get_attribute ('status', make_new_node => 1)->inner_text;
1072     if ($status eq 'CDATA') {
1073     $r = $self->inner_text;
1074     $r =~ s/]]>/]]>]]<![CDATA[>/g;
1075     $r = 'CDATA['.$r;
1076     } else {
1077     my $sl = $self->get_attribute ('status_list', make_new_node => 1);
1078     if ($sl->{flag}->{smxp__defined_with_param_ref}) {
1079     my $isc = $self->_is_same_class ($self->{value});
1080     $status = '';
1081     $status = $sl->{value} unless $isc;
1082     for (($isc?$sl->{value}:()), @{$sl->{node}}) {
1083     $status .= $_->outer_xml unless $_->{type} eq '#attribute';
1084     }
1085     $r = $status.'['.$r;
1086     } elsif ($status) {
1087     $r = $status.'['.$r;
1088     } else {
1089     ## Must be an ignored section
1090     }
1091     my $isc = $self->_is_same_class ($self->{value});
1092     $r .= join '', map {s/\]\]>/]]&gt;/g; $_} $self->{value} unless $isc;
1093     for (($isc?$self->{value}:()), @{$self->{node}}) {
1094     if ($_->{type} eq '#text') {
1095     $r .= join '', map {s/\]\]>/]]&gt;/g; $_} $_->inner_text; ## Anyway, this is error
1096     } elsif ($_->{type} ne '#attribute') {
1097     $r .= $_->outer_xml;
1098     }
1099     }
1100     }
1101 wakaba 1.1 } else {
1102 wakaba 1.10 my $isc = $self->_is_same_class ($self->{value});
1103     unless ($isc) {
1104     if ($self->{type} ne '#xml') {
1105     $r = $self->_entitize ($self->{value});
1106     } else {
1107     $r = $self->{value};
1108     }
1109 wakaba 1.1 }
1110 wakaba 1.10 for (($isc?$self->{value}:()), @{$self->{node}}) {
1111     my $nt = $_->{type};
1112     if (($self->{option}->{indent})
1113     && ($nt eq '#element' || $nt eq '#comment' || $nt eq '#pi' || $nt eq '#declaration')) {
1114 wakaba 1.1 $r .= "\n";
1115     }
1116     $r .= $_->outer_xml unless $_->node_type eq '#attribute';
1117     }
1118     }
1119     $r;
1120     }
1121    
1122    
1123     =item $tag = $x->outer_xml
1124    
1125     Returns the node in XML syntax.
1126    
1127     =cut
1128    
1129     sub outer_xml ($) {
1130     my $self = shift;
1131     if ($self->{type} eq '#attribute') {
1132 wakaba 1.10 return $self->attribute;
1133 wakaba 1.1 } else {
1134 wakaba 1.4 if ($self->{option}->{indent} && $self->{type} eq '#element') {
1135 wakaba 1.1 my $r = $self->start_tag;
1136     my $c = $self->inner_xml;
1137 wakaba 1.10 if (!length ($c) && $self->{option}->{use_EmptyElemTag}) {
1138 wakaba 1.4 substr ($r, -1) = ' />';
1139     } else {
1140     if ($c) {
1141     $c =~ s/\n/\n /g;
1142     $r .= "\n " . $c . "\n";
1143     }
1144     $r .= $self->end_tag;
1145 wakaba 1.1 }
1146 wakaba 1.10 return $r;
1147 wakaba 1.1 } else {
1148 wakaba 1.4 my $r = $self->start_tag;
1149     my $c = $self->inner_xml;
1150 wakaba 1.8 if ($self->{type} eq '#element' && !length ($c) && $self->{option}->{use_EmptyElemTag}) {
1151 wakaba 1.4 substr ($r, -1) = ' />';
1152     } else {
1153     $r .= $c . $self->end_tag;
1154     }
1155 wakaba 1.10 return $r;
1156 wakaba 1.11 #return '{'.$self->{type}.': '.$r.'}'; ## DEBUG: show structure
1157 wakaba 1.1 }
1158     }
1159     }
1160    
1161     =item $tag = $x->inner_text
1162    
1163     Returns the text content of the node. (In many case the returned value is same
1164     as WinIE DOM C<inner_text ()> function's or XPath C<text()> function's.
1165     But some classes that inherits this module might implement to return other
1166     value (eg. to return the value of the alt attribute of html:img element).
1167    
1168 wakaba 1.2 Available options: C<output_ref_as_is>.
1169    
1170 wakaba 1.1 =cut
1171    
1172 wakaba 1.2 sub inner_text ($;%) {
1173 wakaba 1.1 my $self = shift;
1174 wakaba 1.2 my %o = @_;
1175 wakaba 1.5 my $r = '';
1176 wakaba 1.10 if ($self->{type} eq '#reference'
1177     && ($self->{namespace_uri} eq $NS{SGML}.'char:ref'
1178     || $self->{namespace_uri} eq $NS{SGML}.'char:ref:hex')) {
1179     $r = chr $self->{value};
1180 wakaba 1.7 } elsif ($self->{type} eq '#declaration'
1181     && ($self->{namespace_uri} eq $NS{SGML}.'entity'
1182     || $self->{namespace_uri} eq $NS{SGML}.'entity:parameter')) {
1183 wakaba 1.10 ## TODO:
1184     $r = $self->set_attribute ('value')->inner_text;
1185 wakaba 1.7 } else { # not #reference nor #declaration(ENTITY)
1186 wakaba 1.10 my $isc = $self->_is_same_class ($self->{value});
1187     $r = $self->{value} unless $isc;
1188 wakaba 1.7 if ($o{output_ref_as_is}) { ## output as if RCDATA
1189     $r =~ s/&/&amp;/g;
1190 wakaba 1.10 for my $node (($isc?$self->{value}:()), @{$self->{node}}) {
1191 wakaba 1.7 my $nt = $node->node_type;
1192     if ($nt eq '#reference' || $nt eq '#xml') {
1193     $r .= $node->outer_xml;
1194     } elsif ($nt ne '#attribute') {
1195     $r .= map {s/&/&amp;/g; $_} $node->inner_text;
1196     }
1197     }
1198     } else {
1199 wakaba 1.10 for (($isc?$self->{value}:()), @{$self->{node}}) {
1200     $r .= $_->inner_text unless $_->{type} eq '#attribute';
1201 wakaba 1.2 }
1202     }
1203 wakaba 1.1 }
1204     $r;
1205     }
1206    
1207 wakaba 1.9 sub _is_same_class ($$) {
1208     my ($self, $something) = @_;
1209 wakaba 1.10 return 0 if {qw/ARRAY 1 HASH 1 CODE 1 :nonref: 1/}->{ref ($something) || ':nonref:'};
1210     eval q{$self->_CLASS_NAME eq $something->_CLASS_NAME} ? 1 : 0;
1211 wakaba 1.9 }
1212    
1213     sub root_node ($) {
1214     my $self = shift;
1215     if ($self->{type} eq '#document') {
1216     return $self;
1217     } elsif (ref $self->{parent}) {
1218     return $self->{parent}->root_node;
1219     } else {
1220     return $self;
1221     }
1222 wakaba 1.1 }
1223    
1224 wakaba 1.7 sub _get_entity_manager ($) {
1225     my $self = shift;
1226     if ($self->{type} eq '#document') {
1227     unless ($self->{flag}->{smx__entity_manager}) {
1228     require SuikaWiki::Markup::XML::EntityManager;
1229     $self->{flag}->{smx__entity_manager} = SuikaWiki::Markup::XML::EntityManager->new ($self);
1230     }
1231     return $self->{flag}->{smx__entity_manager};
1232     } elsif (ref $self->{parent}) {
1233     return $self->{parent}->_get_entity_manager;
1234     } else {
1235     unless ($self->{flag}->{smx__entity_manager}) {
1236     require SuikaWiki::Markup::XML::EntityManager;
1237     $self->{flag}->{smx__entity_manager} = SuikaWiki::Markup::XML::EntityManager->new ($self);
1238     }
1239     return $self->{flag}->{smx__entity_manager};
1240     }
1241     }
1242    
1243 wakaba 1.10 sub _CLASS_NAME ($) { 'SuikaWiki::Markup::XML' }
1244 wakaba 1.5
1245 wakaba 1.1 # $s = $x->_entitize ($s)
1246 wakaba 1.4 sub _entitize ($$;%) {
1247     my ($self, $s, %o) = (shift, shift, @_);
1248 wakaba 1.1 $s =~ s/&/&amp;/g;
1249     $s =~ s/</&lt;/g;
1250     $s =~ s/>/&gt;/g;
1251     $s =~ s/"/&quot;/g;
1252 wakaba 1.7 $s =~ s/([\x00-\x08\x0B\x0C\x0E-\x1F])/sprintf '&amp;#%d;', ord $1/ge;
1253 w 1.12 $s =~ s/([\x09\x0A\x0D])/sprintf '&#%d;', ord $1/ge if $o{keep_wsp};
1254 wakaba 1.1 $s;
1255     }
1256    
1257 wakaba 1.2 # 1/0 = $x->_check_name ($s)
1258     sub _check_name ($$) {
1259     my $self = shift;
1260     my $s = shift;
1261     return $Cache{name}->{$s} if defined $Cache{name}->{$s};
1262     if ($s =~ /^\p{InXML_NameStartChar}/ && $s !~ /\P{InXMLNameChar}/) {
1263     # \p{...}('*'/'+'/'{n,}') does not work...
1264     $Cache{name}->{$s} = 1;
1265     1;
1266     } else {
1267     $Cache{name}->{$s} = 0;
1268     0;
1269     }
1270     }
1271 wakaba 1.1 # 1/0 = $x->_check_ncname ($s)
1272     sub _check_ncname ($$) {
1273     my $self = shift;
1274     my $s = shift;
1275     return $Cache{ncname}->{$s} if defined $Cache{ncname}->{$s};
1276     if ($s =~ /^\p{InXML_NCNameStartChar}/ && $s !~ /\P{InXMLNCNameChar}/) {
1277     # \p{...}('*'/'+'/'{n,}') does not work...
1278     $Cache{ncname}->{$s} = 1;
1279     1;
1280     } else {
1281     $Cache{ncname}->{$s} = 0;
1282     0;
1283     }
1284     }
1285    
1286     # 1/0 = $x->_check_namespace_prefix ($s)
1287     sub _check_namespace_prefix ($$) {
1288     my $self = shift;
1289     my $s = shift;
1290     return 0 unless defined $s;
1291     return 1 if $s eq '';
1292     substr ($s, -1, 1) = '' if substr ($s, -1, 1) eq ':';
1293     $self->_check_ncname ($s);
1294     }
1295    
1296 wakaba 1.2 ## TODO: cleaning $self->{node} before outputing, to ensure nodes not to have
1297     ## multiple parents.
1298     ## TODO: normalize namespace URI (removing non URI chars)
1299    
1300     sub flag ($$;$) {
1301     my ($self, $name, $value) = @_;
1302     if (defined $value) {
1303     $self->{flag}->{$name} = $value;
1304     }
1305     $self->{flag}->{$name};
1306     }
1307    
1308 wakaba 1.3 sub option ($$;$) {
1309     my ($self, $name, $value) = @_;
1310     if (defined $value) {
1311     $self->{option}->{$name} = $value;
1312     }
1313     $self->{option}->{$name};
1314     }
1315    
1316 wakaba 1.1 =back
1317    
1318 wakaba 1.2 =head1 NODE TYPES
1319    
1320     =over 4
1321    
1322     =item #attribute
1323    
1324     Attribute. Its XML representation takes the form of NAME="VALUE".
1325    
1326     =item #comment
1327    
1328     Comment declarement. <!-- -->
1329    
1330     =item #declarement
1331    
1332     SGML's declarements, such as SGML, DOCTYPE, ENTITY, etc.
1333     <!SGML ...>, <!DOCTYPE root []>, <!ENTITY % name "value">
1334    
1335     =item #element
1336    
1337     Element. Its XML representation consists of start tag, content and end tag,
1338     like <TYPE>content</TYPE>.
1339    
1340     =item #fragment
1341    
1342     Fragment of nodes. It's similar to DOM's fragment node.
1343    
1344     =item #pi
1345    
1346     Prosessing instruction. <?NAME VALUE?>
1347    
1348     =item #reference
1349    
1350     Character reference or general or parameter entity reference.
1351     &#nnnn;, &#xhhhh;, &name;, %name;.
1352    
1353     =item #section
1354    
1355     Markup section. CDATA, INCLUDE and IGNORE are supported by XML.
1356     <![%type;[...]]>
1357    
1358     =item #text
1359    
1360     Text.
1361    
1362     =item #xml
1363    
1364     Preformatted XML text.
1365    
1366     =cut
1367    
1368 wakaba 1.4 =head1 RESTRICTIONS
1369    
1370     =over 4
1371    
1372     =item XML without XML Namespace is not supported.
1373    
1374     =item Before default namespace without bounded URI (xmlns="") is outputed, it must be declared.
1375    
1376     For example, next code generates invalid (non-well-formed) XML Namespace document.
1377    
1378     my $x = SuikaWiki::Markup::XML->new (local_name => 'elementType');
1379     print $x # <ns1:elementType xmlns:ns1=""></ns1:elementType>
1380    
1381     So you must write like:
1382    
1383     my $x = SuikaWiki::Markup::XML->new (local_name => 'elementType');
1384     $x->define_new_namespace ('' => '');
1385     print $x; # <elementType xmlns=""></elementType>
1386    
1387     =back
1388    
1389 wakaba 1.1 =head1 LICENSE
1390    
1391     Copyright 2003 Wakaba <w@suika.fam.cx>
1392    
1393     This program is free software; you can redistribute it and/or
1394     modify it under the same terms as Perl itself.
1395    
1396     =cut
1397    
1398 w 1.12 1; # $Date: 2003/07/05 07:26:05 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24