Class: Teius::Stylesheet

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#==, #[], #attributes, #children, #content, #document, #eql?, #fetch, #find, #hash, #line, #name, #parent, #pointer, #siblings, #to_s, #value, #xpath

Class Method Details

.parse_file(rFilename) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'ext/teius.c', line 246

static VALUE stylesheet_parse_file(VALUE self, VALUE rFilename) {
  xsltStylesheetPtr xsl = xsltParseStylesheetFile(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(cStylesheet, 0, stylesheet_free, xsl);

  return rStylesheet;
}

Instance Method Details

#apply(rDocument) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
# File 'ext/teius.c', line 260

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

#save_to_file(rResult, rFilename) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/teius.c', line 273

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



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'ext/teius.c', line 291

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(mem);
  xmlFree(mem);
  
  return rStr;
}

#transform(doc, filename = nil) ⇒ Object

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



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

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