Class: Saxon::Source
- Inherits:
-
Object
- Object
- Saxon::Source
- Defined in:
- lib/saxon/source.rb
Overview
Provides a wrapper around the JAXP StreamSource class Saxon uses to bring the XML bytestream in. Provides some extra methods to make handling closing the source and its inputstream after consumption more idiomatic
Defined Under Namespace
Modules: Helpers
Constant Summary collapse
- PathChecker =
->(path) { File.file?(path) }
- URIChecker =
->(uri) { begin URI.parse(uri) true rescue URI::InvalidURIError false end }
Class Method Summary collapse
- .create(io_path_uri_or_string, opts = {}) ⇒ Object
-
.from_io(io, opts = {}) ⇒ Saxon::Source
Generate a Saxon::Source given an IO-like.
-
.from_path(path, opts = {}) ⇒ Saxon::Source
Generate a Saxon::Source given a path to a file.
-
.from_string(string, opts = {}) ⇒ Saxon::Source
Generate a Saxon::Source given a string containing XML.
-
.from_uri(uri, opts = {}) ⇒ Saxon::Source
Generate a Saxon::Source given a URI.
Instance Method Summary collapse
-
#base_uri ⇒ String
The base URI of the Source.
-
#base_uri=(uri) ⇒ String
The new base URI of the Source.
-
#close ⇒ TrueClass
Close the Source and its associated InputStream or Reader, allowing those resources to be freed.
-
#closed? ⇒ Boolean
Returns true if the source is closed, false otherwise.
-
#consume {|source| ... } ⇒ Object
Yields itself and then closes itself.
-
#initialize(stream_source, inputstream = nil) ⇒ Source
constructor
private
A new instance of Source.
-
#to_java ⇒ java.xml.transform.stream.StreamSource
The underlying JAXP StreamSource.
Constructor Details
#initialize(stream_source, inputstream = nil) ⇒ Source
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Source.
137 138 139 140 141 |
# File 'lib/saxon/source.rb', line 137 def initialize(stream_source, inputstream = nil) @stream_source = stream_source @inputstream = inputstream @closed = false end |
Class Method Details
.create(io_path_uri_or_string, opts = {}) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/saxon/source.rb', line 118 def self.create(io_path_uri_or_string, opts = {}) case io_path_uri_or_string when IO, File, java.io.InputStream from_io(io_path_uri_or_string, opts) when Pathname, PathChecker from_file(io_path_uri_or_string, opts) when URIChecker from_uri(io_path_uri_or_string, opts) else from_string(io_path_uri_or_string, opts) end end |
.from_io(io, opts = {}) ⇒ Saxon::Source
Generate a Saxon::Source given an IO-like
68 69 70 71 72 73 |
# File 'lib/saxon/source.rb', line 68 def self.from_io(io, opts = {}) base_uri = opts.fetch(:base_uri) { Helpers.base_uri(io) } inputstream = Helpers.inputstream(io) stream_source = Saxon::JAXP::StreamSource.new(inputstream, base_uri) new(stream_source, inputstream) end |
.from_path(path, opts = {}) ⇒ Saxon::Source
Generate a Saxon::Source given a path to a file
83 84 85 86 87 |
# File 'lib/saxon/source.rb', line 83 def self.from_path(path, opts = {}) stream_source = Saxon::JAXP::StreamSource.new(Helpers.file(path)) stream_source.setSystemId(opts[:base_uri]) if opts[:base_uri] new(stream_source) end |
.from_string(string, opts = {}) ⇒ Saxon::Source
Generate a Saxon::Source given a string containing XML
111 112 113 114 115 116 |
# File 'lib/saxon/source.rb', line 111 def self.from_string(string, opts = {}) reader = java.io.StringReader.new(string) stream_source = Saxon::JAXP::StreamSource.new(reader) stream_source.setSystemId(opts[:base_uri]) if opts[:base_uri] new(stream_source, reader) end |
.from_uri(uri, opts = {}) ⇒ Saxon::Source
Generate a Saxon::Source given a URI
97 98 99 100 101 |
# File 'lib/saxon/source.rb', line 97 def self.from_uri(uri, opts = {}) stream_source = Saxon::JAXP::StreamSource.new(uri.to_s) stream_source.setSystemId(opts[:base_uri]) if opts[:base_uri] new(stream_source) end |
Instance Method Details
#base_uri ⇒ String
Returns The base URI of the Source.
144 145 146 |
# File 'lib/saxon/source.rb', line 144 def base_uri stream_source.getSystemId end |
#base_uri=(uri) ⇒ String
Returns The new base URI of the Source.
150 151 152 153 |
# File 'lib/saxon/source.rb', line 150 def base_uri=(uri) stream_source.setSystemId(uri.to_s) base_uri end |
#close ⇒ TrueClass
Close the Source and its associated InputStream or Reader, allowing those resources to be freed.
158 159 160 161 |
# File 'lib/saxon/source.rb', line 158 def close inputstream.close @closed = true end |
#closed? ⇒ Boolean
Returns true if the source is closed, false otherwise
164 165 166 |
# File 'lib/saxon/source.rb', line 164 def closed? @closed end |
#consume {|source| ... } ⇒ Object
Yields itself and then closes itself. To be used by DocumentBuilders or other consumers, making it easy to ensure the source is closed after it has been consumed.
174 175 176 177 178 |
# File 'lib/saxon/source.rb', line 174 def consume(&block) raise SourceClosedError if closed? block.call(self) close end |
#to_java ⇒ java.xml.transform.stream.StreamSource
Returns The underlying JAXP StreamSource.
181 182 183 |
# File 'lib/saxon/source.rb', line 181 def to_java @stream_source end |