Class: Saxon::SourceHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/saxon/source_helper.rb

Overview

Helper methods for converting Ruby File, IO, Path, or String objects into a Javax StreamSource object for Saxon’s Document parser or XSLT compiler to consume

Class Method Summary collapse

Class Method Details

.to_inputstream(path_io_or_string) ⇒ java.io.InputStream, java.io.StringReader

Given a File, IO, or String return a Java InputStream or StringReader

Parameters:

  • path_io_or_string (File, IO, Stringi, org.jruby.util.IOInputStream)

    input to be converted to an input stream

Returns:

  • (java.io.InputStream, java.io.StringReader)

    the wrapped input



39
40
41
42
43
# File 'lib/saxon/source_helper.rb', line 39

def self.to_inputstream(path_io_or_string)
  return path_io_or_string if org.jruby.util.IOInputStream === path_io_or_string
  return path_io_or_string.to_inputstream if path_io_or_string.respond_to?(:read)
  return java.io.StringReader.new(path_io_or_string)
end

.to_stream_source(path_io_or_string, opts = {}) ⇒ java.xml.transform.stream.StreamSource

Creates a StreamSource from its input

Parameters:

  • path_io_or_string (File, IO, String)

    A File, or IO object representing the input XML file or data, or a String containing the XML

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :system_id (String)

    The System ID of the source - an absolute URI or relative path pointing to the location of the source

Returns:

  • (java.xml.transform.stream.StreamSource)

    a StreamSource for consuming the input



17
18
19
20
# File 'lib/saxon/source_helper.rb', line 17

def self.to_stream_source(path_io_or_string, opts = {})
  system_id = opts.fetch(:system_id) { to_system_id(path_io_or_string) }
  StreamSource.new(to_inputstream(path_io_or_string), system_id)
end

.to_system_id(path_io_or_string) ⇒ Object

Given a File, or IO object which will return either #path or #base_uri, return the #base_uri, if present, or the #path, if present, or nil

Parameters:

  • path_io_or_string (File, IO, String, java.org.)

    A Path, File, or IO object representing the input XML file or data, or a String containing the XML



28
29
30
31
32
33
# File 'lib/saxon/source_helper.rb', line 28

def self.to_system_id(path_io_or_string)
  if path_io_or_string.respond_to?(:base_uri)
    return path_io_or_string.base_uri.to_s
  end
  return path_io_or_string.path if path_io_or_string.respond_to?(:path)
end