Source

perl-XML-LibXML / dom.c

  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
#include <libxml/tree.h>
#include <libxml/encoding.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xmlIO.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

xmlDocPtr
domCreateDocument( xmlChar *version, xmlChar *enc ){
    xmlDocPtr doc = NULL;
    doc = xmlNewDoc( version );  
    doc->charset  = XML_CHAR_ENCODING_UTF8;
    if ( enc != NULL && *enc!= 0 ) {
      /* if an encoding is passed, we will assume UTF8, otherwise we set 
       * the passed encoding 
       */
        doc->encoding = xmlStrdup(enc);
    }

    return doc;
}

/* this function is pretty neat, since you can read in well balanced 
 * strings and get a list of nodes, which can be added to any other node.
 *
 * the code is pretty heavy i think, but deep in my heard i believe it's 
 * worth it :) (e.g. if you like to read a chunk of well-balanced code 
 * from a databasefield)
 *
 * in 99% i believe it is faster to create the dom by hand, and skip the 
 * parsing job which has to be done here.
 */
xmlNodePtr 
domReadWellBalancedString( xmlDocPtr doc, xmlChar* block ) {
  int retCode       = -1;
  xmlNodePtr helper = NULL;
  xmlNodePtr nodes  = NULL;
  
  if ( doc && block ) {
    /* read and encode the chunk */
    retCode = xmlParseBalancedChunkMemory( doc, 
                                           NULL,
                                           NULL,
                                           0,
                                           block,
                                           &nodes );
    
    /* error handling */
    if ( retCode != 0 ) {
      /* if the code was not well balanced, we will not return 
       * a bad node list, but we have to free the nodes */
      while( nodes != NULL ) {
        helper = nodes->next;
        xmlFreeNode( nodes );
        nodes = helper;
      }
    }
  }

  return nodes;
}

xmlNodePtr
domUnbindNode( xmlNodePtr );

xmlNodePtr
domIsNotParentOf( xmlNodePtr node1, xmlNodePtr node2 );

xmlNodePtr
domImportNode( xmlDocPtr doc, xmlNodePtr node, int move );

xmlNodePtr
domRemoveChild( xmlNodePtr self, xmlNodePtr old );

xmlNodePtr
domAppendChild( xmlNodePtr self,
		xmlNodePtr newChild ){
  /* unbind the new node if nessecary ...  */

  newChild = domIsNotParentOf( newChild, self );
  if ( newChild == NULL ){
    return NULL;
  }
  if ( self == NULL ) {
    return newChild;
  }


  if ( newChild->doc == self->doc ){
    newChild= domUnbindNode( newChild );
  }
  else {
    newChild= domImportNode( self->doc, newChild, 1 );
  }

  /* fix the document if they are from different documents 
   * actually this has to be done for ALL nodes in the subtree... 
   **/
  if ( self->doc != newChild->doc ) {
    newChild->doc = self->doc;
  }
  
  if ( self->children != NULL ) {
    if ( newChild->type   == XML_TEXT_NODE && 
	 self->last->type == XML_TEXT_NODE ) {
      int len = xmlStrlen(newChild->content);
      xmlNodeAddContentLen(self->last, newChild->content, len);
      xmlFreeNode( newChild );
      return self->last;
    }
    else {
      self->last->next = newChild;
      newChild->prev = self->last;
      self->last = newChild;
      newChild->parent= self;
    }
  }
  else {
    self->children = newChild;
    self->last     = newChild;
    newChild->parent= self;
  }
  return newChild;
}

xmlNodePtr
domReplaceChild( xmlNodePtr self, xmlNodePtr new, xmlNodePtr old ) {
  new = domIsNotParentOf( new, self );
  if ( new == NULL ) {
    /* level2 sais nothing about this case :( */
    return domRemoveChild( self, old );
  }

  if ( self== NULL ){
    return NULL;
  }

  if ( new == old ) {
    /* dom level 2 throw no exception if new and old are equal */ 
    return new;
  }

  if ( old == NULL ) {
    domAppendChild( self, new );
    return old;
  }
  if ( old->parent != self ) {
    /* should not do this!!! */
    return new;
  }

  if ( new->doc == self->doc ) {
    new = domUnbindNode( new ) ;
  }
  else {
    new = domImportNode( self->doc, new, 1 );
  }
  new->parent = self;
  
  /* this piece is quite important */
  if ( new->doc != self->doc ) {
    new->doc = self->doc;
  }

  if ( old->next != NULL ) 
    old->next->prev = new;
  if ( old->prev != NULL ) 
    old->prev->next = new;
  
  new->next = old->next;
  new->prev = old->prev;
  
  if ( old == self->children )
    self->children = new;
  if ( old == self->last )
    self->last = new;
  
  old->parent = NULL;
  old->next   = NULL;
  old->prev   = NULL;
 
  return old;
}

