Class: Libis::Format::Converter::XsltConverter

Inherits:
Base
  • Object
show all
Defined in:
lib/libis/format/converter/xslt_converter.rb

Instance Attribute Summary

Attributes inherited from Base

#flags, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited, #initialize, #using_temp, using_temp

Constructor Details

This class inherits a constructor from Libis::Format::Converter::Base

Class Method Details

.input_typesObject



9
10
11
# File 'lib/libis/format/converter/xslt_converter.rb', line 9

def self.input_types
  [:XML]
end

.output_types(format = nil) ⇒ Object



13
14
15
16
# File 'lib/libis/format/converter/xslt_converter.rb', line 13

def self.output_types(format = nil)
  return [] unless input_types.include?(format) if format
  [:XML, :HTML, :TXT]
end

Instance Method Details

#convert(source, target, _format, opts = {}) ⇒ Object



22
23
24
25
26
27
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
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
88
89
90
91
92
93
94
95
# File 'lib/libis/format/converter/xslt_converter.rb', line 22

def convert(source, target, _format, opts = {})
  super

  unless File.file?(source) && File.exist?(source) && File.readable?(source)
    error "File '#{source}' does not exist or is not readable"
    return nil
  end

  unless @options[:xsl_file]
    error 'No xsl_file supplied'
    return nil
  end

  FileUtils.mkpath(File.dirname(target))

  if RUBY_PLATFORM == "java"
    require 'saxon-xslt'
    xsl = Saxon.XSLT(File.open(@options[:xsl_file]))
    xml = Saxon.XML(File.open(source))
    result = xsl.transform(xml)
    File.open(target, 'w') {|f| f.write(result.to_s)}
  else
    require 'nokogiri'

    doc = nil
    begin
      doc = Nokogiri::XML(File.read(source)) do |config|
        config.options = Nokogiri::XML::ParseOptions::STRICT | Nokogiri::XML::ParseOptions::NOBLANKS
      end
    rescue Nokogiri::XML::SyntaxError => e
      if e.fatal? || e.error?
        error "Error parsing XML input '#{source}': #{e.messsage} @ #{e.backtrace[0]}"
        return nil
      end
    end

    file = @options[:xsl_file]

    unless File.file?(file) && File.exist?(file) && File.readable?(file)
      error "XSL file '#{@options[:xsl_file]}' does not exist or is not readable"
      return nil
    end

    xsl = nil

    begin
      fp = File.open(file, 'r')
      xsl = Nokogiri::XSLT(fp) do |config|
        config.options = Nokogiri::XML::ParseOptions::STRICT | Nokogiri::XML::ParseOptions::NOBLANKS
      end
    rescue Nokogiri::XML::SyntaxError => e
      if e.fatal? || e.error?
        error "Error parsing XSL input '#{file}': #{e.message} @ #{e.backtrace[0]}"
        return nil
      end
    ensure
      fp.close
    end

    begin
      target_xml = xsl.transform(doc)
      fp = File.open(target, 'w')
      fp.write(target_xml)
    rescue Exception => e
      error "Error transforming '#{source}' with '#{file}': #{e.message} @ #{e.backtrace[0]}"
      return nil
    ensure
      fp.close unless fp.nil? or fp.closed?
    end

    target
  end

end

#xsl_file(file_path) ⇒ Object



18
19
20
# File 'lib/libis/format/converter/xslt_converter.rb', line 18

def xsl_file(file_path)
  @options[:xsl_file] = file_path
end