Module: Saxon::Source::Helpers

Defined in:
lib/saxon/source.rb

Overview

Helper methods for getting Java-useful representations of source document strings and files

Class Method Summary collapse

Class Method Details

.base_uri(io) ⇒ String?

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:

  • io (File, IO)

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

Returns:

  • (String, nil)

    the path or URI from the IO (or nil if there is none)



23
24
25
26
27
28
# File 'lib/saxon/source.rb', line 23

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

.file(path) ⇒ java.io.File

Given a path return a Java File object

Parameters:

  • path (String, Pathname)

    the path to the file

Returns:

  • (java.io.File)

    the Java File object



55
56
57
# File 'lib/saxon/source.rb', line 55

def self.file(path)
  java.io.File.new(path.to_s)
end

.file_or_reader(path, encoding = nil) ⇒ java.io.Reader

Return a File or Reader object for a file, depending on whether the encoding must be explicitly specified or not.

Parameters:

  • path (String, Pathname)

    the path to the file

  • encoding (String, Encoding) (defaults to: nil)

    the file’s character encoding

Returns:

  • (java.io.Reader)

    a Java Reader object



76
77
78
# File 'lib/saxon/source.rb', line 76

def self.file_or_reader(path, encoding = nil)
  encoding.nil? ? file(path) : file_reader(path, encoding)
end

.file_reader(path, encoding) ⇒ java.io.InputStreamReader

Given a file path and encoding, return a Java InputStreamReader object for the file.

Parameters:

  • path (String, Pathname)

    the path to the file

  • encoding (String, Encoding)

    the file’s character encoding

Returns:

  • (java.io.InputStreamReader)

    a Java InputStreamReader object wrapping a FileInputStream for the file



66
67
68
# File 'lib/saxon/source.rb', line 66

def self.file_reader(path, encoding)
  java.io.InputStreamReader.new(java.io.FileInputStream.new(file(path)), ruby_encoding_to_charset(encoding))
end

.inputstream(io, encoding = nil) ⇒ java.io.InputStream

Given a File or IO return a Java InputStream, or an InputStreamReader if the Encoding is explicitly specified (rather than inferred from the <?xml charset=“…”?>) declaration in the source.

Parameters:

  • io (File, IO, org.jruby.util.IOInputStream, java.io.InputStream)

    input to be converted to an input stream

  • encoding (Encoding, String) (defaults to: nil)

    the character encoding to be used to for the stream, overriding the XML parser.

Returns:

  • (java.io.InputStream)

    the wrapped input



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/saxon/source.rb', line 39

def self.inputstream(io, encoding = nil)
  stream = case io
  when org.jruby.util.IOInputStream, java.io.InputStream
    io
  else
    io.to_inputstream if io.respond_to?(:read)
  end

  return stream if encoding.nil?
  java.io.InputStreamReader.new(stream, ruby_encoding_to_charset(encoding))
end

.ruby_encoding(encoding) ⇒ Encoding

Given a String with an Encoding name or an Encoding instance, return an Encoding instance

Parameters:

  • encoding (String, Encoding)

    the encoding or encoding name

Returns:

  • (Encoding)

    the encoding



106
107
108
# File 'lib/saxon/source.rb', line 106

def self.ruby_encoding(encoding)
  encoding.nil? ? nil : ::Encoding.find(encoding)
end

.ruby_encoding_to_charset(encoding) ⇒ Object

Figure out the equivalent Java Charset for a Ruby Encoding.

Parameters:

  • encoding (String, Encoding)

    the encoding to find a Charset for



97
98
99
# File 'lib/saxon/source.rb', line 97

def self.ruby_encoding_to_charset(encoding)
  ruby_encoding(encoding).to_java.getEncoding.getCharset
end

.string_reader(string, encoding) ⇒ java.io.InputStream, java.io.Reader

Return a Reader object for the String with an explicitly set encoding. If the encoding is ASCII_8BIT then a binary-mode StreamReader is returned, rather than a character Reader

Parameters:

  • string (String)

    the string

  • encoding (String, Encoding)

    the string’s character encoding

Returns:

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

    a Java InputStream or Reader object



87
88
89
90
91
92
# File 'lib/saxon/source.rb', line 87

def self.string_reader(string, encoding)
  inputstream = StringIO.new(string).to_inputstream
  encoding = ruby_encoding(encoding)
  return inputstream if encoding == ::Encoding::ASCII_8BIT
  java.io.InputStreamReader.new(inputstream, ruby_encoding_to_charset(encoding))
end