Class: RXerces::XML::Node

Inherits:
Object
  • Object
show all
Defined in:
ext/rxerces/rxerces.cpp

Direct Known Subclasses

Element, Text

Instance Method Summary collapse

Instance Method Details

#[](attr_name) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'ext/rxerces/rxerces.cpp', line 380

static VALUE node_get_attribute(VALUE self, VALUE attr_name) {
    NodeWrapper* wrapper;
    TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);

    if (!wrapper->node || wrapper->node->getNodeType() != DOMNode::ELEMENT_NODE) {
        return Qnil;
    }

    Check_Type(attr_name, T_STRING);
    const char* attr_str = StringValueCStr(attr_name);

    DOMElement* element = dynamic_cast<DOMElement*>(wrapper->node);
    const XMLCh* value = element->getAttribute(XStr(attr_str).unicodeForm());

    if (!value || XMLString::stringLen(value) == 0) {
        return Qnil;
    }

    CharStr utf8_value(value);
    return rb_str_new_cstr(utf8_value.localForm());
}

#[]=(attr_name, attr_value) ⇒ Object

node = value



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'ext/rxerces/rxerces.cpp', line 403

static VALUE node_set_attribute(VALUE self, VALUE attr_name, VALUE attr_value) {
    NodeWrapper* wrapper;
    TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);

    if (!wrapper->node || wrapper->node->getNodeType() != DOMNode::ELEMENT_NODE) {
        return Qnil;
    }

    Check_Type(attr_name, T_STRING);
    Check_Type(attr_value, T_STRING);

    const char* attr_str = StringValueCStr(attr_name);
    const char* value_str = StringValueCStr(attr_value);

    DOMElement* element = dynamic_cast<DOMElement*>(wrapper->node);
    element->setAttribute(XStr(attr_str).unicodeForm(), XStr(value_str).unicodeForm());

    return attr_value;
}

#childrenObject

node.children



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'ext/rxerces/rxerces.cpp', line 424

static VALUE node_children(VALUE self) {
    NodeWrapper* wrapper;
    TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);

    VALUE doc_ref = rb_iv_get(self, "@document");
    VALUE children = rb_ary_new();

    if (!wrapper->node) {
        return children;
    }

    DOMNodeList* child_nodes = wrapper->node->getChildNodes();
    XMLSize_t count = child_nodes->getLength();

    for (XMLSize_t i = 0; i < count; i++) {
        DOMNode* child = child_nodes->item(i);
        rb_ary_push(children, wrap_node(child, doc_ref));
    }

    return children;
}

#contentObject

node.text / node.content



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'ext/rxerces/rxerces.cpp', line 345

static VALUE node_text(VALUE self) {
    NodeWrapper* wrapper;
    TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);

    if (!wrapper->node) {
        return rb_str_new_cstr("");
    }

    const XMLCh* content = wrapper->node->getTextContent();
    if (!content) {
        return rb_str_new_cstr("");
    }

    CharStr utf8_content(content);
    return rb_str_new_cstr(utf8_content.localForm());
}

#content=(text) ⇒ Object

node.text = value



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'ext/rxerces/rxerces.cpp', line 363

static VALUE node_text_set(VALUE self, VALUE text) {
    NodeWrapper* wrapper;
    TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);

    if (!wrapper->node) {
        return Qnil;
    }

    Check_Type(text, T_STRING);
    const char* text_str = StringValueCStr(text);

    wrapper->node->setTextContent(XStr(text_str).unicodeForm());

    return text;
}

#nameObject

node.name



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'ext/rxerces/rxerces.cpp', line 330

static VALUE node_name(VALUE self) {
    NodeWrapper* wrapper;
    TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);

    if (!wrapper->node) {
        return Qnil;
    }

    const XMLCh* name = wrapper->node->getNodeName();
    CharStr utf8_name(name);

    return rb_str_new_cstr(utf8_name.localForm());
}

#textObject

node.text / node.content



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'ext/rxerces/rxerces.cpp', line 345

static VALUE node_text(VALUE self) {
    NodeWrapper* wrapper;
    TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);

    if (!wrapper->node) {
        return rb_str_new_cstr("");
    }

    const XMLCh* content = wrapper->node->getTextContent();
    if (!content) {
        return rb_str_new_cstr("");
    }

    CharStr utf8_content(content);
    return rb_str_new_cstr(utf8_content.localForm());
}

#text=(text) ⇒ Object

node.text = value



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'ext/rxerces/rxerces.cpp', line 363

static VALUE node_text_set(VALUE self, VALUE text) {
    NodeWrapper* wrapper;
    TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);

    if (!wrapper->node) {
        return Qnil;
    }

    Check_Type(text, T_STRING);
    const char* text_str = StringValueCStr(text);

    wrapper->node->setTextContent(XStr(text_str).unicodeForm());

    return text;
}

#xpath(path) ⇒ Object

node.xpath(path)



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
# File 'ext/rxerces/rxerces.cpp', line 447

static VALUE node_xpath(VALUE self, VALUE path) {
    NodeWrapper* node_wrapper;
    TypedData_Get_Struct(self, NodeWrapper, &node_type, node_wrapper);

    if (!node_wrapper->node) {
        NodeSetWrapper* wrapper = ALLOC(NodeSetWrapper);
        wrapper->nodes_array = rb_ary_new();
        return TypedData_Wrap_Struct(rb_cNodeSet, &nodeset_type, wrapper);
    }

    Check_Type(path, T_STRING);
    const char* xpath_str = StringValueCStr(path);
    VALUE doc_ref = rb_iv_get(self, "@document");

    try {
        DOMDocument* doc = node_wrapper->node->getOwnerDocument();
        if (!doc) {
            NodeSetWrapper* wrapper = ALLOC(NodeSetWrapper);
            wrapper->nodes_array = rb_ary_new();
            return TypedData_Wrap_Struct(rb_cNodeSet, &nodeset_type, wrapper);
        }

        DOMXPathNSResolver* resolver = doc->createNSResolver(node_wrapper->node);
        DOMXPathExpression* expression = doc->createExpression(
            XStr(xpath_str).unicodeForm(), resolver);

        DOMXPathResult* result = expression->evaluate(
            node_wrapper->node,
            DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
            NULL);

        VALUE nodes_array = rb_ary_new();
        XMLSize_t length = result->getSnapshotLength();

        for (XMLSize_t i = 0; i < length; i++) {
            result->snapshotItem(i);
            DOMNode* node = result->getNodeValue();
            if (node) {
                rb_ary_push(nodes_array, wrap_node(node, doc_ref));
            }
        }

        expression->release();
        resolver->release();
        result->release();

        NodeSetWrapper* wrapper = ALLOC(NodeSetWrapper);
        wrapper->nodes_array = nodes_array;
        return TypedData_Wrap_Struct(rb_cNodeSet, &nodeset_type, wrapper);

    } catch (const DOMXPathException& e) {
        CharStr message(e.getMessage());
        rb_raise(rb_eRuntimeError, "XPath error: %s", message.localForm());
    } catch (const DOMException& e) {
        CharStr message(e.getMessage());
        rb_raise(rb_eRuntimeError, "DOM error: %s", message.localForm());
    } catch (...) {
        rb_raise(rb_eRuntimeError, "Unknown XPath error");
    }

    NodeSetWrapper* wrapper = ALLOC(NodeSetWrapper);
    wrapper->nodes_array = rb_ary_new();
    return TypedData_Wrap_Struct(rb_cNodeSet, &nodeset_type, wrapper);
}