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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations) (download)
Sun Jun 29 08:34:37 2003 UTC (23 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.3: +79 -6 lines
Marked section support

1 wakaba 1.1
2     =head1 NAME
3    
4     SuikaWiki::Markup::XML::Error --- SuikaWiki XML: Error handling module for SuikaWiki::Markup::XML::*
5    
6     =head1 DESCRIPTION
7    
8 wakaba 1.3 This module provides the common error and/or warning handling interface
9     for the SuikaWiki::Markup::XML::* modules. With this module, proper error
10     recovering and proper message outputing (eg. output as HTML element,
11     localized message, etc.) is easily implementable.
12    
13 wakaba 1.1 This module is part of SuikaWiki XML support.
14    
15     =cut
16    
17     package SuikaWiki::Markup::XML::Error;
18     use strict;
19 wakaba 1.4 our $VERSION = do{my @r=(q$Revision: 1.3 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
20 wakaba 1.3
21     ## Prefixes:
22     ## - 'SYNTAX_': don't match with XML 1.0 EBNF rules
23     ## - 'WFC_': violation of well-formedness constraint (fatal error)
24     ## - 'VC_': violation of validity constraint (error)
25     ## - 'NC_': violation of namespace constraint (error)
26     ## - 'NS_SYNTAX_': don't match with XML Namespace 1.0 EBNF rules (error)
27     ## - 'FATAL_ERR_': fatal error specified by XML 1.0 spec
28     ## - 'ERR_': error specified by XML 1.0 spec
29     ## - 'WARN_': don't fullfil XML spec's or implementor's recommendation
30    
31     ## Error levels:
32     ## - wfc: well-formedness (including syntax error)
33     ## - vc: validity
34     ## - nswfc: namespace well-formedness
35     ## - nsvc: namespace validity
36     ## - fatal: fatal error
37     ## - warn: warning
38 wakaba 1.1
39     my %_Error = (
40 wakaba 1.3 ## Forward compatibility error
41     SYNTAX_UNSUPPORTED_XML_VERSION => {
42     description => 'Unsupported XML version (%s)',
43     level => 'wfc',
44     },
45 wakaba 1.1 ## Syntax errors
46     SYNTAX_DATA_OUT_OF_ROOT_ELEMENT => {
47     description => 'Invalid data or markup out of root element',
48     level => 'wfc',
49     },
50 wakaba 1.4 SYNTAX_DOCTYPE_NAME_NOT_FOUND => {
51     description => 'Document type name must be specified in the document type declaration',
52     level => 'wfc',
53     },
54     SYNTAX_DOCTYPE_PID_LITERAL_NOT_FOUND => {
55     description => 'Minimum literal for the public identifier must follow the keyword PUBLIC',
56     level => 'wfc',
57     },
58     SYNTAX_DOCTYPE_SYSID_LITERAL_NOT_FOUND => {
59     description => 'Literal of the system identifier must follow the keyword SYSTEM or the minimum literal of the public identifier in XML',
60     level => 'wfc',
61     },
62 wakaba 1.1 SYNTAX_END_OF_MARKUP_NOT_FOUND => {
63     description => sub {
64     my ($o, $err) = @_;
65     my $o_start = $err->{t}->flag ('p_o_start');
66     my $r = $err->{t}->qname;
67     $r = sprintf 'line %d, position %d%s', $o_start->{line},
68     $o_start->{pos}, ($r ? '; '.$r : '') if ref $o_start;
69     $r ? $r = '; '.$r : 0;
70     $err->{t} = $err->{t}->node_type;
71     'Markup is not closed (%s'.$r.')';
72     },
73     level => 'wfc',
74     },
75     SYNTAX_END_TAG_NOT_FOUND => {
76     description => sub {
77     my ($o, $err) = @_;
78     my $o_start = $err->{t}->flag ('p_o_start');
79     my $r = '';
80     $r = sprintf 'line %d, position %d%s', $o_start->{line},
81     $o_start->{pos}, ($r ? '; '.$r : '') if ref $o_start;
82     $r ? $r = '; '.$r : 0;
83     $err->{t} = $err->{t}->qname;
84     'End tag of element (type = %s'.$r.') not found';
85     },
86     level => 'wfc',
87     },
88     SYNTAX_INVALID => {
89     description => 'This type of markup (%s) cannot appear here',
90     level => 'wfc',
91     },
92     SYNTAX_INVALID_CHAR => {
93     description => 'Invalid character (%s) at this context',
94     level => 'wfc',
95     },
96     SYNTAX_INVALID_DOCTYPE_POSITION => {
97     description => 'DOCTYPE declaration must be between xml declaration and the root element',
98     level => 'wfc',
99     },
100     SYNTAX_INVALID_KEYWORD => {
101     description => 'Invalid keyword (%s) at this context',
102     level => 'wfc',
103     },
104     SYNTAX_INVALID_LITERAL => {
105     description => 'Literal cannot be here ("%s")',
106     level => 'wfc',
107     },
108     SYNTAX_INVALID_MD => {
109     description => 'Invalid syntax of markup declaration',
110     level => 'wfc',
111     },
112     SYNTAX_INVALID_POSITION => {
113     description => sub {
114     my ($o) = shift;
115     'This type of markup (%s) cannot be used '.({
116     document_entity => 'out of DTD',
117     dtd_internal_subset => 'in the internal subset of DTD',
118     dtd_external_subset => 'in the external subset of DTD',
119     external_parsed_entity => 'in the external parsed entity',
120     general_external_parsed_entity => 'in the general external parsed entity',
121     }->{$o->{entity_type}||'document_entity'}||'in '.$o->{entity_type})},
122     level => 'wfc',
123     },
124 wakaba 1.3 SYNTAX_INVALID_PUBID => {
125     description => 'This public identifier contains at least one invalid character (%s)',
126     level => 'wfc',
127     },
128 wakaba 1.1 SYNTAX_LEGAL_CHARACTER => {
129     description => sub {
130     my $r = sprintf 'The character U-%08X is not a legal XML Char',
131     $_[1]->{t};
132     $_[1]->{t} = undef;
133     $r;
134     },
135     level => 'wfc',
136     },
137     SYNTAX_MD_NAME_NOT_FOUND => {
138     description => 'Name is required by this type of declaration',
139     level => 'wfc',
140     },
141     SYNTAX_MD_SYSID_NOT_FOUND => {
142     description => 'System identifier is required by this type of declaration',
143     level => 'wfc',
144     },
145 wakaba 1.4 SYNTAX_MS_IN_INTERNAL_SUBSET => {
146     description => 'Marked section cannot be used in the internal subset of the DTD in XML',
147     level => 'wfc',
148     },
149     SYNTAX_MS_INVALID_STATUS_STRING => {
150     description => 'Invalid string in the status keyword list',
151     level => 'wfc',
152     },
153     SYNTAX_MS_MULTIPLE_STATUS => {
154     description => 'Multiple status keyword (%s) cannot be used',
155     level => 'wfc',
156     },
157     SYNTAX_MS_NO_STATUS_KEYWORD => {
158     description => 'No status keyword found',
159     level => 'wfc',
160     },
161     SYNTAX_MS_NON_XML_STATUS => {
162     description => 'This status keyword (%s) cannot be used in XML',
163     level => 'wfc',
164     },
165     SYNTAX_MS_UNKNOWN_STATUS => {
166     description => 'Unknown status keyword (%s) is used',
167     level => 'wfc',
168     },
169 wakaba 1.1 SYNTAX_PE_NDATA => {
170     description => 'Parameter entity must be a parsed entity',
171     level => 'wfc',
172     },
173 wakaba 1.4 SYNTAX_ROOT_ELEMENT_NOT_FOUND => {
174     description => 'There is no root element (type = %s) in this document entity',
175     level => 'wfc',
176     },
177 wakaba 1.1 SYNTAX_XML_DECLARE => {
178     description => 'Syntax of XML (or text) declaration is invalid',
179     level => 'wfc',
180     },
181     SYNTAX_XML_DECLARE_NO_ATTR => {
182 wakaba 1.3 description => 'XML (or text) declaration has no (valid) pseudo attribute',
183     level => 'wfc',
184     },
185     SYNTAX_XML_DECLARE_NO_ENCODING_ATTR => {
186     description => q(Text declaration must have 'encoding' pseudo attribute),
187     level => 'wfc',
188     },
189     SYNTAX_XML_DECLARE_NO_VERSION_ATTR => {
190     description => q(XML declaration must have 'version' pseudo attribute),
191 wakaba 1.1 level => 'wfc',
192     },
193     SYNTAX_XML_DECLARE_POSITION => {
194     description => 'XML declaration must be at the top of the entity',
195     level => 'wfc',
196     },
197 wakaba 1.3 SYNTAX_XML_DECLARE_STANDALONE_ATTR => {
198     description => q(Text declaration cannot have 'standalone' pseudo attribute),
199     level => 'wfc',
200     },
201 wakaba 1.1 ## Well-formedness error
202     WFC_ELEMENT_TYPE_MATCH => {
203     description => 'End tag (type = %s) does not match with start tag (type = %s)',
204     level => 'wfc',
205     },
206     WFC_ENTITY_DECLARED => {
207     description => 'Entity (%s) must be declared before it is referred',
208     level => 'wfc',
209     },
210     WFC_LEGAL_CHARACTER => {
211     description => sub {
212     my $r = sprintf 'The character referred (U-%08X) is not a legal XML Char',
213     $_[1]->{t};
214     $_[1]->{t} = undef;
215     $r;
216     },
217     level => 'wfc',
218     },
219     WFC_NO_EXTERNAL_ENTITY_REFERENCE => {
220     description => 'Attribute value must not contain reference to an external entity',
221     level => 'wfc',
222     },
223     WFC_NO_LE_IN_ATTRIBUTE_VALUE => {
224     description => 'Attribute value must not contain a less-than sign (<)',
225     level => 'wfc',
226     },
227     WFC_NO_RECURSION => {
228     description => 'Parsed entity (%s) must not contain a recursive reference to itself',
229     level => 'wfc',
230     },
231     WFC_NO_LT_IN_ATTRIBUTE_VALUE => {
232     description => 'Replacement text of entity reference in an attribute value literal cannot contain LESS-THAN SIGN (<) itself',
233     level => 'wfc',
234     },
235 wakaba 1.3 WFC_PARSED_ENTITY => {
236     description => 'Entity reference (%s) must not refer non-parsed entity',
237     level => 'wfc',
238     },
239 wakaba 1.1 WFC_PE_IN_INTERNAL_SUBSET => {
240     description => 'Parameter entity (%s) cannot be referred in markup declaration in internal subset of DTD',
241     level => 'wfc',
242     },
243     WFC_UNIQUE_ATT_SPEC => {
244     description => 'Dupulicate attribute specification',
245     level => 'wfc',
246     },
247 wakaba 1.3 FATAL_ERR_DECODE_IMPL_ERR => {
248     description => 'Decode error (%s)',
249     level => 'fatal',
250     },
251     FATAL_ERR_PREDEFINED_ENTITY => {
252 wakaba 1.4 description => 'Predefined entity (%s := %s) must be declared as of the XML specification defined',
253 wakaba 1.3 level => 'fatal',
254 wakaba 1.2 },
255 wakaba 1.1 ## Validity error
256     VC_ENTITY_DECLARED => {
257     description => 'Entity %s should (or must to be valid) be declared before it is referred',
258     level => 'vc',
259     },
260     VC_NOTATION_DECLARED => {
261     description => 'Notation %s should (or must to be valid) be declared',
262     level => 'vc',
263     },
264 wakaba 1.4 VC_ROOT_ELEMENT_TYPE => {
265     description => 'Document type name (%s) and element type name of the root element (%s) should (or must to be valid) match',
266     level => 'vc',
267     },
268 wakaba 1.1 VC_UNIQUE_NOTATION_NAME => {
269     description => 'Notation %s is already declared',
270     level => 'warn',
271     },
272     ## Namespace well-formedness error
273     NC_PREFIX_NOT_DEFINED => {
274     description => 'Undeclared namespace prefix (%s) is used',
275     level => 'nswfc',
276     },
277     NS_SYNTAX_NAME_IS_NCNAME => {
278     description => 'Name with colon (%s) cannot be used within namespaced XML document',
279     level => 'nswfc',
280     },
281     ## Namespace validity error
282 wakaba 1.4 ## XML (non-fatal) error
283 wakaba 1.3 ERR_EXT_ENTITY_NOT_FOUND => {
284     description => 'External entity (%s == <%s>) cannot be retrived (%s)',
285     level => 'vc',
286     },
287 wakaba 1.4 ERR_XML_NDATA_REF_IN_ENTITY_VALUE => {
288     description => 'Unparsed entity (%s) cannot be referred in EntityValue',
289     level => 'warn', ## Was fatal error but refined by SE Errata
290     },
291     ERR_XML_SYSID_HAS_FRAGMENT => {
292 wakaba 1.3 description => 'URI Reference converted from system identifier should not have the fragment identifier (%s)',
293     level => 'warn',
294     },
295 wakaba 1.4 ## XML warning
296 wakaba 1.1 WARN_PREDEFINED_ENTITY_NOT_DECLARED => {
297     description => 'Predefined general entity (%s) should be declared before it is referred for interoperability',
298     level => 'warn',
299     },
300     WARN_UNICODE_COMPAT_CHARACTER => {
301     description => sub {
302     my $r = sprintf 'Use of the character U+%04X is deprecated, since it is classified as compatible character in the Unicode Standard',
303     $_[1]->{t};
304     $_[1]->{t} = undef;
305     $r;
306     },
307     level => 'warn',
308     },
309     WARN_UNICODE_NONCHARACTER => {
310     description => sub {
311     my $r = sprintf 'Use of the code point U+%04X is deprecated, since it is noncharacter in the Unicode Standard',
312     $_[1]->{t};
313     $_[1]->{t} = undef;
314     $r;
315     },
316     level => 'warn',
317     },
318     WARN_UNICODE_XML_NOT_SUITABLE_CHARACTER => {
319     description => sub {
320     my $r = sprintf 'Use of the character U+%04X is deprecated by W3C Note unicode-xml',
321     $_[1]->{t};
322     $_[1]->{t} = undef;
323     $r;
324     },
325     level => 'warn',
326     },
327     WARN_UNIQUE_ENTITY_NAME => {
328     description => 'General entity %s is already declared',
329     level => 'warn',
330     },
331     WARN_UNIQUE_PARAMETER_ENTITY_NAME => {
332     description => 'Parameter entity %s is already declared',
333     level => 'warn',
334     },
335 wakaba 1.3 ## RFC 3023 'SHOULD'
336     WARN_MT_DTD_EXTERNAL_SUBSET => {
337     description => q(Media type "application/xml-dtd" SHOULD be used for the external subset of the DTD or the external parameter entity),
338     level => 'warn',
339     },
340     WARN_MT_EXTERNAL_GENERAL_PARSED_ENTITY => {
341     description => q(Media type "application/xml-external-parsed-entity" SHOULD be used for the external general parsed entity),
342     level => 'warn',
343     },
344     WARN_MT_XML_FOR_EXT_GENERAL_ENTITY => {
345     description => 'Using media type %s for external general parsed entity is now forbidden unless the entity is also well-formed as a document entity',
346     level => 'warn',
347     },
348 wakaba 1.1 ## Implementation's warning
349 wakaba 1.4 WARN_DOCTYPE_NOT_FOUND => {
350     description => 'No document type definition provided for this document',
351     level => 'warn',
352     },
353     WARN_ENTITY_DECLARATION_NOT_PROCESSED => {
354     description => 'This entity declaration is not processed because unread parameter entity is referred before this declaration',
355     level => 'warn',
356     },
357     WARN_EXTERNALLY_DEFINED_ENTITY_REFERRED => {
358     description => 'The entity referred (%s) is declared in the external entity, so different groove can be constructed when external entity is not read',
359     level => 'warn',
360     },
361 wakaba 1.3 WARN_GUESS_ENCODING_IMPL_ERR => {
362     description => 'Guessing encoding procedure cause some error (%s)',
363     level => 'warn',
364     },
365     WARN_INVALID_URI_CHAR_IN_SYSID => {
366     description => 'System identifier has at least one character that is invalid as part of URI Reference (%s)',
367     level => 'warn',
368     },
369     WARN_MT_TEXT_XML => {
370     description => q(In many case, labeling with the media type "text/xml" is inappropriate. Use "application/xml" or markup language specified type instead),
371     level => 'warn',
372     },
373     WARN_MT_TEXT_XML_EXTERNAL_PARSED_ENTITY => {
374     description => q(In many case, labeling with the media type "text/xml-external-parsed-entity" is inappropriate. Use "application/xml-external-parsed-entity" instead),
375     level => 'warn',
376     },
377     WARN_NO_CHARSET_PARAM => { ## charset parameter is optional
378     description => 'Charset parameter should be specified (%s)',
379     level => 'warn',
380     },
381     WARN_NO_CHARSET_PARAM_FOR_TEXT => { ## charset parameter have default value of ascii
382     description => q(Charset parameter is not specified, so default value 'us-ascii' is assumed (%s)),
383     level => 'warn',
384     },
385     WARN_NO_EXPLICIT_ENCODING_INFO => { ## BOM and '<?' guessing is failed (so general encoding guess is to be called)
386     description => q(Neither upper level protocol nor XML's encoding declaration provide encoding scheme information (or cannot read the encoding declaration because of lack of guessability)),
387     level => 'warn',
388     },
389 wakaba 1.4 WARN_PI_TARGET_NOTATION => {
390     description => 'Target name of the process instruction (%s) should be declared as a notation name to ensure interoperability',
391     level => 'warn',
392     },
393     WARN_PID_EMPTY => {
394     description => 'Public identifier is empty',
395     level => 'warn',
396     },
397 wakaba 1.3 WARN_PID_IS_INVALID_URN => {
398     description => 'Public identifier (%s) seems an invalid URN',
399     level => 'warn',
400     },
401     WARN_PID_IS_NOT_FPI_NOR_URN => {
402     description => 'Public identifier (%s) should be a formal public identifier or a uniform resource name to ensure interoperability',
403     level => 'warn',
404     },
405     WARN_PID_IS_TOO_LONG => {
406     description => 'Public identifier (%s) should be shorter for interoperability',
407     level => 'warn',
408     },
409     WARN_PID_IS_URN_WITH_RESERVED_CHAR => {
410     description => 'Public identifier (%s) seems a URN and it contains one or more reserved character ("/" and/or "?") which should not be used',
411     level => 'warn',
412     },
413 wakaba 1.4 WARN_SYSID_EMPTY => {
414     description => 'System identifier is empty, in most case it is wrong',
415     level => 'warn',
416     },
417 wakaba 1.3 WARN_XML_DECLARE_NO_VERSION_ATTR => {
418     description => 'Text declaration does not have the version pseudo attribute',
419     level => 'warn',
420     },
421 wakaba 1.1 ## Misc
422     UNKNOWN => {
423     description => 'Unknown error',
424 wakaba 1.3 level => 'fatal',
425 wakaba 1.1 },
426     );
427     ## TODO: error handling should be customizable (hookable) by user of this module
428     sub raise ($$%) {
429     my (undef, $o, %err) = @_;
430     my $error_type = $_Error{$err{type}} || $_Error{UNKNOWN};
431     my $error_msg = ref $error_type->{description} ? &{$error_type->{description}} ($o, \%err)
432     : $error_type->{description};
433     my @err_msg;
434     ref $err{t} eq 'ARRAY' ? @err_msg = @{$err{t}} : defined $err{t} ? @err_msg = $err{t} : undef;
435     $error_msg .= ' (%s)' if scalar (@err_msg) && ($error_msg !~ /%s/);
436     $error_msg = sprintf $error_msg, @err_msg;
437     $error_msg = "Line $o->{line}, position $o->{pos}: " . $error_msg;
438     $error_msg = 'Entity '.($err{entity}||$o->{entity}) . ": " . $error_msg if ($err{entity}||$o->{entity});
439 wakaba 1.4 $error_msg = '<'.($o->{uri}) . ">: " . $error_msg ;#if defined $o->{uri};
440 wakaba 1.1 require Carp;
441     Carp::carp ($error_msg);
442     #Carp::croak ("Line $o->{line}, position $o->{pos}: ".$error_msg);
443     }
444    
445     =head1 LICENSE
446    
447     Copyright 2003 Wakaba <[email protected]>
448    
449     This program is free software; you can redistribute it and/or
450     modify it under the same terms as Perl itself.
451    
452     =cut
453    
454 wakaba 1.4 1; # $Date: 2003/06/27 13:05:57 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24