1 ! Copyright (C) 2005, 2006 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: xml.data xml.writer kernel generic io prettyprint math
4 debugger sequences state-parser accessors summary
5 namespaces io.streams.string xml.backend ;
10 M: multitags summary ( obj -- str )
11 drop "XML document contains multiple main tags" ;
13 ERROR: pre/post-content string pre? ;
15 M: pre/post-content summary ( obj -- str )
17 "The text string:" print
20 pre?>> "before" "after" ? write
21 " the main tag." print
22 ] with-string-writer ;
24 TUPLE: no-entity < parsing-error thing ;
26 : no-entity ( string -- * )
27 \ no-entity parsing-error swap >>thing throw ;
29 M: no-entity summary ( obj -- str )
31 dup call-next-method write
32 "Entity does not exist: &" write thing>> write ";" print
33 ] with-string-writer ;
35 TUPLE: xml-string-error < parsing-error string ; ! this should not exist
37 : xml-string-error ( string -- * )
38 \ xml-string-error parsing-error swap >>string throw ;
40 M: xml-string-error summary ( obj -- str )
42 dup call-next-method write
44 ] with-string-writer ;
46 TUPLE: mismatched < parsing-error open close ;
48 : mismatched ( open close -- * )
49 \ mismatched parsing-error swap >>close swap >>open throw ;
51 M: mismatched summary ( obj -- str )
53 dup call-next-method write
54 "Mismatched tags" print
55 "Opening tag: <" write dup open>> print-name ">" print
56 "Closing tag: </" write close>> print-name ">" print
57 ] with-string-writer ;
59 TUPLE: unclosed < parsing-error tags ;
62 \ unclosed parsing-error
63 xml-stack get rest-slice [ first name>> ] map >>tags
66 M: unclosed summary ( obj -- str )
68 dup call-next-method write
71 tags>> [ " <" write print-name ">" print ] each
72 ] with-string-writer ;
74 TUPLE: bad-uri < parsing-error string ;
76 : bad-uri ( string -- * )
77 \ bad-uri parsing-error swap >>string throw ;
79 M: bad-uri summary ( obj -- str )
81 dup call-next-method write
82 "Bad URI:" print string>> .
83 ] with-string-writer ;
85 TUPLE: nonexist-ns < parsing-error name ;
87 : nonexist-ns ( name-string -- * )
88 \ nonexist-ns parsing-error swap >>name throw ;
90 M: nonexist-ns summary ( obj -- str )
92 dup call-next-method write
93 "Namespace " write name>> write " has not been declared" print
94 ] with-string-writer ;
96 TUPLE: unopened < parsing-error ; ! this should give which tag was unopened
99 \ unopened parsing-error throw ;
101 M: unopened summary ( obj -- str )
103 call-next-method write
104 "Closed an unopened tag" print
105 ] with-string-writer ;
107 TUPLE: not-yes/no < parsing-error text ;
109 : not-yes/no ( text -- * )
110 \ not-yes/no parsing-error swap >>text throw ;
112 M: not-yes/no summary ( obj -- str )
114 dup call-next-method write
115 "standalone must be either yes or no, not \"" write
116 text>> write "\"." print
117 ] with-string-writer ;
119 ! this should actually print the names
120 TUPLE: extra-attrs < parsing-error attrs ;
122 : extra-attrs ( attrs -- * )
123 \ extra-attrs parsing-error swap >>attrs throw ;
125 M: extra-attrs summary ( obj -- str )
127 dup call-next-method write
128 "Extra attributes included in xml version declaration:" print
130 ] with-string-writer ;
132 TUPLE: bad-version < parsing-error num ;
134 : bad-version ( num -- * )
135 \ bad-version parsing-error swap >>num throw ;
137 M: bad-version summary ( obj -- str )
139 "XML version must be \"1.0\" or \"1.1\". Version here was " write
141 ] with-string-writer ;
145 M: notags summary ( obj -- str )
146 drop "XML document lacks a main tag" ;
148 TUPLE: bad-prolog < parsing-error prolog ;
150 : bad-prolog ( prolog -- * )
151 \ bad-prolog parsing-error swap >>prolog throw ;
153 M: bad-prolog summary ( obj -- str )
155 dup call-next-method write
156 "Misplaced XML prolog" print
157 prolog>> write-prolog nl
158 ] with-string-writer ;
160 TUPLE: capitalized-prolog < parsing-error name ;
162 : capitalized-prolog ( name -- capitalized-prolog )
163 \ capitalized-prolog parsing-error swap >>name throw ;
165 M: capitalized-prolog summary ( obj -- str )
167 dup call-next-method write
168 "XML prolog name was partially or totally capitalized, using" print
169 "<?" write name>> write "...?>" write
170 " instead of <?xml...?>" print
171 ] with-string-writer ;
173 TUPLE: versionless-prolog < parsing-error ;
175 : versionless-prolog ( -- * )
176 \ versionless-prolog parsing-error throw ;
178 M: versionless-prolog summary ( obj -- str )
180 call-next-method write
181 "XML prolog lacks a version declaration" print
182 ] with-string-writer ;
184 TUPLE: bad-instruction < parsing-error instruction ;
186 : bad-instruction ( instruction -- * )
187 \ bad-instruction parsing-error swap >>instruction throw ;
189 M: bad-instruction summary ( obj -- str )
191 dup call-next-method write
192 "Misplaced processor instruction:" print
193 instruction>> write-xml-chunk nl
194 ] with-string-writer ;
196 TUPLE: bad-directive < parsing-error dir ;
198 : bad-directive ( directive -- * )
199 \ bad-directive parsing-error swap >>dir throw ;
201 M: bad-directive summary ( obj -- str )
203 dup call-next-method write
204 "Unknown directive:" print
206 ] with-string-writer ;
208 TUPLE: bad-doctype-decl < parsing-error ;
210 : bad-doctype-decl ( -- * )
211 \ bad-doctype-decl parsing-error throw ;
213 M: bad-doctype-decl summary ( obj -- str )
214 call-next-method "\nBad DOCTYPE" append ;
216 TUPLE: bad-external-id < parsing-error ;
218 : bad-external-id ( -- * )
219 \ bad-external-id parsing-error throw ;
221 M: bad-external-id summary ( obj -- str )
222 call-next-method "\nBad external ID" append ;
224 TUPLE: misplaced-directive < parsing-error dir ;
226 : misplaced-directive ( directive -- * )
227 \ misplaced-directive parsing-error swap >>dir throw ;
229 M: misplaced-directive summary ( obj -- str )
231 dup call-next-method write
232 "Misplaced directive:" print
233 dir>> write-xml-chunk nl
234 ] with-string-writer ;
236 UNION: xml-parse-error multitags notags extra-attrs nonexist-ns
237 not-yes/no unclosed mismatched xml-string-error expected no-entity
238 bad-prolog versionless-prolog capitalized-prolog bad-instruction