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
-
.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.
-
.file(path) ⇒ java.io.File
Given a path return a Java File object.
-
.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.
-
.file_reader(path, encoding) ⇒ java.io.InputStreamReader
Given a file path and encoding, return a Java InputStreamReader object for the file.
-
.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.
-
.ruby_encoding(encoding) ⇒ Encoding
Given a String with an Encoding name or an Encoding instance, return an Encoding instance.
-
.ruby_encoding_to_charset(encoding) ⇒ Object
Figure out the equivalent Java
Charset
for a Ruby Encoding. -
.string_reader(string, encoding) ⇒ java.io.InputStream, java.io.Reader
Return a Reader object for the String with an explicitly set encoding.
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
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
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.
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.
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.
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
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.
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
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 |