Class: GenX::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/genx4r/builder.rb,
ext/genx4r/genx4r.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newObject



391
392
393
394
395
396
397
# File 'ext/genx4r/genx4r.c', line 391

static VALUE
writer_new (VALUE klass)
{
  genxWriter writer = genxNew (NULL, NULL, NULL);

  return Data_Wrap_Struct (klass, writer_mark, writer_free, writer);
}

Instance Method Details

#attributeObject



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
# File 'ext/genx4r/genx4r.c', line 198

static VALUE
writer_attribute (int argc, VALUE *argv, VALUE self)
{
  genxAttribute attr = 0;
  genxWriter w;
  VALUE xmlns, name, value;
  int nslen = 0;

  switch (argc)
    {
    case 2:
      xmlns = 0;
      if (CLASS_OF (argv[0]) == rb_cGenXAttribute)
        {
          Data_Get_Struct (argv[0], struct genxAttribute_rec, attr);
          name = 0;
        }
      else
        name = argv[0];

      value = argv[1];
      break;

    case 3:
      xmlns = argv[0];
      name = argv[1];
      value = argv[2];
      break;

    default:
      rb_raise (rb_eRuntimeError, "invalid arguments");
    }

  if (xmlns)
    {
      Check_Type (xmlns, T_STRING);
      nslen = RSTRING (xmlns)->len;
    }

  if (name)
    Check_Type (name, T_STRING);

  Check_Type (value, T_STRING);

  Data_Get_Struct (self, struct genxWriter_rec, w);

  if (attr)
    GENX4R_ERR (genxAddAttribute (attr, (constUtf8) RSTRING (value)->ptr), w);
  else
    GENX4R_ERR (genxAddAttributeLiteral
                 (w,
                  nslen ?  (constUtf8) RSTRING (xmlns)->ptr : NULL,
                  (constUtf8) RSTRING (name)->ptr,
                  (constUtf8) RSTRING (value)->ptr), w);

  return Qnil;
}

#begin_documentObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'ext/genx4r/genx4r.c', line 138

static VALUE
writer_begin_document (VALUE self, VALUE file)
{
  genxWriter w;

  Data_Get_Struct (self, struct genxWriter_rec, w);

  if (! rb_respond_to (file, rb_intern ("<<")))
    rb_raise (rb_eRuntimeError, "target must respond to '<<'");

  genxSetUserData(w, (void *) file);

  GENX4R_ERR (genxStartDocSender (w, &writer_sender), w);

  return Qnil;
}

#begin_elementObject



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
# File 'ext/genx4r/genx4r.c', line 256

static VALUE
writer_begin_element (int argc, VALUE *argv, VALUE self)
{
  genxWriter w;
  genxElement elem = 0;
  VALUE xmlns, name;
  int nslen = 0;

  switch (argc)
    {
    case 1:
      xmlns = 0;
      if (CLASS_OF (argv[0]) == rb_cGenXElement)
        {
          Data_Get_Struct (argv[0], struct genxElement_rec, elem);
          name = 0;
        }
      else
        name = argv[0];
      break;

    case 2:
      xmlns = argv[0];
      name = argv[1];
      break;

    default:
      rb_raise (rb_eRuntimeError, "invalid arguments");
    }

  if (xmlns)
    {
      Check_Type (xmlns, T_STRING);
      nslen = RSTRING (xmlns)->len;
    }

  if (name)
    Check_Type (name, T_STRING);

  Data_Get_Struct (self, struct genxWriter_rec, w);

  if (elem)
    GENX4R_ERR (genxStartElement (elem), w);
  else
    GENX4R_ERR (genxStartElementLiteral
                 (w,
                  nslen ? (constUtf8) RSTRING (xmlns)->ptr : NULL,
                  (constUtf8) RSTRING (name)->ptr), w);

  return Qnil;
}

#builder(io, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/genx4r/builder.rb', line 113

def builder(io, &block)
  b = Builder.new(self)
  begin
    b._begin_doc(io)
    block.call(b)
  ensure
    b._end_doc
  end
end

#characterObject



368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'ext/genx4r/genx4r.c', line 368

static VALUE
writer_character (VALUE self, VALUE ch)
{
  genxWriter w;

  int c = NUM2INT (ch);

  Data_Get_Struct (self, struct genxWriter_rec, w);

  GENX4R_ERR (genxAddCharacter (w, c), w);

  return Qnil;
}

#commentObject



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'ext/genx4r/genx4r.c', line 184

static VALUE
writer_comment (VALUE self, VALUE arg)
{
  genxWriter w;

  Check_Type (arg, T_STRING);

  Data_Get_Struct (self, struct genxWriter_rec, w);

  GENX4R_ERR (genxComment (w, (constUtf8) RSTRING (arg)->ptr), w);

  return Qnil;
}

#declare_attributeObject



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
# File 'ext/genx4r/genx4r.c', line 466

