class Libis::Format::Converter::XsltConverter

Public Class Methods

input_types() click to toggle source
# File lib/libis/format/converter/xslt_converter.rb, line 9
def self.input_types
  [:XML]
end
output_types(format = nil) click to toggle source
# 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

Public Instance Methods

convert(source, target, _format, opts = {}) click to toggle source
Calls superclass method Libis::Format::Converter::Base#convert
# 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) click to toggle source
# File lib/libis/format/converter/xslt_converter.rb, line 18
def xsl_file(file_path)
  @options[:xsl_file] = file_path
end