saxon 0.2.2-java → 0.4.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,187 +0,0 @@
1
- require 'java'
2
- require 'saxon/jaxp'
3
- require 'uri'
4
- require 'pathname'
5
-
6
- module Saxon
7
- # Provides a wrapper around the JAXP StreamSource class Saxon uses to bring
8
- # the XML bytestream in. Provides some extra methods to make handling closing
9
- # the source and its inputstream after consumption more idiomatic
10
- class Source
11
- module Helpers
12
- # Given a File, or IO object which will return either #path or
13
- # #base_uri, return the #base_uri, if present, or the #path, if present, or
14
- # nil
15
- # @param [File, IO] io A File or IO
16
- # object representing the input XML file or data, or a String containing
17
- # the XML
18
- # @return [String, nil] the path or URI from the IO (or nil if there is none)
19
- def self.base_uri(io)
20
- if io.respond_to?(:base_uri)
21
- return io.base_uri.to_s
22
- end
23
- io.path if io.respond_to?(:path)
24
- end
25
-
26
- # Given a File or IO return a Java InputStream
27
- # @param [File, IO, org.jruby.util.IOInputStream, java.io.InputStream]
28
- # io input to be converted to an input stream
29
- # @return [java.io.InputStream] the wrapped input
30
- def self.inputstream(io)
31
- case io
32
- when org.jruby.util.IOInputStream, java.io.InputStream
33
- io
34
- else
35
- io.to_inputstream if io.respond_to?(:read)
36
- end
37
- end
38
-
39
- # Given a path return a Java File object
40
- # @param [String, Pathname] path the path to the file
41
- # @return [java.io.File] the Java File object
42
- def self.file(path)
43
- java.io.File.new(path.to_s)
44
- end
45
- end
46
-
47
- PathChecker = ->(path) {
48
- File.file?(path)
49
- }
50
- URIChecker = ->(uri) {
51
- begin
52
- URI.parse(uri)
53
- true
54
- rescue URI::InvalidURIError
55
- false
56
- end
57
- }
58
-
59
- # Generate a Saxon::Source given an IO-like
60
- #
61
- # @param [IO, File] io The IO-like containing XML to be parsed
62
- # @param [Hash] opts
63
- # @option opts [String] :base_uri The Base URI for the Source - an
64
- # absolute URI or relative path that will be used to resolve relative
65
- # URLs in the XML. Setting this will override any path or URI derived
66
- # from the IO-like.
67
- # @return [Saxon::Source] the Saxon::Source wrapping the input
68
- def self.from_io(io, opts = {})
69
- base_uri = opts.fetch(:base_uri) { Helpers.base_uri(io) }
70
- inputstream = Helpers.inputstream(io)
71
- stream_source = Saxon::JAXP::StreamSource.new(inputstream, base_uri)
72
- new(stream_source, inputstream)
73
- end
74
-
75
- # Generate a Saxon::Source given a path to a file
76
- #
77
- # @param [String, Pathname] path The path to the XML file to be parsed
78
- # @param [Hash] opts
79
- # @option opts [String] :base_uri The Base URI for the Source - an
80
- # absolute URI or relative path that will be used to resolve relative
81
- # URLs in the XML. Setting this will override the file path.
82
- # @return [Saxon::Source] the Saxon::Source wrapping the input
83
- def self.from_path(path, opts = {})
84
- stream_source = Saxon::JAXP::StreamSource.new(Helpers.file(path))
85
- stream_source.setSystemId(opts[:base_uri]) if opts[:base_uri]
86
- new(stream_source)
87
- end
88
-
89
- # Generate a Saxon::Source given a URI
90
- #
91
- # @param [String, URI] uri The URI to the XML file to be parsed
92
- # @param [Hash] opts
93
- # @option opts [String] :base_uri The Base URI for the Source - an
94
- # absolute URI or relative path that will be used to resolve relative
95
- # URLs in the XML. Setting this will override the given URI.
96
- # @return [Saxon::Source] the Saxon::Source wrapping the input
97
- def self.from_uri(uri, opts = {})
98
- stream_source = Saxon::JAXP::StreamSource.new(uri.to_s)
99
- stream_source.setSystemId(opts[:base_uri]) if opts[:base_uri]
100
- new(stream_source)
101
- end
102
-
103
- # Generate a Saxon::Source given a string containing XML
104
- #
105
- # @param [String] string The string containing XML to be parsed
106
- # @param [Hash] opts
107
- # @option opts [String] :base_uri The Base URI for the Source - an
108
- # absolute URI or relative path that will be used to resolve relative
109
- # URLs in the XML. This will be nil unless set.
110
- # @return [Saxon::Source] the Saxon::Source wrapping the input
111
- def self.from_string(string, opts = {})
112
- reader = java.io.StringReader.new(string)
113
- stream_source = Saxon::JAXP::StreamSource.new(reader)
114
- stream_source.setSystemId(opts[:base_uri]) if opts[:base_uri]
115
- new(stream_source, reader)
116
- end
117
-
118
- def self.create(io_path_uri_or_string, opts = {})
119
- case io_path_uri_or_string
120
- when IO, File, java.io.InputStream, StringIO
121
- from_io(io_path_uri_or_string, opts)
122
- when Pathname, PathChecker
123
- from_path(io_path_uri_or_string, opts)
124
- when URIChecker
125
- from_uri(io_path_uri_or_string, opts)
126
- else
127
- from_string(io_path_uri_or_string, opts)
128
- end
129
- end
130
-
131
- attr_reader :stream_source, :inputstream
132
- private :stream_source, :inputstream
133
-
134
- # @api private
135
- # @param [java.xml.transform.stream.StreamSource] stream_source The Java JAXP StreamSource
136
- # @param [java.io.InputStream, java.io.StringReader] inputstream The Java InputStream or StringReader
137
- def initialize(stream_source, inputstream = nil)
138
- @stream_source = stream_source
139
- @inputstream = inputstream
140
- @closed = false
141
- end
142
-
143
- # @return [String] The base URI of the Source
144
- def base_uri
145
- stream_source.getSystemId
146
- end
147
-
148
- # @param [String, URI] uri The URI to use as the Source's Base URI
149
- # @return [String] The new base URI of the Source
150
- def base_uri=(uri)
151
- stream_source.setSystemId(uri.to_s)
152
- base_uri
153
- end
154
-
155
- # Close the Source and its associated InputStream or Reader, allowing those
156
- # resources to be freed.
157
- # @return [TrueClass] Returns true
158
- def close
159
- inputstream.close
160
- @closed = true
161
- end
162
-
163
- # @return [Boolean] Returns true if the source is closed, false otherwise
164
- def closed?
165
- @closed
166
- end
167
-
168
- # Yields itself and then closes itself. To be used by DocumentBuilders or
169
- # other consumers, making it easy to ensure the source is closed after it
170
- # has been consumed.
171
- #
172
- # @raise [Saxon::SourceClosedError] if the Source has already been closed
173
- # @yield [source] Yields self to the block
174
- def consume(&block)
175
- raise SourceClosedError if closed?
176
- block.call(self)
177
- close
178
- end
179
-
180
- # @return [java.xml.transform.stream.StreamSource] The underlying JAXP StreamSource
181
- def to_java
182
- @stream_source
183
- end
184
- end
185
-
186
- class SourceClosedError < Exception; end
187
- end
@@ -1,3 +0,0 @@
1
- module Saxon
2
- VERSION = "0.2.2"
3
- end
@@ -1,48 +0,0 @@
1
- require_relative 'qname'
2
-
3
- module Saxon
4
- # An XPath Data Model Node object, representing an XML document, or an element or one of the other node chunks in the XDM.
5
- class XdmAtomicValue
6
- # convert a single Ruby value into an XdmAtomicValue
7
- #
8
- # @param value the value to convert
9
- # @return [Saxon::XdmAtomicValue]
10
- def self.create(value)
11
- new(Saxon::S9API::XdmAtomicValue.new(value))
12
- end
13
-
14
- attr_reader :s9_xdm_atomic_value
15
- private :s9_xdm_atomic_value
16
-
17
- # @api private
18
- def initialize(s9_xdm_atomic_value)
19
- @s9_xdm_atomic_value = s9_xdm_atomic_value
20
- end
21
-
22
- # Return a {QName} representing the type of the value
23
- #
24
- # @return [Saxon::QName] the {QName} of the value's type
25
- def type_name
26
- @type_name ||= Saxon::QName.new(s9_xdm_atomic_value.getTypeName)
27
- end
28
-
29
- # @return [Saxon::S9API::XdmAtomicValue] The underlying Saxon Java XDM atomic value object.
30
- def to_java
31
- s9_xdm_atomic_value
32
- end
33
-
34
- # compares two {XdmAtomicValue}s using the underlying Saxon and XDM comparision rules
35
- # @param other [Saxon::XdmAtomicValue]
36
- # @return [Boolean]
37
- def ==(other)
38
- return false unless other.is_a?(XdmAtomicValue)
39
- s9_xdm_atomic_value.equals(other.to_java)
40
- end
41
-
42
- alias_method :eql?, :==
43
-
44
- def hash
45
- @hash ||= s9_xdm_atomic_value.hashCode
46
- end
47
- end
48
- end
@@ -1,65 +0,0 @@
1
- require_relative 'axis_iterator'
2
-
3
- module Saxon
4
- # An XPath Data Model Node object, representing an XML document, or an element or one of the other node chunks in the XDM.
5
- class XdmNode
6
- include Enumerable
7
-
8
- attr_reader :s9_xdm_node
9
- private :s9_xdm_node
10
-
11
- # @api private
12
- def initialize(s9_xdm_node)
13
- @s9_xdm_node = s9_xdm_node
14
- end
15
-
16
- # @return [Saxon::S9API::XdmNode] The underlying Saxon Java XDM node object.
17
- def to_java
18
- @s9_xdm_node
19
- end
20
-
21
- def node_name
22
- return @node_name if instance_variable_defined?(:@node_name)
23
- node_name = s9_xdm_node.getNodeName
24
- @node_name = node_name.nil? ? nil : Saxon::QName.new(node_name)
25
- end
26
-
27
- def node_kind
28
- @node_kind ||= case s9_xdm_node.nodeKind
29
- when Saxon::S9API::XdmNodeKind::ELEMENT
30
- :element
31
- when Saxon::S9API::XdmNodeKind::TEXT
32
- :text
33
- when Saxon::S9API::XdmNodeKind::ATTRIBUTE
34
- :attribute
35
- when Saxon::S9API::XdmNodeKind::NAMESPACE
36
- :namespace
37
- when Saxon::S9API::XdmNodeKind::COMMENT
38
- :comment
39
- when Saxon::S9API::XdmNodeKind::PROCESSING_INSTRUCTION
40
- :processing_instruction
41
- when Saxon::S9API::XdmNodeKind::DOCUMENT
42
- :document
43
- end
44
- end
45
-
46
- def ==(other)
47
- return false unless other.is_a?(XdmNode)
48
- s9_xdm_node.equals(other.to_java)
49
- end
50
-
51
- alias_method :eql?, :==
52
-
53
- def hash
54
- @hash ||= s9_xdm_node.hashCode
55
- end
56
-
57
- def each(&block)
58
- axis_iterator(:child).each(&block)
59
- end
60
-
61
- def axis_iterator(axis)
62
- AxisIterator.new(self, axis)
63
- end
64
- end
65
- end
@@ -1,129 +0,0 @@
1
- require_relative 'xdm_node'
2
- require_relative 'xdm_atomic_value'
3
-
4
- module Saxon
5
- # An XPath Data Model Value object, representing a Sequence.
6
- class XdmValue
7
- include Enumerable
8
-
9
- def self.wrap_s9_xdm_value(s9_xdm_value)
10
- return new(s9_xdm_value) if s9_xdm_value.instance_of?(Saxon::S9API::XdmValue)
11
- case s9_xdm_value
12
- when Saxon::S9API::XdmEmptySequence
13
- new([])
14
- else
15
- wrap_s9_xdm_item(s9_xdm_value)
16
- end
17
- end
18
-
19
- def self.wrap_s9_xdm_item(s9_xdm_item)
20
- if s9_xdm_item.isAtomicValue
21
- XdmAtomicValue.new(s9_xdm_item)
22
- else
23
- case s9_xdm_item
24
- when Saxon::S9API::XdmNode
25
- XdmNode.new(s9_xdm_item)
26
- else
27
- XdmUnhandledItem.new(s9_xdm_item)
28
- end
29
- end
30
- end
31
-
32
- # Create a new XdmValue sequence containing the items passed in as a Ruby enumerable.
33
- #
34
- # @param items [Enumerable] A list of XDM Item members
35
- # @return [Saxon::XdmValue] The XDM value
36
- def self.create(items)
37
- new(Saxon::S9API::XdmValue.makeSequence(items.map(&:to_java)))
38
- end
39
-
40
- attr_reader :s9_xdm_value
41
- private :s9_xdm_value
42
-
43
- # @api private
44
- def initialize(s9_xdm_value)
45
- @s9_xdm_value = s9_xdm_value
46
- end
47
-
48
- # @return [Fixnum] The size of the sequence
49
- def size
50
- s9_xdm_value.size
51
- end
52
-
53
- # Calls the given block once for each Item in the sequence, passing that
54
- # item as a parameter. Returns the value itself.
55
- #
56
- # If no block is given, an Enumerator is returned.
57
- #
58
- # @overload
59
- # @yield [item] The current XDM Item
60
- # @yieldparam item [Saxon::XdmAtomicValue, Saxon::XdmNode] the item.
61
- def each(&block)
62
- to_enum.each(&block)
63
- end
64
-
65
- # @return [Saxon::S9API::XdmValue] The underlying Saxon Java XDM valuee object.
66
- def to_java
67
- @s9_xdm_value
68
- end
69
-
70
- # Compare this XdmValue with another. Currently this requires iterating
71
- # across the sequence, and the other sequence and comparing each member
72
- # with the corresponding member in the other sequence.
73
- #
74
- # @param other [Saxon::XdmValue] The XdmValue to be compare against
75
- # @return [Boolean] whether the two XdmValues are equal
76
- def ==(other)
77
- return false unless other.is_a?(XdmValue)
78
- return false unless other.size == size
79
- not_okay = to_enum.zip(other.to_enum).find { |mine, theirs|
80
- mine != theirs
81
- }
82
- not_okay.nil? || !not_okay
83
- end
84
-
85
- alias_method :eql?, :==
86
-
87
- # The hash code for the XdmValue
88
- # @return [Fixnum] The hash code
89
- def hash
90
- @hash ||= to_a.hash
91
- end
92
-
93
- # Returns a lazy Enumerator over the sequence
94
- # @return [Enumerator::Lazy] the enumerator
95
- def to_enum
96
- s9_xdm_value.each.lazy.map { |s9_xdm_item|
97
- wrap_s9_xdm_item(s9_xdm_item)
98
- }.each
99
- end
100
-
101
- alias_method :enum_for, :to_enum
102
-
103
- private
104
-
105
- def wrap_s9_xdm_item(s9_xdm_item)
106
- if s9_xdm_item.isAtomicValue
107
- XdmAtomicValue.new(s9_xdm_item)
108
- else
109
- case s9_xdm_item
110
- when Saxon::S9API::XdmNode
111
- XdmNode.new(s9_xdm_item)
112
- else
113
- XdmUnhandledItem.new(s9_xdm_item)
114
- end
115
- end
116
- end
117
- end
118
-
119
- # Placeholder class for Saxon Items that we haven't gotten to yet
120
- class XdmUnhandledItem
121
- def initialize(s9_xdm_item)
122
- @s9_xdm_item = s9_xdm_item
123
- end
124
-
125
- def to_java
126
- @s9_xdm_item
127
- end
128
- end
129
- end
@@ -1,8 +0,0 @@
1
- require_relative './xpath/compiler'
2
-
3
- module Saxon
4
- # Classes for compiling, configuring, and executing XPath queries against
5
- # XDM nodes or documents
6
- module XPath
7
- end
8
- end
@@ -1,69 +0,0 @@
1
- require 'forwardable'
2
- require_relative './static_context'
3
- require_relative './executable'
4
-
5
- module Saxon
6
- module XPath
7
- # Compiles XPath expressions so they can be executed
8
- class Compiler
9
- # Create a new <tt>XPath::Compiler</tt> using the supplied Processor.
10
- # Passing a block gives access to a DSL for setting up the compiler's
11
- # static context.
12
- #
13
- # @param processor [Saxon::Processor] the {Saxon::Processor} to use
14
- # @yield An XPath compiler DSL block
15
- # @return [Saxon::XPath::Compiler] the new compiler instance
16
- def self.create(processor, &block)
17
- static_context = XPath::StaticContext.define(block)
18
- new(processor.to_java, static_context)
19
- end
20
-
21
- extend Forwardable
22
-
23
- attr_reader :static_context
24
- private :static_context
25
-
26
- # @api private
27
- # @param s9_processor [net.sf.saxon.s9api.Processor] the Saxon
28
- # <tt>Processor</tt> to wrap
29
- # @param static_context [Saxon::XPath::StaticContext] the static context
30
- # XPaths compiled using this compiler will have
31
- def initialize(s9_processor, static_context)
32
- @s9_processor, @static_context = s9_processor, static_context
33
- end
34
-
35
- def_delegators :static_context, :default_collation, :declared_namespaces, :declared_variables
36
- # @!attribute [r] default_collation
37
- # @return [String] the URI of the default declared collation
38
- # @!attribute [r] declared_namespaces
39
- # @return [Hash<String => String>] declared namespaces as prefix => URI hash
40
- # @!attribute [r] declared_variables
41
- # @return [Hash<Saxon::QName => Saxon::XPath::VariableDeclaration>] declared variables as QName => Declaration hash
42
-
43
- # @param expression [String] the XPath expression to compile
44
- # @return [Saxon::XPath::Executable] the executable query
45
- def compile(expression)
46
- Saxon::XPath::Executable.new(new_compiler.compile(expression), static_context)
47
- end
48
-
49
- def create(&block)
50
- new_static_context = static_context.define(block)
51
- self.class.new(@s9_processor, new_static_context)
52
- end
53
-
54
- private
55
-
56
- def new_compiler
57
- compiler = @s9_processor.newXPathCompiler
58
- declared_namespaces.each do |prefix, uri|
59
- compiler.declareNamespace(prefix, uri)
60
- end
61
- declared_variables.each do |_, decl|
62
- compiler.declareVariable(*decl.compiler_args)
63
- end
64
- compiler.declareDefaultCollation(default_collation) unless default_collation.nil?
65
- compiler
66
- end
67
- end
68
- end
69
- end