1 /* @include ajdom ************************************************************* 2 ** 3 ** AJAX Document Object Model (DOM) functions 4 ** 5 ** @author Copyright (C) 2006 Alan Bleasby 6 ** @version $Revision: 1.17 $ 7 ** @modified Jul 03 2006 ajb First version 8 ** @modified $Date: 2012/09/03 14:24:14 $ by $Author: rice $ 9 ** @@ 10 ** 11 ** This library is free software; you can redistribute it and/or 12 ** modify it under the terms of the GNU Lesser General Public 13 ** License as published by the Free Software Foundation; either 14 ** version 2.1 of the License, or (at your option) any later version. 15 ** 16 ** This library is distributed in the hope that it will be useful, 17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 ** Lesser General Public License for more details. 20 ** 21 ** You should have received a copy of the GNU Lesser General Public 22 ** License along with this library; if not, write to the Free Software 23 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 24 ** MA 02110-1301, USA. 25 ** 26 ******************************************************************************/ 27 28 #ifndef AJDOM_H 29 #define AJDOM_H 30 31 /* ========================================================================= */ 32 /* ============================= include files ============================= */ 33 /* ========================================================================= */ 34 35 #include "ajdefine.h" 36 #include "ajstr.h" 37 #include "ajlist.h" 38 #include "ajtable.h" 39 #include "ajfile.h" 40 41 AJ_BEGIN_DECLS 42 43 44 45 46 /* ========================================================================= */ 47 /* =============================== constants =============================== */ 48 /* ========================================================================= */ 49 50 51 52 53 /* @enum AjEDomNodeType ******************************************************* 54 ** 55 ** AJAX DOM Node Type enumeration 56 ** 57 ** @value ajEDomNodeTypeNULL NULL 58 ** @value ajEDomNodeTypeElement Element 59 ** @value ajEDomNodeTypeAttribute Attribute 60 ** @value ajEDomNodeTypeText Text 61 ** @value ajEDomNodeTypeCdataSection CDATA Section 62 ** @value ajEDomNodeTypeEntityReference Entity Reference 63 ** @value ajEDomNodeTypeEntityNode Entity 64 ** @value ajEDomNodeTypeProcessingInstruction Processing Instruction 65 ** @value ajEDomNodeTypeComment Comment 66 ** @value ajEDomNodeTypeDocumentNode Document 67 ** @value ajEDomNodeTypeDocumentType Document Type 68 ** @value ajEDomNodeTypeDocumentFragment Document Fragment 69 ** @value ajEDomNodeTypeNotation Notation 70 ** @@ 71 ******************************************************************************/ 72 73 typedef enum AjODomNodeType 74 { 75 ajEDomNodeTypeNULL, 76 ajEDomNodeTypeElement, 77 ajEDomNodeTypeAttribute, 78 ajEDomNodeTypeText, 79 ajEDomNodeTypeCdataSection, 80 ajEDomNodeTypeEntityReference, 81 ajEDomNodeTypeEntityNode, 82 ajEDomNodeTypeProcessingInstruction, 83 ajEDomNodeTypeComment, 84 ajEDomNodeTypeDocumentNode, 85 ajEDomNodeTypeDocumentType, 86 ajEDomNodeTypeDocumentFragment, 87 ajEDomNodeTypeNotation 88 } AjEDomNodeType; 89 90 91 92 93 #define AJDOMDESTROY 1 94 #define AJDOMKEEP 0 95 96 97 98 99 /* ========================================================================= */ 100 /* ============================== public data ============================== */ 101 /* ========================================================================= */ 102 103 104 105 106 /* @data AjSDomNode *********************************************************** 107 ** 108 ** DOM parsing node 109 ** 110 ** @alias AjODomNode 111 ** @alias AjPDomNode 112 ** @alias AjPDomDocument 113 ** @alias AjPDomDocumentType 114 ** @alias AjPDomElement 115 ** @alias AjPDomText 116 ** @alias AjPDomCharacterData 117 ** @alias AjPDomDocumentFragment 118 ** @alias AjPDomCDATASection 119 ** @alias AjPDomComment 120 ** @alias AjPDomAttr 121 ** @alias AjPDomEntityReference 122 ** @alias AjPDomEntity 123 ** @alias AjPDomPi 124 ** @alias AjPDomNotation 125 ** 126 ** @attr name [AjPStr] name of node 127 ** @attr value [AjPStr] value associated with name 128 ** @attr parentnode [struct AjSDomNode*] ancestor 129 ** @attr childnodes [struct AjSDomNodeList*] children 130 ** @attr firstchild [struct AjSDomNode*] first child in list 131 ** @attr lastchild [struct AjSDomNode*] last child in list 132 ** @attr previoussibling [struct AjSDomNode*] last node at this level 133 ** @attr nextsibling [struct AjSDomNode*] next node at this level 134 ** @attr attributes [struct AjSDomNodeList*] attributes 135 ** @attr ownerdocument [struct AjSDomNode*] document owner 136 ** @attr sub [union] substructure depending on node type 137 ** @attr type [AjEDomNodeType] AJAX DOM Node enumeration 138 ** @attr subtreeModified [ajuint] internal flag 139 ** 140 ******************************************************************************/ 141 142 typedef struct AjSDomNode 143 { 144 AjPStr name; 145 AjPStr value; 146 147 struct AjSDomNode *parentnode; 148 struct AjSDomNodeList *childnodes; 149 struct AjSDomNode *firstchild; 150 struct AjSDomNode *lastchild; 151 struct AjSDomNode *previoussibling; 152 struct AjSDomNode *nextsibling; 153 154 struct AjSDomNodeList *attributes; 155 156 struct AjSDomNode *ownerdocument; 157 158 union 159 { 160 struct 161 { 162 struct AjSDomNode *doctype; 163 struct AjSDomNode *documentelement; 164 struct AjSDomNode *document; 165 struct AjSDomNode *defaultview; 166 struct AjSDomNode *commonparent; 167 AjPStr version; 168 AjPStr encoding; 169 ajint standalone; 170 char Padding[4]; 171 } Document; 172 173 struct 174 { 175 AjPStr name; 176 struct AjSDomNodeList *entities; 177 struct AjSDomNodeList *notations; 178 AjPStr publicid; 179 AjPStr systemid; 180 AjPStr internalsubset; 181 } DocumentType; 182 183 struct 184 { 185 AjPStr tagname; 186 } Element; 187 188 struct 189 { 190 AjPStr name; 191 AjPStr value; 192 struct AjSDomNode *ownerelement; 193 ajint specified; 194 char Padding[4]; 195 } Attr; 196 197 struct 198 { 199 AjPStr data; 200 ajint length; 201 char Padding[4]; 202 } CharacterData; 203 204 struct 205 { 206 AjPStr publicid; 207 AjPStr systemid; 208 } Notation; 209 210 struct 211 { 212 AjPStr publicid; 213 AjPStr systemid; 214 AjPStr notationname; 215 } Entity; 216 217 struct 218 { 219 AjPStr target; 220 AjPStr data; 221 } ProcessingInstruction; 222 } sub; 223 224 AjEDomNodeType type; 225 ajuint subtreeModified; 226 227 } AjODomNode; 228 229 #define AjPDomNode AjODomNode* 230 231 232 233 234 /* @data AjSDomNodeEntry ****************************************************** 235 ** 236 ** DOM parsing node entry 237 ** 238 ** @alias AjODomNodeEntry 239 ** @alias AjPDomNodeEntry 240 ** 241 ** @attr prev [struct AjSDomNodeEntry*] previous node 242 ** @attr next [struct AjSDomNodeEntry*] next node 243 ** @attr node [AjPDomNode] linked list node 244 ** 245 ******************************************************************************/ 246 247 typedef struct AjSDomNodeEntry 248 { 249 struct AjSDomNodeEntry *prev; 250 struct AjSDomNodeEntry *next; 251 AjPDomNode node; 252 } AjODomNodeEntry; 253 254 #define AjPDomNodeEntry AjODomNodeEntry* 255 256 257 258 259 /* @data AjSDomNodeList ******************************************************* 260 ** 261 ** DOM parsing node list 262 ** 263 ** @alias AjODomNodeList 264 ** @alias AjPDomNodeList 265 ** @alias AjPDomNodeMap 266 ** 267 ** @attr ownerdocument [AjPDomNode] document owner 268 ** @attr ownerelement [AjPDomNode] element owner 269 ** @attr first [AjPDomNodeEntry] first in list 270 ** @attr last [AjPDomNodeEntry] last in list 271 ** @attr length [ajint] length 272 ** @attr filter [ajuint] filter 273 ** @attr list [struct AjSDomNodeList*] list 274 ** @attr table [AjPTable] fast lookup table 275 ** 276 ******************************************************************************/ 277 278 typedef struct AjSDomNodeList 279 { 280 AjPDomNode ownerdocument; 281 AjPDomNode ownerelement; 282 AjPDomNodeEntry first; 283 AjPDomNodeEntry last; 284 ajint length; 285 ajuint filter; 286 struct AjSDomNodeList *list; 287 AjPTable table; 288 } AjODomNodeList; 289 290 #define AjPDomNodeList AjODomNodeList* 291 292 293 294 295 /* @data AjSDomUserdata ******************************************************* 296 ** 297 ** DOM Userdata used when reading XML 298 ** 299 ** @alias AjODomUserdata 300 ** @alias AjPDomUserdata 301 ** 302 ** @attr Buffer [AjPStr] Utility string 303 ** @attr Stack [AjPList] Stack 304 ** @attr Cdata [AjBool] Cdata 305 ** @attr Padding [AjBool] Pad to alignment boundary 306 ** 307 ******************************************************************************/ 308 309 typedef struct AjSDomUserdata 310 { 311 AjPStr Buffer; 312 AjPList Stack; 313 AjBool Cdata; 314 AjBool Padding; 315 } AjODomUserdata; 316 317 #define AjPDomUserdata AjODomUserdata* 318 319 320 321 322 #define AjPDomDocument AjPDomNode 323 #define AjPDomNodeMap AjPDomNodeList 324 #define AjPDomDocumentType AjPDomNode 325 #define AjPDomElement AjPDomNode 326 #define AjPDomText AjPDomNode 327 #define AjPDomCharacterData AjPDomNode 328 #define AjPDomDocumentFragment AjPDomNode 329 #define AjPDomCDATASection AjPDomNode 330 #define AjPDomComment AjPDomNode 331 #define AjPDomAttr AjPDomNode 332 #define AjPDomEntityReference AjPDomNode 333 #define AjPDomEntity AjPDomNode 334 #define AjPDomPi AjPDomNode 335 #define AjPDomNotation AjPDomNode 336 337 338 339 340 /* ========================================================================= */ 341 /* =========================== public functions ============================ */ 342 /* ========================================================================= */ 343 344 345 346 347 /* 348 ** Prototype definitions 349 */ 350 351 AjPDomNode ajDomDocumentCreateNode(AjPDomDocument doc, 352 AjEDomNodeType nodetype); 353 354 AjPDomDocumentType ajDomImplementationCreateDocumentType(const AjPStr qualname, 355 const AjPStr publicid, 356 const AjPStr systemid); 357 AjPDomDocumentType ajDomImplementationCreateDocumentTypeC(const char *qualname, 358 const char *publicid, 359 const char *systemid); 360 361 AjPDomDocument ajDomImplementationCreateDocument(const AjPStr uri, 362 const AjPStr qualname, 363 AjPDomDocumentType doctype); 364 AjPDomDocument ajDomImplementationCreateDocumentC(const char *uri, 365 const char *qualname, 366 AjPDomDocumentType doctype); 367 368 ajint ajDomNodeListGetLen(const AjPDomNodeList list); 369 370 AjPDomNodeEntry ajDomNodeListAppend(AjPDomNodeList list, 371 AjPDomNode child); 372 373 AjPStr ajDomElementGetAttribute(const AjPDomElement element, 374 const AjPStr name); 375 AjPStr ajDomElementGetAttributeC(const AjPDomElement element, 376 const char *name); 377 378 void ajDomElementSetAttribute(const AjPDomElement element, 379 const AjPStr name, 380 const AjPStr value); 381 void ajDomElementSetAttributeC(const AjPDomElement element, 382 const char *name, 383 const char *value); 384 void ajDomElementRemoveAttribute(AjPDomElement element, 385 const AjPStr name); 386 void ajDomElementRemoveAttributeC(AjPDomElement element, 387 const char *name); 388 389 AjPDomNode ajDomElementRemoveAttributeNode(AjPDomElement element, 390 AjPDomNode oldattr); 391 AjPDomNode ajDomElementGetAttributeNode(const AjPDomElement element, 392 const AjPStr name); 393 AjPDomNode ajDomElementGetAttributeNodeC(const AjPDomElement element, 394 const char *name); 395 396 AjPDomNode ajDomElementSetAttributeNode(AjPDomElement element, 397 AjPDomNode newattr); 398 AjPDomNodeList ajDomElementGetElementsByTagName(AjPDomElement element, 399 const AjPStr name); 400 AjPDomNodeList ajDomElementGetElementsByTagNameC(AjPDomElement element, 401 const char *name); 402 403 void ajDomElementNormalise(AjPDomElement element); 404 AjPStr ajDomCharacterDataSubstringData(const AjPDomCharacterData data, 405 ajint offset, ajint count); 406 void ajDomCharacterDataAppendData(AjPDomCharacterData data, 407 const AjPStr arg); 408 void ajDomCharacterDataAppendDataC(AjPDomCharacterData data, 409 const char *arg); 410 void ajDomCharacterDataInsertData(AjPDomCharacterData data, 411 ajint offset, const AjPStr arg); 412 void ajDomCharacterDataInsertDataC(AjPDomCharacterData data, 413 ajint offset, const char *arg); 414 415 void ajDomCharacterDataDeleteData(AjPDomCharacterData data, 416 ajint offset, ajint count); 417 void ajDomCharacterDataReplaceData(AjPDomCharacterData data, 418 ajint offset, ajint count, 419 const AjPStr arg); 420 void ajDomCharacterDataReplaceDataC(AjPDomCharacterData data, 421 ajint offset, ajint count, 422 const char *arg); 423 424 ajint ajDomCharacterDataGetLength(const AjPDomCharacterData data); 425 AjPDomText ajDomTextSplitText(AjPDomText text, ajint offset); 426 AjPDomElement ajDomDocumentCreateElement(AjPDomDocument doc, 427 const AjPStr tagname); 428 AjPDomElement ajDomDocumentCreateElementC(AjPDomDocument doc, 429 const char *tagname); 430 431 AjPDomDocumentFragment ajDomDocumentCreateDocumentFragment(AjPDomDocument doc); 432 433 AjPDomText ajDomDocumentCreateTextNode(AjPDomDocument doc, 434 const AjPStr data); 435 AjPDomText ajDomDocumentCreateTextNodeC(AjPDomDocument doc, 436 const char *data); 437 438 AjPDomComment ajDomDocumentCreateComment(AjPDomDocument doc, 439 const AjPStr data); 440 AjPDomComment ajDomDocumentCreateCommentC(AjPDomDocument doc, 441 const char *data); 442 443 AjPDomCDATASection ajDomDocumentCreateCDATASection(AjPDomDocument doc, 444 const AjPStr data); 445 AjPDomCDATASection ajDomDocumentCreateCDATASectionC(AjPDomDocument doc, 446 const char *data); 447 448 AjPDomAttr ajDomDocumentCreateAttribute(AjPDomDocument doc, 449 const AjPStr name); 450 AjPDomAttr ajDomDocumentCreateAttributeC(AjPDomDocument doc, 451 const char *name); 452 453 AjPDomEntityReference ajDomDocumentCreateEntityReference(AjPDomDocument doc, 454 const AjPStr name); 455 AjPDomEntityReference ajDomDocumentCreateEntityReferenceC(AjPDomDocument doc, 456 const char *name); 457 458 AjPDomPi ajDomDocumentCreateProcessingInstruction(AjPDomDocument doc, 459 const AjPStr target, 460 const AjPStr data); 461 AjPDomPi ajDomDocumentCreateProcessingInstructionC(AjPDomDocument doc, 462 const char *target, 463 const char *data); 464 465 466 467 AjPDomNodeList ajDomDocumentGetElementsByTagName(AjPDomDocument doc, 468 const AjPStr name); 469 AjPDomNodeList ajDomDocumentGetElementsByTagNameC(AjPDomDocument doc, 470 const char *name); 471 472 AjPDomDocumentType ajDomDocumentGetDoctype(const AjPDomDocument doc); 473 AjPDomElement ajDomDocumentGetDocumentElement(const AjPDomDocument doc); 474 475 AjPDomNode ajDomNodeAppendChild(AjPDomNode node, 476 AjPDomNode extrachild); 477 AjPDomNode ajDomNodeInsertBefore(AjPDomNode node, AjPDomNode newchild, 478 AjPDomNode refchild); 479 AjPDomNode ajDomRemoveChild(AjPDomNode node, AjPDomNode child); 480 void ajDomDocumentDestroyNode(AjPDomDocument doc, 481 AjPDomNode *Pnode); 482 void ajDomDocumentDestroyNodeList(AjPDomDocument doc, 483 AjPDomNodeList *Plist, 484 AjBool donodes); 485 AjPDomNode ajDomNodeReplaceChild(AjPDomNode node, AjPDomNode newchild, 486 AjPDomNode oldchild); 487 AjPDomNode ajDomNodeCloneNode(AjPDomNode node, AjBool deep); 488 AjPDomNode ajDomNodeListItem(const AjPDomNodeList list, ajint indexnum); 489 AjBool ajDomNodeListExists(AjPDomNodeList list, 490 const AjPDomNode child); 491 AjPDomNodeEntry ajDomNodeListRemove(AjPDomNodeList list, AjPDomNode child); 492 AjPDomNodeList ajDomCreateNodeList(AjPDomDocument doc); 493 494 AjPDomNode ajDomNodeMapGetItem(const AjPDomNodeMap map, 495 const AjPStr name); 496 AjPDomNode ajDomNodeMapGetItemC(const AjPDomNodeMap map, 497 const char *name); 498 AjPDomNode ajDomNodeMapSetItem(AjPDomNodeMap map, AjPDomNode arg); 499 500 AjPDomNode ajDomNodeMapRemoveItem(AjPDomNodeMap map, 501 const AjPStr name); 502 AjPDomNode ajDomNodeMapRemoveItemC(AjPDomNodeMap map, 503 const char *name); 504 AjPDomNode ajDomNodeMapItem(const AjPDomNodeMap map, ajint indexnum); 505 506 AjPDomNodeEntry ajDomNodeListInsert(AjPDomNodeList list, AjPDomNode newchild, 507 AjPDomNode refchild); 508 AjPDomNodeEntry ajDomNodeListReplace(AjPDomNodeList list, AjPDomNode newchild, 509 AjPDomNode oldchild); 510 511 AjBool ajDomNodeHasChildNodes(const AjPDomNode node); 512 513 514 ajint ajDomWrite(const AjPDomDocument node, AjPFile outf); 515 ajint ajDomWriteIndent(const AjPDomDocument node, AjPFile outf, 516 ajint indent); 517 518 519 520 void ajDomPrintNode(const AjPDomNode node, ajint indent); 521 void ajDomPrintNode2(const AjPDomNode node); 522 void ajDomNodePrintNode(const AjPDomNode node); 523 524 525 ajint ajDomReadFp(AjPDomDocument node, FILE *stream); 526 ajint ajDomReadFilebuff(AjPDomDocument node, AjPFilebuff buff); 527 ajint ajDomReadFilebuffText(AjPDomDocument node, AjPFilebuff buff, 528 AjBool dotext, AjPStr *Pstr); 529 ajint ajDomReadString(AjPDomDocument node, AjPStr str); 530 531 AjPStr ajDomTextGetText(AjPDomText text); 532 AjPStr ajDomElementGetText(AjPDomElement element); 533 534 AjPDomElement ajDomElementGetNthChildByTagNameC(AjPDomDocument doc, 535 AjPDomElement element, 536 const char *name, 537 ajint n); 538 539 AjPDomElement ajDomElementGetFirstChildByTagNameC(AjPDomDocument doc, 540 AjPDomElement element, 541 const char *name); 542 543 /* 544 ** End of prototype definitions 545 */ 546 547 548 549 550 AJ_END_DECLS 551 552 #endif /* !AJDOM_H */ 553