Class: Teius::Stylesheet

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_file(rFilename) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'ext/teius.c', line 290

static VALUE stylesheet_parse_file(VALUE self, VALUE rFilename) {
  xsltStylesheetPtr xsl = xsltParseStylesheetFile((xmlChar *)StringValuePtr(rFilename));
  if (xsl == NULL) {
    xmlErrorPtr  err = xmlGetLastError();
    rb_raise(cParseError, "could not parse file %s: %s", 
      StringValuePtr(rFilename), err->message);
  }

  /* Load new document */
  VALUE rStylesheet = Data_Wrap_Struct(self, 0, stylesheet_free, xsl);

  return rStylesheet;
}

Instance Method Details

#apply(rDocument) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
# File 'ext/teius.c', line 316

static VALUE stylesheet_apply(VALUE self, VALUE rDocument) {
  xsltStylesheetPtr stylesheet = NULL;
  xmlDocPtr  doc = NULL, res = NULL;
  
  Data_Get_Struct(self, xsltStylesheet, stylesheet);
  Data_Get_Struct(rDocument, xmlDoc, doc);
  res = xsltApplyStylesheet(stylesheet, doc, NULL); 

  VALUE rDoc = Data_Wrap_Struct(cDocument, 0, doc_free, res);
  
  return rDoc;
}

#output_methodObject



304
305
306
307
308
309
310
311
312
313
314
# File 'ext/teius.c', line 304

static VALUE stylesheet_output_method(VALUE self) {
  xsltStylesheetPtr stylesheet = NULL;
  
  Data_Get_Struct(self, xsltStylesheet, stylesheet);
  if (stylesheet->method == NULL) {
    return Qnil;
  } else {
    VALUE rStr = rb_str_new2((char *)stylesheet->method);
    return rStr;
  }
}

#save_to_file(rResult, rFilename) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'ext/teius.c', line 329

static VALUE stylesheet_save_to_file(VALUE self, VALUE rResult, VALUE rFilename) {
  xsltStylesheetPtr stylesheet = NULL;
  xmlDocPtr result = NULL;
  int st;
  
  Data_Get_Struct(self, xsltStylesheet, stylesheet);
  Data_Get_Struct(rResult, xmlDoc, result);

  st = xsltSaveResultToFilename(StringValuePtr(rFilename), result, stylesheet, 0);
  if (st == -1) {
    xmlErrorPtr  err = xmlGetLastError();
    rb_raise(rb_eIOError, "could not save stylesheet result file %s: %s", 
      StringValuePtr(rFilename), err->message);
  }
           
  return Qnil;
}

#save_to_string(rResult) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'ext/teius.c', line 347

static VALUE stylesheet_save_to_string(VALUE self, VALUE rResult) {
  xsltStylesheetPtr stylesheet = NULL;
  xmlDocPtr result = NULL;
  int st;
  xmlChar *mem;
  int size;
  
  Data_Get_Struct(self, xsltStylesheet, stylesheet);
  Data_Get_Struct(rResult, xmlDoc, result);

  st = xsltSaveResultToString(&mem, &size, result, stylesheet);
  if (st == -1) {
    xmlErrorPtr  err = xmlGetLastError();
    rb_raise(rb_eStandardError, "could not save stylesheet result to string: %s", 
      err->message);
  }
  VALUE rStr = rb_str_new2((char *)mem);
  xmlFree(mem);
  
  return rStr;
}

#transform(doc, filename = nil) ⇒ Object

If no filename is provided, will return the result as a String



59
60
61
62
63
64
65
66
67
# File 'lib/teius.rb', line 59

def transform(doc, filename = nil)
  result = apply doc
  if filename
    out = save_to_file result, filename   # returns nil
  else
    out = save_to_string result
  end      
  out
end