/[suikacvs]/messaging/manakai/lib/Message/Markup/XML/Parser.pm
Suika

Contents of /messaging/manakai/lib/Message/Markup/XML/Parser.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Mon Jun 16 09:58:26 2003 UTC (23 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.1: +701 -446 lines
Entity resolving support

1 wakaba 1.1
2     =head1 NAME
3    
4     SuikaWiki::Markup::XML::Parser --- SuikaWiki: Simple XML parser
5    
6     =head1 DESCRIPTION
7    
8     This is a simple XML parser intended to be used with SuikaWiki::Markup::XML.
9     After parsing of the XML document, this module returns a SuikaWiki::Markup::XML
10     object so that you can handle XML document with that module (and other modules
11     implementing same interface).
12    
13     This module is part of SuikaWiki.
14    
15     =cut
16    
17     package SuikaWiki::Markup::XML::Parser;
18     use strict;
19 wakaba 1.2 our $VERSION = do{my @r=(q$Revision: 1.1 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
20     use Char::Class::XML qw!InXML_NameStartChar InXMLNameChar InXML_NCNameStartChar InXMLNCNameChar InXMLChar InXML_deprecated_noncharacter InXML_unicode_xml_not_suitable!;
21 wakaba 1.1 use SuikaWiki::Markup::XML;
22 wakaba 1.2 require SuikaWiki::Markup::XML::Error;
23     *_raise_error = \&SuikaWiki::Markup::XML::Error::raise;
24 wakaba 1.1
25     my %NS = (
26     SGML => 'urn:x-suika-fam-cx:markup:sgml:',
27     internal_ns_invalid => 'http://suika.fam.cx/~wakaba/-temp/2003/05/17/unknown-namespace#',
28     internal_attr_duplicate => 'http://suika.fam.cx/~wakaba/-temp/2003/05/17/invalid-attr#',
29     );
30    
31     =head1 METHODS
32    
33     WARNING: This module is under construction. Interface of this module is not yet fixed.
34    
35     =cut
36    
37     my %xml_re;
38     # [1] document = prolog element *Misc
39     # [2] Char = %x09 / %x0A / %x0D / U+0020-U+D7FF / U+E000-U+FFFD / U+10000-U+10FFFF ;; 1.0
40     # [2] Char = %x09 / %x0A / %x0D / %x20-7E / U+0085 / U+00A0-U+D7FF / U+E000-U+FFFD
41     # / U+10000-U+10FFFF ;; 1.1
42     # [3] s = 1*(%x20 / %x09 / %x0D / %x0A)
43     $xml_re{s} = qr/[\x09\x0D\x0A\x20]+/s;
44     # [4] NameChar = Letter / Digit / "." / "-" / "_" / ":" / CombiningChar / Extender ;; 1.0
45     # [4] NameStartChar = ":" / ALPHA / "_" / U+00C0-U+02FF / U+0370-U+037D
46     # / U+037F-U+1FFF / U+200C-U+200D / U+2070-U+218F
47     # / U+2C00-U+2FEF / U+3001-U+D7FF / U+F900-U+EFFFF ;; 1.1
48     # [4a] NameChar = NameStartChar / "-" / "." / DIGIT / U+00B7 / U+0300-U+036F
49     # / U+203F-U+2040 ;; 1.1
50     # $xml_re{NameChar} = qr/[A-Za-z0-9._:-]|[^\x00-\x7F]/;
51     $xml_re{NameChar} = qr/\p{InXMLNameChar}/;
52     # [5] Name = (Letter / "_" / ":") *NameChar ;; 1.0
53     # [5] Name = NameStartChar *NameChar ;; 1.1
54     # $xml_re{Name} = qr/(?:[A-Z_:]|[^\x00-\x7F])(?:$xml_re{NameChar})*/;
55     $xml_re{Name} = qr/\p{InXML_NameStartChar}\p{InXMLNameChar}*/;
56     # [6] Names = Name *(S Name) ;; 1.0 FE & 1.0 FE-errata (2000-09-27) & 1.0 SE
57     # [6] Names = Name *(%x20 Name) ;; 1.0 FE-errata (2000-04-09) & 1.0 SE-errata
58     $xml_re{Names} = qr/$xml_re{Name}(?:$xml_re{s}$xml_re{Name})*/s;
59     # [7] Nmtoken = 1*NameChar
60     #$xml_re{Nmtoken} = qr/(?:$xml_re{NameChar})+/;
61     $xml_re{Nmtoken} = qr/\p{InXMLNameChar}+/;
62     # [8] Nmtokens = Nmtoken *(S Nmtoken) ;; 1.0 FE & 1.0 FE-errata (2000-09-27) & 1.0 SE
63     # [8] Nmtokens = Nmtoken *(%x20 Nmtoken) ;; 1.0 FE-errata (2000-04-09) & 1.0 SE-errata
64     $xml_re{Nmtokens} = qr/$xml_re{Nmtoken}(?:$xml_re{s}$xml_re{Nmtoken})*/s;
65     # [9] EntityValue = <"> *((Char - ("%" / "&" / <">)) / PEReference / Reference) <">
66     # / "'" *((Char - ("%" / "&" / "'")) / PEReference / Reference) "'"
67     # [10] AttValue = <"> *((Char - ("<" / "&" / <">)) / Reference) <">
68     # / "'" *((Char - ("<" / "&" / "'")) / Reference) "'"
69     $xml_re{__AttValue_simple} = qr/"[^"]*"|'[^']*'/s;
70     # [11] SystemLiteral = <"> *(Char - <">) <"> / "'" *(Char - "'") "'"
71     $xml_re{SystemLiteral} = qr/"[^"]*"|'[^']*'/;
72     # [12] PublicLiteral = <"> *PubidChar <"> / "'" *(PubidChar - "'") "'"
73     # [13] PubidChar = %x20 / %x0D / %x0A / ALPHA / DIGIT / "-" / "'" / "(" / ")"
74     # / "+" / "," / "." / "/" / ":" / "=" / "?" / ";" / "!" / "*"
75     # / "#" / "@" / "$" / "_" / "%"
76     $xml_re{PubidChar} = qr[[\x0D\x0A\x20!\x24#%'()+*,./0-9:;=?\x40A-Z_a-z-]];
77     $xml_re{__non_PubidChar} = qr[[^\x0D\x0A\x20!\x24#%'()+*,./0-9:;=?\x40A-Z_a-z-]];
78     $xml_re{__PubidChar2} = qr[[\x0D\x0A\x20!\x24#%()+*,./0-9:;=?\x40A-Z_a-z-]];
79     $xml_re{PublicLiteral} = qr/"(?:$xml_re{PubidChar})*"|'(?:$xml_re{PubidChar2})*'/;
80     # [14] CharData = *(Char - ("<" / "&")) - (*(Char - ("<" / "&")) "]]>" *(Char - ("<" / "&")))
81     $xml_re{CharData} = qr/(?:(?!\]\]>)[^<&])*/s;
82     $xml_re{__CharDataP} = qr/(?:(?!\]\]>)[^<&])+/s;
83     # [15] Comment = "<!--" *((Char - "-") / ("-" (Char - "-")))) "-->"
84     $xml_re{Comment_M} = qr/<!--((?:(?!--).)*)-->/s;
85     # [16] PI = "<?" PITarget [S (*Char - (*Char "?>" *Char))] "?>"
86     $xml_re{PI_M} = qr/<\?($xml_re{Name})(?:$xml_re{s}((?:(?!\?>).)*))?\?>/s;
87     # [17] PITarget = Name - "xml"
88     # [18] CDSect = CDStart CData CDEnd
89     # [19] CDStart = '<![CDATA['
90     # [20] CDATA = (*Char - (*Char "]]>" *Char))
91     # [21] CDEnd = "]]>"
92     $xml_re{CDSect_M} = qr/<!\[CDATA\[((?:(?!\]\]>).)*)\]\]>/s;
93     # [22] prolog = [XMLDecl] *Misc [doctypedecl *Misc]
94     # [23] XMLDecl = '<?xml' VersionInfo [EncodingDecl] [SDDecl] [S] "?>"
95     # [24] VersionInfo = S 'version' Eq ("'" VersionNum "'" / <"> VersionNum <">)
96     # [25] Eq = [S] "=" [S]
97     # [26] VersionNum = 1*(ALPHA / DIGIT / "_" / "." / ":" / "-") ;; 1.0 FE & 1.0 SE
98     # [26] VersionNum = "1.0" ;; 1.0 SE-errata
99     # [26] VersionNum = "1.1" ;; 1.1
100     # [27] Misc = Comment / PI / S
101     # [28] doctypedecl = '<!DOCTYPE' S Name [S ExternalID] [S]
102     # ["[" *(markupdecl / PEReference / S) "]" [S]] ">" ;; 1.0 FE
103     # [28] doctypedecl = '<!DOCTYPE' S Name [S ExternalID] [S] ["[" *(markupdecl / DeclSep) "]" [S]] ">"
104     # ;; 1.0 FE-errata & 1.0 SE
105     # [28] doctypedecl = '<!DOCTYPE' S Name [S ExternalID] [S] ["[" intSubset "]" [S]] ">"
106     # ;; 1.0 SE-errata
107     $xml_re{__doctypedecl_start_simple_M} = qr/(<!DOCTYPE$xml_re{s})($xml_re{Name})($xml_re{s}SYSTEM$xml_re{s}$xml_re{__AttValue_simple}|$xml_re{s}PUBLIC$xml_re{s}$xml_re{__AttValue_simple}$xml_re{s}$xml_re{__AttValue_simple})?/s;
108     # [28a] DeclSep = PEReference / S ;; 1.0 FE-errata & 1.0 SE
109     # [28b] intSubset = *(markupdecl / DeclSep) ;; 1.0 SE-errata
110     # [29] markupdecl = elementdecl / AttlistDecl / EntityDecl / NotationDecl / PI / Comment
111     # [30] extSubset = [TextDecl] extSubsetDecl
112     # [31] extSubsetDecl = *(markupdecl / conditionalSect / PEReference / S) ;; 1.0 FE
113     # [31] extSubsetDecl = *(markupdecl / conditionalSect / DeclSep) ;; 1.0 FE-errata & 1.0 SE
114     # [32] SDDecl = S 'standalone' Eq ("'" ('yes' / 'no') "'" / <"> ('yes' / 'no') <">)
115     # [33] LanguageID = Langcode *("-" Subcode) ;; 1.0 FE (removed by 1.0 SE)
116     # [34] Langcode = ISO639Cocde / IanaCode / UserCode ;; 1.0 FE (ditto)
117     # [35] ISO639Code = 2ALPHA ;; 1.0 FE (ditto)
118     # [36] IanaCode = "i-" 1*ALPHA ;; 1.0 FE (ditto)
119     # [37] UserCode = "x-" 1*ALPHA ;; 1.0 FE (ditto)
120     # [38] Subcode = 1*ALPHA ;; 1.0 FE (ditto)
121     # [39] element = EmptyElemTag / STag content ETag
122     # [40] STag = "<" Name *(S Attribute) [S] ">"
123     # [41] Attribute = Name Eq AttValue
124     $xml_re{Attribute} = qr/$xml_re{Name}(?:$xml_re{s})?=(?:$xml_re{s})?$xml_re{__AttValue_simple}/s;
125     $xml_re{Attribute_M} = qr/($xml_re{Name})(?:$xml_re{s})?=(?:$xml_re{s})?($xml_re{__AttValue_simple})/s;
126     $xml_re{STag} = qr/<$xml_re{Name}(?:$xml_re{s}$xml_re{Attribute})*(?:$xml_re{s})?>/s;
127     # [42] ETag = "</" Name [S] ">"
128     $xml_re{ETag_M} = qr!</($xml_re{Name})(?:$xml_re{s})?>!s;
129     # [43] content = *(element / CharData / Reference / CDSect / PI / Comment) ;; 1.0 FE
130     # [43] content = [CharData] *((element / Reference / CDSect / PI / Comment) [CharData])
131     # ;; 1.0 FE-errata & 1.0 SE
132     # [44] EmptyElemTag = "<" Name *(S Attribute) [S] "/>"
133     $xml_re{__STag_or_EmptyElemTag} = qr!<$xml_re{Name}(?:$xml_re{s}$xml_re{Attribute})*(?:$xml_re{s})?/?>!s;
134     $xml_re{__STag_or_EmptyElemTag_simple} = qr!<$xml_re{ame}(?:$xml_re{s}|$xml_re{Name}|$xml_re{__AttValue_simple}|=)*/?>!s;
135     # [45] elementdecl = '<!ELEMENT' S Name S contentspec [S] ">"
136     # [46] contentspec = 'EMPTY' / 'ANY' / Mixed / children
137     $xml_re{__contentspec_simple} = qr/(?:$xml_re{Name}|\#PCDATA|[()|,?*+]|$xml_re{s})/s;
138     # [47] children = (choice / seq) ["?" / "*" / "+"]
139     # [48] cp = (Name / choice / seq) ["?" / "*" / "+"]
140     # [49] choice = "(" [S] cp *([S] "|" [S] cp) [S] ")" ;; 1.0 FE
141     # [49] choice = "(" [S] cp 1*([S] "|" [S] cp) [S] ")" ;; 1.0 FE-errata & 1.0 SE
142     # [50] seq = "(" [S] cp *([S] "," [S] cp) [S] ")"
143     # [51] Mixed = "(" [S] '#PCDATA' *([S] "|" [S] Name) [S] ")*"
144     # / "(" [S] '#PCDATA' [S] ")"
145     #$xml_re{seq} = qr/\($xml_re{cp}(?:(?:$xml_re{s})?,(?:$xml_re{s})?$xml_re{cp})*(?:$xml_re{s})?\)/;
146     #$xml_re{cp} = qr/(?:$xml_re{Name}|$xml_re{choice}|$xml_re{seq})[?*+]/;
147     #$xml_re{choice} = qr/\($xml_re{cp}(?:(?:$xml_re{s})?\|(?:$xml_re{s})?$xml_re{cp})+(?:$xml_re{s})?\)/;
148     #$xml_re{children} = qr/(?:$xml_re{choice}|$xml_re{seq})[?*+]/;
149     #$xml_re{Mixed} = qr/(?:\((?:$xml_re{s})?\#PCDATA(?:$xml_re{s})?(?:(?:$xml_re{s})?|(?:$xml_re{s})?$xml_re{Name})*(?:$xml_re{s})?\)|\((?:$xml_re{s})?\#PCDATA(?:$xml_re{s})?\))/;
150     #$xml_re{contentspec} = qr/(?:EMPTY|ANY|$xml_re{Mixed}|$xml_re{children})/;
151     # [52] AttlistDecl = '<!ATTLIST' S Name *AttDef [S] ">"
152     # [53] AttDef = S Name S AttType S DefaultDecl
153     # [54] AttType = StringType / TokenizedType / EnumeratedType
154     # [55] StringType = 'CDATA'
155     # [56] TokenizedType = 'ID' / 'IDREF' / 'IDREFS' / 'ENTITY' / 'ENTITIES' / 'NMTOKEN' / 'NMTOKENS'
156     # [57] EnumeratedType = NotationType / Enumeration
157     # [58] NotationType = 'NOTATION' S "(" [S] Name *([S] "|" [S] Name) [S] ")"
158     # [59] Enumeration = "(" [S] Nmtoken *([S] "|" [S] Nmtoken) [S} ")"
159     # [60] DefaultDecl = '#REQUIRED' / '#IMPLIED' / ['#FIXED' S] AttValue
160     # [61] conditionalSect = includeSect / ignoreSect
161     # [62] includeSect = "<![" [S} 'INCLUDE' [S] "[" extSubsetDecl "]]>"
162     # [63] ignoreSect = "<![" [S] 'IGNORE' [S] "[" *ignoreSectContents "]]>"
163     # [64] ignoreSectContents = Ignore *("<![" ignoreSectContents "]]>" Ignore)
164     # [65] Ignore = *Char - (*Char ("<![" / "]]>") *Char)
165     # [66] CharRef = '&#' 1*DIGIT ";" / '&#x' 1*HEXDIGIT ";"
166     $xml_re{CharRef} = qr/&#[0-9]+;|&#x[0-9A-Fa-f]+;/;
167     # [67] Reference = EntityRef / CharRef
168     # [68] EntityRef = "&" Name ";"
169     $xml_re{EntityRef} = qr/&$xml_re{Name};/;
170     $xml_re{EntityRef_M} = qr/&($xml_re{Name});/;
171     $xml_re{Reference} = qr/$xml_re{EntityRef}|$xml_re{CharRef}/;
172     $xml_re{AttValue} = qr/"(?:$xml_re{Reference}|[^&<"])*"|'(?:$xml_re{Reference}|[^&<'])*'/s;
173     # [69] PEReference = "%" Name ";"
174     $xml_re{PEReference} = qr/%(?:$xml_re{Name});/;
175     $xml_re{PEReference_M} = qr/%($xml_re{Name});/;
176     $xml_re{__elementdecl_simple} = qr/<!ELEMENT(?:$xml_re{s}|$xml_re{PEReference}|$xml_re{Name}|\#PCDATA|[()|,?*+])+>/s;
177     $xml_re{__AttlistDecl_simple} = qr/<!ATTLIST(?:$xml_re{PEReference}|$xml_re{Name}|\#$xml_re{Name}|$xml_re{s}|$xml_re{__AttValue_simple})*>/s;
178     # [70] EntityDecl = GEDecl / PEDecl
179 wakaba 1.2 $xml_re{__EntityDecl_simple} = qr/<!ENTITY(?:$xml_re{__AttValue_simple}|[^"'>])*>/s;
180 wakaba 1.1 # [71] GEDecl = '<!ENTITY' S Name S EntityDef [S] ">"
181     # [72] PEDecl = '<!ENTITY' S "%" S Name S PEDef [S] ">"
182     # [73] EntityDef = EntityValue / ExternalID [NDataDecl]
183     # [74] PEDef = EntityValue / ExternalID
184     # [75] ExternalID = 'SYSTEM' S SystemLiteral / 'PUBLIC' S PublicLiteral S SystemLiteral
185     # [76] NDataDecl = S 'NDATA' S Name
186     # [77] TextDecl = '?xml' [VersionInfo] EncodingDecl [S] "?>"
187     # [78] extParsedEnt = [TextDecl] content
188     # [79] extPE ;; 1.0 FE (removed by errata)
189     # [80] EncodingDecl = S 'encoding' Eq (<"> EncName <"> / "'" EncName "'")
190     # [81] EncName = ALPHA *(ALPHA / DIGIT / "." / "_" / "-")
191     # [82] NotationDecl = '<!NOTATION' S Name S (ExternalID / PublicID) [S] ">"
192 wakaba 1.2 $xml_re{__NotationDecl_simple} = qr/<!NOTATION(?:$xml_re{__AttValue_simple}|[^"'>])*>/s;
193 wakaba 1.1 # [83] PublicID = 'PUBLIC' S PubidLiteral
194     # [84] Letter = BaseChar / Ideographic
195     # [85] Basechar = ...
196     # [86] Ideographic = ...
197     # [87] CombiningChar = ...
198     # [88] Digit = ...
199     # [89] Extender = U+00B7 / U+02D0 / U+02D1 / U+0387 / U+0640 / U+0E46 / U+0EC6
200     # / U+3005 / U+3031-U+3035 / U+309D / U+309E / U+30FC-U+30FE
201     ## [84]-[89] removed by 1.1
202    
203     # [XMLNames]
204     # [1] NSAttName = PrefixedAttName / DefaultAttName
205     # [2] PrefixedAttName = 'xmlns:' NCName
206     # [3] DefaultAttName = 'xmlns'
207     # [4] NCName = (Letter / "_") *NCNameChar ;; 1.0
208     # [4] NCName = NCNameStartChar *NCNameChar ;; 1.1
209     # [5] NCNameChar = Letter / Digit / "." / "-" / "_" / CombiningChar / Extender ;; 1.0
210     # [5] NCNameChar = NameChar - ":" ;; 1.1
211     # [5a] NCNameStartChar = NameStartChar - ":" ;; 1.1
212     # [6] QName = [Prefix ":"] LocalPart ;; 1.0
213     # [6] QName = PrefixedName / UnprefixedName ;; 1.1
214     # [6a] PrefixedName = Prefix ":" LocalPart ;; 1.1
215     # [6b] UnprefixedName = LocalPart ;; 1.1
216     # [7] Prefix = NCName
217     # [8] LocalPart = NCName
218     # [9] STag = "<" QName *(S Attribute) [S] ">"
219     $xml_re{__NCSTag} = qr/<$xml_re{QName}(?:$xml_re{s}$xml_re{Attribute})*(?:$xml_re{s})?>/s;
220     # [10] ETag = "</" QName [S] ">"
221     $xml_re{__NCETag} = qr!</$xml_re{QName}(?:$xml_re{s})?>!s;
222     # [11] EmptyElemTag = "<" QName *(S Attribute) [S] "/>"
223     # [12] Attribute = NSAttName Eq AttValue / QName Eq AttValue
224     # [13] doctypedecl = '<!DOCTYPE' S QName [S ExternalID] [S]
225     # ["[" *(markupdecl / PEReference / S) "]" [S]] ">"
226     # [14] elementdecl = '<!ELEMENT' S QName S contentspec [S] ">"
227     # [15] cp = (QName / choice / sep) ["?" / "*" / "+"]
228     # [16] Mixed = "(" [S] '#PCDATA' *([S] "|" [S] QName) [S] ")*" / "(" [S] '#PCDATA' [S] ")"
229     # [17] AttlistDecl = '<!ATTLIST' S QName *AttDef [S] ">"
230     # [18] AttDef = S (QName / NSAttName) S AttType S DefaultDecl
231     # [19] Name = NameStartChar *NameChar ;; 1.1 draft
232     # [20] NameChar = {XML1.1}.NameChar ;; 1.1 draft
233     # [21] NameStartChar = {XML1.1}.NameStartChar ;; 1.1 draft
234    
235    
236     sub new ($) {
237     bless {}, shift;
238     }
239    
240     sub parse_text ($$;$) {
241     my $self = shift;
242     my $s = shift;
243     my $o = shift || {line => 0, pos => 0, entity_type => ''};
244     my $r = SuikaWiki::Markup::XML->new (type => '#document');
245     my $c = $r;
246 wakaba 1.2 ## NOTE: Even if there are more than one non-XML-Char, error is raised only one time.
247     $self->_warn_char_val ($self->_make_clone_of ($o), $s);
248     if ($s =~ s/^$xml_re{PI_M}//s && $1 eq 'xml') { # <?xml?>
249     my ($data, $all) = ($2, $&);
250     _count_lp ('<?xml', $o);
251     if (length ($data)) {
252     $self->_parse_xml_declaration ($c->append_new_node (type => '#pi', local_name => 'xml'), $data, $o);
253     } else {
254     $self->_raise_error ($o, type => 'SYNTAX_XML_DECLARE_NO_ATTR');
255     }
256     _count_lp ('?>', $o);
257 wakaba 1.1 }
258 wakaba 1.2 while ($s) {
259     if ($s =~ /^$xml_re{__STag_or_EmptyElemTag_simple}/s) { # <element/>
260     $o->{entity_type} ||= 'document_entity';
261     if ($o->{entity_type} eq 'document_entity'
262     || $o->{entity_type} eq 'external_general_parsed_entity') {
263     $self->_parse_element_content ($c, \$s, $o);
264     } else {
265     $self->_raise_error ($o, p => $c, type => 'SYNTAX_INVALID_POSITION', t => $&);
266 wakaba 1.1 }
267 wakaba 1.2 } elsif ($s =~ s/^$xml_re{s}//s) { # s
268     $c->append_text ($&);
269     _count_lp ($&, $o);
270     } elsif ($s =~ s/^$xml_re{__doctypedecl_start_simple_M}//s) { # <!DOCTYPE>
271     my ($d, $name, $extid) = ($1, $2, $3);
272     $o->{entity_type} ||= 'document_entity';
273     if ($o->{entity_type} ne 'document_entity') {
274     $self->_raise_error ($o, p => $c, type => 'SYNTAX_INVALID_POSITION', t => '<!DOCTYPE');
275 wakaba 1.1 }
276     my $D = $c->append_new_node (type => '#declaration', namespace_uri => $NS{SGML}.'doctype');
277     $D->flag (p_o_start => $self->_make_clone_of ($o));
278     $D->set_attribute (qname => $name);
279     _count_lp ($d.$name, $o);
280     if ($extid =~ s/^$xml_re{s}SYSTEM$xml_re{s}//s) {
281     $D->set_attribute (SYSTEM => substr ($extid, 1, length ($extid) - 2));
282     _count_lp ($extid, $o);
283     } elsif ($extid =~ s/^($xml_re{s}PUBLIC$xml_re{s})($xml_re{__AttValue_simple})//s) {
284     _count_lp ($1.'"', $o);
285     my $pubid = substr ($2, 1, length ($2) - 2);
286     if ($pubid =~ /^($xml_re{PubidChar})*($xml_re{__non_PubidChar})/s) {
287     _count_lp ($1, $o);
288     _raise_fatal_error ($o, desc => 'INVALID_PUBID_CHAR', t => $2);
289     } else {
290     $D->set_attribute (PUBLIC => $pubid);
291     _count_lp ($pubid.'"'.$extid, $o);
292     }
293     $extid =~ s/^$xml_re{s}//s;
294     $D->set_attribute (SYSTEM => substr ($extid, 1, length ($extid) - 2));
295     }
296     _count_lp ($1, $o) if $s =~ s/^($xml_re{s})//s;
297     if ($s =~ s/^\[//s) {
298     _count_lp ('[', $o);
299 wakaba 1.2 $self->_parse_dtd ($D, \$s, $o, return_with_dsc => 1);
300 wakaba 1.1 } elsif ($s =~ s/^>//s) {
301     _count_lp ('>', $o);
302     } else {
303     _raise_fatal_error ($o, desc => 'INVALID_DECLARE_SYNTAX', t => substr ($s, 0, 1));
304     substr ($s, 0, 1) = '';
305     }
306 wakaba 1.2 $self->_parse_element_content ($c, \$s, $o);
307     } elsif ($s =~ s/^$xml_re{PI_M}//s) { # <?pi?>
308     my ($target, $data, $all) = ($1, $2, $&);
309     if ($target eq 'xml') {
310 wakaba 1.1 _raise_fatal_error ($o, desc => 'INVALID_XML_DECLARE');
311     } else {
312 wakaba 1.2 $c->append_new_node (type => '#pi', local_name => $1, value => $2);
313     _count_lp ($all, $o);
314 wakaba 1.1 }
315 wakaba 1.2 } elsif ($s =~ s/^$xml_re{Comment_M}//s) { # <!-- -->
316     $c->append_new_node (type => '#comment', value => $1);
317     _count_lp ($&, $o);
318     } elsif ($s =~ /^<![A-Z]/) { # <!DECLARATION>
319     $o->{entity_type} ||= 'dtd_external_subset';
320     $self->_parse_dtd ($c, \$s, $o);
321     ## TODO: ext.general parsed entity
322 wakaba 1.1 } else {
323 wakaba 1.2 $self->_raise_error ($o, p => $c, type => 'SYNTAX_INVALID_CHAR', t => substr ($s, 0, 1));
324     substr ($s, 0, 1) = '';
325 wakaba 1.1 }
326     }
327     wantarray ? ($r, $o) : $r;
328     }
329    
330     sub _uri_escape ($$) {
331     shift;
332     my $s = shift; ## TODO: support utf8 flag
333     $s =~ s/([^0-9A-Za-z_.-])/sprintf '%%%02X', ord $1/ge;
334     $s;
335     }
336    
337 wakaba 1.2 sub _make_clone_of ($$;%) {
338     my ($self, $mother, %o) = @_;
339 wakaba 1.1 if (ref $mother eq 'HASH') {
340     my $child = {};
341 wakaba 1.2 $o{m_vs_c}->{$mother} = $child;
342 wakaba 1.1 for (keys %$mother) {
343 wakaba 1.2 if (ref $mother->{$_}) {
344     $child->{$_} = $o{m_vs_c}->{$mother->{$_}} || $self->_make_clone_of ($mother->{$_});
345     } else {
346     $child->{$_} = $mother->{$_};
347     }
348 wakaba 1.1 }
349     return $child;
350     } else {
351     ## BUG: not supported
352     }
353     }
354    
355     sub _count_lp ($$) {
356     my ($s, $o) = @_;
357     $s =~ s/[^\x0A\x0D]*(?:\x0D\x0A?|\x0A)/$o->{line}++;$o->{pos}=0;''/ges;
358     $o->{pos} += length $s;
359     }
360     sub _ns_parse_qname ($) {
361     my $qname = shift;
362     if ($qname =~ /:/) {
363     return split /:/, $qname, 2;
364     } else {
365     return (undef, $qname);
366     }
367     }
368 wakaba 1.2
369     sub _parse_element_content ($$\$$) {
370     my ($self, $c, $s, $o) = @_;
371     my $c_initial = overload::StrVal ($c);
372     my $entMan;
373     while ($$s) {
374     if ($$s =~ s/^($xml_re{__STag_or_EmptyElemTag_simple})//s) {
375     my ($stag) = ($1);
376     if ($c->node_type eq '#document'
377     && $self->_is_brother_of_root_element ($c)) {
378     $self->_raise_error ($o, p => $c, type => 'SYNTAX_DATA_OUT_OF_ROOT_ELEMENT', t => $stag);
379     }
380     ## Parse element type name
381     $stag =~ s/^<($xml_re{Name})//s;
382     my ($prefix, $lname) = _ns_parse_qname ($1); ## TODO: invalid qname valid name
383     $c = $c->append_new_node (local_name => $lname);
384     $c->flag (p_original_qname => $1);
385     $c->flag (p_o_start => $self->_make_clone_of ($o));
386     _count_lp ($&, $o);
387     ## Parse attributes
388     $stag =~ s/>$//;
389     my $etag = 0;
390     if ($stag =~ s!/$!!) { # empty element
391     $c->option (use_EmptyElemTag => 1);
392     $etag = 1;
393     }
394     $self->_parse_attribute_spec_list ($c, $stag, $o) if $stag;
395     _count_lp (($etag ? '/>' : '>'), $o);
396     ## Parse element type name (namespace)
397     my $uri = $c->defined_namespace_prefix ($prefix || '');
398     if (defined $uri) {
399     $c->namespace_uri ($uri);
400     } elsif (!$prefix) { ## Default NS
401     #$c->namespace_uri ('');
402     $c->define_new_namespace ('' => '');
403     } else {
404     my $o_etypename = $self->_make_clone_of ($c->flag ('p_o_start'));
405     $o_etypename->{pos}++; # '<'
406     $self->_raise_error ($o_etypename, type => 'NC_PREFIX_NOT_DEFINED', t => $prefix);
407     my $uri = $NS{internal_ns_invalid}.$self->_uri_escape ($prefix);
408     $c->namespace_uri ($uri);
409     $c->define_new_namespace ($prefix => $uri);
410     }
411     ## Ending the element if EmptyElemTag
412     if ($etag) {
413     $c = $c->{parent};
414     }
415     } elsif ($$s =~ s/^$xml_re{ETag_M}//s) {
416     my $ename = $1;
417     if ($ename eq $c->flag ('p_original_qname') || $ename eq $c->qname) {
418     $c = $c->{parent};
419     } else { ## Element type name does not match
420     my $o_etn = $self->_make_clone_of ($o);
421     $o_etn->{pos} += 2;
422     $self->_raise_error ($o_etn, p => $c, type => 'WFC_ELEMENT_TYPE_MATCH', t => [$ename, $c->qname]);
423     }
424     _count_lp ($&, $o);
425     } elsif (($c->node_type eq '#document') && ($$s =~ s/^$xml_re{s}//s)) {
426     $c->append_text ($&);
427     _count_lp ($&, $o);
428     } elsif ($$s =~ s/^$xml_re{__CharDataP}//s) {
429     if ($c->node_type eq '#document') {
430     $self->_raise_error ($o, p => $c, type => 'SYNTAX_DATA_OUT_OF_ROOT_ELEMENT', t => $&);
431     }
432     $c->append_text ($&);
433     _count_lp ($&, $o);
434     } elsif ($$s =~ s/^($xml_re{Reference})//s) { ## &foo; | &#1234; | &#x12AB;
435     if ($c->node_type eq '#document') {
436     if ($self->_is_brother_of_root_element ($c)) {
437     $self->_raise_error ($o, p => $c, type => 'SYNTAX_DATA_OUT_OF_ROOT_ELEMENT', t => $1);
438     }
439     }
440     my $entity_ref = $1;
441     my $eref = $self->_parse_reference ($c, $entity_ref, $o);
442     if ($eref->{namespace_uri} !~ 'char') {
443     $entMan ||= $c->_get_entity_manager;
444     my $entity = $entMan->get_entity ($eref, dont_use_predefined_entities => 1);
445     if (!$entity && {qw/&lt; 1 &gt; 1 &amp; 1 &quot; 1 &apos; 1/}->{$entity_ref}) {
446     $self->_raise_error ($o, c => $c, type => 'WARN_PREDEFINED_ENTITY_NOT_DECLARED',
447     t => $entity_ref);
448     $entity = $entMan->get_entity ($eref);
449     }
450     if (!$entity) {
451     $self->_raise_error ($o,
452     type => ($entMan->is_standalone_document_1?'WF':'V').'C_ENTITY_DECLARED',
453     t => $entity_ref);
454     } else {
455     my $o2 = $self->_make_clone_of ($o);
456     if ($o2->{__entities}->{$entity_ref}) {
457     $self->_raise_error ($o, type => 'WFC_NO_RECURSION', t => $entity_ref);
458     } else {
459     my $entity_value = $entity->get_attribute ('value');
460     ## TODO: support external entity
461     if (ref $entity_value) {
462     $o2->{__entities}->{$entity_ref} = 1;
463     $o2->{entity} = $entity_ref; $o2->{line} = 0; $o2->{pos} = 0;
464     my $ev = $entity_value->_entity_parameter_literal_value;
465     $self->_parse_element_content ($eref, \$ev, $o2);
466     $eref->flag (smxp__ref_expanded => 1);
467     } else { ## External non-parsed entity
468     $self->_raise_error ($o, type => 'WFC_NO_EXTERNAL_ENTITY_REFERENCE', t => $entity_ref);
469     }
470     }
471     }
472     } # if &foo;
473     } elsif ($$s =~ s/^$xml_re{CDSect_M}//s) {
474     if ($c->node_type eq '#document') {
475     if ($self->_is_brother_of_root_element ($c)) {
476     $self->_raise_error ($o, p => $c, type => 'SYNTAX_DATA_OUT_OF_ROOT_ELEMENT', t => '<![');
477     }
478     }
479     $c->append_new_node (type => '#section', namespace_uri => $NS{SGML}.'section:cdata', value => $1);
480     _count_lp ($&, $o);
481     } elsif ($$s =~ s/^$xml_re{Comment_M}//s) {
482     $c->append_new_node (type => '#comment', value => $1);
483     _count_lp ($&, $o);
484     } elsif ($$s =~ s/^$xml_re{PI_M}//s) {
485     my ($target, $data, $all) = ($1, $2, $&);
486     if ($target eq 'xml') {
487     if ($c->node_type eq '#document' && $c->count == 0) {
488     _count_lp ('<?xml', $o);
489     if (length ($data)) {
490     $self->_parse_xml_declaration ($c->append_new_node (type => '#pi', local_name => 'xml'), $data, $o);
491     } else {
492     $self->_raise_error ($o, type => 'SYNTAX_XML_DECLARE_NO_ATTR');
493     }
494     _count_lp ('?>', $o);
495     } else {
496     _raise_fatal_error ($o, desc => 'INVALID_XML_DECLARE');
497     }
498     } else {
499     $c->append_new_node (type => '#pi', local_name => $1, value => $2);
500     _count_lp ($all, $o);
501     }
502     } else {
503     $self->_raise_error ($o, p => $c, type => 'SYNTAX_INVALID_CHAR', t => substr ($$s, 0, 1));
504     substr ($$s, 0, 1) = '';
505     }
506     } # while $s
507     while ($c_initial ne overload::StrVal ($c)) {
508     if (ref $c->{parent}) {
509     if ($c->node_type eq '#element') {
510     $self->_raise_error ($o, type => 'SYNTAX_END_TAG_NOT_FOUND', t => $c);
511     }
512     $c = $c->{parent};
513     } else {
514     last;
515     }
516     }
517     }
518    
519     sub _parse_dtd ($$\$$;%) {
520     my ($self, $c, $s, $o, %opt) = @_;
521     my $c_initial = overload::StrVal ($c);
522     my $entMan;
523     while ($$s) {
524     if ($$s =~ s/^$xml_re{PEReference_M}//s) {
525     my $ename = $1;
526     if (index ($ename, ':') > -1) {
527     $self->_raise_error ($o, type => 'NS_SYNTAX_NAME_IS_NCNAME', t => '%'.$ename.';');
528     }
529     $entMan ||= $c->_get_entity_manager;
530     my $entity = $entMan->get_entity ($ename, namespace_uri => $NS{SGML}.'entity:parameter');
531     my $eref = $c->append_new_node (type => '#reference', local_name => $ename,
532     namespace_uri => $NS{SGML}.'entity:parameter');
533     if (!$entity) { ## TODO: if internal subset, flag
534     $self->_raise_error ($o, type => 'VC_ENTITY_DECLARED', t => '%'.$ename.';');
535     } else {
536     my $o2 = $self->_make_clone_of ($o);
537     if ($o2->{__entities}->{'%'.$ename.';'}) {
538     $self->_raise_error ($o, type => 'WFC_NO_RECURSION', t => '%'.$ename.';');
539     } else {
540     my $entity_value = $entity->get_attribute ('value');
541     ## TODO: support external entity
542     if (ref $entity_value) {
543     $o2->{__entities}->{'%'.$ename.';'} = 1;
544     $o2->{entity} = '%'.$ename.';'; $o2->{line} = 0; $o2->{pos} = 0;
545     my $ev = $entity_value->entity_value; $ev = substr ($ev, 1, length ($ev) - 2);
546     $self->_parse_dtd ($eref, \$ev, $o2);
547     $eref->flag (smxp__ref_expanded => 1);
548     } else {
549     $self->_raise_error ($o, type => 'UNKNOWN', t => '%'.$ename.';');
550     }
551     } # not recursive
552     } # entity defined
553     _count_lp ($ename.'%;', $o);
554     } elsif ($$s =~ s/^$xml_re{s}//s) {
555     $c->append_text ($&);
556     _count_lp ($&, $o);
557     } elsif ($$s =~ s/^$xml_re{Comment_M}//s) {
558     $c->append_new_node (type => '#comment', value => $1);
559     _count_lp ($&, $o);
560     } elsif ($$s =~ s/^($xml_re{__EntityDecl_simple})//s) {
561     $self->_parse_entity_declaration ($1, $c, $o);
562     } elsif ($$s =~ s/^($xml_re{__elementdecl_simple})//s) {
563     $self->_parse_element_declaration ($1, $c, $o);
564     } elsif ($$s =~ s/^($xml_re{__AttlistDecl_simple})//s) {
565     $self->_parse_attlist_declaration ($1, $c, $o);
566     } elsif ($opt{return_with_dsc} && $$s =~ s/^\](?:$xml_re{s})?>//s) {
567     _count_lp ($&, $o);
568     $c = $c->{parent};
569     return;
570     } elsif ($$s =~ s/^($xml_re{__NotationDecl_simple})//s) {
571     $self->_parse_entity_declaration ($1, $c, $o);
572     } elsif ($$s =~ s/^$xml_re{PI_M}//s) {
573     my ($target, $data, $all) = ($1, $2, $&);
574     if ($target eq 'xml') {
575     if ($c->node_type eq '#document' && $c->count == 0) {
576     _count_lp ('<?xml', $o);
577     if (length ($data)) {
578     $self->_parse_xml_declaration ($c->append_new_node (type => '#pi', local_name => 'xml'), $data, $o);
579     } else {
580     $self->_raise_error ($o, type => 'SYNTAX_XML_DECLARE_NO_ATTR');
581     }
582     _count_lp ('?>', $o);
583     } else {
584     _raise_fatal_error ($o, desc => 'INVALID_XML_DECLARE');
585     }
586     } else {
587     $c->append_new_node (type => '#pi', local_name => $1, value => $2);
588     _count_lp ($all, $o);
589     }
590     } else {
591     $self->_raise_error ($o, p => $c, type => 'SYNTAX_INVALID_CHAR', t => substr ($$s, 0, 1));
592     substr ($$s, 0, 1) = '';
593     ## TODO: markup section
594     }
595     } # while $$s
596     while ($c_initial ne overload::StrVal ($c)) {
597     if (ref $c->{parent}) {
598     $self->_raise_error ($o, type => 'SYNTAX_END_OF_MARKUP_NOT_FOUND', t => $c);
599     $c = $c->{parent};
600     } else {
601     last;
602     }
603     }
604     }
605    
606    
607 wakaba 1.1 sub _parse_attribute_spec_list ($$$$) {
608     my ($self, $c, $attrs, $o) = @_;
609     my @attrs;
610     my (%defined_attr, %defined_ns_attr);
611     my $no_s = 0;
612     while ($attrs) {
613     if (!$no_s && $attrs =~ s/^$xml_re{Attribute_M}//s) {
614     my ($qname, $qvalue) = ($1, $2);
615     my ($prefix, $name) = _ns_parse_qname ($qname);
616     push @attrs, {prefix => $prefix, lname => $name, qvalue => $qvalue, qname => $qname,
617     o => {line => $o->{line}, pos => ($o->{pos} + length ($&) - length ($qvalue))},
618     o_attr_start => {line => $o->{line}, pos => $o->{pos}}};
619     _count_lp ($&, $o);
620     $no_s = 1;
621     } elsif ($attrs =~ s/^($xml_re{s})//s) {
622     _count_lp ($1, $o);
623     $no_s = 0;
624     } else {
625     $self->_raise_error ($o, type => 'SYNTAX_INVALID_CHAR', t => substr ($attrs, 0, 1));
626     substr ($attrs, 0, 1) = '';
627     $no_s = 1;
628     }
629     }
630     for (grep {($_->{prefix} eq 'xmlns') || (!$_->{prefix} && ($_->{lname} eq 'xmlns'))} @attrs) {
631     my $value = SuikaWiki::Markup::XML->new (type => '#text');
632 wakaba 1.2 _count_lp ('"', $_->{o});
633     my $av = substr ($_->{qvalue}, 1, length ($_->{qvalue}) - 2);
634     $self->_parse_attribute_value ($value, \$av, $_->{o});
635     _count_lp ('"', $_->{o});
636 wakaba 1.1 if (!$defined_attr{$_->{qname}}) {
637     if ($_->{prefix} eq 'xmlns') {
638     $c->define_new_namespace ($_->{lname} => $value); # BUG: Reference
639     } else {
640     $c->define_new_namespace ('' => $value); # BUG: Reference
641     }
642     $defined_attr{$_->{qname}} = 1;
643     } else { ## Already defined
644     $self->_raise_error ($_->{o_attr_start}, type => 'WFC_UNIQUE_ATT_SPEC', t => $_->{qname});
645     my $attr;
646     if ($_->{prefix} eq 'xmlns') {
647     $attr = $c->set_attribute ($_->{lname} => '', namespace_uri => $NS{internal_attr_duplicate}.'xmlns');
648     } else { ## BUG: xmlns="" (1.1)
649     $attr = $c->set_attribute (xmlns => '', namespace_uri => $NS{internal_attr_duplicate});
650     }
651     $attr->append_node ($value);
652     }
653     }
654     for (grep {($_->{prefix} ne 'xmlns') && !(!$_->{prefix} && ($_->{lname} eq 'xmlns'))} @attrs) {
655     my $attr;
656     my $uri; $uri = $c->defined_namespace_prefix ($_->{prefix}) if $_->{prefix};
657     if ($defined_attr{$_->{qname}}) { ## Already-defined-attr is found
658     $self->_raise_error ($_->{o_attr_start}, type => 'WFC_UNIQUE_ATT_SPEC', t => $_->{qname});
659     $uri = $NS{internal_attr_duplicate} . $defined_attr{$_->{qname}};
660     $_->{prefix} = 'dup.' . $defined_attr{$_->{qname}} . ($_->{prefix} ? '.'.$_->{prefix}:'');
661     $c->define_new_namespace ($_->{prefix} => $uri);
662     $defined_attr{$_->{qname}}++;
663     } elsif (defined $uri && $defined_ns_attr{$_->{lname}.':'.$uri}) {
664     ## ns:attr="a" ns2:attr="b" xmlns:ns="ns" xmlns:ns2="ns"
665     $self->_raise_error ($_->{o_attr_start}, type => 'NC_unique_att_spec', t => $_->{qname});
666     my $i = $defined_ns_attr{$_->{lname}.':'.$uri}++;
667     $uri = $NS{internal_attr_duplicate} . 'ns.' . $i;
668     $_->{prefix} = 'dup.ns.' . $i . ($_->{prefix} ? '.'.$_->{prefix}:'');
669     $c->define_new_namespace ($_->{prefix} => $uri);
670     } else {
671     $defined_attr{$_->{qname}} = 1;
672     $defined_ns_attr{$_->{lname}.':'.$uri} = 1;
673     }
674     if ($_->{prefix}) {
675     if (defined $uri) {
676     $attr = $c->set_attribute ($_->{lname} => '', namespace_uri => $uri);
677     } else {
678     $self->_raise_error ($o, type => 'NC_PREFIX_NOT_DEFINED', t => $_->{prefix});
679     my $uri = $NS{internal_ns_invalid}.$self->_uri_escape ($_->{prefix});
680     $attr = $c->set_attribute ($_->{lname} => '', namespace_uri => $uri);
681     $c->define_new_namespace ($_->{prefix} => $uri);
682     }
683     } else {
684     $attr = $c->set_attribute ($_->{lname} => '');
685     }
686 wakaba 1.2 _count_lp ('"', $_->{o});
687     my $av = substr ($_->{qvalue}, 1, length ($_->{qvalue}) - 2);
688     $self->_parse_attribute_value ($attr, \$av, $_->{o}) if $attr;
689     _count_lp ('"', $_->{o});
690 wakaba 1.1 }
691     }
692 wakaba 1.2
693     sub _parse_attribute_value ($$\$$) {
694     my ($self, $attr, $s, $o) = @_;
695     my $entMan;
696     while ($$s) {
697     if ($$s =~ s/^($xml_re{Reference})//) {
698     my $entity_ref = $1;
699     my $eref = $self->_parse_reference ($attr, $entity_ref);
700     if ($eref->{namespace_uri} !~ 'char') { ## general entity ref.
701     $entMan ||= $attr->_get_entity_manager;
702     my $entity = $entMan->get_entity ($eref, dont_use_predefined_entities => 1);
703     if (!$entity && {qw/&lt; 1 &gt; 1 &amp; 1 &quot; 1 &apos; 1/}->{$entity_ref}) {
704     $self->_raise_error ($o, c => $attr, type => 'WARN_PREDEFINED_ENTITY_NOT_DECLARED',
705     t => $entity_ref);
706     $entity = $entMan->get_entity ($eref);
707     }
708     if (!$entity) {
709     $self->_raise_error ($o,
710     type => ($entMan->is_standalone_document_1?'WF':'V').'C_ENTITY_DECLARED',
711     t => $entity_ref);
712     } else {
713     my $o2 = $self->_make_clone_of ($o);
714     if ($o2->{__entities}->{$entity_ref}) {
715     $self->_raise_error ($o, type => 'WFC_NO_RECURSION', t => $entity_ref);
716     } else {
717     my $entity_value = $entity->get_attribute ('value');
718     if (ref $entity_value) {
719     $o2->{__entities}->{$entity_ref} = 1;
720     $o2->{entity} = $entity_ref; $o2->{line} = 0; $o2->{pos} = 0;
721     my $ev = $entity_value->_entity_parameter_literal_value;
722     $self->_parse_attribute_value ($eref, \$ev, $o2);
723     $eref->flag (smxp__ref_expanded => 1);
724     } else {
725     $self->_raise_error ($o, type => 'WFC_NO_EXTERNAL_ENTITY_REFERENCE', t => $entity_ref);
726     }
727     }
728     }
729     }
730     _count_lp ($entity_ref, $o);
731     } elsif ($$s =~ s/^([^&<]+)//) {
732 wakaba 1.1 $attr->append_text ($1);
733     _count_lp ($1, $o);
734 wakaba 1.2 } elsif ($$s =~ s/^<//) {
735     $self->_raise_error ($o, type => 'WFC_NO_LE_IN_ATTRIBUTE_VALUE');
736     substr ($$s, 0, 1) = '';
737 wakaba 1.1 } else {
738 wakaba 1.2 $self->_raise_error ($o, type => 'SYNTAX_INVALID_CHAR', t => substr ($$s, 0, 10));
739     substr ($$s, 0, 1) = '';
740 wakaba 1.1 }
741     }
742     }
743 wakaba 1.2
744     sub _parse_rpdata ($$\$$) {
745     my ($self, $c, $s, $o) = @_;
746     my $entMan;
747     while ($$s) {
748     if ($$s =~ s/^($xml_re{PEReference_M})//) {
749     my ($ref, $ename) = ($1, $2);
750     if ($o->{entity_type} eq 'document_entity') {
751     $self->_raise_error ($o, type => 'WFC_PE_IN_INTERNAL_SUBSET', t => $ref);
752     }
753     $entMan ||= $c->_get_entity_manager;
754     my $eref = $c->append_new_node (type => '#reference', local_name => $ename,
755     namespace_uri => $NS{SGML}.'entity:parameter');
756     my $entity = $entMan->get_entity ($ename, namespace_uri => $NS{SGML}.'entity:parameter');
757     if (!$entity) { ## TODO:
758     $self->_raise_error ($o, c => $c,
759     type => 'VC_ENTITY_DECLARED',
760     t => $ref);
761     } else {
762     my $o2 = $self->_make_clone_of ($o);
763     if ($o2->{__entities}->{$ref}) {
764     $self->_raise_error ($o, c => $c, type => 'WFC_NO_RECURSION', t => $ref);
765     } else {
766     my $entity_value = $entity->get_attribute ('value');
767     if (ref $entity_value) {
768     $o2->{__entities}->{$ref} = 1;
769     $o2->{entity} = $ref; $o2->{line} = 0; $o2->{pos} = 0;
770     my $ev = $entity_value->entity_value; $ev = substr ($ev, 1, length ($ev) - 2);
771     $self->_parse_rpdata ($eref, \$ev, $o2);
772     } else { ## External non-parsed entity ## TODO:
773     $self->_raise_error ($o, c => $eref, type => 'WFC_NO_EXTERNAL_ENTITY_REFERENCE', t => $ref);
774     }
775     }
776 wakaba 1.1 }
777     _count_lp ($1.'%;', $o);
778 wakaba 1.2 } elsif ($$s =~ s/^([^%]+)//) {
779     my $t = $1;
780     if ($t =~ /&(?!(?:$xml_re{Name}|#(?:x[0-9A-Fa-f]+|[0-9]+));)/) {
781     $self->_raise_error ($o, type => 'SYNTAX_INVALID_CHAR', c => $c, t => '&');
782     }
783     $t =~ s{&#(?:x([0-9A-Fa-f]+)|([0-9]+));}{
784     my $char = chr ($1 ? hex $1 : 0+$2);
785     $self->_warn_char_val ($o, $char, ref => 1);
786     $char;
787     }ge;
788     $c->append_new_node (type => '#text', value => $t);
789     _count_lp ($t, $o);
790 wakaba 1.1 } else {
791 wakaba 1.2 $self->_raise_error ($o, type => 'SYNTAX_INVALID_CHAR', t => substr ($$s, 0, 1));
792     substr ($$s, 0, 1) = '';
793     _count_lp (' ', $o);
794 wakaba 1.1 }
795     }
796     }
797 wakaba 1.2
798     sub _parse_markup_declaration_parameters ($$$$$) {
799     my ($self, $c, $s, $o, $params) = @_;
800     my $entMan;
801     while ($$s) {
802     if ($$s =~ s/^$xml_re{PEReference_M}//) {
803     my $ename = $1;
804     if ($o->{entity_type} eq 'document_entity') { ## Internal Subset
805     $self->_raise_error ($o, type => 'WFC_PE_IN_INTERNAL_SUBSET', t => $ename);
806 wakaba 1.1 }
807 wakaba 1.2 if (index ($ename, ':') > -1) {
808     $self->_raise_error ($o, type => 'NS_SYNTAX_NAME_IS_NCNAME', t => '%'.$ename.';');
809     }
810     $entMan ||= $c->_get_entity_manager;
811     my $entity = $entMan->get_entity ($ename, namespace_uri => $NS{SGML}.'entity:parameter');
812     my $eref = $c->append_new_node (type => '#reference', local_name => $ename,
813     namespace_uri => $NS{SGML}.'entity:parameter');
814     if (!$entity) {
815     $self->_raise_error ($o, type => 'VC_ENTITY_DECLARED', t => '%'.$ename.';');
816     } else {
817     my $o2 = $self->_make_clone_of ($o);
818     if ($o2->{__entities}->{'%'.$ename.';'}) {
819     $self->_raise_error ($o, type => 'WFC_NO_RECURSION', t => '%'.$ename.';');
820     } else {
821     my $entity_value = $entity->get_attribute ('value');
822     ## TODO: support external entity
823     if (ref $entity_value) {
824     $o2->{__entities}->{'%'.$ename.';'} = 1;
825     $o2->{entity} = '%'.$ename.';'; $o2->{line} = 0; $o2->{pos} = 0;
826     push @$params, $c->append_text (' ');
827     $$params[$#$params]->flag (smxp__pmdp_type => 'S');
828     my $ev = $entity_value->_entity_parameter_literal_value;
829     my $o22 = $self->_make_clone_of ($o);
830     $$params[$#$params]->flag (smxp__src_pos => $o22);
831     $self->_parse_markup_declaration_parameters ($eref, \$ev, $o2, $params);
832     push @$params, $c->append_text (' ');
833     $$params[$#$params]->flag (smxp__pmdp_type => 'S');
834     $$params[$#$params]->flag (smxp__src_pos => $o22);
835     $eref->flag (smxp__ref_expanded => 1);
836     } else {
837     $self->_raise_error ($o, type => 'UNKNOWN', t => '%'.$ename.';');
838     }
839     } # not recursive
840     } # entity defined
841     $c->flag (smxp__defined_with_param_ref => 1);
842     _count_lp ($ename.'%;', $o);
843     } elsif ($$s =~ s/^($xml_re{__AttValue_simple})//s) {
844     my $all = $1;
845     $c->append_new_node (type => '#xml', value => substr ($all, 0, 1)); # lit(a)
846     push @$params, $c->append_new_node (type => '#xml', value => substr ($1, 1, length ($all) - 2));
847     $$params[$#$params]->flag (smxp__pmdp_type => 'literal');
848     $$params[$#$params]->flag (smxp__src_pos => $self->_make_clone_of ($o));
849     $c->append_new_node (type => '#xml', value => substr ($all, -1, 1)); # lit(a)
850     _count_lp ($all, $o);
851     } elsif ($$s =~ s/^($xml_re{Name})//) {
852     push @$params, $c->append_text ($1);
853     $$params[$#$params]->flag (smxp__pmdp_type => 'Name');
854     $$params[$#$params]->flag (smxp__src_pos => $self->_make_clone_of ($o));
855 wakaba 1.1 _count_lp ($1, $o);
856 wakaba 1.2 } elsif ($$s =~ s/^([%#(),|+?])//) {
857     push @$params, $c->append_text ($1);
858     $$params[$#$params]->flag (smxp__pmdp_type => 'delimiter');
859     $$params[$#$params]->flag (smxp__src_pos => $self->_make_clone_of ($o));
860 wakaba 1.1 _count_lp ($1, $o);
861 wakaba 1.2 } elsif ($$s =~ s/^($xml_re{s})//s) {
862     push @$params, $c->append_text ($1);
863     $$params[$#$params]->flag (smxp__pmdp_type => 'S');
864     $$params[$#$params]->flag (smxp__src_pos => $self->_make_clone_of ($o));
865 wakaba 1.1 _count_lp ($1, $o);
866     } else {
867 wakaba 1.2 $self->_raise_error ($o, type => 'SYNTAX_INVALID_CHAR', t => substr ($$s, 0, 10));
868     substr ($$s, 0, 1) = '';
869 wakaba 1.1 }
870 wakaba 1.2 } # while
871     }
872    
873     sub _warn_char_val ($$$%) {
874     my ($self, $o, $ch, %o) = @_;
875     if ($ch =~ /(\P{InXMLChar})/) {
876     _count_lp ($`, $o);
877     $self->_raise_error ($o, type => (($o{ref}?'WFC':'SYNTAX').'_LEGAL_CHARACTER'), t => ord $1);
878     } elsif ($ch =~ /(\p{InXML_deprecated_noncharacter})/) {
879     _count_lp ($`, $o);
880     $self->_raise_error ($o, type => 'WARN_UCS_NONCHARACTER', t => ord $1);
881     } elsif ($ch =~ /(\p{Compat})/) {
882     _count_lp ($`, $o);
883     $self->_raise_error ($o, type => 'WARN_UNICODE_COMPAT_CHARACTER', t => ord $1);
884     } elsif ($ch =~ /(\p{InXML_unicode_xml_not_suitable})/) {
885     _count_lp ($`, $o);
886     $self->_raise_error ($o, type => 'WARN_UNICODE_XML_NOT_SUITABLE_CHARACTER', t => ord $1);
887 wakaba 1.1 }
888     }
889     sub _parse_reference ($$$$) {
890     my ($self, $c, $ref, $o) = @_;
891 wakaba 1.2 my $r;
892 wakaba 1.1 if ($ref =~ /$xml_re{EntityRef_M}/) { ## BUG: QName
893 wakaba 1.2 $r = $c->append_new_node (type => '#reference', local_name => $1,
894     namespace_uri => $NS{SGML}.'entity');
895 wakaba 1.1 } elsif ($ref =~ /x([0-9A-Fa-f]+)/) {
896     my $ch = hex $1;
897 wakaba 1.2 $self->_warn_char_val ($o, chr $ch, ref => 1);
898     $r = $c->append_new_node (type => '#reference', value => $ch,
899     namespace_uri => 'urn:x-suika-fam-cx:markup:sgml:char:ref:hex');
900 wakaba 1.1 } elsif ($ref =~ /([0-9]+)/) {
901 wakaba 1.2 my $ch = 0+$1;
902     $self->_warn_char_val ($o, chr $ch, ref => 1);
903     $r = $c->append_new_node (type => '#reference', value => $ch,
904     namespace_uri => 'urn:x-suika-fam-cx:markup:sgml:char:ref');
905 wakaba 1.1 } else {
906     $self->_raise_error ($o, type => 'UNKNOWN', t => $ref);
907     }
908     _count_lp ($ref, $o);
909 wakaba 1.2 $r;
910 wakaba 1.1 }
911     sub _parse_xml_declaration ($$$$) {
912     my ($self, $c, $attrs, $o) = @_;
913     my $stage = 0; # 0: <?xml, 1: version="", 2: encoding="", 3: standalone="", 4: ?>
914     $attrs = ' ' . $attrs;
915     while ($attrs) {
916     if ($attrs =~ s/^$xml_re{s}version(?:$xml_re{s})?=(?:$xml_re{s})?("[A-Za-z0-9_.:-]+"|'[A-Za-z0-9_.:-]+')//s) {
917     my $version = substr ($1, 1, length ($1) - 2);
918     if ($stage > 0) {
919     $self->_raise_error ($o, c => $attrs, type => 'SYNTAX_XML_DECLARE', t => 'version');
920     }
921     if ($version eq '1.0') {
922     $c->set_attribute (version => $version);
923     } else {
924     _raise_fatal_error ($o, desc => 'UNSUPPORTED_XML_VERSION', t => $version);
925     }
926     _count_lp ($&, $o); $stage++;
927     } elsif ($attrs =~ s/^$xml_re{s}encoding(?:$xml_re{s})?=(?:$xml_re{s})?("[A-Za-z0-9_.-]+"|'[A-Za-z0-9_.:-]+')//s) {
928     if ($stage > 2) {
929     $self->_raise_error ($o, c => $attrs, type => 'SYNTAX_XML_DECLARE', t => 'encoding');
930     } elsif ($stage == 0) { ## No version pseudo-attr
931     if ($o->{entity_type} eq 'document_entity') {
932     $self->_raise_error ($o, c => $attrs, type => 'SYNTAX_XML_DECLARE_NO_ATTR', t => 'version');
933     $c->set_attribute (version => '1.0');
934     } else {
935     $o->{entity_type} = 'external_parsed_entity';
936     }
937     }
938     $c->set_attribute (encoding => substr ($1, 1, length ($1) - 2));
939     _count_lp ($&, $o); $stage = 2;
940     } elsif ($attrs =~ s/^$xml_re{s}standalone(?:$xml_re{s})?=(?:$xml_re{s})?("(?:yes|no)"|'(?:yes|no)')//s) {
941     if ($stage == 0 || $stage > 3 || $o->{entity_type} eq 'external_parsed_entity'
942     || $o->{entity_type} eq 'dtd_external_subset'
943     || $o->{entity_type} eq 'general_external_parsed_entity') {
944     $self->_raise_error ($o, c => $attrs, type => 'SYNTAX_XML_DECLARE_NO_ATTR', t => 'standalone');
945     }
946     $c->set_attribute (standalone => (substr ($1, 1, 1) eq 'y' ? 'yes' : 'no'));
947     _count_lp ($&, $o); $stage = 3;
948     } elsif ($attrs =~ s/^($xml_re{s})//s) {
949     my $s = $1;
950     if ($stage == 0) {
951     $self->_raise_error ($o, c => $attrs, type => 'SYNTAX_XML_DECLARE_NO_ATTR');
952     $c->set_attribute (version => '1.0');
953     }
954     _count_lp ($s, $o); $stage = 4;
955     } else {
956     $self->_raise_error ($o, c => $attrs, type => 'SYNTAX_XML_DECLARE', t => $attrs);
957     _count_lp ($attrs, $o); undef $attrs;
958     }
959     } # while
960     if ($stage == 0) {
961     $self->_raise_error ($o, c => $attrs, type => 'SYNTAX_XML_DECLARE_NO_ATTR');
962     $c->set_attribute (version => '1.0');
963     }
964     }
965     sub _parse_entity_declaration ($$$$) {
966     my ($self, $all, $c, $o) = @_;
967 wakaba 1.2 my $p; ## notation ? 'n' : parameter entity ? '%' : undef;
968     my $entMan;
969     my $e = $c->append_new_node (type => '#declaration');
970 wakaba 1.1 ## Entity/notation type
971 wakaba 1.2 if ($all =~ s/^<!ENTITY//s) {
972     $e->namespace_uri ($NS{SGML}.'entity');
973 wakaba 1.1 _count_lp ($&, $o);
974 wakaba 1.2 } elsif ($all =~ s/^<!NOTATION//s) {
975     $e->namespace_uri ($NS{SGML}.'notation');
976 wakaba 1.1 _count_lp ($&, $o); $p = 'n';
977     }
978 wakaba 1.2 substr ($all, -1) = ''; # mdc (>)
979     my @params;
980     $self->_parse_markup_declaration_parameters ($e, \$all, $o, \@params);
981     push @params, ref ($e)->new (type => '#text'); ## dummy
982     shift @params while ($params[0]->flag ('smxp__pmdp_type') eq 'S');
983 wakaba 1.1 ## Entity/notation name
984 wakaba 1.2 if ($p ne 'n' && $params[0]->inner_text eq '%') { ## parameter entity
985     shift @params; # '%'
986     $e->namespace_uri ($NS{SGML}.'entity:parameter'); $p = '%';
987     if ($params[0]->flag ('smxp__pmdp_type') eq 'S') {
988     shift @params while ($params[0]->flag ('smxp__pmdp_type') eq 'S');
989     } else { ## <!ENTITY %foo...>
990     my $part = $params[0];
991     $self->_raise_error (($part->flag ('smxp__src_pos')||$o), c => $e,
992     type => 'SYNTAX_INVALID_MD', t => ($part->inner_text||'>'));
993     }
994     }
995     if ($params[0]->flag ('smxp__pmdp_type') ne 'Name') {
996     $self->_raise_error ((shift (@params)->flag ('smxp__src_pos') || $o), c => $e,
997     type => 'SYNTAX_MD_NAME_NOT_FOUND');
998     } else { ## Name exist
999     my $ename = shift (@params)->inner_text;
1000     $entMan ||= $c->_get_entity_manager;
1001     if ($entMan->get_entity ($ename, namespace_uri => $e->namespace_uri,
1002     dont_use_predefined_entities => 1)) {
1003     if ($p eq 'n') {
1004     $self->_raise_error ($o, c => $e, type => 'VC_UNIQUE_NOTATION_NAME', t => $ename);
1005     } else {
1006     $self->_raise_error ($o, c => $e, type => 'WARN_UNIQUE_'
1007     .($p eq '%' ? 'PARAMETER_' : '')
1008     .'ENTITY_NAME', t => $ename);
1009     }
1010     }
1011     $e->local_name ($ename);
1012     if ($params[0]->flag ('smxp__pmdp_type') eq 'S') {
1013     shift @params while ($params[0]->flag ('smxp__pmdp_type') eq 'S');
1014     my ($extid_type_obj, $extid_type);
1015     if ($params[0]->flag ('smxp__pmdp_type') eq 'Name') { # PUBLIC or SYSTEM
1016     $extid_type_obj = shift (@params);
1017     $extid_type = $extid_type_obj->inner_text;
1018     if ($extid_type ne 'PUBLIC' && $extid_type ne 'SYSTEM') {
1019     $self->_raise_error ($extid_type_obj->flag ('smxp__src_pos'), c => $e,
1020     type => 'SYNTAX_INVALID_KEYWORD', t => $extid_type);
1021     undef $extid_type;
1022     }
1023     if ($params[0]->flag ('smxp__pmdp_type') eq 'S') {
1024     shift @params while ($params[0]->flag ('smxp__pmdp_type') eq 'S');
1025     } else { # <!ENTITY PUBLIC>
1026     $self->_raise_error ($extid_type_obj->flag ('smxp__src_pos'), c => $e,
1027     type => 'SYNTAX_INVALID_KEYWORD', t => $extid_type);
1028     }
1029     }
1030     if ($params[0]->flag ('smxp__pmdp_type') eq 'literal') {
1031     if ($extid_type eq 'SYSTEM') { ## System ID
1032     my $sysid = shift (@params)->inner_text;
1033     $e->set_attribute (SYSTEM => $sysid);
1034     } elsif ($extid_type eq 'PUBLIC') { ## Public ID
1035     my $pubid = shift (@params)->inner_text;
1036     $e->set_attribute (PUBLIC => $pubid);
1037     } elsif ($p ne 'n') { ## EntityValue (ENTITY only)
1038     my $vv = shift (@params)->inner_text;
1039     my $v = $e->set_attribute ('value');
1040     $self->_parse_rpdata ($v, \$vv, $o);
1041     } else {
1042     $self->_raise_error (shift (@params)->flag ('smxp__src_pos'), c => $e,
1043     type => 'SYNTAX_INVALID_LITERAL');
1044     }
1045     } # literal 1
1046     if ($params[0]->flag ('smxp__pmdp_type') eq 'S') {
1047     shift @params while ($params[0]->flag ('smxp__pmdp_type') eq 'S');
1048     my $s_exist = 1;
1049     if ($params[0]->flag ('smxp__pmdp_type') eq 'literal') {
1050     $s_exist = 0;
1051     if ($extid_type eq 'PUBLIC') { ## System ID following Public ID
1052     my $pubid = shift (@params)->inner_text;
1053     $e->set_attribute (SYSTEM => $pubid);
1054     } else {
1055     my $param = shift @params;
1056     $self->_raise_error ($param->flag ('smxp__src_pos'), c => $e,
1057     type => 'SYNTAX_INVALID_LITERAL', t => $param);
1058     }
1059     if ($p ne 'n' && $params[0]->flag ('smxp__pmdp_type') eq 'S') {
1060     shift @params while ($params[0]->flag ('smxp__pmdp_type') eq 'S');
1061     $s_exist = 1;
1062     }
1063     } elsif ($p ne 'n' && $extid_type eq 'PUBLIC') { ## System ID required
1064     $self->_raise_error (($params[0]->flag ('smxp__src_pos')||$o), c => $e,
1065     type => 'SYNTAX_MD_SYSID_NOT_FOUND');
1066     $e->set_attribute (SYSTEM => 'http://system.identifier.invalid/');
1067     }
1068     ## NDATA (General entity only)
1069     if ($params[0]->flag ('smxp__pmdp_type') eq 'Name' && $params[0]->inner_text eq 'NDATA') {
1070     if ($p eq '%') { ## parameter entity
1071     $self->_raise_error (shift (@params)->flag ('smxp__src_pos'), c => $e,
1072     type => 'SYNTAX_PE_NDATA');
1073     } elsif (!$extid_type) { ## internal entity
1074     $self->_raise_error (shift (@params)->flag ('smxp__src_pos'), c => $e,
1075     type => 'SYNTAX_INVALID_KEYWORD', t => 'NDATA');
1076     } else {
1077     shift @params; # 'NDATA'
1078     }
1079     if ($p ne 'n' && $params[0]->flag ('smxp__pmdp_type') eq 'S') {
1080     shift @params while ($params[0]->flag ('smxp__pmdp_type') eq 'S');
1081     } else { # <!ENTITY...NDATA> or <!ENTITY...NDATA"foo">, etc.
1082     my $part = shift (@params);
1083     $self->_raise_error ($part->flag ('smxp__src_pos'), c => $e,
1084     type => 'SYNTAX_INVALID_MD', t => ($part->inner_text||'>'));
1085     }
1086     ## Notation name
1087     if ($p ne 'n' && $params[0]->flag ('smxp__pmdp_type') eq 'Name') {
1088     my $part = shift (@params);
1089     my $nname = $part->inner_text;
1090     $entMan ||= $c->_get_entity_manager;
1091     my $notation = $entMan->get_entity ($nname, namespace_uri => $NS{SGML}.'notation');
1092     if (!$notation) {
1093     ## TODO: is notation declared before ENTITY is declared using it? Or, before any
1094     ## REFERENCE to it is occured?
1095     $self->_raise_error ($part->flag ('smxp__src_pos'), type => 'VC_NOTATION_DECLARED',
1096     t => $nname, c => $e);
1097     }
1098     $e->set_attribute (NDATA => $nname);
1099     } # Notation Name
1100     } # NDATA
1101     } else { # no literal 2 nor NDATA
1102     if ($p ne 'n' && $extid_type eq 'PUBLIC') { ## System ID required
1103     $self->_raise_error (shift (@params)->flag ('smxp__src_pos'), c => $e,
1104     type => 'SYNTAX_MD_SYSID_NOT_FOUND');
1105     $e->set_attribute (SYSTEM => 'http://system.identifier.invalid/');
1106     }
1107     } # no literal 2
1108     } else { # <!ENTITY name>
1109     my $part = shift (@params);
1110     $self->_raise_error ($part->flag ('smxp__src_pos'), c => $e,
1111     type => 'SYNTAX_INVALID_MD', t => ($part->inner_text||'>'));
1112     }
1113     } # Name exist
1114     while (@params) {
1115     my $param = shift (@params);
1116     last unless $param->flag ('smxp__pmdp_type'); ## dummy parameter
1117     if ($param->flag ('smxp__pmdp_type') ne 'S') {
1118     $self->_raise_error ($param->flag ('smxp__src_pos'), c => $e,
1119     type => 'SYNTAX_INVALID_MD', t => $param->inner_text);
1120     }
1121 wakaba 1.1 }
1122 wakaba 1.2 if ($p ne 'n') {
1123    
1124 wakaba 1.1 }
1125 wakaba 1.2 _count_lp ($all.'>', $o);
1126     }
1127    
1128     # example: $self->_validate_entity_declaration ($o, $entDec, '&'.$ename.';', {check_general => 1});
1129     sub _validate_entity_declaration ($$$$$) {
1130     my ($self, $o, $c, $s, $opt) = @_;
1131     my $entMan;
1132     while ($s) {
1133     if ($s =~ s/^($xml_re{EntityRef_M}|$xml_re{PEReference_M})//) {
1134     my $ref = $1;
1135     my $entname = $2 || $3;
1136     do {_count_lp ($ref, $o); next} if !$opt->{check_general} && $2;
1137     $opt->{check_general}--;
1138     if (index ($entname, ':') > -1) {
1139     ## TODO:
1140     }
1141     $entMan ||= $c->_get_entity_manager;
1142     my $entity = $entMan->get_entity ($entname, dont_use_predefined_entities => 1,
1143     namespace_uri => $NS{SGML}.'entity'.($2?'':':parameter'));
1144     if (!$entity) {
1145     if ($2 && {qw/lt 1 gt 1 amp 1 quot 1 apos 1/}->{$entname}) {
1146     $self->_raise_error ($o, c => $c, type => 'WARN_PREDEFINED_ENTITY_NOT_DECLARED',
1147     t => $entname);
1148     } else {
1149     $self->_raise_error ($o, c => $c,
1150     type => ($entMan->is_standalone_document_1?'WF':'V').'C_ENTITY_DECLARED',
1151     t => $ref);
1152     }
1153     } else {
1154     do {_count_lp ($ref, $o); next} if $entity->flag ('smxp__ved_validated');
1155     $entity->flag (smxp__ved_validated => 1);
1156     my $o2 = $self->_make_clone_of ($o);
1157     if ($o2->{__entities}->{$ref}) {
1158     $self->_raise_error ($o, c => $c, type => 'WFC_NO_RECURSION', t => $ref);
1159     } else {
1160     my $entity_value = $entity->get_attribute ('value');
1161     ## TODO: support external entity
1162     if (ref $entity_value) {
1163     $o2->{__entities}->{$ref} = 1;
1164     $o2->{entity} = $ref; $o2->{line} = 0; $o2->{pos} = 0;
1165     my $ev = $entity_value->entity_value; $ev = substr ($ev, 1, length ($ev) - 2);
1166     $self->_validate_entity_declaration ($o2, $entity, $ev, $opt);
1167     } else { ## External non-parsed entity
1168     ## TODO:
1169     $self->_raise_error ($o, c => $c, type => 'WFC_NO_EXTERNAL_ENTITY_REFERENCE', t => $ref);
1170     }
1171     }
1172     }
1173     _count_lp ($ref, $o);
1174     } elsif ($s =~ s/^&#(?:x([0-9A-Fa-f]+)|([0-9]+));//) {
1175     my $ch = ($1 ? hex $1 : 0+$2);
1176     $self->_warn_char_val ($o, chr $ch, ref => 1);
1177 wakaba 1.1 _count_lp ($&, $o);
1178 wakaba 1.2 } elsif ($s =~ s/([^&%]+)//s) {
1179     _count_lp ($1, $o);
1180     } elsif ($s =~ s/([&%])//s) { ## TODO:
1181     #$self->_raise_error ($o, type => 'SYNTAX_INVALID_CHAR', t => $1, c => $c);
1182 wakaba 1.1 _count_lp ($1, $o);
1183     }
1184 wakaba 1.2 }
1185 wakaba 1.1 }
1186    
1187     sub _parse_element_declaration ($$$$) {
1188     my ($self, $all, $c, $o) = (@_);
1189     my $e = undef;
1190     $e = $c->append_new_node (type => '#declaration',
1191     namespace_uri => 'urn:x-suika-fam-cx:markup:sgml:element');
1192     $all =~ s/^<!ELEMENT//s;
1193     _count_lp ('<!ELEMENT', $o);
1194     ## Element type name
1195     if ($all =~ s/^$xml_re{s}($xml_re{Name})//s) {
1196     $e->set_attribute (qname => $1);
1197     _count_lp ($&, $o);
1198     }
1199     ## contentspec / PEReference
1200     if ($all =~ s/^(?:$xml_re{s})?(?:$xml_re{PEReference}|$xml_re{Name}|\#PCDATA|\()(?:$xml_re{s}|$xml_re{PEReference}|$xml_re{Name}|\#PCDATA|[()|,+*?])*//s) {
1201     $e->append_new_node (type => '#xml', value => $&); # TODO: temporary
1202     _count_lp ($&, $o);
1203     }
1204     if ($all =~ s/^((?:$xml_re{s})?>)$//s) {
1205     _count_lp ($1, $o);
1206     } else {
1207     _raise_fatal_error ($o, desc => 'INVALID_DECLARE_SYNTAX', t => $all);
1208     }
1209     }
1210    
1211    
1212     sub _parse_attlist_declaration ($$$$) {
1213     my ($self, $all, $c, $o) = (@_);
1214     my $e = undef;
1215     $e = $c->append_new_node (type => '#declaration', local_name => 'ATTLIST');
1216     $all =~ s/^<!ATTLIST//s;
1217     _count_lp ('<!ATTLIST', $o);
1218     ## Element type name
1219     if ($all =~ s/^$xml_re{s}($xml_re{Name})//s) {
1220     $e->target_name ($1);
1221     _count_lp ($&, $o);
1222     }
1223     ## Definition
1224     if ($all =~ s/^(?:$xml_re{PEReference}|$xml_re{Name}|\#$xml_re{Name}|$xml_re{s}|$xml_re{__AttValue_simple})+//s) {
1225     $e->append_new_node (type => '#xml', value => $&); # TODO: temporary
1226     _count_lp ($&, $o);
1227     }
1228     if ($all =~ s/^((?:$xml_re{s})?>)$//s) {
1229     _count_lp ($1, $o);
1230     } else {
1231     _raise_fatal_error ($o, desc => 'INVALID_DECLARE_SYNTAX', t => $all);
1232     }
1233     }
1234    
1235     ## TODO: remove this function. this function is already obsoleted.
1236     sub _raise_fatal_error ($%) {
1237     require Carp;
1238     my ($o, %o) = @_;
1239     $o{desc} = {
1240     INVALID_CHAR => 'Invalid character (%s) at this context',
1241     INVALID_XML_DECLARE => 'XML declaration must be at the top of the entity',
1242     NONDECLARED_NS_PREFIX => 'Undeclared namespace prefix (%s) is used',
1243     NONMATCH_ETAG => 'End tag (element type name = '.$o{_end_qname}.') does not match with start tag (element type name = '.$o{_start_qname}.')',
1244     NOT_ALLOWED_HERE => 'This type of markup (%s) cannot appear here',
1245     NOT_ALLOWED_THIS_MODE => 'This type of markup (%s) cannot be used '.({
1246     document_entity => 'out of DTD',
1247     dtd_internal_subset => 'in internal subset of DTD',
1248     dtd_external_subset => 'in external parsed entity (external subset of DTD)',
1249     }->{$o->{entity_type}||'document_entity'}||'in '.$o->{entity_type}),
1250     UNKNOWN => 'Unknown error (%s)',
1251     }->{$o{desc}} || $o{desc};
1252     $o{desc} .= ' (%s)' if length $o{t} && $o{desc} !~ /%s/;
1253     Carp::croak ("Line $o->{line}, position $o->{pos}: ".sprintf $o{desc}, $o{t});
1254     }
1255    
1256     sub _is_brother_of_root_element ($$) {
1257     my ($self, $c) = @_;
1258     for (@{$c->child_nodes}) {
1259     if ($_->node_type eq '#element') {
1260     return 1;
1261     }
1262     }
1263     return 0;
1264     }
1265    
1266     =head1 LICENSE
1267    
1268     Copyright 2003 Wakaba <[email protected]>
1269    
1270     This program is free software; you can redistribute it and/or
1271     modify it under the same terms as Perl itself.
1272    
1273     =cut
1274    
1275 wakaba 1.2 1; # $Date: 2003/05/24 04:52:19 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24