xmlNodePtr
domRemoveChild( xmlNodePtr self, xmlNodePtr old ) {
  if ( (self != NULL)  && (old!=NULL) && (self == old->parent ) ) {
    domUnbindNode( old );
  }
  return old ;
}

xmlNodePtr
domInsertBefore( xmlNodePtr self, 
                 xmlNodePtr newChild,
                 xmlNodePtr refChild ){

  if ( self == NULL ) {
    return NULL;
  }

  newChild = domIsNotParentOf( newChild, self );

  if ( refChild == newChild ) {
    return newChild;
  }

  if ( refChild == NULL && newChild != NULL ) {
    /* insert newChild as first Child */
    if ( self->children == NULL ) {
      return domAppendChild( self, newChild );
    }
    
    if ( self->doc == newChild->doc ){
      newChild = domUnbindNode( newChild );
    }
    else {
      newChild = domImportNode( self->doc, newChild, 1 );
    }

    newChild->next = self->children;
    self->children->prev = newChild;
   
    self->children = newChild;

    return newChild;
  }

  if ( newChild != NULL && 
       self == refChild->parent ) {
    /* find the refchild, to avoid spoofed parents */
    xmlNodePtr hn = self->children;
    while ( hn ) {
      if ( hn == refChild ) {
        /* found refChild */
        if ( self->doc == newChild->doc ){
          newChild = domUnbindNode( newChild );
        }
        else {
          newChild = domImportNode( self->doc, newChild, 1 );
        }

        newChild->parent = self;
        newChild->next = refChild;
        newChild->prev = refChild->prev;
        if ( refChild->prev != NULL ) {
          refChild->prev->next = newChild;
        }
        refChild->prev = newChild;
        if ( refChild == self->children ) {
          self->children = newChild;
        }
        return newChild;
      }
      hn = hn->next;
    }
  }
  return NULL;
}

xmlNodePtr
domInsertAfter( xmlNodePtr self, 
                xmlNodePtr newChild,
                xmlNodePtr refChild ){
  if ( self == NULL ) {
    return NULL;
  }

  newChild = domIsNotParentOf( newChild, self );

  if ( refChild == newChild ) {
    return newChild;
  }

  if ( refChild == NULL ) {
    return domAppendChild( self, newChild );
  }

  if ( newChild != NULL && 
       self == refChild->parent &&
       refChild != newChild ) {
    /* find the refchild, to avoid spoofed parents */
    xmlNodePtr hn = self->children;
    while ( hn ) {
      if ( hn == refChild ) {
        /* found refChild */
        if ( self->doc == newChild->doc ) {
          newChild = domUnbindNode( newChild );
        }
        else {
          newChild = domImportNode( self->doc , newChild, 1 );
        }

        newChild->parent = self;
        newChild->prev = refChild;
        newChild->next = refChild->next;
        if ( refChild->next != NULL ) {
          refChild->next->prev = newChild;
        }
        refChild->next = newChild;

        if ( refChild == self->last ) {
          self->last = newChild;
        }
        return newChild;
      }
      hn = hn->next;
    }
  }
  return NULL;
}

void
domSetNodeValue( xmlNodePtr n , xmlChar* val ){
  if ( n == NULL ) 
    return;
  if( n->content != NULL ) {
    xmlFree( n->content );
  }
  n->content = xmlStrdup( val );
}


void
domSetParentNode( xmlNodePtr self, xmlNodePtr p ) {
  /* never set the parent to a node in the own subtree */ 
  self = domIsNotParentOf( self, p );
  if( self != NULL ){
    if( self->parent != p ){
      domUnbindNode( self );
      self->parent = p;
      if( p->doc != self->doc ) {
	self->doc = p->doc;
      }
    }
  }
}

xmlNodePtr
domUnbindNode( xmlNodePtr self ) {
  if ( (self != NULL) && (self->parent != NULL) ) { 
    if ( self->next != NULL )
      self->next->prev = self->prev;
    if ( self->prev != NULL )
      self->prev->next = self->next;
    if ( self == self->parent->last ) 
      self->parent->last = self->prev;
    if ( self == self->parent->children ) 
      self->parent->children = self->next;

    self->parent = NULL;
    self->next   = NULL;
    self->prev   = NULL;
  }
  return self;
}

/**
 * donIsNotParentOf tests, if node1 is parent of node2. this test is very
 * important to avoid circular constructs in trees. if node1 is NOT parent
 * of node2 the function returns node1, otherwise NULL.
 **/
