Tag
Hash :
14fff064
Author :
Date :
1999-06-22T21:49:07
Big changes, seems that 1.2.0 wasn't commited, here is 1.3.0, Daniel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
/*
* valid.c : part of the code use to do the DTD handling and the validity
* checking
*
* See Copyright for the status of this software.
*
* Daniel.Veillard@w3.org
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "valid.h"
#include "parser.h"
/****************************************************************
* *
* Util functions for data allocation/deallocation *
* *
****************************************************************/
/**
* xmlNewElementContent:
* @name: the subelement name or NULL
* @type: the type of element content decl
*
* Allocate an element content structure.
*
* Returns NULL if not, othervise the new element content structure
*/
xmlElementContentPtr
xmlNewElementContent(CHAR *name, int type) {
xmlElementContentPtr ret;
switch(type) {
case XML_ELEMENT_CONTENT_ELEMENT:
if (name == NULL) {
fprintf(stderr, "xmlNewElementContent : name == NULL !\n");
}
break;
case XML_ELEMENT_CONTENT_PCDATA:
case XML_ELEMENT_CONTENT_SEQ:
case XML_ELEMENT_CONTENT_OR:
if (name != NULL) {
fprintf(stderr, "xmlNewElementContent : name != NULL !\n");
}
break;
default:
fprintf(stderr, "xmlNewElementContent: unknown type %d\n", type);
exit(1);
}
ret = (xmlElementContentPtr) malloc(sizeof(xmlElementContent));
if (ret == NULL) {
fprintf(stderr, "xmlNewElementContent : out of memory!\n");
return(NULL);
}
ret->type = type;
ret->ocur = XML_ELEMENT_CONTENT_ONCE;
if (name != NULL)
ret->name = xmlStrdup(name);
else
ret->name = NULL;
ret->c1 = ret->c2 = NULL;
return(ret);
}
/**
* xmlCopyElementContent:
* @content: An element content pointer.
*
* Build a copy of an element content description.
*
* Returns the new xmlElementContentPtr or NULL in case of error.
*/
xmlElementContentPtr
xmlCopyElementContent(xmlElementContentPtr cur) {
xmlElementContentPtr ret;
if (cur == NULL) return(NULL);
ret = xmlNewElementContent((CHAR *) cur->name, cur->type);
if (ret == NULL) {
fprintf(stderr, "xmlCopyElementContent : out of memory\n");
return(NULL);
}
ret->ocur = cur->ocur;
if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
return(ret);
}
/**
* xmlFreeElementContent:
* @cur: the element content tree to free
*
* Free an element content structure. This is a recursive call !
*/
void
xmlFreeElementContent(xmlElementContentPtr cur) {
if (cur == NULL) return;
if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
if (cur->name != NULL) free((CHAR *) cur->name);
memset(cur, -1, sizeof(xmlElementContent));
free(cur);
}
/**
* xmlDumpElementContent:
* @buf: An XML buffer
* @content: An element table
* @glob: 1 if one must print the englobing parenthesis, 0 otherwise
*
* This will dump the content of the element table as an XML DTD definition
*/
void
xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
if (content == NULL) return;
if (glob) xmlBufferWriteChar(buf, "(");
switch (content->type) {
case XML_ELEMENT_CONTENT_PCDATA:
xmlBufferWriteChar(buf, "#PCDATA");
break;
case XML_ELEMENT_CONTENT_ELEMENT:
xmlBufferWriteCHAR(buf, content->name);
break;
case XML_ELEMENT_CONTENT_SEQ:
if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
(content->c1->type == XML_ELEMENT_CONTENT_SEQ))
xmlDumpElementContent(buf, content->c1, 1);
else
xmlDumpElementContent(buf, content->c1, 0);
xmlBufferWriteChar(buf, " , ");
if (content->c2->type == XML_ELEMENT_CONTENT_OR)
xmlDumpElementContent(buf, content->c2, 1);
else
xmlDumpElementContent(buf, content->c2, 0);
break;
case XML_ELEMENT_CONTENT_OR:
if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
(content->c1->type == XML_ELEMENT_CONTENT_SEQ))
xmlDumpElementContent(buf, content->c1, 1);
else
xmlDumpElementContent(buf, content->c1, 0);
xmlBufferWriteChar(buf, " | ");
if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
xmlDumpElementContent(buf, content->c2, 1);
else
xmlDumpElementContent(buf, content->c2, 0);
break;
default:
fprintf(stderr, "xmlDumpElementContent: unknown type %d\n",
content->type);
}
if (glob)
xmlBufferWriteChar(buf, ")");
switch (content->ocur) {
case XML_ELEMENT_CONTENT_ONCE:
break;
case XML_ELEMENT_CONTENT_OPT:
xmlBufferWriteChar(buf, "?");
break;
case XML_ELEMENT_CONTENT_MULT:
xmlBufferWriteChar(buf, "*");
break;
case XML_ELEMENT_CONTENT_PLUS:
xmlBufferWriteChar(buf, "+");
break;
}
}
/****************************************************************
* *
* Registration of DTD declarations *
* *
****************************************************************/
/**
* xmlCreateElementTable:
*
* create and initialize an empty element hash table.
*
* Returns the xmlElementTablePtr just created or NULL in case of error.
*/
xmlElementTablePtr
xmlCreateElementTable(void) {
xmlElementTablePtr ret;
ret = (xmlElementTablePtr)
malloc(sizeof(xmlElementTable));
if (ret == NULL) {
fprintf(stderr, "xmlCreateElementTable : malloc(%d) failed\n",
sizeof(xmlElementTable));
return(NULL);
}
ret->max_elements = XML_MIN_ELEMENT_TABLE;
ret->nb_elements = 0;
ret->table = (xmlElementPtr )
malloc(ret->max_elements * sizeof(xmlElement));
if (ret == NULL) {
fprintf(stderr, "xmlCreateElementTable : malloc(%d) failed\n",
ret->max_elements * sizeof(xmlElement));
free(ret);
return(NULL);
}
return(ret);
}
/**
* xmlAddElementDecl:
* @dtd: pointer to the DTD
* @name: the entity name
* @type: the element type
* @content: the element content tree or NULL
*
* Register a new element declaration
*
* Returns NULL if not, othervise the entity
*/
xmlElementPtr
xmlAddElementDecl(xmlDtdPtr dtd, const CHAR *name, int type,
xmlElementContentPtr content) {
xmlElementPtr ret, cur;
xmlElementTablePtr table;
int i;
if (dtd == NULL) {
fprintf(stderr, "xmlAddElementDecl: dtd == NULL\n");
return(NULL);
}
if (name == NULL) {
fprintf(stderr, "xmlAddElementDecl: name == NULL\n");
return(NULL);
}
switch (type) {
case XML_ELEMENT_TYPE_EMPTY:
if (content != NULL) {
fprintf(stderr,
"xmlAddElementDecl: content != NULL for EMPTY\n");
return(NULL);
}
break;
case XML_ELEMENT_TYPE_ANY:
if (content != NULL) {
fprintf(stderr,
"xmlAddElementDecl: content != NULL for ANY\n");
return(NULL);
}
break;
case XML_ELEMENT_TYPE_MIXED:
if (content == NULL) {
fprintf(stderr,
"xmlAddElementDecl: content == NULL for MIXED\n");
return(NULL);
}
break;
case XML_ELEMENT_TYPE_ELEMENT:
if (content == NULL) {
fprintf(stderr,
"xmlAddElementDecl: content == NULL for ELEMENT\n");
return(NULL);
}
break;
default:
fprintf(stderr, "xmlAddElementDecl: unknown type %d\n", type);
return(NULL);
}
/*
* Create the Element table if needed.
*/
table = dtd->elements;
if (table == NULL)
table = dtd->elements = xmlCreateElementTable();
if (table == NULL) {
fprintf(stderr, "xmlAddElementDecl: Table creation failed!\n");
return(NULL);
}
/*
* Validity Check:
* Search the DTD for previous declarations of the ELEMENT
*/
for (i = 0;i < table->nb_elements;i++) {
cur = &table->table[i];
if (!xmlStrcmp(cur->name, name)) {
/*
* The element is already defined in this Dtd.
*/
fprintf(stderr,
"xmlAddElementDecl: %s already defined\n", name);
return(NULL);
}
}
/*
* Grow the table, if needed.
*/
if (table->nb_elements >= table->max_elements) {
/*
* need more elements.
*/
table->max_elements *= 2;
table->table = (xmlElementPtr)
realloc(table->table, table->max_elements * sizeof(xmlElement));
if (table->table) {
fprintf(stderr, "xmlAddElementDecl: out of memory\n");
return(NULL);
}
}
ret = &table->table[table->nb_elements];
/*
* fill the structure.
*/
ret->type = type;
ret->name = xmlStrdup(name);
ret->content = xmlCopyElementContent(content);
table->nb_elements++;
return(ret);
}
/**
* xmlFreeElement:
* @elem: An element
*
* Deallocate the memory used by an element definition
*/
void
xmlFreeElement(xmlElementPtr elem) {
if (elem == NULL) return;
xmlFreeElementContent(elem->content);
if (elem->name != NULL)
free((CHAR *) elem->name);
memset(elem, -1, sizeof(xmlElement));
}
/**
* xmlFreeElementTable:
* @table: An element table
*
* Deallocate the memory used by an element hash table.
*/
void
xmlFreeElementTable(xmlElementTablePtr table) {
int i;
if (table == NULL) return;
for (i = 0;i < table->nb_elements;i++) {
xmlFreeElement(&table->table[i]);
}
free(table->table);
free(table);
}
/**
* xmlCopyElementTable:
* @table: An element table
*
* Build a copy of an element table.
*
* Returns the new xmlElementTablePtr or NULL in case of error.
*/
xmlElementTablePtr
xmlCopyElementTable(xmlElementTablePtr table) {
xmlElementTablePtr ret;
xmlElementPtr cur, ent;
int i;
ret = (xmlElementTablePtr) malloc(sizeof(xmlElementTable));
if (ret == NULL) {
fprintf(stderr, "xmlCopyElementTable: out of memory !\n");
return(NULL);
}
ret->table = (xmlElementPtr) malloc(table->max_elements *
sizeof(xmlElement));
if (ret->table == NULL) {
fprintf(stderr, "xmlCopyElementTable: out of memory !\n");
free(ret);
return(NULL);
}
ret->max_elements = table->max_elements;
ret->nb_elements = table->nb_elements;
for (i = 0;i < ret->nb_elements;i++) {
cur = &ret->table[i];
ent = &table->table[i];
cur->type = ent->type;
if (ent->name != NULL)
cur->name = xmlStrdup(ent->name);
else
cur->name = NULL;
cur->content = xmlCopyElementContent(ent->content);
}
return(ret);
}
/**
* xmlDumpElementTable:
* @buf: the XML buffer output
* @table: An element table
*
* This will dump the content of the element table as an XML DTD definition
*/
void
xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
int i;
xmlElementPtr cur;
if (table == NULL) return;
for (i = 0;i < table->nb_elements;i++) {
cur = &table->table[i];
switch (cur->type) {
case XML_ELEMENT_TYPE_EMPTY:
xmlBufferWriteChar(buf, "<!ELEMENT ");
xmlBufferWriteCHAR(buf, cur->name);
xmlBufferWriteChar(buf, " EMPTY>\n");
break;
case XML_ELEMENT_TYPE_ANY:
xmlBufferWriteChar(buf, "<!ELEMENT ");
xmlBufferWriteCHAR(buf, cur->name);
xmlBufferWriteChar(buf, " ANY>\n");
break;
case XML_ELEMENT_TYPE_MIXED:
xmlBufferWriteChar(buf, "<!ELEMENT ");
xmlBufferWriteCHAR(buf, cur->name);
xmlBufferWriteChar(buf, " ");
xmlDumpElementContent(buf, cur->content, 1);
xmlBufferWriteChar(buf, ">\n");
break;
case XML_ELEMENT_TYPE_ELEMENT:
xmlBufferWriteChar(buf, "<!ELEMENT ");
xmlBufferWriteCHAR(buf, cur->name);
xmlBufferWriteChar(buf, " ");
xmlDumpElementContent(buf, cur->content, 1);
xmlBufferWriteChar(buf, ">\n");
break;
default:
fprintf(stderr,
"xmlDumpElementTable: internal: unknown type %d\n",
cur->type);
}
}
}
/**
* xmlCreateEnumeration:
* @name: the enumeration name or NULL
*
* create and initialize an enumeration attribute node.
*
* Returns the xmlEnumerationPtr just created or NULL in case
* of error.
*/
xmlEnumerationPtr
xmlCreateEnumeration(CHAR *name) {
xmlEnumerationPtr ret;
ret = (xmlEnumerationPtr) malloc(sizeof(xmlEnumeration));
if (ret == NULL) {
fprintf(stderr, "xmlCreateEnumeration : malloc(%d) failed\n",
sizeof(xmlEnumeration));
return(NULL);
}
if (name != NULL)
ret->name = xmlStrdup(name);
else
ret->name = NULL;
ret->next = NULL;
return(ret);
}
/**
* xmlFreeEnumeration:
* @cur: the tree to free.
*
* free an enumeration attribute node (recursive).
*/
void
xmlFreeEnumeration(xmlEnumerationPtr cur) {
if (cur == NULL) return;
if (cur->next != NULL) xmlFreeEnumeration(cur->next);
if (cur->name != NULL) free((CHAR *) cur->name);
memset(cur, -1, sizeof(xmlEnumeration));
free(cur);
}
/**
* xmlCopyEnumeration:
* @cur: the tree to copy.
*
* Copy an enumeration attribute node (recursive).
*
* Returns the xmlEnumerationPtr just created or NULL in case
* of error.
*/
xmlEnumerationPtr
xmlCopyEnumeration(xmlEnumerationPtr cur) {
xmlEnumerationPtr ret;
if (cur == NULL) return(NULL);
ret = xmlCreateEnumeration((CHAR *) cur->name);
if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
else ret->next = NULL;
return(ret);
}
/**
* xmlCreateAttributeTable:
*
* create and initialize an empty attribute hash table.
*
* Returns the xmlAttributeTablePtr just created or NULL in case
* of error.
*/
xmlAttributeTablePtr
xmlCreateAttributeTable(void) {
xmlAttributeTablePtr ret;
ret = (xmlAttributeTablePtr)
malloc(sizeof(xmlAttributeTable));
if (ret == NULL) {
fprintf(stderr, "xmlCreateAttributeTable : malloc(%d) failed\n",
sizeof(xmlAttributeTable));
return(NULL);
}
ret->max_attributes = XML_MIN_ATTRIBUTE_TABLE;
ret->nb_attributes = 0;
ret->table = (xmlAttributePtr )
malloc(ret->max_attributes * sizeof(xmlAttribute));
if (ret == NULL) {
fprintf(stderr, "xmlCreateAttributeTable : malloc(%d) failed\n",
ret->max_attributes * sizeof(xmlAttribute));
free(ret);
return(NULL);
}
return(ret);
}
/**
* xmlAddAttributeDecl:
* @dtd: pointer to the DTD
* @elem: the element name
* @name: the attribute name
* @type: the attribute type
* @def: the attribute default type
* @defaultValue: the attribute default value
* @tree: if it's an enumeration, the associated list
*
* Register a new attribute declaration
*
* Returns NULL if not, othervise the entity
*/
xmlAttributePtr
xmlAddAttributeDecl(xmlDtdPtr dtd, const CHAR *elem, const CHAR *name,
int type, int def, const CHAR *defaultValue,
xmlEnumerationPtr tree) {
xmlAttributePtr ret, cur;
xmlAttributeTablePtr table;
int i;
if (dtd == NULL) {
fprintf(stderr, "xmlAddAttributeDecl: dtd == NULL\n");
return(NULL);
}
if (name == NULL) {
fprintf(stderr, "xmlAddAttributeDecl: name == NULL\n");
return(NULL);
}
if (elem == NULL) {
fprintf(stderr, "xmlAddAttributeDecl: elem == NULL\n");
return(NULL);
}
/* TODO: Lacks verifications !!! */
switch (type) {
case XML_ATTRIBUTE_CDATA:
break;
case XML_ATTRIBUTE_ID:
break;
case XML_ATTRIBUTE_IDREF:
break;
case XML_ATTRIBUTE_IDREFS:
break;
case XML_ATTRIBUTE_ENTITY:
break;
case XML_ATTRIBUTE_ENTITIES:
break;
case XML_ATTRIBUTE_NMTOKEN:
break;
case XML_ATTRIBUTE_NMTOKENS:
break;
case XML_ATTRIBUTE_ENUMERATION:
break;
case XML_ATTRIBUTE_NOTATION:
break;
default:
fprintf(stderr, "xmlAddAttributeDecl: unknown type %d\n", type);
return(NULL);
}
/*
* Create the Attribute table if needed.
*/
table = dtd->attributes;
if (table == NULL)
table = dtd->attributes = xmlCreateAttributeTable();
if (table == NULL) {
fprintf(stderr, "xmlAddAttributeDecl: Table creation failed!\n");
return(NULL);
}
/*
* Validity Check:
* Search the DTD for previous declarations of the ATTLIST
*/
for (i = 0;i < table->nb_attributes;i++) {
cur = &table->table[i];
if ((!xmlStrcmp(cur->name, name)) && (!xmlStrcmp(cur->elem, elem))) {
/*
* The attribute is already defined in this Dtd.
*/
fprintf(stderr,
"xmlAddAttributeDecl: %s already defined\n", name);
}
}
/*
* Grow the table, if needed.
*/
if (table->nb_attributes >= table->max_attributes) {
/*
* need more attributes.
*/
table->max_attributes *= 2;
table->table = (xmlAttributePtr)
realloc(table->table, table->max_attributes * sizeof(xmlAttribute));
if (table->table) {
fprintf(stderr, "xmlAddAttributeDecl: out of memory\n");
return(NULL);
}
}
ret = &table->table[table->nb_attributes];
/*
* fill the structure.
*/
ret->type = type;
ret->name = xmlStrdup(name);
ret->elem = xmlStrdup(elem);
ret->def = def;
ret->tree = tree;
if (defaultValue != NULL)
ret->defaultValue = xmlStrdup(defaultValue);
else
ret->defaultValue = NULL;
table->nb_attributes++;
return(ret);
}
/**
* xmlFreeAttribute:
* @elem: An attribute
*
* Deallocate the memory used by an attribute definition
*/
void
xmlFreeAttribute(xmlAttributePtr attr) {
if (attr == NULL) return;
if (attr->tree != NULL)
xmlFreeEnumeration(attr->tree);
if (attr->elem != NULL)
free((CHAR *) attr->elem);
if (attr->name != NULL)
free((CHAR *) attr->name);
if (attr->defaultValue != NULL)
free((CHAR *) attr->defaultValue);
memset(attr, -1, sizeof(xmlAttribute));
}
/**
* xmlFreeAttributeTable:
* @table: An attribute table
*
* Deallocate the memory used by an entities hash table.
*/
void
xmlFreeAttributeTable(xmlAttributeTablePtr table) {
int i;
if (table == NULL) return;
for (i = 0;i < table->nb_attributes;i++) {
xmlFreeAttribute(&table->table[i]);
}
free(table->table);
free(table);
}
/**
* xmlCopyAttributeTable:
* @table: An attribute table
*
* Build a copy of an attribute table.
*
* Returns the new xmlAttributeTablePtr or NULL in case of error.
*/
xmlAttributeTablePtr
xmlCopyAttributeTable(xmlAttributeTablePtr table) {
xmlAttributeTablePtr ret;
xmlAttributePtr cur, attr;
int i;
ret = (xmlAttributeTablePtr) malloc(sizeof(xmlAttributeTable));
if (ret == NULL) {
fprintf(stderr, "xmlCopyAttributeTable: out of memory !\n");
return(NULL);
}
ret->table = (xmlAttributePtr) malloc(table->max_attributes *
sizeof(xmlAttribute));
if (ret->table == NULL) {
fprintf(stderr, "xmlCopyAttributeTable: out of memory !\n");
free(ret);
return(NULL);
}
ret->max_attributes = table->max_attributes;
ret->nb_attributes = table->nb_attributes;
for (i = 0;i < ret->nb_attributes;i++) {
cur = &ret->table[i];
attr = &table->table[i];
cur->type = attr->type;
cur->def = attr->def;
cur->tree = xmlCopyEnumeration(attr->tree);
if (attr->elem != NULL)
cur->elem = xmlStrdup(attr->elem);
else
cur->elem = NULL;
if (attr->name != NULL)
cur->name = xmlStrdup(attr->name);
else
cur->name = NULL;
if (attr->defaultValue != NULL)
cur->defaultValue = xmlStrdup(attr->defaultValue);
else
cur->defaultValue = NULL;
}
return(ret);
}
/**
* xmlDumpAttributeTable:
* @buf: the XML buffer output
* @table: An attribute table
*
* This will dump the content of the attribute table as an XML DTD definition
*/
void
xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
int i;
xmlAttributePtr cur;
if (table == NULL) return;
for (i = 0;i < table->nb_attributes;i++) {
cur = &table->table[i];
xmlBufferWriteChar(buf, "<!ATTLIST ");
xmlBufferWriteCHAR(buf, cur->elem);
xmlBufferWriteChar(buf, " ");
xmlBufferWriteCHAR(buf, cur->name);
switch (cur->type) {
case XML_ATTRIBUTE_CDATA:
xmlBufferWriteChar(buf, " CDATA");
break;
case XML_ATTRIBUTE_ID:
xmlBufferWriteChar(buf, " ID");
break;
case XML_ATTRIBUTE_IDREF:
xmlBufferWriteChar(buf, " IDREF");
break;
case XML_ATTRIBUTE_IDREFS:
xmlBufferWriteChar(buf, " IDREFS");
break;
case XML_ATTRIBUTE_ENTITY:
xmlBufferWriteChar(buf, " ENTITY");
break;
case XML_ATTRIBUTE_ENTITIES:
xmlBufferWriteChar(buf, " ENTITIES");
break;
case XML_ATTRIBUTE_NMTOKEN:
xmlBufferWriteChar(buf, " NMTOKEN");
break;
case XML_ATTRIBUTE_NMTOKENS:
xmlBufferWriteChar(buf, " NMTOKENS");
break;
case XML_ATTRIBUTE_ENUMERATION:
xmlBufferWriteChar(buf, " (pbm)");
break;
case XML_ATTRIBUTE_NOTATION:
xmlBufferWriteChar(buf, " NOTATION (pbm)");
break;
default:
fprintf(stderr,
"xmlDumpAttributeTable: internal: unknown type %d\n",
cur->type);
}
switch (cur->def) {
case XML_ATTRIBUTE_NONE:
break;
case XML_ATTRIBUTE_REQUIRED:
xmlBufferWriteChar(buf, " #REQUIRED");
break;
case XML_ATTRIBUTE_IMPLIED:
xmlBufferWriteChar(buf, " #IMPLIED");
if (cur->defaultValue != NULL) {
xmlBufferWriteChar(buf, " ");
xmlBufferWriteQuotedString(buf, cur->defaultValue);
}
break;
case XML_ATTRIBUTE_FIXED:
xmlBufferWriteChar(buf, " #FIXED ");
xmlBufferWriteQuotedString(buf, cur->defaultValue);
break;
default:
fprintf(stderr,
"xmlDumpAttributeTable: internal: unknown default %d\n",
cur->def);
}
xmlBufferWriteChar(buf, ">\n");
}
}
/************************************************************************
* *
* NOTATIONs *
* *
************************************************************************/
/**
* xmlCreateNotationTable:
*
* create and initialize an empty notation hash table.
*
* Returns the xmlNotationTablePtr just created or NULL in case
* of error.
*/
xmlNotationTablePtr
xmlCreateNotationTable(void) {
xmlNotationTablePtr ret;
ret = (xmlNotationTablePtr)
malloc(sizeof(xmlNotationTable));
if (ret == NULL) {
fprintf(stderr, "xmlCreateNotationTable : malloc(%d) failed\n",
sizeof(xmlNotationTable));
return(NULL);
}
ret->max_notations = XML_MIN_NOTATION_TABLE;
ret->nb_notations = 0;
ret->table = (xmlNotationPtr )
malloc(ret->max_notations * sizeof(xmlNotation));
if (ret == NULL) {
fprintf(stderr, "xmlCreateNotationTable : malloc(%d) failed\n",
ret->max_notations * sizeof(xmlNotation));
free(ret);
return(NULL);
}
return(ret);
}
/**
* xmlAddNotationDecl:
* @dtd: pointer to the DTD
* @name: the entity name
* @PublicID: the public identifier or NULL
* @SystemID: the system identifier or NULL
*
* Register a new notation declaration
*
* Returns NULL if not, othervise the entity
*/
xmlNotationPtr
xmlAddNotationDecl(xmlDtdPtr dtd, const CHAR *name, const CHAR *PublicID,
const CHAR *SystemID) {
xmlNotationPtr ret, cur;
xmlNotationTablePtr table;
int i;
if (dtd == NULL) {
fprintf(stderr, "xmlAddNotationDecl: dtd == NULL\n");
return(NULL);
}
if (name == NULL) {
fprintf(stderr, "xmlAddNotationDecl: name == NULL\n");
return(NULL);
}
if ((PublicID == NULL) && (SystemID == NULL)) {
fprintf(stderr, "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
}
/*
* Create the Notation table if needed.
*/
table = dtd->notations;
if (table == NULL)
table = dtd->notations = xmlCreateNotationTable();
if (table == NULL) {
fprintf(stderr, "xmlAddNotationDecl: Table creation failed!\n");
return(NULL);
}
/*
* Validity Check:
* Search the DTD for previous declarations of the ATTLIST
*/
for (i = 0;i < table->nb_notations;i++) {
cur = &table->table[i];
if (!xmlStrcmp(cur->name, name)) {
/*
* The notation is already defined in this Dtd.
*/
fprintf(stderr,
"xmlAddNotationDecl: %s already defined\n", name);
}
}
/*
* Grow the table, if needed.
*/
if (table->nb_notations >= table->max_notations) {
/*
* need more notations.
*/
table->max_notations *= 2;
table->table = (xmlNotationPtr)
realloc(table->table, table->max_notations * sizeof(xmlNotation));
if (table->table) {
fprintf(stderr, "xmlAddNotationDecl: out of memory\n");
return(NULL);
}
}
ret = &table->table[table->nb_notations];
/*
* fill the structure.
*/
ret->name = xmlStrdup(name);
if (SystemID != NULL)
ret->SystemID = xmlStrdup(SystemID);
else
ret->SystemID = NULL;
if (PublicID != NULL)
ret->PublicID = xmlStrdup(PublicID);
else
ret->PublicID = NULL;
table->nb_notations++;
return(ret);
}
/**
* xmlFreeNotation:
* @not: A notation
*
* Deallocate the memory used by an notation definition
*/
void
xmlFreeNotation(xmlNotationPtr nota) {
if (nota == NULL) return;
if (nota->name != NULL)
free((CHAR *) nota->name);
if (nota->PublicID != NULL)
free((CHAR *) nota->PublicID);
if (nota->SystemID != NULL)
free((CHAR *) nota->SystemID);
memset(nota, -1, sizeof(xmlNotation));
}
/**
* xmlFreeNotationTable:
* @table: An notation table
*
* Deallocate the memory used by an entities hash table.
*/
void
xmlFreeNotationTable(xmlNotationTablePtr table) {
int i;
if (table == NULL) return;
for (i = 0;i < table->nb_notations;i++) {
xmlFreeNotation(&table->table[i]);
}
free(table->table);
free(table);
}
/**
* xmlCopyNotationTable:
* @table: A notation table
*
* Build a copy of a notation table.
*
* Returns the new xmlNotationTablePtr or NULL in case of error.
*/
xmlNotationTablePtr
xmlCopyNotationTable(xmlNotationTablePtr table) {
xmlNotationTablePtr ret;
xmlNotationPtr cur, nota;
int i;
ret = (xmlNotationTablePtr) malloc(sizeof(xmlNotationTable));
if (ret == NULL) {
fprintf(stderr, "xmlCopyNotationTable: out of memory !\n");
return(NULL);
}
ret->table = (xmlNotationPtr) malloc(table->max_notations *
sizeof(xmlNotation));
if (ret->table == NULL) {
fprintf(stderr, "xmlCopyNotationTable: out of memory !\n");
free(ret);
return(NULL);
}
ret->max_notations = table->max_notations;
ret->nb_notations = table->nb_notations;
for (i = 0;i < ret->nb_notations;i++) {
cur = &ret->table[i];
nota = &table->table[i];
if (nota->name != NULL)
cur->name = xmlStrdup(nota->name);
else
cur->name = NULL;
if (nota->PublicID != NULL)
cur->PublicID = xmlStrdup(nota->PublicID);
else
cur->PublicID = NULL;
if (nota->SystemID != NULL)
cur->SystemID = xmlStrdup(nota->SystemID);
else
cur->SystemID = NULL;
}
return(ret);
}
/**
* xmlDumpNotationTable:
* @buf: the XML buffer output
* @table: A notation table
*
* This will dump the content of the notation table as an XML DTD definition
*/
void
xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
int i;
xmlNotationPtr cur;
if (table == NULL) return;
for (i = 0;i < table->nb_notations;i++) {
cur = &table->table[i];
xmlBufferWriteChar(buf, "<!NOTATION ");
xmlBufferWriteCHAR(buf, cur->name);
if (cur->PublicID != NULL) {
xmlBufferWriteChar(buf, " PUBLIC ");
xmlBufferWriteQuotedString(buf, cur->PublicID);
if (cur->SystemID != NULL) {
xmlBufferWriteChar(buf, " ");
xmlBufferWriteCHAR(buf, cur->SystemID);
}
} else {
xmlBufferWriteChar(buf, " SYSTEM ");
xmlBufferWriteCHAR(buf, cur->SystemID);
}
xmlBufferWriteChar(buf, " >\n");
}
}