Class: Simplyx::XsltProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/simplyx/jxslt.rb

Direct Known Subclasses

Saxon

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.perform_transformation(xsl_file, xml_file, transformer_parameters = nil) ⇒ Object

This is used to perform xsltransformation, you should pass it the xsl and xml file paths, and some parameters (an hash) if you need them



17
18
19
20
21
22
23
24
25
26
# File 'lib/simplyx/jxslt.rb', line 17

def self.perform_transformation(xsl_file, xml_file, transformer_parameters=nil)
  file = File.open(xsl_file)
  xsl = file.read
  file.close
  file = File.open(xml_file)
  xml = file.read
  file.close
  saxon = Simplyx::Saxon.new
  output = saxon.transform(xsl, xml, nil, options = {:in => "string", :out => "string", :xsl => "string", :transformer_parameters => transformer_parameters})
end

Instance Method Details

#transform(xslt, infile, outfile, options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/simplyx/jxslt.rb', line 28

def transform(xslt, infile, outfile, options)
  if options[:in] == "stream"
    in_var = StreamSource.new(infile)
  else
    sr = java.io.StringReader.new(infile)
    in_var =  StreamSource.new(sr)
  end
  if options[:out] == "stream"
    out_var = StreamResult.new(outfile)
  else
    sw = java.io.StringWriter.new()
    out_var = StreamResult.new(sw)
  end
  if options[:xslt] == "stream"
    xslt_var = StreamSource.new(xslt)
  else
    sxs = java.io.StringReader.new(xslt)
    xslt_var = StreamSource.new(sxs)
  end
  transformer = @tf.newTransformer(xslt_var)
  unless options[:transformer_parameters].nil?
    options[:transformer_parameters].each do |key, value|
      transformer.setParameter(key, java.lang.String.new(value))
    end
  end
  transformer.transform(in_var, out_var)
  if options[:out] != "stream"
    outfile = sw.toString()
  end
end