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 xml.state accessors summary
5 namespaces io.streams.string ;
8 TUPLE: xml-error-at line column ;
10 : xml-error-at ( class -- obj )
14 M: xml-error-at summary ( obj -- str )
16 "XML parsing error" print
17 "Line: " write dup line>> .
18 "Column: " write column>> .
19 ] with-string-writer ;
21 TUPLE: expected < xml-error-at should-be was ;
22 : expected ( should-be was -- * )
23 \ expected xml-error-at
25 swap >>should-be throw ;
26 M: expected summary ( obj -- str )
28 dup call-next-method write
29 "Token expected: " write dup should-be>> print
30 "Token present: " write was>> print
31 ] with-string-writer ;
33 TUPLE: unexpected-end < xml-error-at ;
34 : unexpected-end ( -- * ) \ unexpected-end xml-error-at throw ;
35 M: unexpected-end summary ( obj -- str )
37 call-next-method write
38 "File unexpectedly ended." print
39 ] with-string-writer ;
41 TUPLE: missing-close < xml-error-at ;
42 : missing-close ( -- * ) \ missing-close xml-error-at throw ;
43 M: missing-close summary ( obj -- str )
45 call-next-method write
46 "Missing closing token." print
47 ] with-string-writer ;
49 TUPLE: disallowed-char < xml-error-at char ;
51 : disallowed-char ( char -- * )
52 \ disallowed-char xml-error-at swap >>char throw ;
54 M: disallowed-char summary
56 [ char>> "Disallowed character in XML document: " swap suffix ] bi
61 M: multitags summary ( obj -- str )
62 drop "XML document contains multiple main tags" ;
64 ERROR: pre/post-content string pre? ;
66 M: pre/post-content summary ( obj -- str )
68 "The text string:" print
71 pre?>> "before" "after" ? write
72 " the main tag." print
73 ] with-string-writer ;
75 TUPLE: no-entity < xml-error-at thing ;
77 : no-entity ( string -- * )
78 \ no-entity xml-error-at swap >>thing throw ;
80 M: no-entity summary ( obj -- str )
82 dup call-next-method write
83 "Entity does not exist: &" write thing>> write ";" print
84 ] with-string-writer ;
86 TUPLE: mismatched < xml-error-at open close ;
88 : mismatched ( open close -- * )
89 \ mismatched xml-error-at swap >>close swap >>open throw ;
91 M: mismatched summary ( obj -- str )
93 dup call-next-method write
94 "Mismatched tags" print
95 "Opening tag: <" write dup open>> print-name ">" print
96 "Closing tag: </" write close>> print-name ">" print
97 ] with-string-writer ;
99 TUPLE: unclosed < xml-error-at tags ;
102 \ unclosed xml-error-at
103 xml-stack get rest-slice [ first name>> ] map >>tags
106 M: unclosed summary ( obj -- str )
108 dup call-next-method write
109 "Unclosed tags" print
111 tags>> [ " <" write print-name ">" print ] each
112 ] with-string-writer ;
114 TUPLE: bad-uri < xml-error-at string ;
116 : bad-uri ( string -- * )
117 \ bad-uri xml-error-at swap >>string throw ;
119 M: bad-uri summary ( obj -- str )
121 dup call-next-method write
122 "Bad URI:" print string>> .
123 ] with-string-writer ;
125 TUPLE: nonexist-ns < xml-error-at name ;
127 : nonexist-ns ( name-string -- * )
128 \ nonexist-ns xml-error-at swap >>name throw ;
130 M: nonexist-ns summary ( obj -- str )
132 dup call-next-method write
133 "Namespace " write name>> write " has not been declared" print
134 ] with-string-writer ;
136 TUPLE: unopened < xml-error-at ; ! this should give which tag was unopened
139 \ unopened xml-error-at throw ;
141 M: unopened summary ( obj -- str )
143 call-next-method write
144 "Closed an unopened tag" print
145 ] with-string-writer ;
147 TUPLE: not-yes/no < xml-error-at text ;
149 : not-yes/no ( text -- * )
150 \ not-yes/no xml-error-at swap >>text throw ;
152 M: not-yes/no summary ( obj -- str )
154 dup call-next-method write
155 "standalone must be either yes or no, not \"" write
156 text>> write "\"." print
157 ] with-string-writer ;
159 ! this should actually print the names
160 TUPLE: extra-attrs < xml-error-at attrs ;
162 : extra-attrs ( attrs -- * )
163 \ extra-attrs xml-error-at swap >>attrs throw ;
165 M: extra-attrs summary ( obj -- str )
167 dup call-next-method write
168 "Extra attributes included in xml version declaration:" print
170 ] with-string-writer ;
172 TUPLE: bad-version < xml-error-at num ;
174 : bad-version ( num -- * )
175 \ bad-version xml-error-at swap >>num throw ;
177 M: bad-version summary ( obj -- str )
179 "XML version must be \"1.0\" or \"1.1\". Version here was " write
181 ] with-string-writer ;
185 M: notags summary ( obj -- str )
186 drop "XML document lacks a main tag" ;
188 TUPLE: bad-prolog < xml-error-at prolog ;
190 : bad-prolog ( prolog -- * )
191 \ bad-prolog xml-error-at swap >>prolog throw ;
193 M: bad-prolog summary ( obj -- str )
195 dup call-next-method write
196 "Misplaced XML prolog" print
197 prolog>> write-xml nl
198 ] with-string-writer ;
200 TUPLE: capitalized-prolog < xml-error-at name ;
202 : capitalized-prolog ( name -- capitalized-prolog )
203 \ capitalized-prolog xml-error-at swap >>name throw ;
205 M: capitalized-prolog summary ( obj -- str )
207 dup call-next-method write
208 "XML prolog name was partially or totally capitalized, using" print
209 "<?" write name>> write "...?>" write
210 " instead of <?xml...?>" print
211 ] with-string-writer ;
213 TUPLE: versionless-prolog < xml-error-at ;
215 : versionless-prolog ( -- * )
216 \ versionless-prolog xml-error-at throw ;
218 M: versionless-prolog summary ( obj -- str )
220 call-next-method write
221 "XML prolog lacks a version declaration" print
222 ] with-string-writer ;
224 TUPLE: bad-directive < xml-error-at dir ;
226 : bad-directive ( directive -- * )
227 \ bad-directive xml-error-at swap >>dir throw ;
229 M: bad-directive summary ( obj -- str )
231 dup call-next-method write
232 "Unknown directive:" print
234 ] with-string-writer ;
236 TUPLE: bad-decl < xml-error-at ;
239 \ bad-decl xml-error-at throw ;
241 M: bad-decl summary ( obj -- str )
242 call-next-method "\nExtra content in directive" append ;
244 TUPLE: bad-external-id < xml-error-at ;
246 : bad-external-id ( -- * )
247 \ bad-external-id xml-error-at throw ;
249 M: bad-external-id summary ( obj -- str )
250 call-next-method "\nBad external ID" append ;
252 TUPLE: misplaced-directive < xml-error-at dir ;
254 : misplaced-directive ( directive -- * )
255 \ misplaced-directive xml-error-at swap >>dir throw ;
257 M: misplaced-directive summary ( obj -- str )
259 dup call-next-method write
260 "Misplaced directive:" print
262 ] with-string-writer ;
264 TUPLE: bad-name < xml-error-at name ;
266 : bad-name ( name -- * )
267 \ bad-name xml-error-at swap >>name throw ;
269 M: bad-name summary ( obj -- str )
271 [ "Invalid name: " swap name>> "\n" 3append ]
274 TUPLE: unclosed-quote < xml-error-at ;
276 : unclosed-quote ( -- * )
277 \ unclosed-quote xml-error-at throw ;
279 M: unclosed-quote summary
281 "XML document ends with quote still open\n" append ;
283 TUPLE: quoteless-attr < xml-error-at ;
285 : quoteless-attr ( -- * )
286 \ quoteless-attr xml-error-at throw ;
288 M: quoteless-attr summary
289 call-next-method "Attribute lacks quotes around value\n" append ;
291 TUPLE: attr-w/< < xml-error-at ;
293 : attr-w/< ( value -- * )
294 \ attr-w/< xml-error-at throw ;
298 "Attribute value contains literal <" append ;
300 TUPLE: text-w/]]> < xml-error-at ;
302 : text-w/]]> ( text -- * )
303 \ text-w/]]> xml-error-at throw ;
305 M: text-w/]]> summary
307 "Text node contains ']]>'" append ;
309 TUPLE: duplicate-attr < xml-error-at key values ;
311 : duplicate-attr ( key values -- * )
312 \ duplicate-attr xml-error-at
313 swap >>values swap >>key throw ;
315 M: duplicate-attr summary
316 call-next-method "\nDuplicate attribute" append ;
318 TUPLE: bad-cdata < xml-error-at ;
321 \ bad-cdata xml-error-at throw ;
324 call-next-method "\nCDATA occurs before or after main tag" append ;
326 TUPLE: not-enough-characters < xml-error-at ;
327 : not-enough-characters ( -- * )
328 \ not-enough-characters xml-error-at throw ;
329 M: not-enough-characters summary ( obj -- str )
331 call-next-method write
332 "Not enough characters" print
333 ] with-string-writer ;
335 TUPLE: bad-doctype < xml-error-at contents ;
336 : bad-doctype ( contents -- * )
337 \ bad-doctype xml-error-at swap >>contents throw ;
338 M: bad-doctype summary
339 call-next-method "\nDTD contains invalid object" append ;
342 multitags notags pre/post-content xml-error-at ;