Class: Saxon::Source

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

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

    The Java JAXP StreamSource

  • inputstream (java.io.InputStream, java.io.StringReader) (defaults to: nil)

    The Java InputStream or StringReader



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

Parameters:

  • io (IO, File)

    The IO-like containing XML to be parsed

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

Options Hash (opts):

  • :base_uri (String)

    The Base URI for the Source - an absolute URI or relative path that will be used to resolve relative URLs in the XML. Setting this will override any path or URI derived from the IO-like.

Returns:



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

Parameters:

  • path (String, Pathname)

    The path to the XML file to be parsed

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

Options Hash (opts):

  • :base_uri (String)

    The Base URI for the Source - an absolute URI or relative path that will be used to resolve relative URLs in the XML. Setting this will override the file path.

Returns:



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

Parameters:

  • string (String)

    The string containing XML to be parsed

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

Options Hash (opts):

  • :base_uri (String)

    The Base URI for the Source - an absolute URI or relative path that will be used to resolve relative URLs in the XML. This will be nil unless set.

Returns:



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

Parameters:

  • uri (String, URI)

    The URI to the XML file to be parsed

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

Options Hash (opts):

  • :base_uri (String)

    The Base URI for the Source - an absolute URI or relative path that will be used to resolve relative URLs in the XML. Setting this will override the given URI.

Returns:



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_uriString

Returns The base URI of the Source.

Returns:

  • (String)

    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.

Parameters:

  • uri (String, URI)

    The URI to use as the Source’s Base URI

Returns:

  • (String)

    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

#closeTrueClass

Close the Source and its associated InputStream or Reader, allowing those resources to be freed.

Returns:

  • (TrueClass)

    Returns true



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

Returns:

  • (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.

Yields:

  • (source)

    Yields self to the block

Raises:



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_javajava.xml.transform.stream.StreamSource

Returns The underlying JAXP StreamSource.

Returns:

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

    The underlying JAXP StreamSource



181
182
183
# File 'lib/saxon/source.rb', line 181

def to_java
  @stream_source
end