GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — livecode/revxml/src/revxml.cpp at develop · livecode/livecode
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Expand file tree/ Copy pathrevxml.cppMore file actions 3135 lines (2835 loc) · 85.5 KB / Copy pathrevxml.cppFile metadata and controls3135 lines (2835 loc) · 85.5 KB 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 /* Copyright (C) 2003-2015 LiveCode Ltd. This file is part of LiveCode. LiveCode is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License v3 as published by the Free Software Foundation. LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ #include <cstdlib> #include <cstdio> #include <cmath> #include <ctime> #include <vector> #include <revolution/external.h> #include <revolution/support.h> #include <libxml/xpath.h> #include <libxslt/transform.h> #include <libxslt/xsltutils.h> #include "cxml.h" #define istrdup strdup #define INTSTRSIZE 16 #ifdef _MACOSX char *_strrev(char *str) { int SmallIndex = 0; int BigIndex = strlen(str) - 1; while (SmallIndex < BigIndex) { char Temp = str[SmallIndex]; str[SmallIndex] = str[BigIndex]; str[BigIndex] = Temp; SmallIndex++; BigIndex--; } return str; } char *strlwr(char *str) { while (*str) { *str = tolower(*str); str++; } return str; } #endif #if defined _LINUX || defined(TARGET_SUBPLATFORM_IPHONE) || defined(TARGET_SUBPLATFORM_ANDROID) #define stricmp strcasecmp char *_strrev(char * str) { int SmallIndex = 0; int BigIndex = strlen(str) - 1; while (SmallIndex < BigIndex) { char Temp = str[SmallIndex]; str[SmallIndex] = str[BigIndex]; str[BigIndex] = Temp; SmallIndex++; BigIndex--; } return str; } char *strlwr(char *str) { while (*str) { *str = tolower(*str); str++; } return str; } #endif using namespace std; typedef vector<CXMLDocument *> VXMLDocList; enum XMLErrs { XMLERR_BADARGUMENTS, XMLERR_BADXML, XMLERR_BADELEMENT, XMLERR_BADATTRIBUTE, XMLERR_BADDOCID, XMLERR_BADDTD, XMLERR_LICENSE, XMLERR_BADMOVE, XMLERR_BADCOPY, XMLERR_NOFILEPERMS, XPATHERR_BADDOCCONTEXT, XPATHERR_CANTRESOLVE, XPATHERR_BADDOCPOINTER, }; const char *xmlerrors[] = { "xmlerr, invalid number of arguments", "xmlerr, can't parse xml", "xmlerr, can't find element", "xmlerr, can't find attribute", "xmlerr, bad document id", "xmlerr, validation error", "xmlerr, restricted under current license", "xmlerr, can't move node into itself", "xmlerr, can't copy node into itself", "xmlerr, file access not permitted", "xmlerr, couldn't assign document context", "xmlerr, can't resolve xpath", "xmlerr, bad document pointer", }; //HS-2010-10-11: [[ Bug 7586 ]] Reinstate libxml2 to create name spaces. Implement new liveCode commands to suppress name space creation. Bool XML_ProcessNameSpaces = True; //A class wrapped around a vector object used to keep a list of parsed XML documents class XMLDocumentList { public: //when class terminates make sure to free all documents ~XMLDocumentList() {clear();} //loop through, and free all documents void clear() { if (doclist.empty()) return; VXMLDocList::iterator theIterator; for (theIterator = doclist.begin(); theIterator != doclist.end(); theIterator++){ CXMLDocument *curobject = (CXMLDocument *)(*theIterator); // MDW-2013-07-09: [[ RevXmlXPath ]] if (NULL != curobject->GetXPathContext()) xmlXPathFreeContext(curobject->GetXPathContext()); // MDW-2013-09-04: [[ RevXmlXslt ]] if (NULL != curobject->GetXsltContext()) xsltFreeStylesheet(curobject->GetXsltContext()); delete curobject; } doclist.clear(); } VXMLDocList *getList() {return &doclist;} //remove document from list by document id. Bool erase(const int fid) { VXMLDocList::iterator theIterator; for (theIterator = doclist.begin(); theIterator != doclist.end(); theIterator++){ CXMLDocument *curobject = (CXMLDocument *)(*theIterator); if (curobject->GetID() == fid){ delete curobject; doclist.erase(theIterator); return True; } } return False; } //find CXMLDocument by document id CXMLDocument *find(const int fid) { VXMLDocList::iterator theIterator; for (theIterator = doclist.begin(); theIterator != doclist.end(); theIterator++){ CXMLDocument *curobject = (CXMLDocument *)(*theIterator); if (curobject->GetID() == fid) return curobject; } return NULL; } //return size of list int getsize() {return doclist.size();} //add xml document to list void add(CXMLDocument *newxmldoc) {doclist.push_back(newxmldoc);} protected: VXMLDocList doclist; }; XMLDocumentList doclist; //DUMMY FUNCTIONS TO KEEP LIBXML FROM DISPLAYING A CONSOLE WINDOW void REVXML_INIT() { } void REVXML_QUIT() { doclist.clear(); } //------------------------------------UTILITY FUNCTIONS-------------------------- void DispatchMetaCardMessage(const char *messagename, const char *tmessage) { int retvalue = 0; SetGlobal("xmlvariable",tmessage,&retvalue); char mcmessage[256]; // MDW-2013-07-09: [[ RevXmlXPath ]] sprintf(mcmessage,"global xmlvariable;try;send \"%s xmlvariable\" to current card of stack the topstack;catch errno;end try;put 0 into xmlvariable",messagename); SendCardMessage(mcmessage, &retvalue); } bool stringToBool(const char *p_string) { if (stricmp(p_string, "true") == 0) return true; else return false; } //------------------------------------XML MESSAGES---------------------------------------- /*MESSAGE: startDocument - Called when starting parsing document*/ void CB_startDocument() { DispatchMetaCardMessage("revxmlStartTree",""); } /*MESSAGE: endDocument - Called when finished parsing document*/ void CB_endDocument() { DispatchMetaCardMessage("revxmlEndTree",""); } /*MESSAGE: startElement attlist - Called when an element's opening tag is encountered, with a return delimited list of attribute names and values*/ void CB_startElement(const char *name, const char **attributes) { char *buffer = NULL; int bufsize = 0,buflen = 0; if (attributes) { const char **cur; for(cur = attributes; cur && *cur; cur++) { util_concatstring((char *)*cur, strlen(*cur), buffer, buflen , bufsize); util_concatstring(",", 1 ,buffer, buflen , bufsize); cur++; util_concatstring((char *)*cur, strlen(*cur), buffer, buflen , bufsize); util_concatstring("\n", 1 ,buffer, buflen , bufsize); } } if (buflen) buffer[buflen-1] = '\0'; int retvalue = 0; char mcmessage[256]; SetGlobal("xmlvariable",buffer,&retvalue); sprintf(mcmessage,"global xmlvariable;send \"revStartXMLNode %s,xmlvariable\" to current card of stack the topstack;put 0 into xmlvariable", name); SendCardMessage(mcmessage, &retvalue); } /*MESSAGE: endElement - Called when an element's closing tag is encountered, or immediately\par tab after start_element() if the tag is in <tag/> format.*/ void CB_endElement(const char *name) { DispatchMetaCardMessage("revEndXMLNode",(char *)name); } //MESSAGE: XMLElementData data - sent when element data is encountered between tags void CB_elementData(const char *data, int length) { char *buffer = new (nothrow) char[length+1]; memcpy(buffer, data, length); buffer[length] = '\0'; DispatchMetaCardMessage("revStartXMLData",(char *)buffer); delete[] buffer; } static Bool REVXMLinited = False; #define simpleparse(a,b,c) (((b > a) | (c < a))?True:False) #ifndef REVDB static int computehash(char *keystr) { unsigned int value = 0; int length = strlen(keystr); const char *sptr = keystr; while (length--){ value += tolower(*sptr++); value = value * 3; } return value & 96000 -1; } #else extern int computehash(char *keystr); extern char * _strrev(char * str); extern char *strlwr(char *str); #endif // MDW-2013-06-22: [[ RevXmlXPath ]] #define REVXML_VERSIONSTRING "6.5.0" void REVXML_Version(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { char *result = NULL; *error = False; *pass = False; result = istrdup(REVXML_VERSIONSTRING); *retstring = (result != NULL ? result : (char *)calloc(1,1)); } void XML_Init(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { int count; static Bool littlecheat = False; char *buffer; int passkey = 0; char lencode[] = "clubfvhsqkjpymziadgxonrev"; *pass = False; if (nargs == 2){*error = False; buffer = istrdup(args[0]); strlwr(buffer); _strrev(buffer); int keylen = strlen(buffer); if (simpleparse(keylen,5,40) == True || littlecheat == True) {*retstring = istrdup("FALSE"); free(buffer);return;} for (count=0;count<keylen;count++) if (simpleparse(buffer[count]-97,0,26) == True){ *retstring = istrdup("FALSE"); free(buffer); return; } else buffer[count] = lencode[buffer[count]-97]; passkey = computehash(buffer); free(buffer); if (atoi(args[1]) == passkey || REVXMLinited == True) { *retstring = istrdup("TRUE"); REVXMLinited = True; } else { *retstring = istrdup("FALSE"); littlecheat = True; } } else *error = True; *retstring = (char *)calloc(1, 1); } //------------------------------------DOCUMENT CALLBACKS-------------------------------------- /* Function: XML_NewDocument - Parse XML document/data Input: [0]=xml data [1] = flag to determine whether to try parse xml data that is not well formed (ie. be forgiving) Output: XML document ID. On error, 'can't parse xml' and error message. Example: if xml_newdocument(data) is a number then beep */ void XML_NewDocument(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; if (nargs < 2){ *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } else{ CXMLDocument *newdoc = new (nothrow) CXMLDocument; Bool wellformed = util_strnicmp(args[1],"TRUE",4) == 0; Bool buildtree = True; Bool sendmessages = False; if (nargs >= 3) buildtree = util_strnicmp(args[2],"TRUE",4) == 0; if (nargs >= 4) sendmessages = util_strnicmp(args[3],"TRUE",4) == 0; CXMLDocument::allowcallbacks = sendmessages; CXMLDocument::buildtree = buildtree; if (newdoc->Read(args[0],strlen(args[0]),wellformed)){ if (CXMLDocument::buildtree){ doclist.add(newdoc); unsigned int docid = newdoc->GetID(); result = (char *)malloc(INTSTRSIZE); sprintf(result,"%d",docid); } else { delete newdoc; result = (char *)istrdup("0"); } } else { result = (char *)malloc(1024); sprintf(result,"%s\n%s",xmlerrors[XMLERR_BADXML],newdoc->GetError()); delete newdoc; } } *retstring = (result != NULL ? result : (char *)calloc(1,1)); } //HS-2010-10-11: [[ Bug 7586 ]] Reinstate libxml2 to create name spaces. Implement new liveCode commands to suppress name space creation. // See the comments of Function XML_NewDocument for detailed information of the operation void XML_NewDocumentNNS(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { XML_ProcessNameSpaces = False; XML_NewDocument(args, nargs, retstring, pass, error); } //HS-2010-10-11: [[ Bug 7586 ]] Reinstate libxml2 to create name spaces. Implement new liveCode commands to suppress name space creation. // See the comments of Function XML_NewDocument for detailed information of the operation void XML_NewDocumentNS(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { XML_ProcessNameSpaces = True; XML_NewDocument(args, nargs, retstring, pass, error); } /*Command: XML_AddDTD - Add DTD to xml document Input: [0]=document id [1] = dtd data Output: On error, DTD parser or validation error. Example: xml_AddDTD docid,dtddata */ void XML_AddDTD(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; static int dtdcounter = 0; char *result = NULL; if (dtdcounter) { if (nargs != 2) { *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } else { int docid = atoi(args[0]); CXMLDocument *tdoc = doclist.find(docid); if (!tdoc) result = istrdup(xmlerrors[XMLERR_BADDOCID]); else if (!tdoc->AddDTD(args[1], strlen(args[1]))) { result = (char *)malloc(1024); sprintf(result,"%s\n%s",xmlerrors[XMLERR_BADDTD],tdoc->GetError()); } } } *retstring = (result != NULL ? result : (char *)calloc(1,1)); } /*Command: XML_ValidateDTD - validate DTD against xml document Input: [0]=document id [1] = dtd data Output: empty if validated, or validation error. Example: if xml_ValidateDTD(docid,dtddata) is empty then put "validated" */ void XML_ValidateDTD(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; if (nargs != 2){ *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } else{ int docid = atoi(args[0]); CXMLDocument *tdoc = doclist.find(docid); if (!tdoc) result = istrdup(xmlerrors[XMLERR_BADDOCID]); else if (!tdoc->ValidateDTD(args[1], strlen(args[1]))) { result = (char *)malloc(1024); sprintf(result,"%s\n%s",xmlerrors[XMLERR_BADDTD],tdoc->GetError()); } } *retstring = (result != NULL ? result : (char *)calloc(1,1)); } /*Function: XML_NewDocumentFromFile - Parse XML file Input: [0]=xml data [1] = flag to determine whether to try parse xml data that is not well formed (ie. be forgiving) Output: XML document ID. On error, 'can't parse xml' and error message. Example: if xml_newdocument(data) is a number then beep */ void XML_NewDocumentFromFile(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; if (nargs < 2) { *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } if (!*error) { if (!SecurityCanAccessFile(args[0])) { *error = True; result = istrdup(xmlerrors[XMLERR_NOFILEPERMS]); } } if (!*error) { Bool wellformed = util_strnicmp(args[1],"TRUE",4) == 0; CXMLDocument *newdoc = new (nothrow) CXMLDocument; Bool buildtree = True; Bool sendmessages = False; if (nargs >= 3) buildtree = util_strnicmp(args[2],"TRUE",4) == 0; if (nargs >= 4) sendmessages = util_strnicmp(args[3],"TRUE",4) == 0; CXMLDocument::allowcallbacks = sendmessages; CXMLDocument::buildtree = buildtree; char *tfile = istrdup(args[0]); // OK-2008-01-08 : Bug 5702. Resolve ~ characters in the path char *t_resolved_path; t_resolved_path = os_path_resolve(tfile); // OK-2007-12-18 : Bug 4905. New path_to_native function declared which allocates a new path buffer. char *t_native_path; t_native_path = os_path_to_native(t_resolved_path); if (newdoc->ReadFile(t_native_path, wellformed)) { if (CXMLDocument::buildtree) { doclist.add(newdoc); unsigned int docid = newdoc->GetID(); result = (char *)malloc(INTSTRSIZE); sprintf(result,"%d",docid); } else { delete newdoc; result = (char *)istrdup("0"); } } else { result = (char *)malloc(1024); sprintf(result,"%s\n%s",xmlerrors[XMLERR_BADXML],newdoc->GetError()); delete newdoc; } free(tfile); free(t_native_path); delete[] t_resolved_path; /* Allocated with new[] */ } *retstring = (result != NULL ? result : (char *)calloc(1,1)); } //HS-2010-10-11: [[ Bug 7586 ]] Reinstate libxml2 to create name spaces. Implement new liveCode commands to suppress name space creation. // See the comments of Function XML_NewDocumentFromFile for detailed information of the operation void XML_NewDocumentFromFileNNS(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { XML_ProcessNameSpaces = False; XML_NewDocumentFromFile(args, nargs, retstring, pass, error); } //HS-2010-10-11: [[ Bug 7586 ]] Reinstate libxml2 to create name spaces. Implement new liveCode commands to suppress name space creation. // See the comments of Function XML_NewDocumentFromFile for detailed information of the operation void XML_NewDocumentFromFileNS(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { XML_ProcessNameSpaces = True; XML_NewDocumentFromFile(args, nargs, retstring, pass, error); } /* Function: XML_GetDocText - Dump contents of XML document or XML element Input: [0]=xml document id [1]=element path (if empty dumps contents of entire document) Output: XML formatted data Example: put XML_GetDocText(docid,"") into field "xmlcontents" */ void XML_GetDocText(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; if (nargs < 1){ *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } else{ int docid = atoi(args[0]); Bool isformatted = False; if (nargs ==3 && util_strnicmp(args[2],"TRUE",4) == 0) isformatted = True; CXMLDocument *tdoc = doclist.find(docid); if (!tdoc) result = istrdup(xmlerrors[XMLERR_BADDOCID]); else { int doclength; CXMLElement telement; if (nargs > 1 && *args[1]){ if (!tdoc->GetElementByPath(&telement,args[1])) result = istrdup(xmlerrors[XMLERR_BADELEMENT]); else telement.Write(&result,&doclength,isformatted); } else tdoc->Write(&result,&doclength,isformatted); } } *retstring = (result != NULL ? result : (char *)calloc(1,1)); } /* Command: XML_FreeDocument - Delete/Release XML Document Input: [0]=xml document id Output: empty or bad doc id error Example: XML_FreeDocument docid */ void XML_FreeDocument(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; if (nargs != 1){ *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } else{ int docid = atoi(*args); if (doclist.find(docid)) doclist.erase(docid); else result = istrdup(xmlerrors[XMLERR_BADDOCID]); } *retstring = (result != NULL ? result : (char *)calloc(1,1)); } /* Command: XML_BuildTree - set true to build xml tree Input: [0]=TRUE or FALSE Output: empty or bad doc id error Example: XML_BuildTree true */ /*void XML_BuildTree(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; if (nargs != 1){ *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } else CXMLDocument::buildtree = util_strnicmp(*args,"TRUE",4) == 0; *retstring = (result != NULL ? result : (char *)calloc(1,1)); }*/ /* Command: XML_AllowCallbacks - set true to recieve messages while parsing Input: [0]=TRUE or FALSE Output: empty or bad doc id error Example: XML_AllowCallbacks true */ /*void XML_AllowCallbacks(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; if (nargs != 1){ *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } else CXMLDocument::allowcallbacks = util_strnicmp(*args,"TRUE",4) == 0; *retstring = (result != NULL ? result : (char *)calloc(1,1));; }*/ /* Function: XML_Documents - Returns list of loaded xml documents Input: none Output: return delimited list of xml document id Example: put XML_Documents() into doclist */ void XML_Documents(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { char *result = NULL; *error = False; *pass = False; VXMLDocList *vdoclist = doclist.getList(); VXMLDocList::iterator theIterator; if (!vdoclist->empty()){ result = (char *)malloc(vdoclist->size() * INTSTRSIZE); result[0] = '\0'; int numdocs = 0; for (theIterator = vdoclist->begin(); theIterator != vdoclist->end(); theIterator++){ CXMLDocument *tdoc = (CXMLDocument *)(*theIterator); char idbuffer[INTSTRSIZE]; sprintf(idbuffer,"%d",tdoc->GetID()); strcat(result,idbuffer); if (++numdocs != vdoclist->size()) strcat(result,"\n"); } } *retstring = (result != NULL ? result : (char *)calloc(1,1)); } /* Command: XML_FreeAll - unload/release all xml documents Input: none Output: none Example: XML_FreeAll */ void XML_FreeAll(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { char *result = NULL; *error = False; *pass = False; doclist.clear(); *retstring = (result != NULL ? result : (char *)calloc(1,1)); } //-----------------------ELEMENT CALLBACKS----------------------------------------- /* Command: XML_AddElement - Add a child element and sets its value Input: [0]=xml document id [1]=parent element path [2]=new child element name [3]=new child element value [4]=[ 'before' or 'after' ] Output: full path of new child element or bad doc id error Example: XML_AddElement docid,elementpath,"newelement",newvalue */ void XML_AddElement(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; bool before; if (nargs == 5) { nargs = 4; if (strcasecmp(args[4], "before") == 0) before = true; else if (strcasecmp(args[4], "after") == 0) before = false; else nargs = 5; } else before = false; if (nargs != 4){ *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } else{ int docid = atoi(args[0]); CXMLDocument *tdoc = doclist.find(docid); CXMLElement telement; if (!tdoc) result = istrdup(xmlerrors[XMLERR_BADDOCID]); else { if (!tdoc->GetElementByPath(&telement,args[1])) result = istrdup(xmlerrors[XMLERR_BADELEMENT]); else { CXMLElement newelement; if (!telement.AddChild(args[2],args[3],&newelement, before)) result = istrdup(xmlerrors[XMLERR_BADELEMENT]); else result = newelement.GetPath(); } } } *retstring = (result != NULL ? result : (char *)calloc(1,1)); } /* Command: XML_InsertElement - Insert an element before the specified element as a sibling Input: [0]=xml document id [1]=next sibling element [2]=new element name [3]=new element value Output: full path of new sibling element or bad doc id error Example: XML_InsertElement docid,elementpath,"newelement",newvalue */ void XML_InsertElement(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; bool before; if (nargs == 5) { nargs = 4; if (strcasecmp(args[4], "before") == 0) before = true; else if (strcasecmp(args[4], "after") == 0) before = false; else nargs = 5; } else before = false; if (nargs != 4){ *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } else{ int docid = atoi(args[0]); CXMLDocument *tdoc = doclist.find(docid); CXMLElement telement; if (!tdoc) result = istrdup(xmlerrors[XMLERR_BADDOCID]); else { if (!tdoc->GetElementByPath(&telement,args[1])) result = istrdup(xmlerrors[XMLERR_BADELEMENT]); else { CXMLElement newelement; if (!telement.AddSibling(args[2],args[3],&newelement, before)) result = istrdup(xmlerrors[XMLERR_BADELEMENT]); else result = newelement.GetPath(); } } } *retstring = (result != NULL ? result : (char *)calloc(1,1)); } char *XML_ManipulateElement(int p_source_tree, char *p_source_node, int p_destination_tree, char *p_destination_node, bool p_copy, bool p_before, bool p_sibling) { // Locate the source and destination documents in the list of open xml docs. CXMLDocument *t_source_doc; t_source_doc = doclist . find(p_source_tree); if (t_source_doc == NULL) return istrdup(xmlerrors[XMLERR_BADDOCID]); CXMLDocument *t_destination_doc; t_destination_doc = doclist . find(p_destination_tree); if (t_destination_doc == NULL) return istrdup(xmlerrors[XMLERR_BADDOCID]); // Locate the source and destination elements in their respective documents CXMLElement t_source_element; if (!t_source_doc -> GetElementByPath(&t_source_element, p_source_node)) return istrdup(xmlerrors[XMLERR_BADELEMENT]); CXMLElement t_destination_element; if (!t_destination_doc -> GetElementByPath(&t_destination_element, p_destination_node)) return istrdup(xmlerrors[XMLERR_BADELEMENT]); // If we are copying, then we first duplicate the node before moving it if (p_copy) t_source_element . CopyElement(&t_source_element, true); if (p_source_tree == p_destination_tree) { // Now move the source element to the new location, returning an error if fails if (!t_destination_element . MoveElement(&t_source_element, p_sibling, p_before)) return istrdup(xmlerrors[XMLERR_BADCOPY]); } else { if (!t_destination_element . MoveRemoteElement(&t_source_element, p_sibling, p_before)) return istrdup(xmlerrors[XMLERR_BADCOPY]); } // If succeeded, we return the new path of the moved element. return t_source_element . GetPath(); } // [0] Doc ID // [1] Source Element // [2] Target Element // [3] before/after [after] // [4] sibling/child [child] // void XML_MoveElement(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; bool before; if (nargs >= 4) { nargs -= 1; if (strcasecmp(args[3], "before") == 0) before = true; else if (strcasecmp(args[3], "after") == 0) before = false; else nargs = 6; } else before = false; bool sibling; if (nargs >= 4) { nargs -= 1; if (strcasecmp(args[4], "sibling") == 0) sibling = true; else if (strcasecmp(args[4], "child") == 0) sibling = false; else nargs = 6; } else sibling = false; if (nargs != 3) { *error = True; result = istrdup(xmlerrors[XMLERR_BADARGUMENTS]); } else { int t_doc_id; t_doc_id = atoi(args[0]); result = XML_ManipulateElement(t_doc_id, args[1], t_doc_id, args[2], false, before, sibling); } *retstring = (result != NULL ? result : (char *)calloc(1,1)); } // [0] Doc ID // [1] Source Element // [2] Target Element // [3] before/after [after] // [4] sibling/child [child] // void XML_CopyElement(char *args[], int nargs, char **retstring, Bool *pass, Bool *error) { *pass = False; *error = False; char *result = NULL; bool before; if (nargs >= 4) { nargs -= 1; if (strcasecmp(args[3], "before") == 0) before = true; else if (strcasecmp(args[3], "after") == 0) before = false; else nargs = 6; } else before = false; |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |