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



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

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

  document->rnv->verror_handler = &ruby_verror_handler;
  document->rnv->user_data = (void *)self;

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

  document->opened = document->ok = 0;

  document->nexp = 256; /* maximum number of candidates to display */
  document->text = NULL;

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

  rb_iv_set(self, "@libraries", rb_hash_new());

  document->last_line = -1;
  document->last_col = -1;

  rb_iv_set(self, "@last_line", INT2NUM(-1));
  rb_iv_set(self, "@last_col", INT2NUM(-1));

  //document->rx_st->rx_compact = 1;
  //document->drv_st->drv_compact = 1;

  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)

#xpathString

xpath to node, set by SAX handler

Returns:

  • (String)

Instance Method Details

#add_datatype_library(r_ns, r_cb_obj) ⇒ nil

add a new datatype library

Parameters:

Returns:

  • (nil)

See Also:



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'ext/rnv/ruby_rnv.c', line 208

VALUE rb_document_add_dtl(VALUE self, VALUE r_ns, VALUE r_cb_obj)
{
  document_t *document;
  Data_Get_Struct(self, document_t, document);

  if (document->opened)
  {
    Check_Type(r_ns, T_STRING);

    char *suri = RSTRING_PTR(r_ns);

    drv_add_dtl(document->rnv, document->drv_st, document->rn_st, suri, &rb_dtl_equal, &rb_dtl_allows);

    int uri = document->drv_st->dtl[document->drv_st->n_dtl - 1].uri;

    VALUE libraries = rb_iv_get(self, "@libraries");

    rb_iv_set(r_cb_obj, "@document", self);

    rb_hash_aset(libraries, INT2FIX(uri), r_cb_obj);
  }
  return Qnil;
}

#characters(r_str) ⇒ Boolean

to be called by SAX characters handler

Parameters:

  • r_str (String)

    characters

Returns:

  • (Boolean)


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

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

  if (!document->opened)
    rb_raise(SchemaNotLoaded, "schema was not loaded correctly");

  if (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 RTEST(document->ok);
}

#end_tag(r_name) ⇒ Boolean

to be called by SAX end tag handler

Parameters:

  • r_name (String)

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

Returns:

  • (Boolean)


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

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

  if (!document->opened)
    rb_raise(SchemaNotLoaded, "schema was not loaded correctly");

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

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

    flush_text(document);

    document->ok = rnv_end_tag(document->rnv, document->drv_st, document->rn_st,
                               &document->current, &document->previous, (char *)name) &&
                   document->ok;

    document->mixed = 1;
  }
  else
  {
    if (document->level == 0)
      document->current = document->previous;
    else
      --document->level;
  }

  return RTEST(document->ok);
}

#load_file(r_fn) ⇒ Boolean

load schema from a file

Parameters:

  • r_fn (String)

    filename

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'ext/rnv/ruby_rnv.c', line 136

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

  Check_Type(r_fn, 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);

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

#load_string(r_str) ⇒ Boolean

load schema from a buffer

Parameters:

  • r_str (String)

    buffer

Returns:

  • (Boolean)


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

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));

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

#start_documentnil

begin parsing a new document

Returns:

  • (nil)


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

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

  if (!document->opened)
    rb_raise(SchemaNotLoaded, "schema was not loaded correctly");

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

  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->last_line = 0;
  document->last_col = 0;

  document->level = 0;

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

  return Qnil;
}

#start_tag(r_name, r_attrs) ⇒ Boolean

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<Array<String>>)

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

Returns:

  • (Boolean)


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

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)
    rb_raise(SchemaNotLoaded, "schema was not loaded correctly");

  if (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);

    // lazyly flatten with ruby
    VALUE r_flat_attrs = rb_funcall(r_attrs, rb_intern("flatten"), 0);
    unsigned int attrs_len = RARRAY_LEN(r_flat_attrs);

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

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

    document->mixed = 1;

    flush_text(document);
    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);
  }
  else
  {
    ++document->level;
  }

  return RTEST(document->ok);
}

#valid?Array<RNV::Error>

is current document valid ?

Returns:



164
165
166
167
168
169
170
171
172
173
# File 'ext/rnv/ruby_rnv.c', line 164

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

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