Class: RNV::Document

Inherits:
Object
  • Object
show all
Defined in:
ext/rnv/ruby_rnv.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'ext/rnv/ruby_rnv.c', line 440

VALUE rb_document_init(VALUE self)
{
  document_t *document;
  Data_Get_Struct(self, document_t, document);

  rnl_init(document->rnv, document->rn_st, document->rnc_st, document->rnd_st);
  rnv_init(document->rnv, document->drv_st, document->rn_st, document->rx_st);
  rnx_init(document->rnv, document->rnx_st);

  document->opened = document->ok = 0;
  document->rnv->user_data = (void *)self;
  document->rnv->verror_handler = &ruby_verror_handler;
  document->nexp = 16; /* maximum number of candidates to display */

  rb_iv_set(self, "@errors", rb_ary_new2(0));

  return self;
}

Instance Attribute Details

#errorsArray<RNV::Error>

errors from current document

Returns:

#last_colInteger

last column processed, set by SAX handler

Returns:

  • (Integer)

#last_lineInteger

last line processed, set by SAX handler

Returns:

  • (Integer)

Instance Method Details

#characters(r_str) ⇒ Integer

to be called by SAX characters handler

Parameters:

  • r_str (String)

    characters

Returns:

  • (Integer)


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
# File 'ext/rnv/ruby_rnv.c', line 584

VALUE rb_document_characters(VALUE self, VALUE r_str)
{
  document_t *document;
  Data_Get_Struct(self, document_t, document);

  if (document->opened && document->current != document->rnv->rn_notAllowed)
  {
    Check_Type(r_str, T_STRING);
    char *s = RSTRING_PTR(r_str);
    int len = RSTRING_LEN(r_str);

    int newlen_txt = document->n_txt + len + 1;
    if (newlen_txt <= LIM_T && LIM_T < document->len_txt)
      newlen_txt = LIM_T;
    else if (newlen_txt < document->len_txt)
      newlen_txt = document->len_txt;
    if (document->len_txt != newlen_txt)
    {
      document->text = (char *)m_stretch(document->text, document->len_txt = newlen_txt, document->n_txt, sizeof(char));
    }

    memcpy(document->text + document->n_txt, s, len);
    document->n_txt += len;
    document->text[document->n_txt] = '\0'; /* '\0' guarantees that the text is bounded, and strto[ld] work for data */
  }

  return INT2NUM(document->ok);
}

#end_tag(r_name) ⇒ Integer

to be called by SAX end tag handler

Parameters:

  • r_name (String)

    tag name, must be in the form ‘NS_URI:TAG_NAME’

Returns:

  • (Integer)


664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'ext/rnv/ruby_rnv.c', line 664

VALUE rb_document_end_tag(VALUE self, VALUE r_name)
{
  document_t *document;
  Data_Get_Struct(self, document_t, document);

  if (document->opened && document->current != document->rnv->rn_notAllowed)
  {
    char *name;

    Check_Type(r_name, T_STRING);
    name = RSTRING_PTR(r_name);

    flush_text(document);

    //printf("RNV END %d/%d %s\n", current, previous, name);
    document->ok = rnv_end_tag(document->rnv, document->drv_st, document->rn_st,
                               &document->current, &document->previous, (char *)name) &&
                   document->ok;

    document->mixed = 1;
  }

  return INT2NUM(document->ok);
}

#load_file(r_fn) ⇒ Boolean

load schema from a file

Parameters:

  • r_fn (String)

    filename

Returns:

  • (Boolean)


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
# File 'ext/rnv/ruby_rnv.c', line 505

VALUE rb_document_load_file(VALUE self, VALUE r_fn)
{
  document_t *document;
  Data_Get_Struct(self, document_t, document);

  switch (TYPE(r_fn))
  {
  case T_STRING:
    document->fn = RSTRING_PTR(r_fn);

    document->opened = rnl_fn(document->rnv,
                              document->rnc_st,
                              document->rn_st,
                              document->rnd_st,
                              document->fn);
    break;
  case T_FILE: // TODO
  default:
    rb_raise(rb_eTypeError, "invalid argument");
    break;
  }

  document_load(document);

  if (document->opened)
    return Qtrue;
  else
    return Qfalse;
}

#load_string(r_str) ⇒ Boolean

load schema from a buffer

Parameters:

  • r_str (String)

    buffer

Returns:

  • (Boolean)


475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'ext/rnv/ruby_rnv.c', line 475

VALUE rb_document_load_string(VALUE self, VALUE r_str)
{
  document_t *document;
  Data_Get_Struct(self, document_t, document);

  Check_Type(r_str, T_STRING);

  VALUE r_fn = rb_str_new2("");
  document->fn = RSTRING_PTR(r_fn);

  document->opened = rnl_s(document->rnv,
                           document->rnc_st,
                           document->rn_st,
                           document->rnd_st,
                           document->fn,
                           RSTRING_PTR(r_str), RSTRING_LEN(r_str));

  document_load(document);

  if (document->opened)
    return Qtrue;
  else
    return Qfalse;
}

#start_documentnil

begin a new document

Returns:

  • (nil)


562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'ext/rnv/ruby_rnv.c', line 562

VALUE rb_document_begin(VALUE self)
{
  document_t *document;
  Data_Get_Struct(self, document_t, document);

  m_free(document->text);
  document->text = (char *)m_alloc(document->len_txt = LEN_T, sizeof(char));

  document->ok = document->current = document->previous = document->start;

  document->text[0] = '\0';
  document->n_txt = 0;
  document->mixed = 0;

  return Qnil;
}

#start_tag(r_name, r_attrs) ⇒ Integer

to be called by SAX start tag handler

Parameters:

  • r_name (String)

    tag name, must be in the form ‘NS_URI:TAG_NAME’

  • r_attrs (Array<String>)

    flattened array of tag attributes in the form [‘NS_URI:ATTR_NAME’,‘ATTR_VALUE’]

Returns:

  • (Integer)


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
# File 'ext/rnv/ruby_rnv.c', line 619

VALUE rb_document_start_tag(VALUE self, VALUE r_name, VALUE r_attrs)
{
  document_t *document;
  Data_Get_Struct(self, document_t, document);

  if (document->opened && document->current != document->rnv->rn_notAllowed)
  {
    int i;
    char *name;
    char **attrs;

    Check_Type(r_name, T_STRING);
    name = RSTRING_PTR(r_name);

    Check_Type(r_attrs, T_ARRAY);
    unsigned int attrs_len = RARRAY_LEN(r_attrs);

    attrs = malloc(sizeof(char *) * (attrs_len + 1));

    for (i = 0; i < attrs_len; i++)
    {
      attrs[i] = RSTRING_PTR(rb_ary_entry(r_attrs, i));
    }
    attrs[attrs_len] = 0; // zero terminated

    document->mixed = 1;

    flush_text(document);
    //printf("RNV START %d/%d %s %d\n", current, previous, name, attrs_len);
    document->ok = rnv_start_tag(document->rnv, document->drv_st, document->rn_st, document->rx_st,
                                 &document->current, &document->previous, (char *)name, (char **)attrs) &&
                   document->ok;

    document->mixed = 0;
    free(attrs);
  }

  return INT2NUM(document->ok);
}

#valid?Array<RNV::Error>

is current document valid ?

Returns:



539
540
541
542
543
544
545
546
547
548
# File 'ext/rnv/ruby_rnv.c', line 539

VALUE rb_document_valid(VALUE self)
{
  document_t *document;
  Data_Get_Struct(self, document_t, document);

  if (document->ok)
    return Qtrue;
  else
    return Qfalse;
}