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

Last change on this file since 156 was 153, checked in by umoeller, 23 years ago

Lots of changes from the last three weeks.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 111.1 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-2001 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 */
717
718APIRET xmlCreateAttributeNode(PDOMNODE pElement, // in: element node
719 const char *pcszName, // in: attribute name (null-terminated)
720 const char *pcszValue, // in: attribute value (null-terminated)
721 PDOMNODE *ppNew)
722{
723 APIRET arc = NO_ERROR;
724
725 if ( (!pElement)
726 || (pElement->NodeBase.ulNodeType != DOMNODE_ELEMENT)
727 )
728 arc = ERROR_DOM_NO_ELEMENT;
729 else
730 {
731 PDOMNODE pNew = NULL;
732 if (!(arc = xmlCreateDomNode(pElement, // this takes care of adding to the list
733 DOMNODE_ATTRIBUTE,
734 pcszName,
735 0,
736 &pNew)))
737 {
738 pNew->pstrNodeValue = xstrCreate(0);
739 xstrcpy(pNew->pstrNodeValue, pcszValue, 0);
740
741 *ppNew = pNew;
742 }
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 ulLength, // 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(ulLength + 1))
775 {
776 memcpy(pszNodeValue, pcszText, ulLength);
777 pszNodeValue[ulLength] = '\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 if (!(pDom->arcDOM = xmlCreateAttributeNode(pNew, // element,
1555 papcszAttribs[i], // attr name
1556 papcszAttribs[i + 1], // attr value
1557 &pAttrib)))
1558 {
1559 // shall we validate?
1560 if (pDom->pDocTypeNode)
1561 ValidateAttributeType(pDom,
1562 pAttrib,
1563 &pAttribDeclBase);
1564 }
1565 else
1566 {
1567 xmlSetError(pDom,
1568 pDom->arcDOM,
1569 papcszAttribs[i],
1570 FALSE); // validation
1571 break;
1572 }
1573 }
1574
1575 // OK, now we got all attributes:
1576 // now look for defaults in the DTD,
1577 // if we shall validate
1578 if ( (pDom->pDocTypeNode)
1579 && (!pDom->arcDOM)
1580 && (pAttribDeclBase)
1581 )
1582 ValidateAllAttributes(pDom,
1583 pAttribDeclBase,
1584 pNew);
1585 }
1586 }
1587
1588 pDom->pLastWasTextNode = NULL;
1589 }
1590}
1591
1592/*
1593 *@@ EndElementHandler:
1594 * @expat handler for when parsing an element is done.
1595 * We pop the element off of our stack then.
1596 */
1597
1598static void EXPATENTRY EndElementHandler(void *pUserData, // in: our PXMLDOM really
1599 const XML_Char *name)
1600{
1601 PXMLDOM pDom = (PXMLDOM)pUserData;
1602 // continue parsing only if we had no errors so far
1603 if (!pDom->arcDOM)
1604 {
1605 PLISTNODE pStackLN = NULL;
1606 PDOMSTACKITEM pSI = PopElementStack(pDom,
1607 &pStackLN);
1608
1609 if (!pDom->arcDOM)
1610 {
1611 // shall we validate?
1612 /* if (pDom->pDocTypeNode)
1613 // yes:
1614 ValidateElementChildren(pDom,
1615 pSI->pDomNode); */
1616
1617 lstRemoveNode(&pDom->llElementStack, pStackLN); // auto-free
1618 }
1619 else
1620 pDom->arcDOM = ERROR_DOM_INTEGRITY;
1621
1622 pDom->pLastWasTextNode = NULL;
1623 }
1624}
1625
1626/*
1627 *@@ CharacterDataHandler:
1628 * @expat handler for character data (@content).
1629 *
1630 * Note: expat passes chunks of content without zero-terminating
1631 * them. We must concatenate the chunks to a full text node.
1632 */
1633
1634static void EXPATENTRY CharacterDataHandler(void *pUserData, // in: our PXMLDOM really
1635 const XML_Char *s,
1636 int len)
1637{
1638 PXMLDOM pDom = (PXMLDOM)pUserData;
1639
1640 // continue parsing only if we had no errors so far
1641 if ( (!pDom->arcDOM)
1642 && (len)
1643 )
1644 {
1645 // we need a new text node:
1646 PDOMSTACKITEM pSI = PopElementStack(pDom,
1647 NULL); // no free
1648 if (!pDom->arcDOM)
1649 {
1650 PDOMNODE pParent = pSI->pDomNode;
1651 // pNew = NULL;
1652
1653 BOOL fIsWhitespace = FALSE;
1654
1655 // shall we validate?
1656 if (pDom->pDocTypeNode)
1657 {
1658 // yes: check if the parent element allows
1659 // for content at all (must be "mixed" model)
1660
1661 // get the element decl from the tree
1662 PCMELEMENTDECLNODE pElementDecl;
1663 if (pElementDecl = pSI->pElementDecl)
1664 {
1665 switch (pElementDecl->Particle.NodeBase.ulNodeType)
1666 {
1667 case ELEMENTPARTICLE_ANY:
1668 case ELEMENTPARTICLE_MIXED:
1669 // those two are okay
1670 break;
1671
1672 case ELEMENTPARTICLE_EMPTY:
1673 // that's an error for sure
1674 pDom->arcDOM = ERROR_ELEMENT_CANNOT_HAVE_CONTENT;
1675 break;
1676
1677 default:
1678 {
1679 // ELEMENTPARTICLE_CHOICE:
1680 // ELEMENTPARTICLE_SEQ:
1681 // with these two, we accept whitespace, but nothing
1682 // else... so if we have characters other than
1683 // whitespace, terminate
1684 ULONG ul;
1685 const char *p = s;
1686
1687 if (pDom->flParserFlags & DF_DROP_WHITESPACE)
1688 fIsWhitespace = TRUE;
1689
1690 for (ul = 0;
1691 ul < len;
1692 ul++, p++)
1693 if (!strchr("\r\n\t ", *p))
1694 {
1695 // other character:
1696 xmlSetError(pDom,
1697 ERROR_ELEMENT_CANNOT_HAVE_CONTENT,
1698 pParent->NodeBase.strNodeName.psz,
1699 TRUE);
1700 fIsWhitespace = FALSE;
1701 break;
1702 }
1703 }
1704 }
1705 }
1706
1707 } // end if (pDom->pDocTypeNode)
1708
1709 if (!fIsWhitespace)
1710 // this is false if any of the following
1711 // is true:
1712 // -- we are not validating at all
1713 // -- we are validating, but the the element
1714 // can have mixed content
1715 // -- we are validating and the element does
1716 // _not_ have mixed content and DF_DROP_WHITESPACE
1717 // is set, but the string is whitespace only
1718 // --> drop it then
1719
1720 if (pDom->pLastWasTextNode)
1721 // we had a text node, and no elements or other
1722 // stuff in between:
1723 xstrcat(pDom->pLastWasTextNode->pstrNodeValue,
1724 s,
1725 len);
1726 else
1727 pDom->arcDOM = xmlCreateTextNode(pParent,
1728 s,
1729 len,
1730 &pDom->pLastWasTextNode);
1731 }
1732 }
1733}
1734
1735/*
1736 *@@ CommentHandler:
1737 * @expat handler for @comments.
1738 *
1739 * Note: This is only set if DF_PARSECOMMENTS is
1740 * flagged with xmlCreateDOM.
1741 *
1742 *@@added V0.9.9 (2001-02-14) [umoeller]
1743 */
1744
1745static void EXPATENTRY CommentHandler(void *pUserData, // in: our PXMLDOM really
1746 const XML_Char *data)
1747{
1748 PXMLDOM pDom = (PXMLDOM)pUserData;
1749
1750 // continue parsing only if we had no errors so far
1751 if (!pDom->arcDOM)
1752 {
1753 // we need a new text node:
1754 PDOMSTACKITEM pSI = PopElementStack(pDom,
1755 NULL); // no free
1756 if (!pDom->arcDOM)
1757 {
1758 PDOMNODE pParent = pSI->pDomNode,
1759 pNew = NULL;
1760
1761 pDom->arcDOM = xmlCreateCommentNode(pParent,
1762 data,
1763 &pNew);
1764 }
1765 }
1766}
1767
1768/*
1769 *@@ StartDoctypeDeclHandler:
1770 * @expat handler that is called at the start of a DOCTYPE
1771 * declaration, before any external or internal subset is
1772 * parsed.
1773 *
1774 * Both pcszSysid and pcszPubid may be NULL. "fHasInternalSubset"
1775 * will be non-zero if the DOCTYPE declaration has an internal subset.
1776 *
1777 *@@added V0.9.9 (2001-02-14) [umoeller]
1778 */
1779
1780static void EXPATENTRY StartDoctypeDeclHandler(void *pUserData,
1781 const XML_Char *pcszDoctypeName,
1782 const XML_Char *pcszSysid,
1783 const XML_Char *pcszPubid,
1784 int fHasInternalSubset)
1785{
1786 PXMLDOM pDom = (PXMLDOM)pUserData;
1787
1788 // continue parsing only if we had no errors so far
1789 if (!pDom->arcDOM)
1790 {
1791 // get the document node
1792 PDOMDOCUMENTNODE pDocumentNode = pDom->pDocumentNode;
1793 if (!pDocumentNode)
1794 pDom->arcDOM = ERROR_DOM_NO_DOCUMENT;
1795 else
1796 {
1797 // doctype must be null
1798 if (pDom->pDocTypeNode)
1799 pDom->arcDOM = ERROR_DOM_DUPLICATE_DOCTYPE;
1800 else
1801 pDom->arcDOM = xmlCreateDocumentTypeNode(pDocumentNode,
1802 pcszDoctypeName,
1803 pcszSysid,
1804 pcszPubid,
1805 fHasInternalSubset,
1806 &pDom->pDocTypeNode);
1807 }
1808 }
1809}
1810
1811/*
1812 *@@ EndDoctypeDeclHandler:
1813 * @expat handler that is called at the end of a DOCTYPE
1814 * declaration, after parsing any external subset.
1815 *
1816 *@@added V0.9.9 (2001-02-14) [umoeller]
1817 */
1818
1819static void EXPATENTRY EndDoctypeDeclHandler(void *pUserData) // in: our PXMLDOM really
1820{
1821 PXMLDOM pDom = (PXMLDOM)pUserData;
1822
1823 // continue parsing only if we had no errors so far
1824 if (!pDom->arcDOM)
1825 {
1826 }
1827}
1828
1829/*
1830 *@@ NotationDeclHandler:
1831 * @expat handler for @notation_declarations.
1832 *
1833 * @@todo
1834 *
1835 *@@added V0.9.9 (2001-02-14) [umoeller]
1836 */
1837
1838static void EXPATENTRY NotationDeclHandler(void *pUserData, // in: our PXMLDOM really
1839 const XML_Char *pcszNotationName,
1840 const XML_Char *pcszBase,
1841 const XML_Char *pcszSystemId,
1842 const XML_Char *pcszPublicId)
1843{
1844 PXMLDOM pDom = (PXMLDOM)pUserData;
1845
1846 // continue parsing only if we had no errors so far
1847 if (!pDom->arcDOM)
1848 {
1849 }
1850}
1851
1852/*
1853 *@@ ExternalEntityRefHandler:
1854 * @expat handler for references to @external_entities.
1855 *
1856 * This handler is also called for processing an external DTD
1857 * subset if parameter entity parsing is in effect.
1858 * (See XML_SetParamEntityParsing.)
1859 *
1860 * The pcszContext argument specifies the parsing context in the
1861 * format expected by the context argument to
1862 * XML_ExternalEntityParserCreate; pcszContext is valid only until
1863 * the handler returns, so if the referenced entity is to be
1864 * parsed later, it must be copied.
1865 *
1866 * The pcszBase parameter is the base to use for relative system
1867 * identifiers. It is set by XML_SetBase and may be null.
1868 *
1869 * The pcszPublicId parameter is the public id given in the entity
1870 * declaration and may be null (since XML doesn't require public
1871 * identifiers).
1872 *
1873 * The pcszSystemId is the system identifier specified in the
1874 * entity declaration and is never null. This is an exact copy
1875 * of what was specified in the reference.
1876 *
1877 * There are a couple of ways in which this handler differs
1878 * from others. First, this handler returns an integer. A
1879 * non-zero value should be returned for successful handling
1880 * of the external entity reference. Returning a zero indicates
1881 * failure, and causes the calling parser to return an
1882 * ERROR_EXPAT_EXTERNAL_ENTITY_HANDLING error.
1883 *
1884 * Second, instead of having pUserData as its first argument,
1885 * it receives the parser that encountered the entity reference.
1886 * This, along with the context parameter, may be used as
1887 * arguments to a call to XML_ExternalEntityParserCreate.
1888 * Using the returned parser, the body of the external entity
1889 * can be recursively parsed.
1890 *
1891 * Since this handler may be called recursively, it should not
1892 * be saving information into global or static variables.
1893 *
1894 * Your handler isn't actually responsible for parsing the entity,
1895 * but it is responsible for creating a subsidiary parser with
1896 * XML_ExternalEntityParserCreate that will do the job. That returns
1897 * an instance of XML_Parser that has handlers and other data
1898 * structures initialized from the parent parser. You may then use
1899 * XML_Parse or XML_ParseBuffer calls against that parser. Since
1900 * external entities may refer to other external entities, your
1901 * handler should be prepared to be called recursively.
1902 *
1903 *@@added V0.9.14 (2001-08-09) [umoeller]
1904 */
1905
1906static int EXPATENTRY ExternalEntityRefHandler(void *pUserData, // in: our PXMLDOM really
1907 XML_Parser parser,
1908 const XML_Char *pcszContext,
1909 const XML_Char *pcszBase,
1910 const XML_Char *pcszSystemId,
1911 const XML_Char *pcszPublicId)
1912{
1913 PXMLDOM pDom = (PXMLDOM)pUserData;
1914
1915 int i = 0; // return error per default
1916
1917 APIRET arc = NO_ERROR;
1918
1919 // store the previous parser because
1920 // all the callbacks use the parser pointer
1921 XML_Parser pOldParser = pDom->pParser;
1922 pDom->pParser = NULL;
1923
1924 if ( (pDom->pfnExternalHandler)
1925 // create sub-parser and replace the one
1926 // in the DOM with it
1927 && (pDom->pParser = XML_ExternalEntityParserCreate(parser,
1928 pcszContext,
1929 "US-ASCII"))
1930 )
1931 {
1932 if ((arc = pDom->pfnExternalHandler(pDom,
1933 pDom->pParser,
1934 pcszSystemId,
1935 pcszPublicId)))
1936 {
1937 // error:
1938 // now this needs special handling, since we're
1939 // dealing with a sub-handler here...
1940
1941 if (arc == -1)
1942 // parser error: well, then xmlSetError has been
1943 // called from somewhere in the callbacks already,
1944 // and we can safely ignore this
1945 ;
1946 else
1947 {
1948 pDom->arcDOM = arc;
1949 if (pcszSystemId)
1950 {
1951 if (!pDom->pxstrFailingNode)
1952 pDom->pxstrFailingNode = xstrCreate(0);
1953 xstrcpy(pDom->pxstrFailingNode, pcszSystemId, 0);
1954 }
1955 pDom->pcszErrorDescription = xmlDescribeError(arc);
1956 pDom->ulErrorLine = XML_GetCurrentLineNumber(pDom->pParser);
1957 pDom->ulErrorColumn = XML_GetCurrentColumnNumber(pDom->pParser);
1958 }
1959 }
1960
1961 i = 1; // success
1962 }
1963 else
1964 xmlSetError(pDom,
1965 (!arc) ? ERROR_DOM_INVALID_EXTERNAL_HANDLER : arc,
1966 NULL,
1967 FALSE);
1968
1969 if (pDom->pParser)
1970 XML_ParserFree(pDom->pParser);
1971
1972 pDom->pParser = pOldParser;
1973
1974 return (i);
1975}
1976
1977/*
1978 *@@ ElementDeclHandler:
1979 * @expat handler for element declarations in a DTD. The
1980 * handler gets called with the name of the element in
1981 * the declaration and a pointer to a structure that contains
1982 * the element model.
1983 *
1984 * It is the application's responsibility to free this data
1985 * structure. @@todo
1986 *
1987 * The XML spec defines that no element may be declared more
1988 * than once.
1989 *
1990 *@@added V0.9.9 (2001-02-14) [umoeller]
1991 */
1992
1993static void EXPATENTRY ElementDeclHandler(void *pUserData, // in: our PXMLDOM really
1994 const XML_Char *pcszName,
1995 XMLCONTENT *pModel)
1996{
1997 PXMLDOM pDom = (PXMLDOM)pUserData;
1998
1999 // continue parsing only if we had no errors so far
2000 if (!pDom->arcDOM)
2001 {
2002 // OK, we're in a DOCTYPE node:
2003 PDOMDOCTYPENODE pDocType = pDom->pDocTypeNode;
2004 if (!pDocType)
2005 xmlSetError(pDom,
2006 ERROR_DOM_ELEMENT_DECL_OUTSIDE_DOCTYPE,
2007 pcszName,
2008 TRUE);
2009 else
2010 {
2011 // create an element declaration and push it unto the
2012 // declarations tree
2013 PCMELEMENTDECLNODE pNew = NULL;
2014 if (!(pDom->arcDOM = xmlCreateElementDecl(pcszName,
2015 pModel,
2016 &pNew)))
2017 // this recurses!!
2018 // after this, pModel is invalid
2019 {
2020 // add this to the doctype's declarations tree
2021 if (treeInsert(&pDocType->ElementDeclsTree,
2022 NULL,
2023 (TREE*)pNew,
2024 CompareXStrings))
2025 // element already declared:
2026 // according to the XML specs, this is a validity
2027 // constraint, so we report a validation error
2028 xmlSetError(pDom,
2029 ERROR_DOM_DUPLICATE_ELEMENT_DECL,
2030 pNew->Particle.NodeBase.strNodeName.psz,
2031 TRUE);
2032 }
2033 }
2034 }
2035}
2036
2037/*
2038 *@@ AddEnum:
2039 *
2040 *@@added V0.9.9 (2001-02-16) [umoeller]
2041 */
2042
2043static APIRET AddEnum(PCMATTRIBUTEDECL pDecl,
2044 const char *p, // in: start of name
2045 const char *pNext) // in: end of name (not included)
2046{
2047 // PSZ pszType = strhSubstr(p, pNext);
2048 PNODEBASE pNew = NULL;
2049 APIRET arc;
2050
2051 if (!(arc = xmlCreateNodeBase(ATTRIBUTE_DECLARATION_ENUM,
2052 sizeof(NODEBASE),
2053 p,
2054 (pNext - p),
2055 &pNew)))
2056 treeInsert(&pDecl->ValuesTree,
2057 NULL,
2058 (TREE*)pNew,
2059 CompareXStrings);
2060
2061 return (arc);
2062}
2063
2064/*
2065 *@@ AttlistDeclHandler:
2066 * @expat handler for attlist declarations in the DTD.
2067 *
2068 * This handler is called for each attribute. So a single attlist
2069 * declaration with multiple attributes declared will generate
2070 * multiple calls to this handler.
2071 *
2072 * -- pcszElementName is the name of the element for which the
2073 * attribute is being declared.
2074 *
2075 * -- pcszAttribName has the attribute name being declared.
2076 *
2077 * -- pcszAttribType is the attribute type.
2078 * It is the string representing the type in the declaration
2079 * with whitespace removed.
2080 *
2081 * -- pcszDefault holds the default value. It will be
2082 * NULL in the case of "#IMPLIED" or "#REQUIRED" attributes.
2083 * You can distinguish these two cases by checking the
2084 * fIsRequired parameter, which will be true in the case of
2085 * "#REQUIRED" attributes. Attributes which are "#FIXED"
2086 * will have also have a TRUE fIsRequired, but they will have
2087 * the non-NULL fixed value in the pcszDefault parameter.
2088 *
2089 *@@added V0.9.9 (2001-02-14) [umoeller]
2090 */
2091
2092static void EXPATENTRY AttlistDeclHandler(void *pUserData, // in: our PXMLDOM really
2093 const XML_Char *pcszElementName,
2094 const XML_Char *pcszAttribName,
2095 const XML_Char *pcszAttribType,
2096 const XML_Char *pcszDefault,
2097 int fIsRequired)
2098{
2099 PXMLDOM pDom = (PXMLDOM)pUserData;
2100
2101 // continue parsing only if we had no errors so far
2102 if (!pDom->arcDOM)
2103 {
2104 // OK, we're in a DOCTYPE node:
2105 PDOMDOCTYPENODE pDocType = pDom->pDocTypeNode;
2106 if (!pDocType)
2107 xmlSetError(pDom,
2108 ERROR_DOM_ATTLIST_DECL_OUTSIDE_DOCTYPE,
2109 pcszElementName,
2110 TRUE);
2111 else
2112 {
2113 PCMATTRIBUTEDECLBASE pThis = NULL,
2114 pCache = pDom->pAttListDeclCache;
2115
2116 // check if this is for the same attlist as the previous
2117 // call (we cache the pointer for speed)
2118 if ( (pCache)
2119 && (!strhcmp(pCache->NodeBase.strNodeName.psz,
2120 pcszElementName))
2121 )
2122 // this attdecl is for the same element:
2123 // use that (we won't have to search the tree)
2124 pThis = pDom->pAttListDeclCache;
2125
2126 if (!pThis)
2127 {
2128 // cache didn't match: look up attributes tree then...
2129 // note: cheap trick, we need an XSTRING for treeFind
2130 // but don't want malloc, so we use xstrInitSet
2131 XSTRING strElementName;
2132 xstrInitSet(&strElementName, (PSZ)pcszElementName);
2133 if (!(pThis = (PCMATTRIBUTEDECLBASE)treeFind(
2134 pDocType->AttribDeclBasesTree,
2135 (ULONG)&strElementName,
2136 CompareXStrings)))
2137 {
2138 // still not found:
2139 // we need a new node then
2140 if (!(pDom->arcDOM = xmlCreateNodeBase(ATTRIBUTE_DECLARATION_BASE,
2141 sizeof(CMATTRIBUTEDECLBASE),
2142 strElementName.psz,
2143 strElementName.ulLength,
2144 (PNODEBASE*)&pThis)))
2145 {
2146 // initialize the subtree
2147 treeInit(&pThis->AttribDeclsTree, NULL);
2148
2149 treeInsert(&pDocType->AttribDeclBasesTree,
2150 NULL,
2151 (TREE*)pThis,
2152 CompareXStrings);
2153 }
2154 }
2155
2156 pDom->pAttListDeclCache = pThis;
2157 }
2158
2159 if (pThis)
2160 {
2161 // pThis now has either an existing or a new CMATTRIBUTEDECLBASE;
2162 // add a new attribute def (CMATTRIBUTEDEDECL) to that
2163 PCMATTRIBUTEDECL pNew = NULL;
2164 if (!(pDom->arcDOM = xmlCreateNodeBase(ATTRIBUTE_DECLARATION,
2165 sizeof(CMATTRIBUTEDECL),
2166 pcszAttribName,
2167 0,
2168 (PNODEBASE*)&pNew)))
2169 {
2170 treeInit(&pNew->ValuesTree, NULL);
2171
2172 // check the type... expat is too lazy to parse this for
2173 // us, so we must check manually. Expat only normalizes
2174 // the "type" string to kick out whitespace, so we get:
2175 // (TYPE1|TYPE2|TYPE3)
2176 if (*pcszAttribType == '(')
2177 {
2178 // enumeration:
2179 const char *p = pcszAttribType + 1,
2180 *pNext;
2181 while ( (pNext = strchr(p, '|'))
2182 && (!pDom->arcDOM)
2183 )
2184 {
2185 pDom->arcDOM = AddEnum(pNew, p, pNext);
2186 p = pNext + 1;
2187 }
2188
2189 if (!pDom->arcDOM)
2190 {
2191 pNext = strchr(p, ')');
2192 AddEnum(pNew, p, pNext);
2193
2194 pNew->ulAttrType = CMAT_ENUM;
2195 }
2196 }
2197 else if (!strcmp(pcszAttribType, "CDATA"))
2198 pNew->ulAttrType = CMAT_CDATA;
2199 else if (!strcmp(pcszAttribType, "ID"))
2200 pNew->ulAttrType = CMAT_ID;
2201 else if (!strcmp(pcszAttribType, "IDREF"))
2202 pNew->ulAttrType = CMAT_IDREF;
2203 else if (!strcmp(pcszAttribType, "IDREFS"))
2204 pNew->ulAttrType = CMAT_IDREFS;
2205 else if (!strcmp(pcszAttribType, "ENTITY"))
2206 pNew->ulAttrType = CMAT_ENTITY;
2207 else if (!strcmp(pcszAttribType, "ENTITIES"))
2208 pNew->ulAttrType = CMAT_ENTITIES;
2209 else if (!strcmp(pcszAttribType, "NMTOKEN"))
2210 pNew->ulAttrType = CMAT_NMTOKEN;
2211 else if (!strcmp(pcszAttribType, "NMTOKENS"))
2212 pNew->ulAttrType = CMAT_NMTOKENS;
2213
2214 if (!pDom->arcDOM)
2215 {
2216 if (pcszDefault)
2217 {
2218 // fixed or default:
2219 if (fIsRequired)
2220 // fixed:
2221 pNew->ulConstraint = CMAT_FIXED_VALUE;
2222 else
2223 pNew->ulConstraint = CMAT_DEFAULT_VALUE;
2224
2225 pNew->pstrDefaultValue = xstrCreate(0);
2226 xstrcpy(pNew->pstrDefaultValue, pcszDefault, 0);
2227 }
2228 else
2229 // implied or required:
2230 if (fIsRequired)
2231 pNew->ulConstraint = CMAT_REQUIRED;
2232 else
2233 pNew->ulConstraint = CMAT_IMPLIED;
2234
2235 if (treeInsert(&pThis->AttribDeclsTree,
2236 NULL,
2237 (TREE*)pNew,
2238 CompareXStrings))
2239 xmlSetError(pDom,
2240 ERROR_DOM_DUPLICATE_ATTRIBUTE_DECL,
2241 pcszAttribName,
2242 TRUE);
2243 }
2244 }
2245 }
2246 }
2247 }
2248}
2249
2250/*
2251 *@@ EntityDeclHandler:
2252 * @expat handler that will be called for all entity declarations.
2253 *
2254 * The fIsParameterEntity argument will be non-zero in the case
2255 * of parameter entities and zero otherwise.
2256 *
2257 * For internal entities (<!ENTITY foo "bar">), pcszValue will be
2258 * non-NULL and pcszSystemId, pcszPublicId, and pcszNotationName
2259 * will all be NULL. The value string is not NULL terminated; the
2260 * length is provided in the iValueLength parameter. Do not use
2261 * iValueLength to test for internal entities, since it is legal
2262 * to have zero-length values. Instead check for whether or not
2263 * pcszValue is NULL.
2264 *
2265 * The pcszNotationName argument will have a non-NULL value only
2266 * for unparsed entity declarations.
2267 *
2268 *@@added V0.9.9 (2001-02-14) [umoeller]
2269 */
2270
2271static void EXPATENTRY EntityDeclHandler(void *pUserData, // in: our PXMLDOM really
2272 const XML_Char *pcszEntityName,
2273 int fIsParameterEntity,
2274 const XML_Char *pcszValue,
2275 int iValueLength,
2276 const XML_Char *pcszBase,
2277 const XML_Char *pcszSystemId,
2278 const XML_Char *pcszPublicId,
2279 const XML_Char *pcszNotationName)
2280{
2281 PXMLDOM pDom = (PXMLDOM)pUserData;
2282
2283 // continue parsing only if we had no errors so far
2284 if (!pDom->arcDOM)
2285 {
2286 }
2287}
2288
2289/* ******************************************************************
2290 *
2291 * DOM parser APIs
2292 *
2293 ********************************************************************/
2294
2295/*
2296 *@@ xmlCreateDOM:
2297 * creates an XMLDOM instance, which can be used for parsing
2298 * an XML document and building a @DOM tree from it at the
2299 * same time.
2300 *
2301 * Pass the XMLDOM returned here to xmlParse afterwards.
2302 *
2303 * Simplest possible usage:
2304 *
2305 * 1) Create a DOM instance.
2306 *
2307 + PXMLDOM pDom = NULL;
2308 + APIRET arc = xmlCreateDOM(flags, NULL, NULL, NULL, &pDom);
2309 +
2310 * 2) Give chunks of data (or an entire buffer)
2311 * to the DOM instance for parsing.
2312 *
2313 + arc = xmlParse(pDom,
2314 + pBuf,
2315 + TRUE); // if last, this will clean up the parser
2316 *
2317 * 3) Process the data in the DOM tree.
2318 *
2319 * Look at the DOMNODE definition to see how you
2320 * can traverse the data. Essentially, everything
2321 * is based on linked lists and string maps.
2322 *
2323 * A few helper functions have been added for
2324 * quick lookup. See xmlGetRootElement,
2325 * xmlGetFirstChild, xmlGetLastChild, xmlGetFirstText,
2326 * xmlGetElementsByTagName, xmlGetAttribute.
2327 *
2328 * 4) When done, call xmlFreeDOM, which will free all memory.
2329 *
2330 * The above code has limitations: only a few character
2331 * @encodings are supported, and @external_entities are
2332 * silently ignored.
2333 *
2334 * This function supports a number of callbacks and flags
2335 * to allow for maximum flexibility. Note however that
2336 * not all @expat features are supported yet.
2337 *
2338 * The following callbacks can be specified (any of these
2339 * can be NULL):
2340 *
2341 * -- pfnGetCPData should be specified if you want to
2342 * support character @encodings other than the
2343 * four that built into expat itself (see
2344 * XML_SetUnknownEncodingHandler). This is probably
2345 * a good idea to do under OS/2 since most OS/2
2346 * documents are in a PC-specific codepage such as
2347 * CP 850.
2348 *
2349 * This callback must have the following prototype:
2350 *
2351 + int APIENTRY FNGETCPDATA(PXMLDOM pDom, ULONG ulCP, int *piMap)
2352 *
2353 * The callback will only be called once for each
2354 * document if the "encoding" attribute of the
2355 * XML @text_declaration starts with "cp" (e.g.
2356 * "cp850") and will then receive the following
2357 * parameters:
2358 *
2359 * -- "pDom" will be the XMLDOM created by this function.
2360 *
2361 * -- ulCP has the IBM code page number, such as "850".
2362 *
2363 * -- piMap is an array of 256 integer values which must
2364 * be filled with the callback. Each array item index
2365 * is the codepage value, and the value of each field
2366 * is the corresponding Unicode value, or -1 if the
2367 * character is invalid (shouldn't happen with codepages).
2368 *
2369 * For example, the German o-umlaut character is
2370 * 0x94 in CP850 and 0x00f6 in Unicode. So set
2371 * the int at index 0x94 to 0x00f6.
2372 *
2373 * pvCallbackUser is a user parameter which is simply stored
2374 * in the XMLDOM struct which is returned. Since the XMLDOM
2375 * is passed to all the callbacks, you can access that pointer
2376 * from them.
2377 *
2378 * flParserFlags is any combination of the following:
2379 *
2380 * -- DF_PARSECOMMENTS: XML @comments are to be returned in
2381 * the DOM tree. Otherwise they are discarded.
2382 *
2383 * -- DF_PARSEDTD: add the @DTD of the document into the DOM tree
2384 * as well and validate the document, if a DTD was found.
2385 * Otherwise just parse and do not validate.
2386 *
2387 * DF_PARSEDTD is required for external entities to work
2388 * also.
2389 *
2390 * -- DF_FAIL_IF_NO_DTD: fail if no @DTD was found. Useful
2391 * if you want to enforce validation. @@todo
2392 *
2393 * -- DF_DROP_WHITESPACE: discard all @whitespace for those
2394 * elements that can only have element content. Whitespace
2395 * will be preserved only for elements that can have
2396 * mixed content. -- If this flag is not set, all whitespace
2397 * is preserved.
2398 *
2399 *@@added V0.9.9 (2001-02-14) [umoeller]
2400 *@@changed V0.9.14 (2001-08-09) [umoeller]: added DF_DROP_WHITESPACE support
2401 */
2402
2403APIRET xmlCreateDOM(ULONG flParserFlags, // in: DF_* parser flags
2404 PFNGETCPDATA pfnGetCPData, // in: codepage callback or NULL
2405 PFNEXTERNALHANDLER pfnExternalHandler, // in: external entity callback or NULL
2406 PVOID pvCallbackUser, // in: user param for callbacks
2407 PXMLDOM *ppDom) // out: XMLDOM struct created
2408{
2409 APIRET arc = NO_ERROR;
2410
2411 PXMLDOM pDom = (PXMLDOM)malloc(sizeof(*pDom));
2412 if (!pDom)
2413 arc = ERROR_NOT_ENOUGH_MEMORY;
2414 else
2415 {
2416 PDOMNODE pDocument = NULL;
2417
2418 memset(pDom, 0, sizeof(XMLDOM));
2419
2420 pDom->flParserFlags = flParserFlags;
2421 pDom->pfnGetCPData = pfnGetCPData;
2422 pDom->pfnExternalHandler = pfnExternalHandler;
2423 pDom->pvCallbackUser = pvCallbackUser;
2424
2425 lstInit(&pDom->llElementStack,
2426 TRUE); // auto-free
2427
2428 // create the document node
2429 if (!(arc = xmlCreateDomNode(NULL, // no parent
2430 DOMNODE_DOCUMENT,
2431 NULL,
2432 0,
2433 &pDocument)))
2434 {
2435 // store the document in the DOM
2436 pDom->pDocumentNode = (PDOMDOCUMENTNODE)pDocument;
2437
2438 // push the document on the stack so the handlers
2439 // will append to that
2440 PushElementStack(pDom,
2441 pDocument);
2442
2443 pDom->pParser = XML_ParserCreate(NULL);
2444
2445 if (!pDom->pParser)
2446 arc = ERROR_NOT_ENOUGH_MEMORY;
2447 else
2448 {
2449 if (pfnGetCPData)
2450 XML_SetUnknownEncodingHandler(pDom->pParser,
2451 UnknownEncodingHandler,
2452 pDom); // user data
2453
2454 XML_SetParamEntityParsing(pDom->pParser,
2455 XML_PARAM_ENTITY_PARSING_ALWAYS);
2456
2457 XML_SetElementHandler(pDom->pParser,
2458 StartElementHandler,
2459 EndElementHandler);
2460
2461 XML_SetCharacterDataHandler(pDom->pParser,
2462 CharacterDataHandler);
2463
2464 // XML_SetProcessingInstructionHandler(XML_Parser parser,
2465 // XML_ProcessingInstructionHandler handler);
2466
2467
2468 if (flParserFlags & DF_PARSECOMMENTS)
2469 XML_SetCommentHandler(pDom->pParser,
2470 CommentHandler);
2471
2472 if (pfnExternalHandler)
2473 XML_SetExternalEntityRefHandler(pDom->pParser,
2474 ExternalEntityRefHandler);
2475
2476 if (flParserFlags & DF_PARSEDTD)
2477 {
2478 XML_SetDoctypeDeclHandler(pDom->pParser,
2479 StartDoctypeDeclHandler,
2480 EndDoctypeDeclHandler);
2481
2482 XML_SetNotationDeclHandler(pDom->pParser,
2483 NotationDeclHandler);
2484
2485 XML_SetElementDeclHandler(pDom->pParser,
2486 ElementDeclHandler);
2487
2488 XML_SetAttlistDeclHandler(pDom->pParser,
2489 AttlistDeclHandler);
2490
2491 XML_SetEntityDeclHandler(pDom->pParser,
2492 EntityDeclHandler);
2493
2494 XML_SetParamEntityParsing(pDom->pParser,
2495 XML_PARAM_ENTITY_PARSING_ALWAYS);
2496 }
2497
2498 // XML_SetXmlDeclHandler ... do we care for this? I guess not
2499
2500 // pass the XMLDOM as user data to the handlers
2501 XML_SetUserData(pDom->pParser,
2502 pDom);
2503 }
2504 }
2505 }
2506
2507 if (arc == NO_ERROR)
2508 *ppDom = pDom;
2509 else
2510 xmlFreeDOM(pDom);
2511
2512 return (arc);
2513}
2514
2515/*
2516 *@@ xmlParse:
2517 * parses another chunk of XML data.
2518 *
2519 * If (fIsLast == TRUE), the internal @expat parser
2520 * will be freed, but not the DOM itself.
2521 *
2522 * You can pass an XML document to this function
2523 * in one flush. Set fIsLast = TRUE on the first
2524 * and only call then.
2525 *
2526 * This returns NO_ERROR if the chunk was successfully
2527 * parsed. Otherwise one of the following errors is
2528 * returned:
2529 *
2530 * -- ERROR_INVALID_PARAMETER
2531 *
2532 * -- ERROR_DOM_PARSING: an @expat parsing error occured.
2533 * This might also be memory problems.
2534 * With this error code, you will find specific
2535 * error information in the XMLDOM fields.
2536 *
2537 * -- ERROR_DOM_VALIDITY: the document is not @valid.
2538 * This can only happen if @DTD parsing was enabled
2539 * with xmlCreateDOM.
2540 * With this error code, you will find specific
2541 * error information in the XMLDOM fields.
2542 *
2543 *@@added V0.9.9 (2001-02-14) [umoeller]
2544 */
2545
2546APIRET xmlParse(PXMLDOM pDom, // in: DOM created by xmlCreateDOM
2547 const char *pcszBuf, // in: chunk of XML document data (or full document)
2548 ULONG cb, // in: size of that chunk (required)
2549 BOOL fIsLast) // in: set to TRUE if this is the last chunk
2550{
2551 APIRET arc = NO_ERROR;
2552
2553 if (!pDom)
2554 arc = ERROR_INVALID_PARAMETER;
2555 else
2556 {
2557 // go parse then
2558 if (!XML_Parse(pDom->pParser,
2559 pcszBuf,
2560 cb,
2561 fIsLast))
2562 {
2563 // expat parsing error:
2564 xmlSetError(pDom,
2565 XML_GetErrorCode(pDom->pParser),
2566 NULL,
2567 FALSE);
2568
2569 if (pDom->pDocumentNode)
2570 {
2571 xmlDeleteNode((PNODEBASE)pDom->pDocumentNode);
2572 pDom->pDocumentNode = NULL;
2573 }
2574
2575 arc = ERROR_DOM_PARSING;
2576 }
2577 else if (pDom->fInvalid)
2578 {
2579 // expat was doing OK, but the handlers' validation failed:
2580 arc = ERROR_DOM_VALIDITY;
2581 // error info has already been set
2582 }
2583 else
2584 // expat was doing OK, but maybe we have integrity errors
2585 // from our DOM callbacks:
2586 if (pDom->arcDOM)
2587 arc = pDom->arcDOM;
2588
2589 if (arc != NO_ERROR || fIsLast)
2590 {
2591 // last call or error: clean up
2592 XML_ParserFree(pDom->pParser);
2593 pDom->pParser = NULL;
2594
2595 // clean up the stack (but not the DOM itself)
2596 lstClear(&pDom->llElementStack);
2597 }
2598 }
2599
2600 return (arc);
2601}
2602
2603/*
2604 *@@ xmlFreeDOM:
2605 * cleans up all resources allocated by
2606 * xmlCreateDOM and xmlParse, including
2607 * the entire DOM tree.
2608 *
2609 * If you wish to keep any data, make
2610 * a copy of the respective pointers in pDom
2611 * or subitems and set them to NULL before
2612 * calling this function.
2613 *
2614 *@@added V0.9.9 (2001-02-14) [umoeller]
2615 */
2616
2617APIRET xmlFreeDOM(PXMLDOM pDom)
2618{
2619 APIRET arc = NO_ERROR;
2620 if (pDom)
2621 {
2622 // if the parser is still alive for some reason, close it.
2623 if (pDom->pParser)
2624 {
2625 XML_ParserFree(pDom->pParser);
2626 pDom->pParser = NULL;
2627 }
2628
2629 xmlDeleteNode((PNODEBASE)pDom->pDocumentNode);
2630
2631 if (pDom->pxstrSystemID)
2632 xstrFree(&pDom->pxstrSystemID);
2633 if (pDom->pxstrFailingNode)
2634 xstrFree(&pDom->pxstrFailingNode);
2635
2636 lstClear(&pDom->llElementStack);
2637
2638 free(pDom);
2639 }
2640
2641 return (arc);
2642}
2643
2644/* ******************************************************************
2645 *
2646 * DOM lookup
2647 *
2648 ********************************************************************/
2649
2650/*
2651 *@@ xmlFindElementDecl:
2652 * returns the CMELEMENTDECLNODE for the element
2653 * with the specified name or NULL if there's none.
2654 *
2655 *@@added V0.9.9 (2001-02-16) [umoeller]
2656 */
2657
2658PCMELEMENTDECLNODE xmlFindElementDecl(PXMLDOM pDom,
2659 const XSTRING *pcstrElementName)
2660{
2661 PCMELEMENTDECLNODE pElementDecl = NULL;
2662
2663 PDOMDOCTYPENODE pDocTypeNode = pDom->pDocTypeNode;
2664 if ( (pDocTypeNode)
2665 && (pcstrElementName)
2666 && (pcstrElementName->ulLength)
2667 )
2668 {
2669 pElementDecl = (PCMELEMENTDECLNODE)treeFind(
2670 pDocTypeNode->ElementDeclsTree,
2671 (ULONG)pcstrElementName,
2672 CompareXStrings);
2673 }
2674
2675 return (pElementDecl);
2676}
2677
2678/*
2679 *@@ xmlFindAttribDeclBase:
2680 * returns the CMATTRIBUTEDECLBASE for the specified
2681 * element name, or NULL if none exists.
2682 *
2683 * To find a specific attribute declaration from both
2684 * an element and an attribute name, use xmlFindAttribDecl
2685 * instead.
2686 *
2687 *@@added V0.9.9 (2001-02-16) [umoeller]
2688 */
2689
2690PCMATTRIBUTEDECLBASE xmlFindAttribDeclBase(PXMLDOM pDom,
2691 const XSTRING *pstrElementName)
2692{
2693 PDOMDOCTYPENODE pDocTypeNode = pDom->pDocTypeNode;
2694 if ( (pDocTypeNode)
2695 && (pstrElementName)
2696 && (pstrElementName->ulLength)
2697 )
2698 {
2699 return ((PCMATTRIBUTEDECLBASE)treeFind(
2700 pDocTypeNode->AttribDeclBasesTree,
2701 (ULONG)pstrElementName,
2702 CompareXStrings));
2703 }
2704
2705 return (NULL);
2706}
2707
2708/*
2709 *@@ xmlFindAttribDecl:
2710 * returns the CMATTRIBUTEDEDECL for the specified
2711 * element and attribute name, or NULL if none exists.
2712 *
2713 *@@added V0.9.9 (2001-02-16) [umoeller]
2714 */
2715
2716PCMATTRIBUTEDECL xmlFindAttribDecl(PXMLDOM pDom,
2717 const XSTRING *pstrElementName,
2718 const XSTRING *pstrAttribName,
2719 PCMATTRIBUTEDECLBASE *ppAttribDeclBase)
2720 // in/out: attr decl base cache;
2721 // the pointer pointed to by this
2722 // must be NULL on the first call
2723{
2724 if (pstrElementName && pstrAttribName)
2725 {
2726 if (!*ppAttribDeclBase)
2727 // first call for this:
2728 *ppAttribDeclBase = xmlFindAttribDeclBase(pDom,
2729 pstrElementName);
2730 if (*ppAttribDeclBase)
2731 {
2732 return ((PCMATTRIBUTEDECL)treeFind(
2733 ((**ppAttribDeclBase).AttribDeclsTree),
2734 (ULONG)pstrAttribName,
2735 CompareXStrings));
2736 }
2737 }
2738
2739 return (NULL);
2740}
2741
2742/*
2743 *@@ xmlGetRootElement:
2744 * returns the root element node from the specified
2745 * DOM. Useful helper to start enumerating elements.
2746 *
2747 *@@added V0.9.11 (2001-04-22) [umoeller]
2748 */
2749
2750PDOMNODE xmlGetRootElement(PXMLDOM pDom)
2751{
2752 PDOMDOCUMENTNODE pDocumentNode;
2753 PLISTNODE pListNode;
2754 if ( (pDom)
2755 && (pDocumentNode = pDom->pDocumentNode)
2756 && (pListNode = lstQueryFirstNode(&pDocumentNode->DomNode.llChildren))
2757 )
2758 {
2759 return ((PDOMNODE)pListNode->pItemData);
2760 }
2761
2762 return (NULL);
2763}
2764
2765/*
2766 *@@ xmlGetFirstChild:
2767 * returns the first child node of pDomNode.
2768 * See DOMNODE for what a "child" can be for the
2769 * various node types.
2770 *
2771 *@@added V0.9.9 (2001-02-14) [umoeller]
2772 */
2773
2774PDOMNODE xmlGetFirstChild(PDOMNODE pDomNode)
2775{
2776 PLISTNODE pListNode = lstQueryFirstNode(&pDomNode->llChildren);
2777 if (pListNode)
2778 return ((PDOMNODE)pListNode->pItemData);
2779
2780 return (0);
2781}
2782
2783/*
2784 *@@ xmlGetLastChild:
2785 * returns the last child node of pDomNode.
2786 * See DOMNODE for what a "child" can be for the
2787 * various node types.
2788 *
2789 *@@added V0.9.9 (2001-02-14) [umoeller]
2790 */
2791
2792PDOMNODE xmlGetLastChild(PDOMNODE pDomNode)
2793{
2794 PLISTNODE pListNode = lstQueryLastNode(&pDomNode->llChildren);
2795 if (pListNode)
2796 return ((PDOMNODE)pListNode->pItemData);
2797
2798 return (0);
2799}
2800
2801/*
2802 *@@ xmlGetFirstText:
2803 * returns the first text (character data) node
2804 * of pElement or NULL if there's none.
2805 *
2806 *@@added V0.9.11 (2001-04-22) [umoeller]
2807 */
2808
2809PDOMNODE xmlGetFirstText(PDOMNODE pElement)
2810{
2811 PLISTNODE pNode;
2812 PDOMNODE pDomNodeThis;
2813
2814 for (pNode = lstQueryFirstNode(&pElement->llChildren);
2815 pNode;
2816 pNode = pNode->pNext)
2817 {
2818 if ( (pDomNodeThis = (PDOMNODE)pNode->pItemData)
2819 && (pDomNodeThis->NodeBase.ulNodeType == DOMNODE_TEXT)
2820 )
2821 return (pDomNodeThis);
2822 }
2823
2824 return (NULL);
2825}
2826
2827/*
2828 *@@ xmlGetElementsByTagName:
2829 * returns a linked list of DOMNODE nodes which
2830 * match the specified element name. The special name
2831 * "*" matches all elements.
2832 *
2833 * pParent must be the parent element DOMNODE...
2834 * the only allowed exception is
2835 *
2836 * The caller must free the list by calling lstFree.
2837 * Returns NULL if no such elements could be found.
2838 *
2839 *@@added V0.9.9 (2001-02-14) [umoeller]
2840 */
2841
2842PLINKLIST xmlGetElementsByTagName(PDOMNODE pParent,
2843 const char *pcszName)
2844{
2845 // APIRET arc = NO_ERROR;
2846
2847 PLINKLIST pll = lstCreate(FALSE); // no free
2848 if (pll)
2849 {
2850 ULONG cItems = 0;
2851 BOOL fFindAll = !strcmp(pcszName, "*");
2852
2853 PLISTNODE pNode;
2854 PDOMNODE pDomNodeThis;
2855
2856 for (pNode = lstQueryFirstNode(&pParent->llChildren);
2857 pNode;
2858 pNode = pNode->pNext)
2859 {
2860 if ( (pDomNodeThis = (PDOMNODE)pNode->pItemData)
2861 && (pDomNodeThis->NodeBase.ulNodeType == DOMNODE_ELEMENT)
2862 && ( (fFindAll)
2863 || (!strcmp(pcszName, pDomNodeThis->NodeBase.strNodeName.psz))
2864 )
2865 )
2866 {
2867 // element matches:
2868 lstAppendItem(pll, pDomNodeThis);
2869 cItems++;
2870 }
2871 }
2872
2873 if (cItems)
2874 return (pll);
2875 else
2876 lstFree(&pll);
2877 }
2878
2879 return (0);
2880}
2881
2882/*
2883 *@@ xmlGetAttribute:
2884 * returns the value of pElement's attribute
2885 * with the given name or NULL.
2886 *
2887 * This is a const pointer into the element's
2888 * attribute list.
2889 *
2890 *@@added V0.9.11 (2001-04-22) [umoeller]
2891 */
2892
2893const XSTRING* xmlGetAttribute(PDOMNODE pElement,
2894 const char *pcszAttribName)
2895{
2896 XSTRING str;
2897 PDOMNODE pAttrNode;
2898 xstrInitSet(&str, (PSZ)pcszAttribName);
2899 // note, cheap trick: no malloc here, but we need
2900 // an XSTRING for treeFind
2901 if (pAttrNode = (PDOMNODE)treeFind(pElement->AttributesMap,
2902 (ULONG)&str,
2903 CompareXStrings))
2904 return (pAttrNode->pstrNodeValue);
2905
2906 return (NULL);
2907}
2908
2909/* ******************************************************************
2910 *
2911 * DOM build
2912 *
2913 ********************************************************************/
2914
2915/*
2916 *@@ xmlCreateDocument:
2917 * creates a new XML document.
2918 *
2919 * This is the first step in creating a DOM
2920 * tree in memory.
2921 *
2922 * This function creates a DOCUMENT node
2923 * (which is returned) and a root ELEMENT node
2924 * within the document. For convenience, the
2925 * root ELEMENT node is returned as well.
2926 *
2927 * This does not create a DOCTYPE node in
2928 * the document.
2929 *
2930 * After this, you can add sub-elements and
2931 * attributes to the root element using
2932 * xmlCreateElementNode and xmlCreateAttributeNode.
2933 *
2934 * Use xmlDeleteNode on the DOCUMENT node
2935 * to delete the entire DOM tree.
2936 *
2937 *@@added V0.9.12 (2001-05-21) [umoeller]
2938 */
2939
2940APIRET xmlCreateDocument(const char *pcszRootElementName, // in: root element name
2941 PDOMDOCUMENTNODE *ppDocument, // out: DOCUMENT node
2942 PDOMNODE *ppRootElement) // out: root ELEMENT node within DOCUMENT
2943{
2944 APIRET arc = NO_ERROR;
2945 PDOMDOCUMENTNODE pDocument = NULL;
2946 PDOMNODE pRootElement = NULL;
2947
2948 if ( (!pcszRootElementName) || (!ppDocument) || (!ppRootElement) )
2949 arc = ERROR_INVALID_PARAMETER;
2950 else
2951 // create the document node
2952 if (!(arc = xmlCreateDomNode(NULL, // no parent
2953 DOMNODE_DOCUMENT,
2954 NULL,
2955 0,
2956 (PDOMNODE*)&pDocument)))
2957 if (!(arc = xmlCreateDomNode((PDOMNODE)pDocument, // parent
2958 DOMNODE_ELEMENT,
2959 pcszRootElementName,
2960 0,
2961 &pRootElement)))
2962 {
2963 *ppDocument = pDocument;
2964 *ppRootElement = pRootElement;
2965 }
2966
2967 return (arc);
2968}
2969
2970/*
2971 *@@ WriteNodes:
2972 * internal helper for writing out the nodes.
2973 *
2974 *@@added V0.9.12 (2001-05-21) [umoeller]
2975 */
2976
2977static VOID WriteNodes(PXSTRING pxstr,
2978 PDOMNODE pDomNode) // in: node whose children are to be written (initially DOCUMENT)
2979{
2980 PLISTNODE pListNode;
2981
2982 BOOL fMixedContent = (xmlGetFirstText(pDomNode) != NULL);
2983
2984 for (pListNode = lstQueryFirstNode(&pDomNode->llChildren);
2985 (pListNode);
2986 pListNode = pListNode->pNext)
2987 {
2988 PDOMNODE pChildNode = (PDOMNODE)pListNode->pItemData;
2989
2990 switch (pChildNode->NodeBase.ulNodeType)
2991 {
2992 case DOMNODE_ELEMENT:
2993 {
2994 PDOMNODE pAttribNode;
2995 // write out opening ELEMENT tag
2996 // add a line break if this does NOT have mixed
2997 // content
2998 if (!fMixedContent)
2999 xstrcatc(pxstr, '\n');
3000
3001 xstrcatc(pxstr, '<');
3002 xstrcats(pxstr, &pChildNode->NodeBase.strNodeName);
3003
3004 // go through attributes
3005 for (pAttribNode = (PDOMNODE)treeFirst(pChildNode->AttributesMap);
3006 (pAttribNode);
3007 pAttribNode = (PDOMNODE)treeNext((TREE*)pAttribNode))
3008 {
3009 xstrcat(pxstr, "\n ", 0);
3010 xstrcats(pxstr, &pAttribNode->NodeBase.strNodeName);
3011 xstrcat(pxstr, "=\"", 0);
3012 xstrcats(pxstr, pAttribNode->pstrNodeValue);
3013 xstrcatc(pxstr, '\"');
3014 }
3015
3016 // now check... do we have child nodes?
3017 if (lstCountItems(&pChildNode->llChildren))
3018 {
3019 // yes:
3020 xstrcatc(pxstr, '>');
3021
3022 // recurse into this child element
3023 WriteNodes(pxstr, pChildNode);
3024
3025 if (!fMixedContent)
3026 xstrcatc(pxstr, '\n');
3027
3028 // write closing tag
3029 xstrcat(pxstr, "</", 0);
3030 xstrcats(pxstr, &pChildNode->NodeBase.strNodeName);
3031 xstrcatc(pxstr, '>');
3032 }
3033 else
3034 {
3035 // no child nodes:
3036 // mark this tag as "empty"
3037 xstrcat(pxstr, "/>", 0);
3038 }
3039 }
3040 break;
3041
3042 case DOMNODE_TEXT:
3043 case DOMNODE_COMMENT:
3044 // that's simple
3045 xstrcats(pxstr, pChildNode->pstrNodeValue);
3046 break;
3047
3048 case DOMNODE_DOCUMENT_TYPE:
3049 // @@todo
3050 break;
3051
3052 }
3053 }
3054}
3055
3056/*
3057 *@@ xmlWriteDocument:
3058 * creates a complete XML document in the specified
3059 * string buffer from the specified DOMDOCUMENTNODE.
3060 *
3061 * This creates a full XML document, starting with
3062 * the <?xml...?> header, the DTD (if present),
3063 * and the elements and attributes.
3064 *
3065 * The input XSTRING must be initialized. Its
3066 * contents will be overwritten, if any exists.
3067 *
3068 * Sooo... to write a full XML document to disk,
3069 * do the following:
3070 *
3071 * 1) Call xmlCreateDocument to have an empty
3072 * document with a root element created.
3073 *
3074 * 2) Add elements, subelements, and attributes
3075 * using xmlCreateElementNode and
3076 * xmlCreateAttributeNode.
3077 *
3078 * 3) Call xmlWriteDocument to have the XML
3079 * document written into an XSTRING.
3080 *
3081 * 4) Write the XSTRING to disk, e.g. using
3082 * fwrite().
3083 *
3084 * Note: You can also use doshWriteTextFile,
3085 * but you should then first convert the
3086 * line format using xstrConvertLineFormat.
3087 *
3088 * Example:
3089 *
3090 + APIRET arc = NO_ERROR;
3091 + PDOMDOCUMENTNODE pDocument = NULL;
3092 + PDOMNODE pRootElement = NULL;
3093 +
3094 + // create a DOM
3095 + if (!(arc = xmlCreateDocument("MYROOTNODE",
3096 + &pDocument,
3097 + &pRootElement)))
3098 + {
3099 + // add subelements to the root element
3100 + PDOMNODE pSubelement;
3101 + if (!(arc = xmlCreateElementNode(pRootElement,
3102 + "MYSUBELEMENT",
3103 + &pSubelement)))
3104 + {
3105 + // add an attribute
3106 + PDOMNODE pAttribute;
3107 + if (!(arc = xmlCreateAttributeNode(pSubElement,
3108 + "MYATTRIB",
3109 + "VALUE",
3110 + &pAttribute)))
3111 + {
3112 + // alright, turn this into a string
3113 + XSTRING str;
3114 + xstrInit(&str, 1000);
3115 + if (!(arc = xmlWriteDocument(pDocument,
3116 + "ISO-8859-1",
3117 + NULL, // or DOCTYPE
3118 + &str)))
3119 + {
3120 + FILE *file = fopen("myfile.xml", "w");
3121 + fwrite(str.psz,
3122 + 1,
3123 + str.ulLength,
3124 + file);
3125 + fclose(file);
3126 + }
3127 + }
3128 + }
3129 +
3130 + // this kills the entire tree
3131 + xmlDeleteNode((PNODEBASE)pDocument);
3132 +
3133 + }
3134 +
3135 *
3136 * A note about whitespace handling. Presently, this
3137 * adds line breaks after the opening tag of an
3138 * element if the element has element content only.
3139 * However, if the element has mixed content, this
3140 * line break is NOT automatically added because
3141 * white space may then be significant.
3142 *
3143 *@@added V0.9.12 (2001-05-21) [umoeller]
3144 */
3145
3146APIRET xmlWriteDocument(PDOMDOCUMENTNODE pDocument, // in: document node
3147 const char *pcszEncoding, // in: encoding string (e.g. "ISO-8859-1")
3148 const char *pcszDoctype, // in: entire DOCTYPE statement or NULL
3149 PXSTRING pxstr) // out: document
3150{
3151 APIRET arc = NO_ERROR;
3152
3153 if ( (!pDocument) || (!pcszEncoding) || (!pxstr) )
3154 arc = ERROR_INVALID_PARAMETER;
3155 else
3156 {
3157 // <?xml version="1.0" encoding="ISO-8859-1"?>
3158 xstrcpy(pxstr, "<?xml version=\"1.0\" encoding=\"", 0);
3159 xstrcat(pxstr, pcszEncoding, 0);
3160 xstrcat(pxstr, "\"?>\n", 0);
3161
3162 // write entire DOCTYPE statement
3163 if (pcszDoctype)
3164 {
3165 xstrcatc(pxstr, '\n');
3166 xstrcat(pxstr, pcszDoctype, 0);
3167 xstrcatc(pxstr, '\n');
3168 }
3169
3170 // write out children
3171 WriteNodes(pxstr, (PDOMNODE)pDocument);
3172
3173 xstrcatc(pxstr, '\n');
3174 }
3175
3176 return (arc);
3177}
3178
3179
Note: See TracBrowser for help on using the repository browser.