Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SAX.c : Default SAX handler to build a tree. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3 | * |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 6 | * Daniel Veillard <Daniel.Veillard@w3.org> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
Seth Alves | e7f12e6 | 1998-10-01 20:51:15 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 11 | #include "tree.h" |
| 12 | #include "parser.h" |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 13 | #include "entities.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 14 | #include "error.h" |
| 15 | |
| 16 | /* #define DEBUG_SAX */ |
| 17 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 18 | /** |
| 19 | * getPublicId: |
| 20 | * @ctxt: An XML parser context |
| 21 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 22 | * Return the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN" |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 23 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame^] | 24 | * Returns a CHAR * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 25 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 26 | const CHAR * |
| 27 | getPublicId(xmlParserCtxtPtr ctxt) |
| 28 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 29 | return(NULL); |
| 30 | } |
| 31 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 32 | /** |
| 33 | * getSystemId: |
| 34 | * @ctxt: An XML parser context |
| 35 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 36 | * Return the system ID, basically URI or filename e.g. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 37 | * http://www.sgmlsource.com/dtds/memo.dtd |
| 38 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame^] | 39 | * Returns a CHAR * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 40 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 41 | const CHAR * |
| 42 | getSystemId(xmlParserCtxtPtr ctxt) |
| 43 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 44 | return(ctxt->input->filename); |
| 45 | } |
| 46 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 47 | /** |
| 48 | * getLineNumber: |
| 49 | * @ctxt: An XML parser context |
| 50 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 51 | * Return the line number of the current parsing point. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 52 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame^] | 53 | * Returns an int |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 54 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 55 | int |
| 56 | getLineNumber(xmlParserCtxtPtr ctxt) |
| 57 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 58 | return(ctxt->input->line); |
| 59 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * getColumnNumber: |
| 63 | * @ctxt: An XML parser context |
| 64 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 65 | * Return the column number of the current parsing point. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 66 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame^] | 67 | * Returns an int |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 68 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 69 | int |
| 70 | getColumnNumber(xmlParserCtxtPtr ctxt) |
| 71 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 72 | return(ctxt->input->col); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * The default SAX Locator. |
| 77 | */ |
| 78 | |
| 79 | xmlSAXLocator xmlDefaultSAXLocator = { |
| 80 | getPublicId, getSystemId, getLineNumber, getColumnNumber |
| 81 | }; |
| 82 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 83 | /** |
| 84 | * resolveEntity: |
| 85 | * @ctxt: An XML parser context |
| 86 | * @publicId: The public ID of the entity |
| 87 | * @systemId: The system ID of the entity |
| 88 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 89 | * Special entity resolver, better left to the parser, it has |
| 90 | * more context than the application layer. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 91 | * The default behaviour is to NOT resolve the entities, in that case |
| 92 | * the ENTITY_REF nodes are built in the structure (and the parameter |
| 93 | * values). |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 94 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame^] | 95 | * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 96 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 97 | xmlParserInputPtr |
| 98 | resolveEntity(xmlParserCtxtPtr ctxt, const CHAR *publicId, const CHAR *systemId) |
| 99 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 100 | |
| 101 | #ifdef DEBUG_SAX |
| 102 | fprintf(stderr, "SAX.resolveEntity(%s, %s)\n", publicId, systemId); |
| 103 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 104 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 105 | return(NULL); |
| 106 | } |
| 107 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 108 | /** |
| 109 | * notationDecl: |
| 110 | * @ctxt: An XML parser context |
| 111 | * @name: The name of the notation |
| 112 | * @publicId: The public ID of the entity |
| 113 | * @systemId: The system ID of the entity |
| 114 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 115 | * What to do when a notation declaration has been parsed. |
| 116 | * TODO Not handled currently. |
| 117 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 118 | void |
| 119 | notationDecl(xmlParserCtxtPtr ctxt, const CHAR *name, |
| 120 | const CHAR *publicId, const CHAR *systemId) |
| 121 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 122 | #ifdef DEBUG_SAX |
| 123 | fprintf(stderr, "SAX.notationDecl(%s, %s, %s)\n", name, publicId, systemId); |
| 124 | #endif |
| 125 | } |
| 126 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 127 | /** |
| 128 | * unparsedEntityDecl: |
| 129 | * @ctxt: An XML parser context |
| 130 | * @name: The name of the entity |
| 131 | * @publicId: The public ID of the entity |
| 132 | * @systemId: The system ID of the entity |
| 133 | * @notationName: the name of the notation |
| 134 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 135 | * What to do when an unparsed entity declaration is parsed |
| 136 | * TODO Create an Entity node. |
| 137 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 138 | void |
| 139 | unparsedEntityDecl(xmlParserCtxtPtr ctxt, const CHAR *name, |
| 140 | const CHAR *publicId, const CHAR *systemId, |
| 141 | const CHAR *notationName) |
| 142 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 143 | #ifdef DEBUG_SAX |
| 144 | fprintf(stderr, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n", |
| 145 | name, publicId, systemId, notationName); |
| 146 | #endif |
| 147 | } |
| 148 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 149 | /** |
| 150 | * setDocumentLocator: |
| 151 | * @ctxt: An XML parser context |
| 152 | * @loc: A SAX Locator |
| 153 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 154 | * Receive the document locator at startup, actually xmlDefaultSAXLocator |
| 155 | * Everything is available on the context, so this is useless in our case. |
| 156 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 157 | void |
| 158 | setDocumentLocator(xmlParserCtxtPtr ctxt, xmlSAXLocatorPtr loc) |
| 159 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 160 | #ifdef DEBUG_SAX |
| 161 | fprintf(stderr, "SAX.setDocumentLocator()\n"); |
| 162 | #endif |
| 163 | } |
| 164 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 165 | /** |
| 166 | * startDocument: |
| 167 | * @ctxt: An XML parser context |
| 168 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 169 | * called when the document start being processed. |
| 170 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 171 | void |
| 172 | startDocument(xmlParserCtxtPtr ctxt) |
| 173 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 174 | #ifdef DEBUG_SAX |
| 175 | fprintf(stderr, "SAX.startDocument()\n"); |
| 176 | #endif |
| 177 | } |
| 178 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 179 | /** |
| 180 | * endDocument: |
| 181 | * @ctxt: An XML parser context |
| 182 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 183 | * called when the document end has been detected. |
| 184 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 185 | void |
| 186 | endDocument(xmlParserCtxtPtr ctxt) |
| 187 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 188 | #ifdef DEBUG_SAX |
| 189 | fprintf(stderr, "SAX.endDocument()\n"); |
| 190 | #endif |
| 191 | } |
| 192 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 193 | /** |
| 194 | * startElement: |
| 195 | * @ctxt: An XML parser context |
| 196 | * @name: The element name |
| 197 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 198 | * called when an opening tag has been processed. |
| 199 | * TODO We currently have a small pblm with the arguments ... |
| 200 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 201 | void |
| 202 | startElement(xmlParserCtxtPtr ctxt, const CHAR *name) |
| 203 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 204 | #ifdef DEBUG_SAX |
| 205 | fprintf(stderr, "SAX.startElement(%s)\n", name); |
| 206 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 209 | /** |
| 210 | * endElement: |
| 211 | * @ctxt: An XML parser context |
| 212 | * @name: The element name |
| 213 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 214 | * called when the end of an element has been detected. |
| 215 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 216 | void |
| 217 | endElement(xmlParserCtxtPtr ctxt, const CHAR *name) |
| 218 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 219 | #ifdef DEBUG_SAX |
| 220 | fprintf(stderr, "SAX.endElement(%s)\n", name); |
| 221 | #endif |
| 222 | } |
| 223 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 224 | /** |
Daniel Veillard | 11e0058 | 1998-10-24 18:27:49 +0000 | [diff] [blame] | 225 | * attribute: |
| 226 | * @ctxt: An XML parser context |
| 227 | * @name: The attribute name |
| 228 | * @value: The attribute value |
| 229 | * |
| 230 | * called when an attribute has been read by the parser. |
| 231 | * The default handling is to convert the attribute into an |
| 232 | * DOM subtree and past it in a new xmlAttr element added to |
| 233 | * the element. |
Daniel Veillard | 11e0058 | 1998-10-24 18:27:49 +0000 | [diff] [blame] | 234 | */ |
| 235 | void |
| 236 | attribute(xmlParserCtxtPtr ctxt, const CHAR *name, const CHAR *value) |
| 237 | { |
| 238 | #ifdef DEBUG_SAX |
| 239 | fprintf(stderr, "SAX.attribute(%s, %s)\n", name, value); |
| 240 | #endif |
Daniel Veillard | 11e0058 | 1998-10-24 18:27:49 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 244 | * characters: |
| 245 | * @ctxt: An XML parser context |
| 246 | * @ch: a CHAR string |
| 247 | * @start: the first char in the string |
| 248 | * @len: the number of CHAR |
| 249 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 250 | * receiving some chars from the parser. |
| 251 | * Question: how much at a time ??? |
| 252 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 253 | void |
| 254 | characters(xmlParserCtxtPtr ctxt, const CHAR *ch, int start, int len) |
| 255 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 256 | xmlNodePtr lastChild; |
| 257 | |
| 258 | #ifdef DEBUG_SAX |
| 259 | fprintf(stderr, "SAX.characters(%.30s, %d, %d)\n", ch, start, len); |
| 260 | #endif |
| 261 | /* |
| 262 | * Handle the data if any. If there is no child |
| 263 | * add it as content, otherwise if the last child is text, |
| 264 | * concatenate it, else create a new node of type text. |
| 265 | */ |
| 266 | |
| 267 | lastChild = xmlGetLastChild(ctxt->node); |
| 268 | if (lastChild == NULL) |
| 269 | xmlNodeAddContentLen(ctxt->node, &ch[start], len); |
| 270 | else { |
| 271 | if (xmlNodeIsText(lastChild)) |
| 272 | xmlTextConcat(lastChild, &ch[start], len); |
| 273 | else { |
| 274 | lastChild = xmlNewTextLen(&ch[start], len); |
| 275 | xmlAddChild(ctxt->node, lastChild); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 280 | /** |
| 281 | * ignorableWhitespace: |
| 282 | * @ctxt: An XML parser context |
| 283 | * @ch: a CHAR string |
| 284 | * @start: the first char in the string |
| 285 | * @len: the number of CHAR |
| 286 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 287 | * receiving some ignorable whitespaces from the parser. |
| 288 | * Question: how much at a time ??? |
| 289 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 290 | void |
| 291 | ignorableWhitespace(xmlParserCtxtPtr ctxt, const CHAR *ch, int start, int len) |
| 292 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 293 | #ifdef DEBUG_SAX |
| 294 | fprintf(stderr, "SAX.ignorableWhitespace(%.30s, %d, %d)\n", ch, start, len); |
| 295 | #endif |
| 296 | } |
| 297 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 298 | /** |
| 299 | * processingInstruction: |
| 300 | * @ctxt: An XML parser context |
| 301 | * @target: the target name |
| 302 | * @data: the PI data's |
| 303 | * @len: the number of CHAR |
| 304 | * |
| 305 | * A processing instruction has been parsed. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 306 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 307 | void |
| 308 | processingInstruction(xmlParserCtxtPtr ctxt, const CHAR *target, |
| 309 | const CHAR *data) |
| 310 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 311 | #ifdef DEBUG_SAX |
| 312 | fprintf(stderr, "SAX.processingInstruction(%s, %s)\n", target, data); |
| 313 | #endif |
| 314 | } |
| 315 | |
| 316 | xmlSAXHandler xmlDefaultSAXHandler = { |
| 317 | resolveEntity, |
| 318 | notationDecl, |
| 319 | unparsedEntityDecl, |
| 320 | setDocumentLocator, |
| 321 | startDocument, |
| 322 | endDocument, |
| 323 | startElement, |
| 324 | endElement, |
Daniel Veillard | 11e0058 | 1998-10-24 18:27:49 +0000 | [diff] [blame] | 325 | attribute, |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 326 | characters, |
| 327 | ignorableWhitespace, |
| 328 | processingInstruction, |
| 329 | xmlParserWarning, |
| 330 | xmlParserError, |
| 331 | xmlParserError, |
| 332 | }; |
| 333 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 334 | /** |
| 335 | * xmlDefaultSAXHandlerInit: |
| 336 | * |
| 337 | * Initialize the default SAX handler |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 338 | */ |
| 339 | void |
| 340 | xmlDefaultSAXHandlerInit(void) |
| 341 | { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 342 | xmlDefaultSAXHandler.resolveEntity = resolveEntity; |
| 343 | xmlDefaultSAXHandler.notationDecl = notationDecl; |
| 344 | xmlDefaultSAXHandler.unparsedEntityDecl = unparsedEntityDecl; |
| 345 | xmlDefaultSAXHandler.setDocumentLocator = setDocumentLocator; |
| 346 | xmlDefaultSAXHandler.startDocument = startDocument; |
| 347 | xmlDefaultSAXHandler.endDocument = endDocument; |
| 348 | xmlDefaultSAXHandler.startElement = startElement; |
| 349 | xmlDefaultSAXHandler.endElement = endElement; |
Daniel Veillard | 11e0058 | 1998-10-24 18:27:49 +0000 | [diff] [blame] | 350 | xmlDefaultSAXHandler.attribute = attribute; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 351 | xmlDefaultSAXHandler.characters = characters; |
| 352 | xmlDefaultSAXHandler.ignorableWhitespace = ignorableWhitespace; |
| 353 | xmlDefaultSAXHandler.processingInstruction = processingInstruction; |
| 354 | xmlDefaultSAXHandler.warning = xmlParserWarning; |
| 355 | xmlDefaultSAXHandler.error = xmlParserError; |
| 356 | xmlDefaultSAXHandler.fatalError = xmlParserError; |
| 357 | } |