source: trunk/src/helpers/xml.c@ 337

Last change on this file since 337 was 265, checked in by pr, 21 years ago

Fixed spelling errors.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 120.3 KB
Line 
1
2/*
3 *@@sourcefile xml.c:
4 * XML document handling.
5 *
6 * XML support in the XWorkplace Helpers is broken into two
7 * layers:
8 *
9 * -- The bottom layer is implemented by the @expat parser,
10 * which I have ported and hacked to the xwphelpers.
11 *
12 * See xmlparse.c for an introduction.
13 *
14 * -- Because expat requires so many callbacks and is non-validating,
15 * I have added a top layer above the expat library
16 * which is vaguely modelled after the Document Object Model
17 * (DOM) standardized by the W3C. That's this file.
18 *
19 * This top layer allows you to do two things VERY EASILY:
20 *
21 * 1) Parse an XML document (which uses expat internally)
22 * and build a DOM tree from that. See xmlCreateDOM.
23 *
24 * 2) Create a DOM tree in memory and write an XML
25 * document from that. See xmlCreateDocument.
26 *
27 * <B>XML</B>
28 *
29 * In order to understand XML myself, I have written a couple of
30 * glossary entries for the complex XML terminology. See @XML
31 * for a start.
32 *
33 * <B>Document Object Model (DOM)</B>
34 *
35 * See @DOM for a general introduction.
36 *
37 * DOM really calls for object oriented programming so the various
38 * structs can inherit from each other. Since this implementation
39 * was supposed to be a C-only interface, we cannot implement
40 * inheritance at the language level. Instead, each XML document
41 * is broken up into a tree of node structures only (see DOMNODE),
42 * each of which has a special type. The W3C DOM allows this
43 * (and calls this the "flattened" view, as opposed to the
44 * "inheritance view").
45 *
46 * The W3C DOM specification prescribes tons of methods, which I
47 * really had no use for, so I didn't implement them. This implementation
48 * is only a DOM insofar as it uses nodes which represent @documents,
49 * @elements, @attributes, @comments, and @processing_instructions.
50 *
51 * Most notably, there are the following differences:
52 *
53 * -- External entities don't work yet. As a result, DOCTYPE's
54 * only make sense if the entire DTD is in the same document
55 * (internal subset).
56 *
57 * -- Not all node types are implemented. See DOMNODE for
58 * the supported types.
59 *
60 * -- Only a subset of the standardized methods is implemented,
61 * and they are called differently to adhere to the xwphelpers
62 * conventions.
63 *
64 * -- DOM uses UTF-16 for its DOMString type. @expat gives UTF-8
65 * strings to all the handlers though, so all data in the DOM nodes
66 * is UTF-8 encoded. This still needs to be fixed.
67 *
68 * -- DOM defines the DOMException class. This isn't supported in C.
69 * Instead, we use special error codes which add to the standard
70 * OS/2 error codes (APIRET). All our error codes are >= 40000
71 * to avoid conflicts.
72 *
73 * It shouldn't be too difficult to write a C++ encapsulation
74 * of this though which fully implements all the DOM methods.
75 *
76 * However, we do implement node management as in the standard.
77 * See xmlCreateDomNode and xmlDeleteNode.
78 *
79 * The main entry point into this is xmlCreateDOM. See remarks
80 * there for how this will be typically used.
81 *
82 * <B>Validation</B>
83 *
84 * @expat doesn't check XML documents for whether they are @valid.
85 * In other words, expat is a non-validating XML processor.
86 *
87 * By contrast, this pseudo-DOM implementation can validate to
88 * a certain extent.
89 *
90 * -- If you pass DF_PARSEDTD to xmlCreateDOM, the DTD will be
91 * parsed and the document will be validated against it.
92 * Validation is working as far as elements and attributes
93 * are checked for proper nesting. However, we cannot fully
94 * check for proper ordering etc. in (children) mode of
95 * @element_declarations. This will only check for whether
96 * elements may appear in another element at all -- not for
97 * the correct order.
98 *
99 * -- Otherwise the @DTD entries will not be stored in the DOM
100 * nodes, and no validation occurs. Still, if a DTD exists,
101 * @expat will insert attributes that have a default value
102 * in their @attribute declaraion and have not been specified.
103 *
104 *@@header "helpers\xml.h"
105 *@@added V0.9.6 (2000-10-29) [umoeller]
106 */
107
108/*
109 * Copyright (C) 2000-2002 Ulrich M”ller.
110 * This file is part of the "XWorkplace helpers" source package.
111 * This is free software; you can redistribute it and/or modify
112 * it under the terms of the GNU General Public License as published
113 * by the Free Software Foundation, in version 2 as it comes in the
114 * "COPYING" file of the XWorkplace main distribution.
115 * This program is distributed in the hope that it will be useful,
116 * but WITHOUT ANY WARRANTY; without even the implied warranty of
117 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
118 * GNU General Public License for more details.
119 */
120
121#define OS2EMX_PLAIN_CHAR
122 // this is needed for "os2emx.h"; if this is defined,
123 // emx will define PSZ as _signed_ char, otherwise
124 // as unsigned char
125
126#define INCL_DOSERRORS
127#include <os2.h>
128
129#include <stdlib.h>
130#include <string.h>
131#include <stdio.h>
132
133#include "setup.h" // code generation and debugging options
134
135#include "expat\expat.h"
136
137#include "helpers\linklist.h"
138#include "helpers\standards.h"
139#include "helpers\stringh.h"
140#include "helpers\tree.h"
141#include "helpers\xstring.h"
142#include "helpers\xml.h"
143
144#pragma hdrstop
145
146/*
147 *@@category: Helpers\XML
148 * see xml.c.
149 */
150
151/*
152 *@@category: Helpers\XML\Document Object Model (DOM)
153 * see xml.c.
154 */
155
156/* ******************************************************************
157 *
158 * Error handling
159 *
160 ********************************************************************/
161
162/*
163 *@@ xmlDescribeError:
164 * returns a string describing the error corresponding to code.
165 * The code should be one of the enums that can be returned from
166 * XML_GetErrorCode.
167 *
168 *@@changed V0.9.9 (2001-02-14) [umoeller]: adjusted for new error codes
169 *@@changed V0.9.9 (2001-02-16) [umoeller]: moved this here from xmlparse.c
170 */
171
172const char* xmlDescribeError(int code)
173{
174 switch (code)
175 {
176 // start of expat (parser) errors
177 case ERROR_EXPAT_NO_MEMORY:
178 return "Out of memory";
179
180 case ERROR_EXPAT_SYNTAX:
181 return "Syntax error";
182 case ERROR_EXPAT_NO_ELEMENTS:
183 return "No element found";
184 case ERROR_EXPAT_INVALID_TOKEN:
185 return "Not well-formed (invalid token)";
186 case ERROR_EXPAT_UNCLOSED_TOKEN:
187 return "Unclosed token";
188 case ERROR_EXPAT_PARTIAL_CHAR:
189 return "Unclosed token";
190 case ERROR_EXPAT_TAG_MISMATCH:
191 return "Mismatched tag";
192 case ERROR_EXPAT_DUPLICATE_ATTRIBUTE:
193 return "Duplicate attribute";
194 case ERROR_EXPAT_JUNK_AFTER_DOC_ELEMENT:
195 return "Junk after root element";
196 case ERROR_EXPAT_PARAM_ENTITY_REF:
197 return "Illegal parameter entity reference";
198 case ERROR_EXPAT_UNDEFINED_ENTITY:
199 return "Undefined entity";
200 case ERROR_EXPAT_RECURSIVE_ENTITY_REF:
201 return "Recursive entity reference";
202 case ERROR_EXPAT_ASYNC_ENTITY:
203 return "Asynchronous entity";
204 case ERROR_EXPAT_BAD_CHAR_REF:
205 return "Reference to invalid character number";
206 case ERROR_EXPAT_BINARY_ENTITY_REF:
207 return "Reference to binary entity";
208 case ERROR_EXPAT_ATTRIBUTE_EXTERNAL_ENTITY_REF:
209 return "Reference to external entity in attribute";
210 case ERROR_EXPAT_MISPLACED_XML_PI:
211 return "XML processing instruction not at start of external entity";
212 case ERROR_EXPAT_UNKNOWN_ENCODING:
213 return "Unknown encoding";
214 case ERROR_EXPAT_INCORRECT_ENCODING:
215 return "Encoding specified in XML declaration is incorrect";
216 case ERROR_EXPAT_UNCLOSED_CDATA_SECTION:
217 return "Unclosed CDATA section";
218 case ERROR_EXPAT_EXTERNAL_ENTITY_HANDLING:
219 return "Error in processing external entity reference";
220 case ERROR_EXPAT_NOT_STANDALONE:
221 return "Document is not standalone";
222 case ERROR_EXPAT_UNEXPECTED_STATE:
223 return "Unexpected parser state - please send a bug report";
224 // end of expat (parser) errors
225
226 // start of validation errors
227
228 case ERROR_DOM_UNDECLARED_ELEMENT:
229 return "Element has not been declared";
230 case ERROR_DOM_ROOT_ELEMENT_MISNAMED:
231 return "Root element name does not match DOCTYPE name";
232 case ERROR_DOM_INVALID_ROOT_ELEMENT:
233 return "Invalid or duplicate root element";
234
235 case ERROR_DOM_INVALID_SUBELEMENT:
236 return "Invalid sub-element in parent element";
237 case ERROR_DOM_DUPLICATE_ELEMENT_DECL:
238 return "Duplicate element declaration";
239 case ERROR_DOM_DUPLICATE_ATTRIBUTE_DECL:
240 return "Duplicate attribute declaration";
241 case ERROR_DOM_UNDECLARED_ATTRIBUTE:
242 return "Undeclared attribute in element";
243 case ERROR_ELEMENT_CANNOT_HAVE_CONTENT:
244 return "Element cannot have content";
245 case ERROR_DOM_INVALID_ATTRIB_VALUE:
246 return "Invalid attribute value";
247 case ERROR_DOM_REQUIRED_ATTRIBUTE_MISSING:
248 return "Required attribute is missing";
249 case ERROR_DOM_SUBELEMENT_IN_EMPTY_ELEMENT:
250 return "Subelement in empty element";
251
252 case ERROR_DOM_PARSING:
253 return "Parsing error";
254 case ERROR_DOM_VALIDITY:
255 return "Validity error";
256
257 case ERROR_DOM_NODETYPE_NOT_SUPPORTED:
258 return "DOM node type not supported";
259 case ERROR_DOM_NO_DOCUMENT:
260 return "No DOM document";
261 case ERROR_DOM_NO_ELEMENT:
262 return "No DOM element";
263 case ERROR_DOM_DUPLICATE_DOCTYPE:
264 return "Duplicate doctype";
265 case ERROR_DOM_DOCTYPE_ROOT_NAMES_MISMATCH:
266 return "Root element doesn't match doctype name";
267 case ERROR_DOM_INTEGRITY:
268 return "DOM integrity error";
269 case ERROR_DOM_DUPLICATE_ATTRIBUTE:
270 return "Duplicate attribute";
271
272 case ERROR_DOM_VALIDATE_INVALID_ELEMENT:
273 return "Validation error: Undeclared element name";
274 case ERROR_DOM_ELEMENT_DECL_OUTSIDE_DOCTYPE:
275 return "Element declaration outside doctype";
276 case ERROR_DOM_ATTLIST_DECL_OUTSIDE_DOCTYPE:
277 return "Attlist declaration outside doctype";
278
279 case ERROR_DOM_INCOMPLETE_ENCODING_MAP:
280 return "Incomplete encoding map specified";
281
282 case ERROR_DOM_INVALID_EXTERNAL_HANDLER:
283 return "Invalid 'external' handler specified";
284 }
285
286 return NULL;
287}
288
289/*
290 *@@ xmlSetError:
291 * sets the DOM's error state and stores error information
292 * and parser position.
293 *
294 *@@added V0.9.9 (2001-02-16) [umoeller]
295 */
296
297VOID xmlSetError(PXMLDOM pDom,
298 APIRET arc,
299 const char *pcszFailing,
300 BOOL fValidityError) // in: if TRUE, this is a validation error;
301 // if FALSE, this is a parser error
302{
303 pDom->arcDOM = arc;
304 pDom->pcszErrorDescription = xmlDescribeError(pDom->arcDOM);
305 pDom->ulErrorLine = XML_GetCurrentLineNumber(pDom->pParser);
306 pDom->ulErrorColumn = XML_GetCurrentColumnNumber(pDom->pParser);
307
308 if (pcszFailing)
309 {
310 if (!pDom->pxstrFailingNode)
311 pDom->pxstrFailingNode = xstrCreate(0);
312
313 xstrcpy(pDom->pxstrFailingNode, pcszFailing, 0);
314 }
315
316 if (fValidityError)
317 pDom->fInvalid = TRUE;
318}
319
320/* ******************************************************************
321 *
322 * Most basic node management
323 *
324 ********************************************************************/
325
326/*
327 *@@ CompareXStrings:
328 * tree comparison func for NodeBases.
329 * This works for all trees which contain structures
330 * whose first item is a _NODEBASE because NODEBASE's first
331 * member is a TREE.
332 *
333 * Used in two places:
334 *
335 * -- to insert _CMELEMENTDECLNODE nodes into
336 * _DOMDOCTYPENODE.ElementDeclsTree;
337 *
338 * -- to insert _CMELEMENTPARTICLE nodes into
339 * _CMELEMENTDECLNODE.ElementNamesTree.
340 *
341 *@@added V0.9.9 (2001-02-16) [umoeller]
342 *@@changed V0.9.14 (2001-08-09) [umoeller]: fixed map bug which caused the whole XML stuff to fail
343 */
344
345STATIC int TREEENTRY CompareXStrings(ULONG ul1,
346 ULONG ul2)
347{
348 return strhcmp(((PXSTRING)ul1)->psz,
349 ((PXSTRING)ul2)->psz);
350}
351
352/*
353 *@@ xmlCreateNodeBase:
354 * creates a new NODEBASE node.
355 *
356 * Gets called from xmlCreateDomNode also to create
357 * a DOMNODE, since that in turn has a NODEBASE.
358 *
359 *@@added V0.9.9 (2001-02-16) [umoeller]
360 */
361
362APIRET xmlCreateNodeBase(NODEBASETYPE ulNodeType, // in: node type
363 ULONG cb, // in: size of struct
364 const char *pcszNodeName, // in: node name or NULL
365 ULONG ulNodeNameLength, // in: node name length
366 // or 0 to run strlen(pcszNodeName)
367 PNODEBASE *ppNew) // out: new node
368{
369 APIRET arc = NO_ERROR;
370 PNODEBASE pNewNode = (PNODEBASE)malloc(cb);
371
372 if (!pNewNode)
373 arc = ERROR_NOT_ENOUGH_MEMORY;
374 else
375 {
376 memset(pNewNode, 0, cb);
377 pNewNode->ulNodeType = ulNodeType;
378
379 pNewNode->Tree.ulKey = (ULONG)&pNewNode->strNodeName;
380
381 xstrInit(&pNewNode->strNodeName, 0);
382 if (pcszNodeName)
383 {
384 xstrcpy(&pNewNode->strNodeName,
385 pcszNodeName,
386 ulNodeNameLength);
387 }
388
389 *ppNew = pNewNode;
390 }
391
392 return arc;
393}
394
395/*
396 *@@ xmlDeleteNode:
397 * deletes a NODEBASE and frees memory that was
398 * associated with its members.
399 *
400 * After calling this, pNode is no longer valid.
401 *
402 * If you invoke this on a DOCUMENT node, the
403 * entire DOM tree will get deleted recursively.
404 *
405 *@@added V0.9.9 (2001-02-16) [umoeller]
406 *@@changed V0.9.14 (2001-08-09) [umoeller]: fixed crash on string delete
407 */
408
409VOID xmlDeleteNode(PNODEBASE pNode)
410{
411 if (pNode)
412 {
413 PLISTNODE pNodeThis;
414 PDOMNODE pDomNode = NULL;
415
416 LINKLIST llDeleteNodes; // list that nodes to be deleted
417 // can be appended to
418 PLISTNODE pDelNode;
419 lstInit(&llDeleteNodes, FALSE);
420
421 // now handle special types and their allocations
422 switch (pNode->ulNodeType)
423 {
424 case DOMNODE_ELEMENT:
425 {
426 PDOMNODE pAttrib;
427
428 pDomNode = (PDOMNODE)pNode;
429
430 // delete all attribute nodes
431 pAttrib = (PDOMNODE)treeFirst(pDomNode->AttributesMap);
432 while (pAttrib)
433 {
434 // call delete recursively
435 PDOMNODE pNext = (PDOMNODE)treeNext((TREE*)pAttrib);
436 xmlDeleteNode((PNODEBASE)pAttrib);
437 // this will remove pAttrib from pNode's attrib
438 // tree and rebalance the tree
439 pAttrib = pNext;
440 }
441 break; }
442
443 case DOMNODE_ATTRIBUTE:
444 case DOMNODE_TEXT:
445 case DOMNODE_PROCESSING_INSTRUCTION:
446 case DOMNODE_COMMENT:
447 pDomNode = (PDOMNODE)pNode;
448 break;
449
450 case DOMNODE_DOCUMENT:
451 if (((PDOMDOCUMENTNODE)pNode)->pDocType)
452 xmlDeleteNode((PNODEBASE)((PDOMDOCUMENTNODE)pNode)->pDocType);
453 pDomNode = (PDOMNODE)pNode;
454 break;
455
456 case DOMNODE_DOCUMENT_TYPE:
457 {
458 PDOMDOCTYPENODE pDocType = (PDOMDOCTYPENODE)pNode;
459 PCMELEMENTDECLNODE pElDecl;
460 PCMATTRIBUTEDECLBASE pAttrDeclBase;
461
462 pDomNode = (PDOMNODE)pNode;
463
464 pElDecl = (PCMELEMENTDECLNODE)treeFirst(pDocType->ElementDeclsTree);
465 while (pElDecl)
466 {
467 lstAppendItem(&llDeleteNodes, pElDecl);
468 pElDecl = (PCMELEMENTDECLNODE)treeNext((TREE*)pElDecl);
469 }
470
471 pAttrDeclBase = (PCMATTRIBUTEDECLBASE)treeFirst(pDocType->AttribDeclBasesTree);
472 while (pAttrDeclBase)
473 {
474 lstAppendItem(&llDeleteNodes, pAttrDeclBase);
475 pAttrDeclBase = (PCMATTRIBUTEDECLBASE)treeNext((TREE*)pAttrDeclBase);
476 }
477
478 xstrClear(&pDocType->strPublicID);
479 xstrClear(&pDocType->strSystemID);
480 break; }
481
482 case ELEMENTPARTICLE_EMPTY:
483 case ELEMENTPARTICLE_ANY:
484 case ELEMENTPARTICLE_MIXED:
485 case ELEMENTPARTICLE_CHOICE:
486 case ELEMENTPARTICLE_SEQ:
487 case ELEMENTPARTICLE_NAME:
488 {
489 PCMELEMENTPARTICLE pp = (PCMELEMENTPARTICLE)pNode;
490 if (pp->pllSubNodes)
491 {
492 pDelNode = lstQueryFirstNode(pp->pllSubNodes);
493 while (pDelNode)
494 {
495 PCMELEMENTPARTICLE
496 pParticle = (PCMELEMENTPARTICLE)pDelNode->pItemData;
497 xmlDeleteNode((PNODEBASE)pParticle);
498 // treeDelete(pp-> // @@todo
499 pDelNode = pDelNode->pNext;
500 }
501 }
502 break; }
503
504 // case ATTRIBUTE_DECLARATION_ENUM: // this is a plain NODEBASE
505
506 case ATTRIBUTE_DECLARATION:
507 {
508 // PCMATTRIBUTEDECL pDecl = (PCMATTRIBUTEDECL)pNode;
509 break; }
510
511 case ATTRIBUTE_DECLARATION_BASE:
512 break;
513 }
514
515 if (pDomNode)
516 {
517 // recurse into child nodes
518 while (pNodeThis = lstQueryFirstNode(&pDomNode->llChildren))
519 // recurse!!
520 xmlDeleteNode((PNODEBASE)(pNodeThis->pItemData));
521 // this updates llChildren
522
523 if (pDomNode->pParentNode)
524 {
525 // node has a parent:
526 if (pNode->ulNodeType == DOMNODE_ATTRIBUTE)
527 // this is an attribute:
528 // remove from parent's attributes map
529 treeDelete(&pDomNode->pParentNode->AttributesMap,
530 NULL,
531 (TREE*)pNode);
532 else
533 // remove this node from the parent's list
534 // of child nodes before deleting this node
535 lstRemoveItem(&pDomNode->pParentNode->llChildren,
536 pNode);
537
538 pDomNode->pParentNode = NULL;
539 }
540
541 xstrFree(&pDomNode->pstrNodeValue);
542 lstClear(&pDomNode->llChildren);
543 }
544
545 pDelNode = lstQueryFirstNode(&llDeleteNodes);
546 while (pDelNode)
547 {
548 PNODEBASE pNodeBase = (PNODEBASE)pDelNode->pItemData;
549 xmlDeleteNode(pNodeBase);
550 pDelNode = pDelNode->pNext;
551 }
552
553 lstClear(&llDeleteNodes);
554
555 xstrClear(&pNode->strNodeName);
556 free(pNode);
557 }
558}
559
560/*
561 *@@ xmlCreateDomNode:
562 * creates a new DOMNODE with the specified
563 * type and parent. Other than that, the
564 * node fields are zeroed.
565 *
566 * If pParentNode is specified (which is required,
567 * unless you are creating a document node),
568 * its children list is automatically updated
569 * (unless this is an attribute node, which updates
570 * the attributes map).
571 *
572 * This returns the following errors:
573 *
574 * -- ERROR_NOT_ENOUGH_MEMORY
575 *
576 * -- ERROR_DOM_NOT_SUPPORTED: invalid ulNodeType
577 * specified.
578 *
579 * -- ERROR_DOM_WRONG_DOCUMENT: cannot find the
580 * document for this node. This happens if you do
581 * not have a document node at the root of your tree.
582 */
583
584APIRET xmlCreateDomNode(PDOMNODE pParentNode, // in: parent node or NULL if root
585 NODEBASETYPE ulNodeType, // in: DOMNODE_* type
586 const char *pcszNodeName, // in: node name or NULL
587 ULONG ulNodeNameLength, // in: node name length
588 // or 0 to run strlen(pcszNodeName)
589 PDOMNODE *ppNew) // out: new node
590{
591 PDOMNODE pNewNode = NULL;
592 APIRET arc = NO_ERROR;
593
594 ULONG cb = 0;
595
596 switch (ulNodeType)
597 {
598 case DOMNODE_DOCUMENT:
599 cb = sizeof(DOMDOCUMENTNODE);
600 break;
601
602 case DOMNODE_DOCUMENT_TYPE:
603 cb = sizeof(DOMDOCTYPENODE);
604 break;
605
606 default:
607 cb = sizeof(DOMNODE);
608 break;
609 }
610
611 if (!(arc = xmlCreateNodeBase(ulNodeType,
612 cb,
613 pcszNodeName,
614 ulNodeNameLength,
615 (PNODEBASE*)&pNewNode)))
616 {
617 pNewNode->pParentNode = pParentNode;
618
619 if (pParentNode)
620 {
621 // parent specified:
622 // check if this is an attribute
623 if (ulNodeType == DOMNODE_ATTRIBUTE)
624 {
625 // attribute:
626 // add to parent's attributes list
627 if (treeInsert(&pParentNode->AttributesMap,
628 NULL,
629 &pNewNode->NodeBase.Tree,
630 CompareXStrings))
631 arc = ERROR_DOM_DUPLICATE_ATTRIBUTE;
632 // shouldn't happen, because expat takes care of this
633 }
634 else
635 // append this new node to the parent's
636 // list of child nodes
637 lstAppendItem(&pParentNode->llChildren,
638 pNewNode);
639
640 if (!arc)
641 {
642 // set document pointer...
643 // if the parent node has a document pointer,
644 // we can copy that
645 if (pParentNode->pDocumentNode)
646 pNewNode->pDocumentNode = pParentNode->pDocumentNode;
647 else
648 // parent has no document pointer: then it is probably
649 // the document itself... check
650 if (pParentNode->NodeBase.ulNodeType == DOMNODE_DOCUMENT)
651 pNewNode->pDocumentNode = pParentNode;
652 else
653 arc = ERROR_DOM_NO_DOCUMENT;
654 }
655 }
656
657 lstInit(&pNewNode->llChildren, FALSE);
658 treeInit(&pNewNode->AttributesMap, NULL);
659 }
660
661 if (!arc)
662 *ppNew = pNewNode;
663 else
664 if (pNewNode)
665 free(pNewNode);
666
667 return arc;
668}
669
670/* ******************************************************************
671 *
672 * Specific DOM node constructors
673 *
674 ********************************************************************/
675
676/*
677 *@@ xmlCreateElementNode:
678 * creates a new element node with the specified name.
679 *
680 *@@added V0.9.9 (2001-02-14) [umoeller]
681 */
682
683APIRET xmlCreateElementNode(PDOMNODE pParent, // in: parent node (either document or element)
684 const char *pcszElement, // in: element name (null-terminated)
685 PDOMNODE *ppNew)
686{
687 PDOMNODE pNew = NULL;
688 APIRET arc;
689
690 if (!(arc = xmlCreateDomNode(pParent,
691 DOMNODE_ELEMENT,
692 pcszElement,
693 0,
694 &pNew)))
695 *ppNew = pNew;
696
697 return arc;
698}
699
700/*
701 *@@ xmlCreateAttributeNode:
702 * creates a new attribute node with the specified data.
703 *
704 * NOTE: Attributes have no "parent" node, technically.
705 * They are added to a special, separate list in @DOM_ELEMENT
706 * nodes.
707 *
708 * This returns the following errors:
709 *
710 * -- Error codes from xmlCreateDomNode.
711 *
712 * -- ERROR_DOM_NO_ELEMENT: pElement is invalid or does
713 * not point to an @DOM_ELEMENT node.
714 *
715 *@@added V0.9.9 (2001-02-14) [umoeller]
716 *@@changed V1.0.2 (2003-02-07) [umoeller]: added lenValue param
717 */
718
719APIRET xmlCreateAttributeNode(PDOMNODE pElement, // in: element node
720 const char *pcszName, // in: attribute name (null-terminated)
721 const char *pcszValue, // in: attribute value (null-terminated)
722 ULONG lenValue, // in: length of value (must be specified)
723 PDOMNODE *ppNew)
724{
725 APIRET arc;
726 PDOMNODE pNew = NULL;
727
728 if ( (!pElement)
729 || (pElement->NodeBase.ulNodeType != DOMNODE_ELEMENT)
730 )
731 return ERROR_DOM_NO_ELEMENT;
732
733 if (!(arc = xmlCreateDomNode(pElement, // this takes care of adding to the list
734 DOMNODE_ATTRIBUTE,
735 pcszName,
736 0,
737 &pNew)))
738 {
739 pNew->pstrNodeValue = xstrCreate(lenValue + 1);
740 xstrcpy(pNew->pstrNodeValue, pcszValue, lenValue);
741
742 *ppNew = pNew;
743 }
744
745 return arc;
746}
747
748/*
749 *@@ xmlCreateTextNode:
750 * creates a new text node with the specified content.
751 *
752 * Note: This differs from the createText method
753 * as specified by DOM, which has no ulLength parameter.
754 * We need this for speed with @expat though.
755 *
756 *@@added V0.9.9 (2001-02-14) [umoeller]
757 */
758
759APIRET xmlCreateTextNode(PDOMNODE pParent, // in: parent element node
760 const char *pcszText, // in: ptr to start of text
761 ULONG lenText, // in: length of *pcszText
762 PDOMNODE *ppNew)
763{
764 PDOMNODE pNew = NULL;
765 APIRET arc;
766
767 if (!(arc = xmlCreateDomNode(pParent,
768 DOMNODE_TEXT,
769 NULL,
770 0,
771 &pNew)))
772 {
773 PSZ pszNodeValue;
774 if (pszNodeValue = (PSZ)malloc(lenText + 1))
775 {
776 memcpy(pszNodeValue, pcszText, lenText);
777 pszNodeValue[lenText] = '\0';
778 pNew->pstrNodeValue = xstrCreate(0);
779 xstrset(pNew->pstrNodeValue, pszNodeValue);
780
781 *ppNew = pNew;
782 }
783 else
784 {
785 arc = ERROR_NOT_ENOUGH_MEMORY;
786 xmlDeleteNode((PNODEBASE)pNew);
787 }
788 }
789
790 return arc;
791}
792
793/*
794 *@@ xmlCreateCommentNode:
795 * creates a new comment node with the specified
796 * content.
797 *
798 *@@added V0.9.9 (2001-02-14) [umoeller]
799 */
800
801APIRET xmlCreateCommentNode(PDOMNODE pParent, // in: parent element node
802 const char *pcszText, // in: comment (null-terminated)
803 PDOMNODE *ppNew)
804{
805 PDOMNODE pNew = NULL;
806 APIRET arc;
807 if (!(arc = xmlCreateDomNode(pParent,
808 DOMNODE_COMMENT,
809 NULL,
810 0,
811 &pNew)))
812 {
813 pNew->pstrNodeValue = xstrCreate(0);
814 xstrcpy(pNew->pstrNodeValue, pcszText, 0);
815 *ppNew = pNew;
816 }
817
818 return arc;
819}
820
821/*
822 *@@ xmlCreatePINode:
823 * creates a new processing instruction node with the
824 * specified data.
825 *
826 *@@added V0.9.9 (2001-02-14) [umoeller]
827 */
828
829APIRET xmlCreatePINode(PDOMNODE pParent, // in: parent element node
830 const char *pcszTarget, // in: PI target (null-terminated)
831 const char *pcszData, // in: PI data (null-terminated)
832 PDOMNODE *ppNew)
833{
834 PDOMNODE pNew = NULL;
835 APIRET arc;
836
837 if (!(arc = xmlCreateDomNode(pParent,
838 DOMNODE_PROCESSING_INSTRUCTION,
839 pcszTarget,
840 0,
841 &pNew)))
842 {
843 pNew->pstrNodeValue = xstrCreate(0);
844 xstrcpy(pNew->pstrNodeValue, pcszData, 0);
845
846 *ppNew = pNew;
847 }
848
849 return arc;
850}
851
852/*
853 *@@ xmlCreateDocumentTypeNode:
854 * creates a new document type node with the
855 * specified data.
856 *
857 *@@added V0.9.9 (2001-02-14) [umoeller]
858 */
859
860APIRET xmlCreateDocumentTypeNode(PDOMDOCUMENTNODE pDocumentNode, // in: document node
861 const char *pcszDoctypeName,
862 const char *pcszSysid,
863 const char *pcszPubid,
864 int fHasInternalSubset,
865 PDOMDOCTYPENODE *ppNew)
866{
867 APIRET arc = NO_ERROR;
868
869 if (pDocumentNode->pDocType)
870 // we already have a doctype:
871 arc = ERROR_DOM_DUPLICATE_DOCTYPE;
872 else
873 {
874 // create doctype node
875 PDOMDOCTYPENODE pNew = NULL;
876 if (!(arc = xmlCreateDomNode((PDOMNODE)pDocumentNode,
877 DOMNODE_DOCUMENT_TYPE,
878 pcszDoctypeName,
879 0,
880 (PDOMNODE*)&pNew)))
881 {
882 // the node has already been added to the children
883 // list of the document node... in addition, set
884 // the doctype field in the document
885 pDocumentNode->pDocType = pNew;
886
887 // initialize the extra fields
888 xstrcpy(&pNew->strPublicID, pcszPubid, 0);
889 xstrcpy(&pNew->strSystemID, pcszSysid, 0);
890 pNew->fHasInternalSubset = fHasInternalSubset;
891
892 treeInit(&pNew->ElementDeclsTree, NULL);
893 treeInit(&pNew->AttribDeclBasesTree, NULL);
894
895 *ppNew = pNew;
896 }
897 }
898 return arc;
899}
900
901/* ******************************************************************
902 *
903 * DOM level 3 content models
904 *
905 ********************************************************************/
906
907/*
908 *@@ SetupParticleAndSubs:
909 *
910 * This creates sub-particles and recurses to set them up,
911 * if necessary.
912 *
913 *@@added V0.9.9 (2001-02-16) [umoeller]
914 */
915
916STATIC APIRET SetupParticleAndSubs(PCMELEMENTPARTICLE pParticle,
917 PXMLCONTENT pModel,
918 TREE **ppElementNamesTree) // in: ptr to _CMELEMENTDECLNODE.ElementNamesTree
919 // (passed to all recursions)
920{
921 APIRET arc = NO_ERROR;
922
923 // set up member NODEBASE
924 switch (pModel->type)
925 {
926 case XML_CTYPE_EMPTY: // that's easy
927 pParticle->NodeBase.ulNodeType = ELEMENTPARTICLE_EMPTY;
928 break;
929
930 case XML_CTYPE_ANY: // that's easy
931 pParticle->NodeBase.ulNodeType = ELEMENTPARTICLE_ANY;
932 break;
933
934 case XML_CTYPE_NAME: // that's easy
935 pParticle->NodeBase.ulNodeType = ELEMENTPARTICLE_NAME;
936 xstrcpy(&pParticle->NodeBase.strNodeName, pModel->name, 0);
937 treeInsert(ppElementNamesTree,
938 NULL,
939 &pParticle->NodeBase.Tree,
940 CompareXStrings);
941 break;
942
943 case XML_CTYPE_MIXED:
944 pParticle->NodeBase.ulNodeType = ELEMENTPARTICLE_MIXED;
945 break;
946
947 case XML_CTYPE_CHOICE:
948 pParticle->NodeBase.ulNodeType = ELEMENTPARTICLE_CHOICE;
949 break;
950
951 case XML_CTYPE_SEQ:
952 pParticle->NodeBase.ulNodeType = ELEMENTPARTICLE_SEQ;
953 break;
954 }
955
956 pParticle->ulRepeater = pModel->quant;
957
958 if (pModel->numchildren)
959 {
960 // these are the three cases where we have subnodes
961 // in the XMLCONTENT... go for these and recurse
962 ULONG ul;
963 pParticle->pllSubNodes = lstCreate(FALSE);
964 for (ul = 0;
965 ul < pModel->numchildren;
966 ul++)
967 {
968 PXMLCONTENT pSubModel = &pModel->children[ul];
969 PCMELEMENTPARTICLE pSubNew = NULL;
970 if (!(arc = xmlCreateNodeBase(TYPE_UNKNOWN, // node type... for now
971 sizeof(CMELEMENTPARTICLE),
972 0,
973 0,
974 (PNODEBASE*)&pSubNew)))
975 {
976 if (!(arc = SetupParticleAndSubs(pSubNew,
977 pSubModel,
978 ppElementNamesTree)))
979 {
980 // no error: append sub-particle to this particle's
981 // children list
982 lstAppendItem(pParticle->pllSubNodes,
983 pSubNew);
984 // and store this particle as the parent in the
985 // sub-particle
986 pSubNew->pParentParticle = pParticle;
987 }
988 }
989
990 if (arc)
991 break;
992 }
993 }
994
995 return arc;
996}
997
998/*
999 *@@ xmlCreateElementDecl:
1000 * creates a new _CMELEMENTDECLNODE for the specified
1001 * _XMLCONTENT content model (which is the @expat structure).
1002 * This recurses, if necessary.
1003 *
1004 * NOTE: As opposed to the other "create" functions,
1005 * this does not take a parent node as input. If this
1006 * returns NO_ERROR, it is the caller's responsibility
1007 * to add the produced node to the document's DOCTYPE node.
1008 *
1009 *@@added V0.9.9 (2001-02-16) [umoeller]
1010 */
1011
1012APIRET xmlCreateElementDecl(const char *pcszName,
1013 PXMLCONTENT pModel,
1014 PCMELEMENTDECLNODE *ppNew)
1015{
1016 APIRET arc = NO_ERROR;
1017 PCMELEMENTDECLNODE pNew = NULL;
1018
1019 if (!(arc = xmlCreateNodeBase(TYPE_UNKNOWN, // for now
1020 sizeof(CMELEMENTDECLNODE),
1021 pcszName,
1022 0,
1023 (PNODEBASE*)&pNew)))
1024 {
1025 treeInit(&pNew->ParticleNamesTree, NULL);
1026
1027 // set up the "particle" member and recurse into sub-particles
1028 if (!(arc = SetupParticleAndSubs(&pNew->Particle,
1029 pModel,
1030 &pNew->ParticleNamesTree)))
1031 *ppNew = pNew;
1032 else
1033 free(pNew);
1034 }
1035
1036 return arc;
1037}
1038
1039/*
1040 *@@ ValidateElementChildren
1041 * validates the specified element against the document's @DTD,
1042 * more specifically, against the element declaration of the
1043 * new element's parent.
1044 *
1045 * This sets arcDOM in XMLDOM on errors.
1046 *
1047 * According to the XML spec, an element is valid if there
1048 * is a declaration matching the element declaration where the
1049 * element's name matches the element type, and _one_ of the
1050 * following holds:
1051 *
1052 * (1) The declaration matches EMPTY and the element has no @content. (done)
1053 *
1054 * (2) The declaration matches (children) (see @element_declaration)
1055 * and the sequence of child elements belongs to the language
1056 * generated by the regular expression in the content model, with
1057 * optional @white_space between the start-tag and the first child
1058 * element, between child elements, or between the last
1059 * child element and the end-tag. Note that a CDATA section
1060 * is never considered "whitespace", even if it contains
1061 * white space only. @@todo
1062 *
1063 * (3) The declaration matches (mixed) (see @element_declaration)
1064 * and the content consists of @content and child elements
1065 * whose types match names in the content model. (done)
1066 *
1067 * (4) The declaration matches ANY, and the types of any child
1068 * elements have been declared. (done)
1069 *
1070 * Preconditions: The element must already have been inserted
1071 * into the parent element's list, or we cannot validate sequences.
1072 *
1073 *@@added V0.9.9 (2001-02-16) [umoeller]
1074 */
1075
1076STATIC VOID ValidateElement(PXMLDOM pDom,
1077 PDOMNODE pNewElement, // in: new element
1078 PCMELEMENTDECLNODE pParentElementDecl)
1079 // in: element decl of element's parent
1080{
1081 if (pDom && pNewElement)
1082 {
1083 if (!pParentElementDecl)
1084 {
1085 // this is always missing for the root element, of course,
1086 // because the parent is the document
1087 if (pNewElement->pParentNode == (PDOMNODE)pDom->pDocumentNode)
1088 return; // that's OK
1089 else
1090 xmlSetError(pDom,
1091 ERROR_DOM_VALIDATE_INVALID_ELEMENT,
1092 pNewElement->NodeBase.strNodeName.psz,
1093 TRUE);
1094 }
1095 else
1096 {
1097 ULONG ulDeclType = pParentElementDecl->Particle.NodeBase.ulNodeType;
1098
1099 switch (ulDeclType)
1100 {
1101 case ELEMENTPARTICLE_EMPTY:
1102 // this is an error for sure
1103 xmlSetError(pDom,
1104 ERROR_DOM_SUBELEMENT_IN_EMPTY_ELEMENT,
1105 pNewElement->NodeBase.strNodeName.psz,
1106 TRUE);
1107 break;
1108
1109 case ELEMENTPARTICLE_ANY:
1110 // that's always OK
1111 break;
1112
1113 case ELEMENTPARTICLE_MIXED:
1114 case ELEMENTPARTICLE_CHOICE:
1115 case ELEMENTPARTICLE_SEQ:
1116 {
1117 PXSTRING pstrNewElementName
1118 = &pNewElement->NodeBase.strNodeName;
1119
1120 // for all these, we first need to check if
1121 // the element is allowed at all
1122 PCMELEMENTPARTICLE pParticle
1123 = (PCMELEMENTPARTICLE)treeFind(
1124 pParentElementDecl->ParticleNamesTree,
1125 (ULONG)pstrNewElementName,
1126 CompareXStrings);
1127 if (!pParticle)
1128 // not found: then this element is not allowed within this
1129 // parent
1130 xmlSetError(pDom,
1131 ERROR_DOM_INVALID_SUBELEMENT,
1132 pstrNewElementName->psz,
1133 TRUE);
1134 else
1135 {
1136 // the element is allowed at all: now check for the
1137 // lists case... @@todo
1138 switch (ulDeclType)
1139 {
1140 case ELEMENTPARTICLE_CHOICE:
1141 break;
1142
1143 case ELEMENTPARTICLE_SEQ:
1144 break;
1145 }
1146 }
1147
1148 break; }
1149 }
1150 }
1151 }
1152 else
1153 pDom->arcDOM = ERROR_INVALID_PARAMETER;
1154
1155 // yes: get the element decl from the tree
1156 /* PCMELEMENTDECLNODE pElementDecl = xmlFindElementDecl(pDom,
1157 &pElement->NodeBase.strNodeName);
1158 if (!pElementDecl)
1159 {
1160 xmlSetError(pDom,
1161 ERROR_DOM_UNDECLARED_ELEMENT,
1162 pElement->NodeBase.strNodeName.psz,
1163 TRUE);
1164 }
1165 else
1166 {
1167 // element has been declared:
1168 // check if it may appear in this element's parent...
1169 PDOMNODE pParentElement = pElement->pParentNode;
1170
1171 if (!pParentElement)
1172 pDom->arcDOM = ERROR_DOM_INTEGRITY;
1173 else switch (pParentElement->NodeBase.ulNodeType)
1174 {
1175 case DOMNODE_DOCUMENT:
1176 {
1177 // if this is the root element, compare its name
1178 // to the DOCTYPE name
1179 if (pParentElement != (PDOMNODE)pDom->pDocumentNode)
1180 xmlSetError(pDom,
1181 ERROR_DOM_INVALID_ROOT_ELEMENT,
1182 pElement->NodeBase.strNodeName.psz,
1183 TRUE);
1184 else if (strcmp(pDom->pDocumentNode->DomNode.NodeBase.strNodeName.psz,
1185 pElement->NodeBase.strNodeName.psz))
1186 // no match:
1187 xmlSetError(pDom,
1188 ERROR_DOM_ROOT_ELEMENT_MISNAMED,
1189 pElement->NodeBase.strNodeName.psz,
1190 TRUE);
1191 break; }
1192
1193 case DOMNODE_ELEMENT:
1194 {
1195 // parent of element is another element:
1196 // check the parent in the DTD and find out if
1197 // this element may appear in the parent element
1198 PCMELEMENTDECLNODE pParentElementDecl
1199 = xmlFindElementDecl(pDom,
1200 &pParentElement->NodeBase.strNodeName);
1201 if (!pParentElementDecl)
1202 pDom->arcDOM = ERROR_DOM_INTEGRITY;
1203 else
1204 {
1205 // now check the element names tree of the parent element decl
1206 // for whether this element is allowed as a sub-element at all
1207 PCMELEMENTPARTICLE pParticle
1208 = treeFindEQData(&pParentElementDecl->ParticleNamesTree,
1209 (void*)pElement->NodeBase.strNodeName.psz,
1210 CompareNodeBaseData);
1211 if (!pParticle)
1212 // not found: then this element is not allowed within this
1213 // parent
1214 xmlSetError(pDom,
1215 ERROR_DOM_INVALID_SUBELEMENT,
1216 pElement->NodeBase.strNodeName.psz,
1217 TRUE);
1218 }
1219 break; }
1220 }
1221 }
1222 */
1223}
1224
1225/*
1226 *@@ ValidateAttributeType:
1227 * validates the specified attribute's type against the
1228 * document's @DTD.
1229 *
1230 * This sets arcDOM in XMLDOM on errors.
1231 *
1232 *@@added V0.9.9 (2001-02-16) [umoeller]
1233 */
1234
1235STATIC VOID ValidateAttributeType(PXMLDOM pDom,
1236 PDOMNODE pAttrib,
1237 PCMATTRIBUTEDECLBASE *ppAttribDeclBase)
1238{
1239 PDOMNODE pElement = pAttrib->pParentNode;
1240
1241 PCMATTRIBUTEDECL pAttribDecl = xmlFindAttribDecl(pDom,
1242 &pElement->NodeBase.strNodeName,
1243 &pAttrib->NodeBase.strNodeName,
1244 ppAttribDeclBase);
1245 if (!pAttribDecl)
1246 xmlSetError(pDom,
1247 ERROR_DOM_UNDECLARED_ATTRIBUTE,
1248 pAttrib->NodeBase.strNodeName.psz,
1249 TRUE);
1250 else
1251 {
1252 // check if the attribute value is allowed
1253 switch (pAttribDecl->ulAttrType)
1254 {
1255 case CMAT_CDATA:
1256 case CMAT_ID:
1257 case CMAT_IDREF:
1258 case CMAT_IDREFS: // ###
1259 case CMAT_ENTITY:
1260 case CMAT_ENTITIES:
1261 case CMAT_NMTOKEN:
1262 case CMAT_NMTOKENS:
1263 break;
1264
1265 case CMAT_ENUM:
1266 {
1267 // enumeration: then check if it has one of the
1268 // allowed values
1269 PNODEBASE pValue = (PNODEBASE)treeFind(
1270 pAttribDecl->ValuesTree,
1271 (ULONG)pAttrib->pstrNodeValue,
1272 CompareXStrings);
1273 if (!pValue)
1274 xmlSetError(pDom,
1275 ERROR_DOM_INVALID_ATTRIB_VALUE,
1276 pAttrib->NodeBase.strNodeName.psz,
1277 TRUE);
1278 }
1279 }
1280
1281 if (pAttribDecl->ulConstraint == CMAT_FIXED_VALUE)
1282 if (strcmp(pAttrib->pstrNodeValue->psz, pAttribDecl->pstrDefaultValue->psz))
1283 // fixed value doesn't match:
1284 xmlSetError(pDom,
1285 ERROR_DOM_INVALID_ATTRIB_VALUE,
1286 pAttrib->NodeBase.strNodeName.psz,
1287 TRUE);
1288 }
1289}
1290
1291/*
1292 *@@ ValidateAllAttributes:
1293 * validates the constraints of all attributes of the specified
1294 * element against the document's @DTD.
1295 *
1296 *@@added V0.9.9 (2001-02-16) [umoeller]
1297 */
1298
1299STATIC VOID ValidateAllAttributes(PXMLDOM pDom,
1300 PCMATTRIBUTEDECLBASE pAttribDeclBase,
1301 PDOMNODE pNewElement)
1302{
1303 PCMATTRIBUTEDECL pDeclThis
1304 = (PCMATTRIBUTEDECL)treeFirst(pAttribDeclBase->AttribDeclsTree);
1305
1306 while ((pDeclThis) && (!pDom->arcDOM))
1307 {
1308 // if attribute is all optional: then we don't need
1309 // to check for whether it's here
1310 if ( (pDeclThis->ulConstraint != CMAT_IMPLIED)
1311 && (pDeclThis->ulConstraint != CMAT_DEFAULT_VALUE)
1312 // we don't have to check this case because expat
1313 // already adds default attributes for us
1314 )
1315 {
1316 // for all others , we need to find the attribute
1317 PXSTRING pstrAttrNameThis = &pDeclThis->NodeBase.strNodeName;
1318 PDOMNODE pAttrNode = (PDOMNODE)treeFind(
1319 pNewElement->AttributesMap,
1320 (ULONG)pstrAttrNameThis,
1321 CompareXStrings);
1322
1323 // now switch again
1324 switch (pDeclThis->ulConstraint)
1325 {
1326 case CMAT_REQUIRED:
1327 if (!pAttrNode)
1328 // required, but no attribute with this name exists:
1329 xmlSetError(pDom,
1330 ERROR_DOM_REQUIRED_ATTRIBUTE_MISSING,
1331 pstrAttrNameThis->psz,
1332 TRUE);
1333 break;
1334 }
1335 }
1336
1337 pDeclThis = (PCMATTRIBUTEDECL)treeNext((TREE*)pDeclThis);
1338 }
1339}
1340
1341/* ******************************************************************
1342 *
1343 * Expat stack
1344 *
1345 ********************************************************************/
1346
1347/*
1348 *@@ DOMSTACKITEM:
1349 *
1350 *@@added V0.9.9 (2001-02-16) [umoeller]
1351 */
1352
1353typedef struct _DOMSTACKITEM
1354{
1355 PDOMNODE pDomNode;
1356 PCMELEMENTDECLNODE pElementDecl;
1357
1358} DOMSTACKITEM, *PDOMSTACKITEM;
1359
1360/*
1361 *@@ PopElementStack:
1362 *
1363 * NOTE:
1364 *
1365 *@@added V0.9.9 (2001-02-16) [umoeller]
1366 */
1367
1368STATIC PDOMSTACKITEM PopElementStack(PXMLDOM pDom,
1369 PLISTNODE *ppListNode)
1370{
1371 PDOMSTACKITEM pStackItem = NULL;
1372 PLISTNODE pParentLN = lstPop(&pDom->llElementStack);
1373
1374 if (!pParentLN)
1375 pDom->arcDOM = ERROR_DOM_NO_ELEMENT;
1376 else
1377 {
1378 // we have at least one node:
1379 pStackItem = (PDOMSTACKITEM)pParentLN->pItemData;
1380
1381 if (ppListNode)
1382 *ppListNode = pParentLN;
1383 }
1384
1385 return pStackItem;
1386}
1387
1388/*
1389 *@@ PushElementStack:
1390 *
1391 * NOTE: pDomNode will most frequently be an element
1392 * node, but will also be the document for root and
1393 * a DOCTYPE node while parsing the DTD.
1394 *
1395 *@@added V0.9.9 (2001-02-16) [umoeller]
1396 */
1397
1398STATIC VOID PushElementStack(PXMLDOM pDom,
1399 PDOMNODE pDomNode)
1400{
1401 PDOMSTACKITEM pNew = (PDOMSTACKITEM)malloc(sizeof(*pNew));
1402 if (!pNew)
1403 pDom->arcDOM = ERROR_NOT_ENOUGH_MEMORY;
1404 else
1405 {
1406 memset(pNew, 0, sizeof(*pNew));
1407 pNew->pDomNode = pDomNode;
1408
1409 // shall we validate?
1410 if ( (pDom->pDocTypeNode)
1411 && (pDomNode->NodeBase.ulNodeType == DOMNODE_ELEMENT)
1412 )
1413 pNew->pElementDecl = xmlFindElementDecl(pDom,
1414 &pDomNode->NodeBase.strNodeName);
1415
1416 lstPush(&pDom->llElementStack,
1417 pNew);
1418 }
1419}
1420
1421/* ******************************************************************
1422 *
1423 * Expat handlers
1424 *
1425 ********************************************************************/
1426
1427/*
1428 *@@ UnknownEncodingHandler:
1429 * @expat handler called when the xml
1430 * @text_declaration has an @encoding that is not
1431 * one of the four encodings built into expat.
1432 *
1433 * See XML_SetUnknownEncodingHandler.
1434 *
1435 *@@added V0.9.14 (2001-08-09) [umoeller]
1436 */
1437
1438STATIC int EXPATENTRY UnknownEncodingHandler(void *pUserData, // in: out PXMLDOM really
1439 const XML_Char *pcszName,
1440 XML_Encoding *pEncoding)
1441{
1442 PXMLDOM pDom = (PXMLDOM)pUserData;
1443
1444 ULONG ulCP;
1445 if ( (pDom->pfnGetCPData) // callback exists?
1446 && (!strncmp(pcszName, "cp", 2))
1447 && (strlen(pcszName) > 4) // at least five chars (e.g. "cp850")
1448 && (ulCP = atoi(pcszName + 2))
1449 )
1450 {
1451 // this is a PC codepage:
1452/* typedef struct _XML_Encoding
1453{
1454 int map[256];
1455 void *data;
1456 int (* EXPATENTRY convert)(void *data, const char *s);
1457 void (* EXPATENTRY release)(void *data);
1458} XML_Encoding; */
1459
1460 // ZERO(pEncoding);
1461
1462 pEncoding->convert = NULL;
1463 pEncoding->release = NULL;
1464
1465 memset(&pEncoding->map, -1, sizeof(pEncoding->map));
1466
1467 if (pDom->pfnGetCPData(pDom,
1468 ulCP,
1469 pEncoding->map))
1470 {
1471 // go check if there's any -1 chars left
1472 ULONG ul;
1473 for (ul = 0;
1474 ul < 256;
1475 ul++)
1476 {
1477 if (pEncoding->map[ul] < 0)
1478 xmlSetError(pDom,
1479 ERROR_DOM_INCOMPLETE_ENCODING_MAP,
1480 NULL,
1481 FALSE);
1482 }
1483 // return success
1484 return 1;
1485 }
1486 }
1487
1488 // error
1489 return 0;
1490}
1491
1492/*
1493 *@@ StartElementHandler:
1494 * @expat handler called when a new element is
1495 * found.
1496 *
1497 * We create a new record in the container and
1498 * push it onto our stack so we can insert
1499 * children into it. We first start with the
1500 * attributes.
1501 */
1502
1503STATIC void EXPATENTRY StartElementHandler(void *pUserData, // in: our PXMLDOM really
1504 const char *pcszElement,
1505 const char **papcszAttribs)
1506{
1507 PXMLDOM pDom = (PXMLDOM)pUserData;
1508
1509 // continue parsing only if we had no errors so far
1510 if (!pDom->arcDOM)
1511 {
1512 ULONG i;
1513
1514 PDOMSTACKITEM pSI = PopElementStack(pDom,
1515 NULL); // no free
1516 if (!pDom->arcDOM)
1517 {
1518 PDOMNODE pParent = pSI->pDomNode,
1519 pNew = NULL;
1520
1521 if (!(pDom->arcDOM = xmlCreateElementNode(pParent,
1522 pcszElement,
1523 &pNew)))
1524 // OK, node is valid:
1525 // push this on the stack so we can add child elements
1526 PushElementStack(pDom,
1527 pNew);
1528
1529 // shall we validate?
1530 if ( (!pDom->arcDOM)
1531 && (pDom->pDocTypeNode)
1532 )
1533 ValidateElement(pDom,
1534 pNew, // new element
1535 pSI->pElementDecl); // parent's elem decl
1536
1537 if (!pDom->arcDOM)
1538 {
1539 PCMATTRIBUTEDECLBASE pAttribDeclBase = NULL;
1540
1541 // shall we validate?
1542 if (pDom->pDocTypeNode)
1543 // yes: get attrib decl base for speed
1544 pAttribDeclBase
1545 = xmlFindAttribDeclBase(pDom,
1546 &pNew->NodeBase.strNodeName);
1547
1548 // now for the attribs
1549 for (i = 0;
1550 (papcszAttribs[i]) && (!pDom->arcDOM);
1551 i += 2)
1552 {
1553 PDOMNODE pAttrib;
1554 PCSZ pcszValue = papcszAttribs[i + 1]; // attr value
1555 if (!(pDom->arcDOM = xmlCreateAttributeNode(pNew, // element,
1556 papcszAttribs[i], // attr name
1557 pcszValue,
1558 strlen(pcszValue),
1559 &pAttrib)))
1560 {
1561 // shall we validate?
1562 if (pDom->pDocTypeNode)
1563 ValidateAttributeType(pDom,
1564 pAttrib,
1565 &pAttribDeclBase);
1566 }
1567 else
1568 {
1569 xmlSetError(pDom,
1570 pDom->arcDOM,
1571 papcszAttribs[i],
1572 FALSE); // validation
1573 break;
1574 }
1575 }
1576
1577 // OK, now we got all attributes:
1578 // now look for defaults in the DTD,
1579 // if we shall validate
1580 if ( (pDom->pDocTypeNode)
1581 && (!pDom->arcDOM)
1582 && (pAttribDeclBase)
1583 )
1584 ValidateAllAttributes(pDom,
1585 pAttribDeclBase,
1586 pNew);
1587 }
1588 }
1589
1590 pDom->pLastWasTextNode = NULL;
1591 }
1592}
1593
1594/*
1595 *@@ EndElementHandler:
1596 * @expat handler for when parsing an element is done.
1597 * We pop the element off of our stack then.
1598 */
1599
1600STATIC void EXPATENTRY EndElementHandler(void *pUserData, // in: our PXMLDOM really
1601 const XML_Char *name)
1602{
1603 PXMLDOM pDom = (PXMLDOM)pUserData;
1604 // continue parsing only if we had no errors so far
1605 if (!pDom->arcDOM)
1606 {
1607 PLISTNODE pStackLN = NULL;
1608 PDOMSTACKITEM pSI = PopElementStack(pDom,
1609 &pStackLN);
1610
1611 if (!pDom->arcDOM)
1612 {
1613 // shall we validate?
1614 /* if (pDom->pDocTypeNode)
1615 // yes:
1616 ValidateElementChildren(pDom,
1617 pSI->pDomNode); */
1618
1619 lstRemoveNode(&pDom->llElementStack, pStackLN); // auto-free
1620 }
1621 else
1622 pDom->arcDOM = ERROR_DOM_INTEGRITY;
1623
1624 pDom->pLastWasTextNode = NULL;
1625 }
1626}
1627
1628/*
1629 *@@ CharacterDataHandler:
1630 * @expat handler for character data (@content).
1631 *
1632 * Note: expat passes chunks of content without zero-terminating
1633 * them. We must concatenate the chunks to a full text node.
1634 */
1635
1636STATIC void EXPATENTRY CharacterDataHandler(void *pUserData, // in: our PXMLDOM really
1637 const XML_Char *s,
1638 int len)
1639{
1640 PXMLDOM pDom = (PXMLDOM)pUserData;
1641
1642 // continue parsing only if we had no errors so far
1643 if ( (!pDom->arcDOM)
1644 && (len)
1645 )
1646 {
1647 // we need a new text node:
1648 PDOMSTACKITEM pSI = PopElementStack(pDom,
1649 NULL); // no free
1650 if (!pDom->arcDOM)
1651 {
1652 PDOMNODE pParent = pSI->pDomNode;
1653 // pNew = NULL;
1654
1655 BOOL fIsWhitespace = FALSE;
1656
1657 // shall we validate?
1658 if (pDom->pDocTypeNode)
1659 {
1660 // yes: check if the parent element allows
1661 // for content at all (must be "mixed" model)
1662
1663 // get the element decl from the tree
1664 PCMELEMENTDECLNODE pElementDecl;
1665 if (pElementDecl = pSI->pElementDecl)
1666 {
1667 switch (pElementDecl->Particle.NodeBase.ulNodeType)
1668 {
1669 case ELEMENTPARTICLE_ANY:
1670 case ELEMENTPARTICLE_MIXED:
1671 // those two are okay
1672 break;
1673
1674 case ELEMENTPARTICLE_EMPTY:
1675 // that's an error for sure
1676 pDom->arcDOM = ERROR_ELEMENT_CANNOT_HAVE_CONTENT;
1677 break;
1678
1679 default:
1680 {
1681 // ELEMENTPARTICLE_CHOICE:
1682 // ELEMENTPARTICLE_SEQ:
1683 // with these two, we accept whitespace, but nothing
1684 // else... so if we have characters other than
1685 // whitespace, terminate
1686 ULONG ul;
1687 const char *p = s;
1688
1689 if (pDom->flParserFlags & DF_DROP_WHITESPACE)
1690 fIsWhitespace = TRUE;
1691
1692 for (ul = 0;
1693 ul < len;
1694 ul++, p++)
1695 if (!strchr("\r\n\t ", *p))
1696 {
1697 // other character:
1698 xmlSetError(pDom,
1699 ERROR_ELEMENT_CANNOT_HAVE_CONTENT,
1700 pParent->NodeBase.strNodeName.psz,
1701 TRUE);
1702 fIsWhitespace = FALSE;
1703 break;
1704 }
1705 }
1706 }
1707 }
1708
1709 } // end if (pDom->pDocTypeNode)
1710
1711 if (!fIsWhitespace)
1712 // this is false if any of the following
1713 // is true:
1714 // -- we are not validating at all
1715 // -- we are validating, but the the element
1716 // can have mixed content
1717 // -- we are validating and the element does
1718 // _not_ have mixed content and DF_DROP_WHITESPACE
1719 // is set, but the string is whitespace only
1720 // --> drop it then
1721
1722 if (pDom->pLastWasTextNode)
1723 // we had a text node, and no elements or other
1724 // stuff in between:
1725 xstrcat(pDom->pLastWasTextNode->pstrNodeValue,
1726 s,
1727 len);
1728 else
1729 pDom->arcDOM = xmlCreateTextNode(pParent,
1730 s,
1731 len,
1732 &pDom->pLastWasTextNode);
1733 }
1734 }
1735}
1736
1737/*
1738 *@@ CommentHandler:
1739 * @expat handler for @comments.
1740 *
1741 * Note: This is only set if DF_PARSECOMMENTS is
1742 * flagged with xmlCreateDOM.
1743 *
1744 *@@added V0.9.9 (2001-02-14) [umoeller]
1745 */
1746
1747STATIC void EXPATENTRY CommentHandler(void *pUserData, // in: our PXMLDOM really
1748 const XML_Char *data)
1749{
1750 PXMLDOM pDom = (PXMLDOM)pUserData;
1751
1752 // continue parsing only if we had no errors so far
1753 if (!pDom->arcDOM)
1754 {
1755 // we need a new text node:
1756 PDOMSTACKITEM pSI = PopElementStack(pDom,
1757 NULL); // no free
1758 if (!pDom->arcDOM)
1759 {
1760 PDOMNODE pParent = pSI->pDomNode,
1761 pNew = NULL;
1762
1763 pDom->arcDOM = xmlCreateCommentNode(pParent,
1764 data,
1765 &pNew);
1766 }
1767 }
1768}
1769
1770/*
1771 *@@ StartDoctypeDeclHandler:
1772 * @expat handler that is called at the start of a DOCTYPE
1773 * declaration, before any external or internal subset is
1774 * parsed.
1775 *
1776 * Both pcszSysid and pcszPubid may be NULL. "fHasInternalSubset"
1777 * will be non-zero if the DOCTYPE declaration has an internal subset.
1778 *
1779 *@@added V0.9.9 (2001-02-14) [umoeller]
1780 */
1781
1782STATIC void EXPATENTRY StartDoctypeDeclHandler(void *pUserData,
1783 const XML_Char *pcszDoctypeName,
1784 const XML_Char *pcszSysid,
1785 const XML_Char *pcszPubid,
1786 int fHasInternalSubset)
1787{
1788 PXMLDOM pDom = (PXMLDOM)pUserData;
1789
1790 // continue parsing only if we had no errors so far
1791 if (!pDom->arcDOM)
1792 {
1793 // get the document node
1794 PDOMDOCUMENTNODE pDocumentNode = pDom->pDocumentNode;
1795 if (!pDocumentNode)
1796 pDom->arcDOM = ERROR_DOM_NO_DOCUMENT;
1797 else
1798 {
1799 // doctype must be null
1800 if (pDom->pDocTypeNode)
1801 pDom->arcDOM = ERROR_DOM_DUPLICATE_DOCTYPE;
1802 else
1803 pDom->arcDOM = xmlCreateDocumentTypeNode(pDocumentNode,
1804 pcszDoctypeName,
1805 pcszSysid,
1806 pcszPubid,
1807 fHasInternalSubset,
1808 &pDom->pDocTypeNode);
1809 }
1810 }
1811}
1812
1813/*
1814 *@@ EndDoctypeDeclHandler:
1815 * @expat handler that is called at the end of a DOCTYPE
1816 * declaration, after parsing any external subset.
1817 *
1818 *@@added V0.9.9 (2001-02-14) [umoeller]
1819 */
1820
1821STATIC void EXPATENTRY EndDoctypeDeclHandler(void *pUserData) // in: our PXMLDOM really
1822{
1823 PXMLDOM pDom = (PXMLDOM)pUserData;
1824
1825 // continue parsing only if we had no errors so far
1826 if (!pDom->arcDOM)
1827 {
1828 }
1829}
1830
1831/*
1832 *@@ NotationDeclHandler:
1833 * @expat handler for @notation_declarations.
1834 *
1835 * @@todo
1836 *
1837 *@@added V0.9.9 (2001-02-14) [umoeller]
1838 */
1839
1840STATIC void EXPATENTRY NotationDeclHandler(void *pUserData, // in: our PXMLDOM really
1841 const XML_Char *pcszNotationName,
1842 const XML_Char *pcszBase,
1843 const XML_Char *pcszSystemId,
1844 const XML_Char *pcszPublicId)
1845{
1846 PXMLDOM pDom = (PXMLDOM)pUserData;
1847
1848 // continue parsing only if we had no errors so far
1849 if (!pDom->arcDOM)
1850 {
1851 }
1852}
1853
1854/*
1855 *@@ ExternalEntityRefHandler:
1856 * @expat handler for references to @external_entities.
1857 *
1858 * This handler is also called for processing an external DTD
1859 * subset if parameter entity parsing is in effect.
1860 * (See XML_SetParamEntityParsing.)
1861 *
1862 * The pcszContext argument specifies the parsing context in the
1863 * format expected by the context argument to
1864 * XML_ExternalEntityParserCreate; pcszContext is valid only until
1865 * the handler returns, so if the referenced entity is to be
1866 * parsed later, it must be copied.
1867 *
1868 * The pcszBase parameter is the base to use for relative system
1869 * identifiers. It is set by XML_SetBase and may be null.
1870 *
1871 * The pcszPublicId parameter is the public id given in the entity
1872 * declaration and may be null (since XML doesn't require public
1873 * identifiers).
1874 *
1875 * The pcszSystemId is the system identifier specified in the
1876 * entity declaration and is never null. This is an exact copy
1877 * of what was specified in the reference.
1878 *
1879 * There are a couple of ways in which this handler differs
1880 * from others. First, this handler returns an integer. A
1881 * non-zero value should be returned for successful handling
1882 * of the external entity reference. Returning a zero indicates
1883 * failure, and causes the calling parser to return an
1884 * ERROR_EXPAT_EXTERNAL_ENTITY_HANDLING error.
1885 *
1886 * Second, instead of having pUserData as its first argument,
1887 * it receives the parser that encountered the entity reference.
1888 * This, along with the context parameter, may be used as
1889 * arguments to a call to XML_ExternalEntityParserCreate.
1890 * Using the returned parser, the body of the external entity
1891 * can be recursively parsed.
1892 *
1893 * Since this handler may be called recursively, it should not
1894 * be saving information into global or static variables.
1895 *
1896 * Your handler isn't actually responsible for parsing the entity,
1897 * but it is responsible for creating a subsidiary parser with
1898 * XML_ExternalEntityParserCreate that will do the job. That returns
1899 * an instance of XML_Parser that has handlers and other data
1900 * structures initialized from the parent parser. You may then use
1901 * XML_Parse or XML_ParseBuffer calls against that parser. Since
1902 * external entities may refer to other external entities, your
1903 * handler should be prepared to be called recursively.
1904 *
1905 *@@added V0.9.14 (2001-08-09) [umoeller]
1906 *@@changed V0.9.20 (2002-07-06) [umoeller]: added automatic doctype support
1907 */
1908
1909STATIC int EXPATENTRY ExternalEntityRefHandler(void *pUserData, // in: our PXMLDOM really
1910 XML_Parser parser,
1911 const XML_Char *pcszContext,
1912 const XML_Char *pcszBase,
1913 const XML_Char *pcszSystemId,
1914 const XML_Char *pcszPublicId)
1915{
1916 PXMLDOM pDom = (PXMLDOM)pUserData;
1917
1918 int i = 0; // return error per default
1919
1920 // store the previous parser because
1921 // all the callbacks use the parser pointer
1922 XML_Parser pOldParser = pDom->pParser;
1923 pDom->pParser = NULL;
1924
1925 if ( ( (pDom->pfnExternalHandler)
1926 || (pDom->cSystemIds) // V0.9.20 (2002-07-06) [umoeller]
1927 )
1928 // create sub-parser and replace the one
1929 // in the DOM with it
1930 && (pDom->pParser = XML_ExternalEntityParserCreate(parser,
1931 pcszContext,
1932 "US-ASCII"))
1933 )
1934 {
1935 // run through the predefined doctypes given to us
1936 // in xmlCreateDOM, if any
1937 // V0.9.20 (2002-07-06) [umoeller]
1938 BOOL fCallExternal = TRUE;
1939 ULONG ul;
1940
1941 for (ul = 0;
1942 ul < pDom->cSystemIds;
1943 ++ul)
1944 {
1945 const STATICSYSTEMID *pThis = &pDom->paSystemIds[ul];
1946 if (!strcmp(pThis->pcszSystemId, pcszSystemId))
1947 {
1948 // this one matches:
1949 // then parse the corresponding entry given
1950 // to us
1951 if (XML_Parse(pDom->pParser,
1952 pThis->pcszContent,
1953 strlen(pThis->pcszContent),
1954 TRUE))
1955 i = 1; // success
1956
1957 fCallExternal = FALSE;
1958
1959 break;
1960 }
1961 }
1962
1963 if ( (fCallExternal) // not handled above
1964 && (pDom->pfnExternalHandler) // user handler set
1965 )
1966 {
1967 APIRET arc;
1968
1969 if (!(arc = pDom->pfnExternalHandler(pDom,
1970 pDom->pParser,
1971 pcszSystemId,
1972 pcszPublicId)))
1973 i = 1; // success
1974 else
1975 {
1976 // error:
1977 // now this needs special handling, since we're
1978 // dealing with a sub-handler here...
1979
1980 if (arc == -1)
1981 // parser error: well, then xmlSetError has been
1982 // called from somewhere in the callbacks already,
1983 // and we can safely ignore this
1984 ;
1985 else
1986 {
1987 pDom->arcDOM = arc;
1988 if (pcszSystemId)
1989 {
1990 if (!pDom->pxstrFailingNode)
1991 pDom->pxstrFailingNode = xstrCreate(0);
1992 xstrcpy(pDom->pxstrFailingNode, pcszSystemId, 0);
1993 }
1994 pDom->pcszErrorDescription = xmlDescribeError(arc);
1995 pDom->ulErrorLine = XML_GetCurrentLineNumber(pDom->pParser);
1996 pDom->ulErrorColumn = XML_GetCurrentColumnNumber(pDom->pParser);
1997 }
1998 }
1999 }
2000 }
2001 else
2002 xmlSetError(pDom,
2003 ERROR_DOM_INVALID_EXTERNAL_HANDLER,
2004 NULL,
2005 FALSE);
2006
2007 if (pDom->pParser)
2008 XML_ParserFree(pDom->pParser);
2009
2010 pDom->pParser = pOldParser;
2011
2012 return i;
2013}
2014
2015/*
2016 *@@ ElementDeclHandler:
2017 * @expat handler for element declarations in a DTD. The
2018 * handler gets called with the name of the element in
2019 * the declaration and a pointer to a structure that contains
2020 * the element model.
2021 *
2022 * It is the application's responsibility to free this data
2023 * structure. @@todo
2024 *
2025 * The XML spec defines that no element may be declared more
2026 * than once.
2027 *
2028 *@@added V0.9.9 (2001-02-14) [umoeller]
2029 */
2030
2031STATIC void EXPATENTRY ElementDeclHandler(void *pUserData, // in: our PXMLDOM really
2032 const XML_Char *pcszName,
2033 XMLCONTENT *pModel)
2034{
2035 PXMLDOM pDom = (PXMLDOM)pUserData;
2036
2037 // continue parsing only if we had no errors so far
2038 if (!pDom->arcDOM)
2039 {
2040 // OK, we're in a DOCTYPE node:
2041 PDOMDOCTYPENODE pDocType = pDom->pDocTypeNode;
2042 if (!pDocType)
2043 xmlSetError(pDom,
2044 ERROR_DOM_ELEMENT_DECL_OUTSIDE_DOCTYPE,
2045 pcszName,
2046 TRUE);
2047 else
2048 {
2049 // create an element declaration and push it unto the
2050 // declarations tree
2051 PCMELEMENTDECLNODE pNew = NULL;
2052 if (!(pDom->arcDOM = xmlCreateElementDecl(pcszName,
2053 pModel,
2054 &pNew)))
2055 // this recurses!!
2056 // after this, pModel is invalid
2057 {
2058 // add this to the doctype's declarations tree
2059 if (treeInsert(&pDocType->ElementDeclsTree,
2060 NULL,
2061 (TREE*)pNew,
2062 CompareXStrings))
2063 // element already declared:
2064 // according to the XML specs, this is a validity
2065 // constraint, so we report a validation error
2066 xmlSetError(pDom,
2067 ERROR_DOM_DUPLICATE_ELEMENT_DECL,
2068 pNew->Particle.NodeBase.strNodeName.psz,
2069 TRUE);
2070 }
2071 }
2072 }
2073}
2074
2075/*
2076 *@@ AddEnum:
2077 *
2078 *@@added V0.9.9 (2001-02-16) [umoeller]
2079 */
2080
2081STATIC APIRET AddEnum(PCMATTRIBUTEDECL pDecl,
2082 const char *p, // in: start of name
2083 const char *pNext) // in: end of name (not included)
2084{
2085 // PSZ pszType = strhSubstr(p, pNext);
2086 PNODEBASE pNew = NULL;
2087 APIRET arc;
2088
2089 if (!(arc = xmlCreateNodeBase(ATTRIBUTE_DECLARATION_ENUM,
2090 sizeof(NODEBASE),
2091 p,
2092 (pNext - p),
2093 &pNew)))
2094 treeInsert(&pDecl->ValuesTree,
2095 NULL,
2096 (TREE*)pNew,
2097 CompareXStrings);
2098
2099 return arc;
2100}
2101
2102/*
2103 *@@ AttlistDeclHandler:
2104 * @expat handler for attlist declarations in the DTD.
2105 *
2106 * This handler is called for each attribute. So a single attlist
2107 * declaration with multiple attributes declared will generate
2108 * multiple calls to this handler.
2109 *
2110 * -- pcszElementName is the name of the element for which the
2111 * attribute is being declared.
2112 *
2113 * -- pcszAttribName has the attribute name being declared.
2114 *
2115 * -- pcszAttribType is the attribute type.
2116 * It is the string representing the type in the declaration
2117 * with whitespace removed.
2118 *
2119 * -- pcszDefault holds the default value. It will be
2120 * NULL in the case of "#IMPLIED" or "#REQUIRED" attributes.
2121 * You can distinguish these two cases by checking the
2122 * fIsRequired parameter, which will be true in the case of
2123 * "#REQUIRED" attributes. Attributes which are "#FIXED"
2124 * will have also have a TRUE fIsRequired, but they will have
2125 * the non-NULL fixed value in the pcszDefault parameter.
2126 *
2127 *@@added V0.9.9 (2001-02-14) [umoeller]
2128 */
2129
2130STATIC void EXPATENTRY AttlistDeclHandler(void *pUserData, // in: our PXMLDOM really
2131 const XML_Char *pcszElementName,
2132 const XML_Char *pcszAttribName,
2133 const XML_Char *pcszAttribType,
2134 const XML_Char *pcszDefault,
2135 int fIsRequired)
2136{
2137 PXMLDOM pDom = (PXMLDOM)pUserData;
2138
2139 // continue parsing only if we had no errors so far
2140 if (!pDom->arcDOM)
2141 {
2142 // OK, we're in a DOCTYPE node:
2143 PDOMDOCTYPENODE pDocType = pDom->pDocTypeNode;
2144 if (!pDocType)
2145 xmlSetError(pDom,
2146 ERROR_DOM_ATTLIST_DECL_OUTSIDE_DOCTYPE,
2147 pcszElementName,
2148 TRUE);
2149 else
2150 {
2151 PCMATTRIBUTEDECLBASE pThis = NULL,
2152 pCache = pDom->pAttListDeclCache;
2153
2154 // check if this is for the same attlist as the previous
2155 // call (we cache the pointer for speed)
2156 if ( (pCache)
2157 && (!strhcmp(pCache->NodeBase.strNodeName.psz,
2158 pcszElementName))
2159 )
2160 // this attdecl is for the same element:
2161 // use that (we won't have to search the tree)
2162 pThis = pDom->pAttListDeclCache;
2163
2164 if (!pThis)
2165 {
2166 // cache didn't match: look up attributes tree then...
2167 // note: cheap trick, we need an XSTRING for treeFind
2168 // but don't want malloc, so we use xstrInitSet
2169 XSTRING strElementName;
2170 xstrInitSet(&strElementName, (PSZ)pcszElementName);
2171 if (!(pThis = (PCMATTRIBUTEDECLBASE)treeFind(
2172 pDocType->AttribDeclBasesTree,
2173 (ULONG)&strElementName,
2174 CompareXStrings)))
2175 {
2176 // still not found:
2177 // we need a new node then
2178 if (!(pDom->arcDOM = xmlCreateNodeBase(ATTRIBUTE_DECLARATION_BASE,
2179 sizeof(CMATTRIBUTEDECLBASE),
2180 strElementName.psz,
2181 strElementName.ulLength,
2182 (PNODEBASE*)&pThis)))
2183 {
2184 // initialize the subtree
2185 treeInit(&pThis->AttribDeclsTree, NULL);
2186
2187 treeInsert(&pDocType->AttribDeclBasesTree,
2188 NULL,
2189 (TREE*)pThis,
2190 CompareXStrings);
2191 }
2192 }
2193
2194 pDom->pAttListDeclCache = pThis;
2195 }
2196
2197 if (pThis)
2198 {
2199 // pThis now has either an existing or a new CMATTRIBUTEDECLBASE;
2200 // add a new attribute def (CMATTRIBUTEDEDECL) to that
2201 PCMATTRIBUTEDECL pNew = NULL;
2202 if (!(pDom->arcDOM = xmlCreateNodeBase(ATTRIBUTE_DECLARATION,
2203 sizeof(CMATTRIBUTEDECL),
2204 pcszAttribName,
2205 0,
2206 (PNODEBASE*)&pNew)))
2207 {
2208 treeInit(&pNew->ValuesTree, NULL);
2209
2210 // check the type... expat is too lazy to parse this for
2211 // us, so we must check manually. Expat only normalizes
2212 // the "type" string to kick out whitespace, so we get:
2213 // (TYPE1|TYPE2|TYPE3)
2214 if (*pcszAttribType == '(')
2215 {
2216 // enumeration:
2217 const char *p = pcszAttribType + 1,
2218 *pNext;
2219 while ( (pNext = strchr(p, '|'))
2220 && (!pDom->arcDOM)
2221 )
2222 {
2223 pDom->arcDOM = AddEnum(pNew, p, pNext);
2224 p = pNext + 1;
2225 }
2226
2227 if (!pDom->arcDOM)
2228 {
2229 pNext = strchr(p, ')');
2230 AddEnum(pNew, p, pNext);
2231
2232 pNew->ulAttrType = CMAT_ENUM;
2233 }
2234 }
2235 else if (!strcmp(pcszAttribType, "CDATA"))
2236 pNew->ulAttrType = CMAT_CDATA;
2237 else if (!strcmp(pcszAttribType, "ID"))
2238 pNew->ulAttrType = CMAT_ID;
2239 else if (!strcmp(pcszAttribType, "IDREF"))
2240 pNew->ulAttrType = CMAT_IDREF;
2241 else if (!strcmp(pcszAttribType, "IDREFS"))
2242 pNew->ulAttrType = CMAT_IDREFS;
2243 else if (!strcmp(pcszAttribType, "ENTITY"))
2244 pNew->ulAttrType = CMAT_ENTITY;
2245 else if (!strcmp(pcszAttribType, "ENTITIES"))
2246 pNew->ulAttrType = CMAT_ENTITIES;
2247 else if (!strcmp(pcszAttribType, "NMTOKEN"))
2248 pNew->ulAttrType = CMAT_NMTOKEN;
2249 else if (!strcmp(pcszAttribType, "NMTOKENS"))
2250 pNew->ulAttrType = CMAT_NMTOKENS;
2251
2252 if (!pDom->arcDOM)
2253 {
2254 if (pcszDefault)
2255 {
2256 // fixed or default:
2257 if (fIsRequired)
2258 // fixed:
2259 pNew->ulConstraint = CMAT_FIXED_VALUE;
2260 else
2261 pNew->ulConstraint = CMAT_DEFAULT_VALUE;
2262
2263 pNew->pstrDefaultValue = xstrCreate(0);
2264 xstrcpy(pNew->pstrDefaultValue, pcszDefault, 0);
2265 }
2266 else
2267 // implied or required:
2268 if (fIsRequired)
2269 pNew->ulConstraint = CMAT_REQUIRED;
2270 else
2271 pNew->ulConstraint = CMAT_IMPLIED;
2272
2273 if (treeInsert(&pThis->AttribDeclsTree,
2274 NULL,
2275 (TREE*)pNew,
2276 CompareXStrings))
2277 xmlSetError(pDom,
2278 ERROR_DOM_DUPLICATE_ATTRIBUTE_DECL,
2279 pcszAttribName,
2280 TRUE);
2281 }
2282 }
2283 }
2284 }
2285 }
2286}
2287
2288/*
2289 *@@ EntityDeclHandler:
2290 * @expat handler that will be called for all entity declarations.
2291 *
2292 * The fIsParameterEntity argument will be non-zero in the case
2293 * of parameter entities and zero otherwise.
2294 *
2295 * For internal entities (<!ENTITY foo "bar">), pcszValue will be
2296 * non-NULL and pcszSystemId, pcszPublicId, and pcszNotationName
2297 * will all be NULL. The value string is not NULL terminated; the
2298 * length is provided in the iValueLength parameter. Do not use
2299 * iValueLength to test for internal entities, since it is legal
2300 * to have zero-length values. Instead check for whether or not
2301 * pcszValue is NULL.
2302 *
2303 * The pcszNotationName argument will have a non-NULL value only
2304 * for unparsed entity declarations.
2305 *
2306 *@@added V0.9.9 (2001-02-14) [umoeller]
2307 */
2308
2309STATIC void EXPATENTRY EntityDeclHandler(void *pUserData, // in: our PXMLDOM really
2310 const XML_Char *pcszEntityName,
2311 int fIsParameterEntity,
2312 const XML_Char *pcszValue,
2313 int iValueLength,
2314 const XML_Char *pcszBase,
2315 const XML_Char *pcszSystemId,
2316 const XML_Char *pcszPublicId,
2317 const XML_Char *pcszNotationName)
2318{
2319 PXMLDOM pDom = (PXMLDOM)pUserData;
2320
2321 // continue parsing only if we had no errors so far
2322 if (!pDom->arcDOM)
2323 {
2324 }
2325}
2326
2327/* ******************************************************************
2328 *
2329 * DOM parser APIs
2330 *
2331 ********************************************************************/
2332
2333/*
2334 *@@ xmlCreateDOM:
2335 * creates an XMLDOM instance, which can be used for parsing
2336 * an XML document and building a @DOM tree from it at the
2337 * same time.
2338 *
2339 * Pass the XMLDOM returned here to xmlParse afterwards.
2340 *
2341 * Simplest possible usage:
2342 *
2343 * 1) Create a DOM instance.
2344 *
2345 + PXMLDOM pDom = NULL;
2346 + APIRET arc = xmlCreateDOM(flags, NULL, NULL, NULL, &pDom);
2347 +
2348 * 2) Give chunks of data (or an entire buffer)
2349 * to the DOM instance for parsing.
2350 *
2351 + arc = xmlParse(pDom,
2352 + pBuf,
2353 + TRUE); // if last, this will clean up the parser
2354 *
2355 * 3) Process the data in the DOM tree.
2356 *
2357 * Look at the DOMNODE definition to see how you
2358 * can traverse the data. Essentially, everything
2359 * is based on linked lists and string maps.
2360 *
2361 * A few helper functions have been added for
2362 * quick lookup. See xmlGetRootElement,
2363 * xmlGetFirstChild, xmlGetLastChild, xmlGetFirstText,
2364 * xmlGetElementsByTagName, xmlGetAttribute.
2365 *
2366 * 4) When done, call xmlFreeDOM, which will free all memory.
2367 *
2368 * The above code has limitations: only a few character
2369 * @encodings are supported, and @external_entities are
2370 * silently ignored.
2371 *
2372 * This function supports a number of callbacks and flags
2373 * to allow for maximum flexibility. Note however that
2374 * not all @expat features are supported yet.
2375 *
2376 * flParserFlags is any combination of the following:
2377 *
2378 * -- DF_PARSECOMMENTS: XML @comments are to be returned in
2379 * the DOM tree. Otherwise they are discarded.
2380 *
2381 * -- DF_PARSEDTD: add the @DTD of the document into the DOM tree
2382 * as well and validate the document, if a DTD was found.
2383 * Otherwise just parse and do not validate.
2384 *
2385 * DF_PARSEDTD is required for external entities to work
2386 * also.
2387 *
2388 * -- DF_FAIL_IF_NO_DTD: fail if no @DTD was found. Useful
2389 * if you want to enforce validation. @@todo
2390 *
2391 * -- DF_DROP_WHITESPACE: discard all @whitespace for those
2392 * elements that can only have element content. Whitespace
2393 * will be preserved only for elements that can have
2394 * mixed content. -- If this flag is not set, all whitespace
2395 * is preserved.
2396 *
2397 * The following callbacks can be specified (any of these
2398 * can be NULL):
2399 *
2400 * -- pfnGetCPData should be specified if you want to
2401 * support character @encodings other than the
2402 * four that built into expat itself (see
2403 * XML_SetUnknownEncodingHandler). This is probably
2404 * a good idea to do under OS/2 since most OS/2
2405 * documents are in a PC-specific codepage such as
2406 * CP 850.
2407 *
2408 * This callback must have the following prototype:
2409 *
2410 + int APIENTRY FNGETCPDATA(PXMLDOM pDom, ULONG ulCP, int *piMap)
2411 *
2412 * The callback will only be called once for each
2413 * document if the "encoding" attribute of the
2414 * XML @text_declaration starts with "cp" (e.g.
2415 * "cp850") and will then receive the following
2416 * parameters:
2417 *
2418 * -- "pDom" will be the XMLDOM created by this function.
2419 *
2420 * -- ulCP has the IBM code page number, such as "850".
2421 *
2422 * -- piMap is an array of 256 integer values which must
2423 * be filled with the callback. Each array item index
2424 * is the codepage value, and the value of each field
2425 * is the corresponding Unicode value, or -1 if the
2426 * character is invalid (shouldn't happen with codepages).
2427 *
2428 * For example, the German o-umlaut character is
2429 * 0x94 in CP850 and 0x00f6 in Unicode. So set
2430 * the int at index 0x94 to 0x00f6.
2431 *
2432 * -- pfnExternalHandler should be specified if you want the
2433 * parser to be able to handle @external_entities. Since
2434 * the parser has no concept of storage whatsoever, it is
2435 * the responsibility of this callback to supply the parser
2436 * with additional XML data when an external entity reference
2437 * is encountered.
2438 *
2439 * This callback must have the following prototype:
2440 *
2441 + APIRET APIENTRY ParseExternal(PXMLDOM pDom,
2442 + XML_Parser pSubParser,
2443 + const char *pcszSystemID,
2444 + const char *pcszPublicID)
2445 +
2446 *
2447 * The callback will be called for each reference that refers
2448 * to an external entity. pSubParser is a sub-parser created
2449 * by the DOM engine, and pcszSystemID and pcszPublicID
2450 * reference the external entity by means of a URI. As always
2451 * with XML, the system ID is required, while the public ID is
2452 * optional.
2453 *
2454 * In the simplest case, this code could look as follows:
2455 *
2456 + APIRET arc = ERROR_FILE_NOT_FOUND;
2457 +
2458 + if (pcszSystemID)
2459 + {
2460 + PSZ pszContents = NULL;
2461 + if (!(arc = doshLoadTextFile(pcszSystemID,
2462 + &pszContents,
2463 + NULL)))
2464 + {
2465 + if (!XML_Parse(pSubParser,
2466 + pszContents,
2467 + strlen(pszContents),
2468 + TRUE))
2469 + arc = -1;
2470 +
2471 + free(pszContents);
2472 + }
2473 + }
2474 +
2475 + return arc;
2476 *
2477 * -- pvCallbackUser is a user parameter which is simply stored
2478 * in the XMLDOM struct which is returned. Since the XMLDOM
2479 * is passed to all the callbacks, you can access that pointer
2480 * from them.
2481 *
2482 *@@added V0.9.9 (2001-02-14) [umoeller]
2483 *@@changed V0.9.14 (2001-08-09) [umoeller]: added DF_DROP_WHITESPACE support
2484 *@@changed V0.9.20 (2002-07-06) [umoeller]: added static system IDs
2485 */
2486
2487APIRET xmlCreateDOM(ULONG flParserFlags, // in: DF_* parser flags
2488 const STATICSYSTEMID *paSystemIds, // in: array of STATICSYSTEMID's or NULL
2489 ULONG cSystemIds, // in: array item count
2490 PFNGETCPDATA pfnGetCPData, // in: codepage callback or NULL
2491 PFNEXTERNALHANDLER pfnExternalHandler, // in: external entity callback or NULL
2492 PVOID pvCallbackUser, // in: user param for callbacks
2493 PXMLDOM *ppDom) // out: XMLDOM struct created
2494{
2495 APIRET arc = NO_ERROR;
2496 PXMLDOM pDom;
2497 PDOMNODE pDocument = NULL;
2498
2499 if (!(pDom = (PXMLDOM)malloc(sizeof(*pDom))))
2500 return ERROR_NOT_ENOUGH_MEMORY;
2501
2502 memset(pDom, 0, sizeof(XMLDOM));
2503
2504 pDom->flParserFlags = flParserFlags;
2505 pDom->pfnGetCPData = pfnGetCPData;
2506 pDom->pfnExternalHandler = pfnExternalHandler;
2507 pDom->pvCallbackUser = pvCallbackUser;
2508
2509 // these added with V0.9.20 (2002-07-06) [umoeller]
2510 pDom->paSystemIds = paSystemIds;
2511 pDom->cSystemIds = cSystemIds;
2512
2513 lstInit(&pDom->llElementStack,
2514 TRUE); // auto-free
2515
2516 // create the document node
2517 if (!(arc = xmlCreateDomNode(NULL, // no parent
2518 DOMNODE_DOCUMENT,
2519 NULL,
2520 0,
2521 &pDocument)))
2522 {
2523 // store the document in the DOM
2524 pDom->pDocumentNode = (PDOMDOCUMENTNODE)pDocument;
2525
2526 // push the document on the stack so the handlers
2527 // will append to that
2528 PushElementStack(pDom,
2529 pDocument);
2530
2531 pDom->pParser = XML_ParserCreate(NULL);
2532
2533 if (!pDom->pParser)
2534 arc = ERROR_NOT_ENOUGH_MEMORY;
2535 else
2536 {
2537 if (pfnGetCPData)
2538 XML_SetUnknownEncodingHandler(pDom->pParser,
2539 UnknownEncodingHandler,
2540 pDom); // user data
2541
2542 XML_SetParamEntityParsing(pDom->pParser,
2543 XML_PARAM_ENTITY_PARSING_ALWAYS);
2544
2545 XML_SetElementHandler(pDom->pParser,
2546 StartElementHandler,
2547 EndElementHandler);
2548
2549 XML_SetCharacterDataHandler(pDom->pParser,
2550 CharacterDataHandler);
2551
2552 // XML_SetProcessingInstructionHandler(XML_Parser parser,
2553 // XML_ProcessingInstructionHandler handler);
2554
2555
2556 if (flParserFlags & DF_PARSECOMMENTS)
2557 XML_SetCommentHandler(pDom->pParser,
2558 CommentHandler);
2559
2560 if ( (pfnExternalHandler)
2561 || (cSystemIds) // V0.9.20 (2002-07-06) [umoeller]
2562 )
2563 XML_SetExternalEntityRefHandler(pDom->pParser,
2564 ExternalEntityRefHandler);
2565
2566 if (flParserFlags & DF_PARSEDTD)
2567 {
2568 XML_SetDoctypeDeclHandler(pDom->pParser,
2569 StartDoctypeDeclHandler,
2570 EndDoctypeDeclHandler);
2571
2572 XML_SetNotationDeclHandler(pDom->pParser,
2573 NotationDeclHandler);
2574
2575 XML_SetElementDeclHandler(pDom->pParser,
2576 ElementDeclHandler);
2577
2578 XML_SetAttlistDeclHandler(pDom->pParser,
2579 AttlistDeclHandler);
2580
2581 XML_SetEntityDeclHandler(pDom->pParser,
2582 EntityDeclHandler);
2583
2584 XML_SetParamEntityParsing(pDom->pParser,
2585 XML_PARAM_ENTITY_PARSING_ALWAYS);
2586 }
2587
2588 // XML_SetXmlDeclHandler ... do we care for this? I guess not
2589
2590 // pass the XMLDOM as user data to the handlers
2591 XML_SetUserData(pDom->pParser,
2592 pDom);
2593 }
2594 }
2595
2596 if (arc == NO_ERROR)
2597 *ppDom = pDom;
2598 else
2599 xmlFreeDOM(pDom);
2600
2601 return arc;
2602}
2603
2604/*
2605 *@@ xmlParse:
2606 * parses another chunk of XML data.
2607 *
2608 * If (fIsLast == TRUE), the internal @expat parser
2609 * will be freed, but not the DOM itself.
2610 *
2611 * You can pass an XML document to this function
2612 * in one flush. Set fIsLast = TRUE on the first
2613 * and only call then.
2614 *
2615 * This returns NO_ERROR if the chunk was successfully
2616 * parsed. Otherwise one of the following errors is
2617 * returned:
2618 *
2619 * -- ERROR_INVALID_PARAMETER
2620 *
2621 * -- ERROR_DOM_PARSING: an @expat parsing error occurred.
2622 * This might also be memory problems.
2623 * With this error code, you will find specific
2624 * error information in the XMLDOM fields.
2625 *
2626 * -- ERROR_DOM_VALIDITY: the document is not @valid.
2627 * This can only happen if @DTD parsing was enabled
2628 * with xmlCreateDOM.
2629 * With this error code, you will find specific
2630 * error information in the XMLDOM fields.
2631 *
2632 *@@added V0.9.9 (2001-02-14) [umoeller]
2633 */
2634
2635APIRET xmlParse(PXMLDOM pDom, // in: DOM created by xmlCreateDOM
2636 const char *pcszBuf, // in: chunk of XML document data (or full document)
2637 ULONG cb, // in: size of that chunk (required)
2638 BOOL fIsLast) // in: set to TRUE if this is the last chunk
2639{
2640 APIRET arc = NO_ERROR;
2641
2642 if (!pDom)
2643 arc = ERROR_INVALID_PARAMETER;
2644 else
2645 {
2646 // go parse then
2647 if (!XML_Parse(pDom->pParser,
2648 pcszBuf,
2649 cb,
2650 fIsLast))
2651 {
2652 // expat parsing error:
2653 xmlSetError(pDom,
2654 XML_GetErrorCode(pDom->pParser),
2655 NULL,
2656 FALSE);
2657
2658 if (pDom->pDocumentNode)
2659 {
2660 xmlDeleteNode((PNODEBASE)pDom->pDocumentNode);
2661 pDom->pDocumentNode = NULL;
2662 }
2663
2664 arc = ERROR_DOM_PARSING;
2665 }
2666 else if (pDom->fInvalid)
2667 {
2668 // expat was doing OK, but the handlers' validation failed:
2669 arc = ERROR_DOM_VALIDITY;
2670 // error info has already been set
2671 }
2672 else
2673 // expat was doing OK, but maybe we have integrity errors
2674 // from our DOM callbacks:
2675 if (pDom->arcDOM)
2676 arc = pDom->arcDOM;
2677
2678 if (arc != NO_ERROR || fIsLast)
2679 {
2680 // last call or error: clean up
2681 XML_ParserFree(pDom->pParser);
2682 pDom->pParser = NULL;
2683
2684 // clean up the stack (but not the DOM itself)
2685 lstClear(&pDom->llElementStack);
2686 }
2687 }
2688
2689 return arc;
2690}
2691
2692#ifdef __DEBUG__
2693
2694/*
2695 *@@ Dump:
2696 *
2697 *@@added V0.9.20 (2002-07-03) [umoeller]
2698 */
2699
2700STATIC VOID Dump(int iIndent,
2701 PDOMNODE pDomNode)
2702{
2703 PLISTNODE pChildNode;
2704 int i;
2705 for (i = 0;
2706 i < iIndent;
2707 ++i)
2708 {
2709 printf(" ");
2710 }
2711
2712 switch (pDomNode->NodeBase.ulNodeType)
2713 {
2714 #define DUMPTYPE(t) case t: printf(#t); break;
2715 DUMPTYPE(DOMNODE_ELEMENT)
2716 DUMPTYPE(DOMNODE_ATTRIBUTE)
2717 DUMPTYPE(DOMNODE_TEXT)
2718 DUMPTYPE(DOMNODE_PROCESSING_INSTRUCTION)
2719 DUMPTYPE(DOMNODE_COMMENT)
2720 DUMPTYPE(DOMNODE_DOCUMENT)
2721 DUMPTYPE(DOMNODE_DOCUMENT_TYPE)
2722 DUMPTYPE(ELEMENTPARTICLE_EMPTY)
2723 DUMPTYPE(ELEMENTPARTICLE_ANY)
2724 DUMPTYPE(ELEMENTPARTICLE_MIXED)
2725 DUMPTYPE(ELEMENTPARTICLE_CHOICE)
2726 DUMPTYPE(ELEMENTPARTICLE_SEQ)
2727 DUMPTYPE(ELEMENTPARTICLE_NAME)
2728 DUMPTYPE(ATTRIBUTE_DECLARATION_BASE)
2729 DUMPTYPE(ATTRIBUTE_DECLARATION)
2730 DUMPTYPE(ATTRIBUTE_DECLARATION_ENUM)
2731 }
2732
2733 printf(" \"%s\"\n", STRINGORNULL(pDomNode->NodeBase.strNodeName.psz));
2734
2735 ++iIndent;
2736 for (pChildNode = lstQueryFirstNode(&pDomNode->llChildren);
2737 pChildNode;
2738 pChildNode = pChildNode->pNext)
2739 {
2740 Dump(iIndent, (PDOMNODE)pChildNode->pItemData);
2741 }
2742 --iIndent;
2743}
2744
2745#endif
2746
2747/*
2748 *@@ xmlDump:
2749 * debug function which dumps the DOM to stdout.
2750 *
2751 *@@added V0.9.20 (2002-07-03) [umoeller]
2752 */
2753
2754VOID xmlDump(PXMLDOM pDom)
2755{
2756#ifdef __DEBUG__
2757 if (!pDom)
2758 {
2759 printf(__FUNCTION__ ": pDom is NULL\n");
2760 return;
2761 }
2762
2763 printf(__FUNCTION__ ": dumping document node ");
2764
2765 Dump(0, (PDOMNODE)pDom->pDocumentNode);
2766#endif
2767}
2768
2769/*
2770 *@@ xmlFreeDOM:
2771 * cleans up all resources allocated by
2772 * xmlCreateDOM and xmlParse, including
2773 * the entire DOM tree.
2774 *
2775 * If you wish to keep any data, make
2776 * a copy of the respective pointers in pDom
2777 * or subitems and set them to NULL before
2778 * calling this function.
2779 *
2780 *@@added V0.9.9 (2001-02-14) [umoeller]
2781 */
2782
2783APIRET xmlFreeDOM(PXMLDOM pDom)
2784{
2785 APIRET arc = NO_ERROR;
2786 if (pDom)
2787 {
2788 // if the parser is still alive for some reason, close it.
2789 if (pDom->pParser)
2790 {
2791 XML_ParserFree(pDom->pParser);
2792 pDom->pParser = NULL;
2793 }
2794
2795 xmlDeleteNode((PNODEBASE)pDom->pDocumentNode);
2796
2797 if (pDom->pxstrSystemID)
2798 xstrFree(&pDom->pxstrSystemID);
2799 if (pDom->pxstrFailingNode)
2800 xstrFree(&pDom->pxstrFailingNode);
2801
2802 lstClear(&pDom->llElementStack);
2803
2804 free(pDom);
2805 }
2806
2807 return arc;
2808}
2809
2810/* ******************************************************************
2811 *
2812 * DOM lookup
2813 *
2814 ********************************************************************/
2815
2816/*
2817 *@@ xmlFindElementDecl:
2818 * returns the CMELEMENTDECLNODE for the element
2819 * with the specified name or NULL if there's none.
2820 *
2821 *@@added V0.9.9 (2001-02-16) [umoeller]
2822 */
2823
2824PCMELEMENTDECLNODE xmlFindElementDecl(PXMLDOM pDom,
2825 const XSTRING *pcstrElementName)
2826{
2827 PCMELEMENTDECLNODE pElementDecl = NULL;
2828
2829 PDOMDOCTYPENODE pDocTypeNode = pDom->pDocTypeNode;
2830 if ( (pDocTypeNode)
2831 && (pcstrElementName)
2832 && (pcstrElementName->ulLength)
2833 )
2834 {
2835 pElementDecl = (PCMELEMENTDECLNODE)treeFind(
2836 pDocTypeNode->ElementDeclsTree,
2837 (ULONG)pcstrElementName,
2838 CompareXStrings);
2839 }
2840
2841 return pElementDecl;
2842}
2843
2844/*
2845 *@@ xmlFindAttribDeclBase:
2846 * returns the CMATTRIBUTEDECLBASE for the specified
2847 * element name, or NULL if none exists.
2848 *
2849 * To find a specific attribute declaration from both
2850 * an element and an attribute name, use xmlFindAttribDecl
2851 * instead.
2852 *
2853 *@@added V0.9.9 (2001-02-16) [umoeller]
2854 */
2855
2856PCMATTRIBUTEDECLBASE xmlFindAttribDeclBase(PXMLDOM pDom,
2857 const XSTRING *pstrElementName)
2858{
2859 PDOMDOCTYPENODE pDocTypeNode = pDom->pDocTypeNode;
2860 if ( (pDocTypeNode)
2861 && (pstrElementName)
2862 && (pstrElementName->ulLength)
2863 )
2864 {
2865 return (PCMATTRIBUTEDECLBASE)treeFind(pDocTypeNode->AttribDeclBasesTree,
2866 (ULONG)pstrElementName,
2867 CompareXStrings);
2868 }
2869
2870 return NULL;
2871}
2872
2873/*
2874 *@@ xmlFindAttribDecl:
2875 * returns the CMATTRIBUTEDEDECL for the specified
2876 * element and attribute name, or NULL if none exists.
2877 *
2878 *@@added V0.9.9 (2001-02-16) [umoeller]
2879 */
2880
2881PCMATTRIBUTEDECL xmlFindAttribDecl(PXMLDOM pDom,
2882 const XSTRING *pstrElementName,
2883 const XSTRING *pstrAttribName,
2884 PCMATTRIBUTEDECLBASE *ppAttribDeclBase)
2885 // in/out: attr decl base cache;
2886 // the pointer pointed to by this
2887 // must be NULL on the first call
2888{
2889 if (pstrElementName && pstrAttribName)
2890 {
2891 if (!*ppAttribDeclBase)
2892 // first call for this:
2893 *ppAttribDeclBase = xmlFindAttribDeclBase(pDom,
2894 pstrElementName);
2895 if (*ppAttribDeclBase)
2896 {
2897 return (PCMATTRIBUTEDECL)treeFind(((**ppAttribDeclBase).AttribDeclsTree),
2898 (ULONG)pstrAttribName,
2899 CompareXStrings);
2900 }
2901 }
2902
2903 return NULL;
2904}
2905
2906/*
2907 *@@ xmlGetRootElement:
2908 * returns the root element node from the specified
2909 * DOM. Useful helper to start enumerating elements.
2910 *
2911 *@@added V0.9.11 (2001-04-22) [umoeller]
2912 *@@changed V0.9.20 (2002-07-03) [umoeller]: this never worked with DTDs, fixed
2913 */
2914
2915PDOMNODE xmlGetRootElement(PXMLDOM pDom)
2916{
2917 PDOMDOCUMENTNODE pDocumentNode;
2918 PLISTNODE pListNode;
2919 if ( (pDom)
2920 && (pDocumentNode = pDom->pDocumentNode)
2921 && (pListNode = lstQueryFirstNode(&pDocumentNode->DomNode.llChildren))
2922 )
2923 {
2924 // V0.9.20 (2002-07-03) [umoeller]:
2925 // we can't just return the first node on the
2926 // list, because if we have DTD, this might
2927 // be the doctype... so loop until we find
2928 // an element, which must be the root element
2929 while (pListNode)
2930 {
2931 PDOMNODE pDomNode = (PDOMNODE)pListNode->pItemData;
2932 if (pDomNode->NodeBase.ulNodeType == DOMNODE_ELEMENT)
2933 return pDomNode;
2934
2935 pListNode = pListNode->pNext;
2936 }
2937 }
2938
2939 return NULL;
2940}
2941
2942/*
2943 *@@ xmlGetFirstChild:
2944 * returns the first child node of pDomNode.
2945 * See DOMNODE for what a "child" can be for the
2946 * various node types.
2947 *
2948 *@@added V0.9.9 (2001-02-14) [umoeller]
2949 */
2950
2951PDOMNODE xmlGetFirstChild(PDOMNODE pDomNode)
2952{
2953 PLISTNODE pListNode = lstQueryFirstNode(&pDomNode->llChildren);
2954 if (pListNode)
2955 return (PDOMNODE)pListNode->pItemData;
2956
2957 return 0;
2958}
2959
2960/*
2961 *@@ xmlGetLastChild:
2962 * returns the last child node of pDomNode.
2963 * See DOMNODE for what a "child" can be for the
2964 * various node types.
2965 *
2966 *@@added V0.9.9 (2001-02-14) [umoeller]
2967 */
2968
2969PDOMNODE xmlGetLastChild(PDOMNODE pDomNode)
2970{
2971 PLISTNODE pListNode = lstQueryLastNode(&pDomNode->llChildren);
2972 if (pListNode)
2973 return (PDOMNODE)pListNode->pItemData;
2974
2975 return 0;
2976}
2977
2978/*
2979 *@@ xmlGetFirstText:
2980 * returns the first text (character data) node
2981 * of pElement or NULL if there's none.
2982 *
2983 *@@added V0.9.11 (2001-04-22) [umoeller]
2984 */
2985
2986PDOMNODE xmlGetFirstText(PDOMNODE pElement)
2987{
2988 PLISTNODE pNode;
2989 PDOMNODE pDomNodeThis;
2990
2991 for (pNode = lstQueryFirstNode(&pElement->llChildren);
2992 pNode;
2993 pNode = pNode->pNext)
2994 {
2995 if ( (pDomNodeThis = (PDOMNODE)pNode->pItemData)
2996 && (pDomNodeThis->NodeBase.ulNodeType == DOMNODE_TEXT)
2997 )
2998 return pDomNodeThis;
2999 }
3000
3001 return NULL;
3002}
3003
3004/*
3005 *@@ xmlGetElementsByTagName:
3006 * returns a linked list of DOMNODE nodes which
3007 * match the specified element name. The special name
3008 * "*" matches all elements.
3009 *
3010 * pParent must be the parent element DOMNODE...
3011 * the only allowed exception is
3012 *
3013 * The caller must free the list by calling lstFree.
3014 * Returns NULL if no such elements could be found.
3015 *
3016 *@@added V0.9.9 (2001-02-14) [umoeller]
3017 */
3018
3019PLINKLIST xmlGetElementsByTagName(PDOMNODE pParent,
3020 const char *pcszName)
3021{
3022 PLINKLIST pll = lstCreate(FALSE); // no free
3023 if (pll)
3024 {
3025 ULONG cItems = 0;
3026 BOOL fFindAll = !strcmp(pcszName, "*");
3027
3028 PLISTNODE pNode;
3029 PDOMNODE pDomNodeThis;
3030
3031 for (pNode = lstQueryFirstNode(&pParent->llChildren);
3032 pNode;
3033 pNode = pNode->pNext)
3034 {
3035 if ( (pDomNodeThis = (PDOMNODE)pNode->pItemData)
3036 && (pDomNodeThis->NodeBase.ulNodeType == DOMNODE_ELEMENT)
3037 && ( (fFindAll)
3038 || (!strcmp(pcszName, pDomNodeThis->NodeBase.strNodeName.psz))
3039 )
3040 )
3041 {
3042 // element matches:
3043 lstAppendItem(pll, pDomNodeThis);
3044 cItems++;
3045 }
3046 }
3047
3048 if (cItems)
3049 return pll;
3050
3051 lstFree(&pll);
3052 }
3053
3054 return 0;
3055}
3056
3057/*
3058 *@@ xmlGetAttribute:
3059 * returns the value of pElement's attribute
3060 * with the given name or NULL.
3061 *
3062 * This is a const pointer into the element's
3063 * attribute list.
3064 *
3065 *@@added V0.9.11 (2001-04-22) [umoeller]
3066 */
3067
3068const XSTRING* xmlGetAttribute(PDOMNODE pElement,
3069 const char *pcszAttribName)
3070{
3071 XSTRING str;
3072 PDOMNODE pAttrNode;
3073 xstrInitSet(&str, (PSZ)pcszAttribName);
3074 // note, cheap trick: no malloc here, but we need
3075 // an XSTRING for treeFind
3076 if (pAttrNode = (PDOMNODE)treeFind(pElement->AttributesMap,
3077 (ULONG)&str,
3078 CompareXStrings))
3079 return pAttrNode->pstrNodeValue;
3080
3081 return NULL;
3082}
3083
3084/* ******************************************************************
3085 *
3086 * DOM build
3087 *
3088 ********************************************************************/
3089
3090/*
3091 *@@ xmlCreateDocument:
3092 * creates a new XML document.
3093 *
3094 * This is the first step in creating a DOM
3095 * tree in memory.
3096 *
3097 * This function creates a DOCUMENT node
3098 * (which is returned) and a root ELEMENT node
3099 * within the document. For convenience, the
3100 * root ELEMENT node is returned as well.
3101 *
3102 * This does not create a DOCTYPE node in
3103 * the document.
3104 *
3105 * After this, you can add sub-elements and
3106 * attributes to the root element using
3107 * xmlCreateElementNode and xmlCreateAttributeNode.
3108 *
3109 * Use xmlDeleteNode on the DOCUMENT node
3110 * to delete the entire DOM tree.
3111 *
3112 *@@added V0.9.12 (2001-05-21) [umoeller]
3113 */
3114
3115APIRET xmlCreateDocument(const char *pcszRootElementName, // in: root element name
3116 PDOMDOCUMENTNODE *ppDocument, // out: DOCUMENT node
3117 PDOMNODE *ppRootElement) // out: root ELEMENT node within DOCUMENT
3118{
3119 APIRET arc = NO_ERROR;
3120 PDOMDOCUMENTNODE pDocument = NULL;
3121 PDOMNODE pRootElement = NULL;
3122
3123 if ( (!pcszRootElementName) || (!ppDocument) || (!ppRootElement) )
3124 arc = ERROR_INVALID_PARAMETER;
3125 else
3126 // create the document node
3127 if (!(arc = xmlCreateDomNode(NULL, // no parent
3128 DOMNODE_DOCUMENT,
3129 NULL,
3130 0,
3131 (PDOMNODE*)&pDocument)))
3132 if (!(arc = xmlCreateDomNode((PDOMNODE)pDocument, // parent
3133 DOMNODE_ELEMENT,
3134 pcszRootElementName,
3135 0,
3136 &pRootElement)))
3137 {
3138 *ppDocument = pDocument;
3139 *ppRootElement = pRootElement;
3140 }
3141
3142 return arc;
3143}
3144
3145/*
3146 *@@ ESCAPES:
3147 *
3148 *@@added V1.0.0 (2002-08-21) [umoeller]
3149 */
3150
3151typedef struct _ESCAPES
3152{
3153 XSTRING strQuot1, // "
3154 strQuot2, // &quot;
3155 strAmp1, // &
3156 strAmp2, // &amp;
3157 strLT1, // <
3158 strLT2, // &lt;
3159 strGT1, // >
3160 strGT2; // &gt;
3161
3162 XSTRING strTemp; // temp buffer
3163
3164} ESCAPES, *PESCAPES;
3165
3166/*
3167 *@@ DoEscapes:
3168 *
3169 *@@added V1.0.0 (2002-08-21) [umoeller]
3170 */
3171
3172VOID DoEscapes(PESCAPES pEscapes,
3173 BOOL fQuotesToo)
3174{
3175 ULONG ulOfs;
3176 size_t ShiftTable[256];
3177 BOOL fRepeat;
3178
3179 if (fQuotesToo)
3180 {
3181 ulOfs = 0;
3182 fRepeat = FALSE;
3183 while (xstrFindReplace(&pEscapes->strTemp,
3184 &ulOfs,
3185 &pEscapes->strQuot1,
3186 &pEscapes->strQuot2,
3187 ShiftTable,
3188 &fRepeat))
3189 ;
3190 }
3191
3192 ulOfs = 0;
3193 fRepeat = FALSE;
3194 while (xstrFindReplace(&pEscapes->strTemp,
3195 &ulOfs,
3196 &pEscapes->strLT1,
3197 &pEscapes->strLT2,
3198 ShiftTable,
3199 &fRepeat))
3200 ;
3201
3202 ulOfs = 0;
3203 fRepeat = FALSE;
3204 while (xstrFindReplace(&pEscapes->strTemp,
3205 &ulOfs,
3206 &pEscapes->strGT1,
3207 &pEscapes->strGT2,
3208 ShiftTable,
3209 &fRepeat))
3210 ;
3211
3212 // replace ampersands last
3213 ulOfs = 0;
3214 fRepeat = FALSE;
3215 while (xstrFindReplace(&pEscapes->strTemp,
3216 &ulOfs,
3217 &pEscapes->strAmp1,
3218 &pEscapes->strAmp2,
3219 ShiftTable,
3220 &fRepeat))
3221 ;
3222}
3223
3224/*
3225 *@@ WriteNodes:
3226 * internal helper for writing out the nodes.
3227 * This recurses.
3228 *
3229 *@@added V0.9.12 (2001-05-21) [umoeller]
3230 *@@changed V1.0.0 (2002-08-21) [umoeller]: changed prototype, fixed unescaped characters in attributes and content
3231 */
3232
3233STATIC VOID WriteNodes(PXSTRING pxstr,
3234 PESCAPES pEscapes,
3235 PDOMNODE pDomNode) // in: node whose children are to be written (initially DOCUMENT)
3236{
3237 PLISTNODE pListNode;
3238
3239 BOOL fMixedContent = (xmlGetFirstText(pDomNode) != NULL);
3240
3241 for (pListNode = lstQueryFirstNode(&pDomNode->llChildren);
3242 (pListNode);
3243 pListNode = pListNode->pNext)
3244 {
3245 PDOMNODE pChildNode = (PDOMNODE)pListNode->pItemData;
3246
3247 switch (pChildNode->NodeBase.ulNodeType)
3248 {
3249 case DOMNODE_ELEMENT:
3250 {
3251 PDOMNODE pAttribNode;
3252 // write out opening ELEMENT tag
3253 // add a line break if this does NOT have mixed
3254 // content
3255 if (!fMixedContent)
3256 xstrcatc(pxstr, '\n');
3257
3258 xstrcatc(pxstr, '<');
3259 xstrcats(pxstr, &pChildNode->NodeBase.strNodeName);
3260
3261 // go through attributes
3262 for (pAttribNode = (PDOMNODE)treeFirst(pChildNode->AttributesMap);
3263 (pAttribNode);
3264 pAttribNode = (PDOMNODE)treeNext((TREE*)pAttribNode))
3265 {
3266 xstrcat(pxstr, "\n ", 0);
3267 xstrcats(pxstr, &pAttribNode->NodeBase.strNodeName);
3268 xstrcat(pxstr, "=\"", 0);
3269
3270 // copy attribute value to temp buffer first
3271 // so we can escape quotes and ampersands
3272 // V1.0.0 (2002-08-21) [umoeller]
3273 xstrcpys(&pEscapes->strTemp, pAttribNode->pstrNodeValue);
3274
3275 DoEscapes(pEscapes,
3276 TRUE); // quotes too
3277
3278 // alright, use that
3279 xstrcats(pxstr, &pEscapes->strTemp);
3280 xstrcatc(pxstr, '\"');
3281 }
3282
3283 // now check... do we have child nodes?
3284 if (lstCountItems(&pChildNode->llChildren))
3285 {
3286 // yes:
3287 xstrcatc(pxstr, '>');
3288
3289 // recurse into this child element
3290 WriteNodes(pxstr, pEscapes, pChildNode);
3291
3292 if (!fMixedContent)
3293 xstrcatc(pxstr, '\n');
3294
3295 // write closing tag
3296 xstrcat(pxstr, "</", 0);
3297 xstrcats(pxstr, &pChildNode->NodeBase.strNodeName);
3298 xstrcatc(pxstr, '>');
3299 }
3300 else
3301 {
3302 // no child nodes:
3303 // mark this tag as "empty"
3304 xstrcat(pxstr, "/>", 0);
3305 }
3306 }
3307 break;
3308
3309 case DOMNODE_TEXT:
3310 case DOMNODE_COMMENT:
3311 // that's simple
3312 xstrcpys(&pEscapes->strTemp,
3313 pChildNode->pstrNodeValue);
3314
3315 DoEscapes(pEscapes, // V1.0.0 (2002-08-21) [umoeller]
3316 FALSE); // quotes not
3317
3318 xstrcats(pxstr, &pEscapes->strTemp);
3319 break;
3320
3321 case DOMNODE_DOCUMENT_TYPE:
3322 // @@todo
3323 break;
3324
3325 }
3326 }
3327}
3328
3329/*
3330 *@@ xmlWriteDocument:
3331 * creates a complete XML document in the specified
3332 * string buffer from the specified DOMDOCUMENTNODE.
3333 *
3334 * This creates a full XML document, starting with
3335 * the <?xml...?> header, the DTD (if present),
3336 * and the elements and attributes.
3337 *
3338 * The input XSTRING must be initialized. Its
3339 * contents will be overwritten, if any exists.
3340 *
3341 * Sooo... to write a full XML document to disk,
3342 * do the following:
3343 *
3344 * 1) Call xmlCreateDocument to have an empty
3345 * document with a root element created.
3346 *
3347 * 2) Add elements, subelements, and attributes
3348 * using xmlCreateElementNode and
3349 * xmlCreateAttributeNode.
3350 *
3351 * 3) Call xmlWriteDocument to have the XML
3352 * document written into an XSTRING.
3353 *
3354 * 4) Write the XSTRING to disk, e.g. using
3355 * fwrite().
3356 *
3357 * Note: You can also use doshWriteTextFile,
3358 * but you should then first convert the
3359 * line format using xstrConvertLineFormat.
3360 *
3361 * Example:
3362 *
3363 + APIRET arc = NO_ERROR;
3364 + PDOMDOCUMENTNODE pDocument = NULL;
3365 + PDOMNODE pRootElement = NULL;
3366 +
3367 + // create a DOM
3368 + if (!(arc = xmlCreateDocument("MYROOTNODE",
3369 + &pDocument,
3370 + &pRootElement)))
3371 + {
3372 + // add subelements to the root element
3373 + PDOMNODE pSubelement;
3374 + if (!(arc = xmlCreateElementNode(pRootElement,
3375 + "MYSUBELEMENT",
3376 + &pSubelement)))
3377 + {
3378 + // add an attribute
3379 + PDOMNODE pAttribute;
3380 + if (!(arc = xmlCreateAttributeNode(pSubElement,
3381 + "MYATTRIB",
3382 + "VALUE",
3383 + &pAttribute)))
3384 + {
3385 + // alright, turn this into a string
3386 + XSTRING str;
3387 + xstrInit(&str, 1000);
3388 + if (!(arc = xmlWriteDocument(pDocument,
3389 + "ISO-8859-1",
3390 + NULL, // or DOCTYPE
3391 + &str)))
3392 + {
3393 + FILE *file = fopen("myfile.xml", "w");
3394 + fwrite(str.psz,
3395 + 1,
3396 + str.ulLength,
3397 + file);
3398 + fclose(file);
3399 + }
3400 + }
3401 + }
3402 +
3403 + // this kills the entire tree
3404 + xmlDeleteNode((PNODEBASE)pDocument);
3405 +
3406 + }
3407 +
3408 *
3409 * A note about whitespace handling. Presently, this
3410 * adds line breaks after the opening tag of an
3411 * element if the element has element content only.
3412 * However, if the element has mixed content, this
3413 * line break is NOT automatically added because
3414 * white space may then be significant.
3415 *
3416 *@@added V0.9.12 (2001-05-21) [umoeller]
3417 */
3418
3419APIRET xmlWriteDocument(PDOMDOCUMENTNODE pDocument, // in: document node
3420 const char *pcszEncoding, // in: encoding string (e.g. "ISO-8859-1")
3421 const char *pcszDoctype, // in: entire DOCTYPE statement or NULL
3422 PXSTRING pxstr) // out: document
3423{
3424 APIRET arc = NO_ERROR;
3425
3426 if ( (!pDocument) || (!pcszEncoding) || (!pxstr) )
3427 arc = ERROR_INVALID_PARAMETER;
3428 else
3429 {
3430 ESCAPES esc;
3431
3432 // <?xml version="1.0" encoding="ISO-8859-1"?>
3433 xstrcpy(pxstr, "<?xml version=\"1.0\" encoding=\"", 0);
3434 xstrcat(pxstr, pcszEncoding, 0);
3435 xstrcat(pxstr, "\"?>\n", 0);
3436
3437 // write entire DOCTYPE statement
3438 if (pcszDoctype)
3439 {
3440 xstrcatc(pxstr, '\n');
3441 xstrcat(pxstr, pcszDoctype, 0);
3442 xstrcatc(pxstr, '\n');
3443 }
3444
3445 xstrInitCopy(&esc.strQuot1, "\"", 0);
3446 xstrInitCopy(&esc.strQuot2, "&quot;", 0);
3447 xstrInitCopy(&esc.strAmp1, "&", 0);
3448 xstrInitCopy(&esc.strAmp2, "&amp;", 0);
3449 xstrInitCopy(&esc.strLT1, "<", 0);
3450 xstrInitCopy(&esc.strLT2, "&lt;", 0);
3451 xstrInitCopy(&esc.strGT1, ">", 0);
3452 xstrInitCopy(&esc.strGT2, "&gt;", 0);
3453
3454 xstrInit(&esc.strTemp, 0); // temp buffer
3455
3456 // write out children
3457 WriteNodes(pxstr, &esc, (PDOMNODE)pDocument);
3458
3459 xstrClear(&esc.strQuot1);
3460 xstrClear(&esc.strQuot2);
3461 xstrClear(&esc.strAmp1);
3462 xstrClear(&esc.strAmp2);
3463 xstrClear(&esc.strLT1);
3464 xstrClear(&esc.strLT2);
3465 xstrClear(&esc.strGT1);
3466 xstrClear(&esc.strGT2);
3467
3468 xstrClear(&esc.strTemp); // temp buffer
3469
3470 xstrcatc(pxstr, '\n');
3471 }
3472
3473 return arc;
3474}
3475
3476
Note: See TracBrowser for help on using the repository browser.