static VALUE
writer_declare_attribute (int argc, VALUE *argv, VALUE self)
{
  genxNamespace ns = 0;
  genxAttribute attr;
  genxStatus st = 0;
  genxWriter w;
  VALUE name;

  switch (argc)
    {
    case 1:
      name = argv[0];
      break;

    case 2:
      if (CLASS_OF (argv[0]) == rb_cGenXNamespace) {
        Data_Get_Struct (argv[0], struct genxNamespace_rec, ns);
      } else {
        rb_raise (rb_eRuntimeError, "invalid arguments");
      }
      name = argv[1];
      break;

    default:
      rb_raise (rb_eRuntimeError, "invalid arguments");
    }

  Check_Type (name, T_STRING);

  Data_Get_Struct (self, struct genxWriter_rec, w);

  attr = genxDeclareAttribute (w, ns, RSTRING (name)->ptr, &st);
  if (st)
    rb_raise (rb_cGenXException, "%s", genxGetErrorMessage (w, st));

  return Data_Wrap_Struct (rb_cGenXAttribute, NULL, NULL, attr);
}

#declare_elementObject



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
# File 'ext/genx4r/genx4r.c', line 427

static VALUE
writer_declare_element (int argc, VALUE *argv, VALUE self)
{
  genxNamespace ns = 0;
  genxStatus st = 0;
  genxElement elem;
  genxWriter w;
  VALUE name;

  switch (argc)
    {
    case 1:
      name = argv[0];
      break;

    case 2:
      if (CLASS_OF (argv[0]) == rb_cGenXNamespace) {
        Data_Get_Struct (argv[0], struct genxNamespace_rec, ns);
      } else {
        rb_raise (rb_eRuntimeError, "invalid arguments");
      }
      name = argv[1];
      break;

    default:
      rb_raise (rb_eRuntimeError, "invalid arguments");
    }

  Check_Type (name, T_STRING);

  Data_Get_Struct (self, struct genxWriter_rec, w);

  elem = genxDeclareElement (w, ns, RSTRING (name)->ptr, &st);
  if (st)
    rb_raise (rb_cGenXException, "%s", genxGetErrorMessage (w, st));

  return Data_Wrap_Struct (rb_cGenXElement, NULL, NULL, elem);
}

#declare_namespaceObject



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
# File 'ext/genx4r/genx4r.c', line 400

static VALUE
writer_declare_namespace (int argc, VALUE *argv, VALUE self)
{
  VALUE uri, prefix = 0;
  genxStatus st = 0;
  genxNamespace ns;
  genxWriter w;

  rb_scan_args (argc, argv, "11", &uri, &prefix);

  Check_Type (uri, T_STRING);
  if (prefix)
    Check_Type (prefix, T_STRING);

  Data_Get_Struct (self, struct genxWriter_rec, w);

  ns = genxDeclareNamespace (w,
                             (constUtf8) RSTRING (uri)->ptr,
                             prefix ? (constUtf8) RSTRING (prefix)->ptr : NULL,
                             &st);

  if (st)
    rb_raise (rb_cGenXException, "%s", genxGetErrorMessage (w, st));

  return Data_Wrap_Struct (rb_cGenXNamespace, NULL, NULL, ns);
}

#documentObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'ext/genx4r/genx4r.c', line 167

static VALUE
writer_document (VALUE self, VALUE file)
{
  writer_begin_document (self, file);

  if (rb_block_given_p ())
    {
      rb_yield (Qnil);

      writer_end_document (self);

      return Qnil;
    }
  else
    rb_raise (rb_eRuntimeError, "must be called with a block");
}

#elementObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'ext/genx4r/genx4r.c', line 320

static VALUE
writer_element (int argc, VALUE *argv, VALUE self)
{
  writer_begin_element (argc, argv, self);

  if (rb_block_given_p ())
    {
      rb_yield (Qnil);

      writer_end_element (self);

      return Qnil;
    }
  else
    rb_raise (rb_eRuntimeError, "must be called with a block");
}

#end_documentObject



155
156
157
158
159
160
161
162
163
164
165
# File 'ext/genx4r/genx4r.c', line 155

static VALUE
writer_end_document (VALUE self)
{
  genxWriter w;

  Data_Get_Struct (self, struct genxWriter_rec, w);

  GENX4R_ERR (genxEndDocument (w), w);

  return Qnil;
}

#end_elementObject



308
309
310
311
312
313
314
315
316
317
318
# File 'ext/genx4r/genx4r.c', line 308

static VALUE
writer_end_element (VALUE self)
{
  genxWriter w;

  Data_Get_Struct (self, struct genxWriter_rec, w);

  GENX4R_ERR (genxEndElement (w), w);

  return Qnil;
}

#piObject



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'ext/genx4r/genx4r.c', line 337

static VALUE
writer_pi (VALUE self, VALUE target, VALUE text)
{
  genxWriter w;

  Check_Type (target, T_STRING);
  Check_Type (text, T_STRING);

  Data_Get_Struct (self, struct genxWriter_rec, w);

  GENX4R_ERR (genxPI (w,
                      (constUtf8) RSTRING (target)->ptr,
                      (constUtf8) RSTRING (text)->ptr), w);

  return Qnil;
}

#textObject



354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'ext/genx4r/genx4r.c', line 354

static VALUE
writer_text (VALUE self, VALUE text)
{
  genxWriter w;

  Check_Type (text, T_STRING);

  Data_Get_Struct (self, struct genxWriter_rec, w);

  GENX4R_ERR (genxAddText (w, (constUtf8) RSTRING (text)->ptr), w);

  return Qnil;
}