xmlNodePtr
domIsNotParentOf( xmlNodePtr node1, xmlNodePtr node2 ) {
  xmlNodePtr helper = NULL;

  if ( node1 == NULL ) {
    return NULL;
  }

  if( node2 == NULL || node1->doc != node2->doc) {
    return node1;
  }
  
  helper= node2;
  while ( helper!=NULL ) {
    if( helper == node1 ) {
      return NULL;
    }
    
    helper = helper->parent;
    if ( (xmlDocPtr) helper == node2->doc ) {
      helper = NULL;
    }
  }

  return node1;
}

/**
 * this is a wrapper function that does the type evaluation for the 
 * node. this makes the code a little more readable in the .XS
 * 
 * the code is not really portable, but i think we'll avoid some 
 * memory leak problems that way.
 **/

const char*
domNodeTypeName( xmlNodePtr elem ){
  const char *name = "XML::LibXML::Node";

  if ( elem != NULL ) {
    char * ptrHlp;
    switch ( elem->type ) {
    case XML_ELEMENT_NODE:
      name = "XML::LibXML::Element";   
      break;
    case XML_TEXT_NODE:
      name = "XML::LibXML::Text";
      break;
    case XML_COMMENT_NODE:
      name = "XML::LibXML::Comment";
      break;
    case XML_CDATA_SECTION_NODE:
      name = "XML::LibXML::CDATASection";
      break;
    default:
      name = "XML::LibXML::Node";
      break;
    };
    return name;
  }

  return "";
}

xmlNodePtr
domCreateCDATASection( xmlDocPtr self , xmlChar * strNodeContent ){
  xmlNodePtr elem = NULL;

  if ( ( self != NULL ) && ( strNodeContent != NULL ) ) {
    elem = xmlNewCDataBlock( self, strNodeContent, xmlStrlen(strNodeContent) );
    elem->next = NULL;
    elem->prev = NULL;
    elem->children = NULL ;
    elem->last = NULL;
    elem->doc = self->doc;   
  }

  return elem;
}


xmlNodePtr 
domDocumentElement( xmlDocPtr doc ) {
  xmlNodePtr cld=NULL;
  if ( doc != NULL && doc->doc != NULL && doc->doc->children != NULL ) {
    cld= doc->doc->children;
    while ( cld != NULL && cld->type != XML_ELEMENT_NODE ) 
      cld= cld->next;
  
  }
  return cld;
}

/**
 * setDocumentElement:
 * @doc: the document
 * @newRoot: the new rootnode
 *
 * a document can have only ONE root node, so this function searches
 * the first element and relaces this element with newRoot.
 * 
 * Returns the old root node.
 **/
xmlNodePtr
domSetDocumentElement( xmlDocPtr doc, xmlNodePtr newRoot ) { 
  return domReplaceChild( (xmlNodePtr)doc->doc, 
			  newRoot, 
			  domDocumentElement( doc )) ;
}


xmlNodePtr
domSetOwnerDocument( xmlNodePtr self, xmlDocPtr newDoc );

xmlNodePtr
domImportNode( xmlDocPtr doc, xmlNodePtr node, int move ) {
  xmlNodePtr return_node;

  if ( !doc ) {
    return node;
  }

  if ( node && node->doc != doc ) {
    if ( move ) {
      return_node = domUnbindNode( node );
    }
    else {
      return_node = xmlCopyNode( node, 1 );
    }
    /* tell all children about the new boss */ 
    return_node = domSetOwnerDocument( return_node, doc ); 
  }

  return return_node;
}

xmlNodeSetPtr
domGetElementsByTagName( xmlNodePtr n, xmlChar* name ){
  xmlNodeSetPtr rv = NULL;
  xmlNodePtr cld = NULL;

  if ( n != NULL && name != NULL ) {
    cld = n->children;
    while ( cld ) {
      if ( xmlStrcmp( name, cld->name ) == 0 ){
        if ( rv == NULL ) {
          rv = xmlXPathNodeSetCreate( cld ) ;
        }
        else {
          xmlXPathNodeSetAdd( rv, cld );
        }
      }
      cld = cld->next;
    }
  }
  
  return rv;
}

xmlNodePtr
domSetOwnerDocument( xmlNodePtr self, xmlDocPtr newDoc ) {
    if ( newDoc == NULL ) {
        return NULL;
    }

    if ( self != NULL ) {
        xmlNodePtr pNode = self->children;

        self->doc = newDoc;
        while ( pNode != NULL ) {
            domSetOwnerDocument( pNode, newDoc );
            pNode = pNode->next;
        }
    }

    return self;
}