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,33 +0,0 @@
1
- require_relative 's9api'
2
-
3
- module Saxon
4
- # Provides simple access to Saxon's OccurrenceIndicator constants,
5
- # for use in declaring variable types
6
- module OccurrenceIndicator
7
- class << self
8
- def one
9
- @one ||= Saxon::S9API::OccurrenceIndicator::ONE
10
- end
11
-
12
- def one_or_more
13
- @one_or_more ||= Saxon::S9API::OccurrenceIndicator::ONE_OR_MORE
14
- end
15
-
16
- def zero
17
- @zero ||= Saxon::S9API::OccurrenceIndicator::ZERO
18
- end
19
-
20
- def zero_or_more
21
- @zero_or_more ||= Saxon::S9API::OccurrenceIndicator::ZERO_OR_MORE
22
- end
23
-
24
- def zero_or_one
25
- @zero_or_one ||= Saxon::S9API::OccurrenceIndicator::ZERO_OR_ONE
26
- end
27
-
28
- def indicator_names
29
- @indicator_names ||= (public_methods(false) - Object.public_methods - [:indicator_names])
30
- end
31
- end
32
- end
33
- end
@@ -1,127 +0,0 @@
1
- require 'saxon/s9api'
2
-
3
- module Saxon
4
- # Wraps the <tt>net.sf.saxon.lib.ParseOptions</tt> class, which holds default
5
- # options to be passed to the parser
6
- class ParseOptions
7
- attr_reader :parse_options
8
- private :parse_options
9
-
10
- # @api private
11
- # @param parse_options [net.sf.saxon.lib.ParseOptions, nil] The Saxon
12
- # ParseOptions instance to wrap
13
- def initialize(parse_options = nil)
14
- @parse_options = parse_options || Saxon::S9API::ParseOptions.new
15
- end
16
-
17
- # Provide hash-like access to SAX parser properties
18
- # @return [Saxon::ParseOptions::ParserProperties] the properties object
19
- def properties
20
- @properties ||= ParserProperties.new(parse_options)
21
- end
22
-
23
- # Provide hash-like access to SAX parser features
24
- # @return [Saxon::ParseOptions::ParserFeatures] the features object
25
- def features
26
- @features ||= ParserFeatures.new(parse_options)
27
- end
28
- end
29
-
30
- # Provides idiomatic access to parser properties
31
- class ParserProperties
32
- include Enumerable
33
-
34
- attr_reader :parse_options
35
- private :parse_options
36
-
37
- # @api private
38
- # @param [Saxon::ParseOptions] parse_options The ParseOptions object this
39
- # belongs to
40
- def initialize(parse_options)
41
- @parse_options ||= parse_options
42
- end
43
-
44
- # @return [Hash] the currently-set properties as a (frozen) hash
45
- def to_h
46
- (parse_options.getParserProperties || {}).freeze
47
- end
48
-
49
- # Fetch the current value of a Parser Property
50
- #
51
- # @param [String, URI] uri The Parser Property URI to fetch
52
- # @return [Object] The current value of the property
53
- def [](uri)
54
- return nil if parse_options.getParserProperties.nil?
55
- parse_options.getParserProperty(uri)
56
- end
57
-
58
- # Set the value of a Parser Property
59
- #
60
- # @param [String, URI] uri The Parser Property URI to set
61
- # @param [Object] value The value to set the property to
62
- # @return [Object] The new value of the property
63
- def []=(uri, value)
64
- parse_options.addParserProperties(uri, value)
65
- self[uri]
66
- end
67
-
68
- # Iterate across each currently-set property, in the same way as a hash
69
- #
70
- # @yield [uri, value]
71
- # @yieldparam uri [String] The URI of the property as a String
72
- # @yieldparam value [Object] The value of the property
73
- def each(&block)
74
- to_h.each(&block)
75
- end
76
- end
77
-
78
- # Provides idiomatic access to parser properties
79
- class ParserFeatures
80
- include Enumerable
81
-
82
- attr_reader :parse_options
83
- private :parse_options
84
-
85
- # @api private
86
- # @param [Saxon::ParseOptions] parse_options The ParseOptions object this
87
- # belongs to
88
- def initialize(parse_options)
89
- @parse_options ||= parse_options
90
- end
91
-
92
- # @return [Hash] the currently-set features as a (frozen) hash
93
- def to_h
94
- (parse_options.getParserFeatures || {}).freeze
95
- end
96
-
97
- # Fetch the current value of a Parser Feature
98
- #
99
- # @param [String, URI] uri The Parser Feature URI to fetch
100
- # @return [Boolean, nil] The current value of the feature, or nil if it's
101
- # unset
102
- def [](uri)
103
- pf = parse_options.getParserFeatures
104
- return nil if pf.nil? || !pf.include?(uri)
105
- parse_options.getParserFeature(uri)
106
- end
107
-
108
- # Set the value of a Parser Feature
109
- #
110
- # @param [String, URI] uri The Parser Property URI to set
111
- # @param [Boolean] value The value to set the property to
112
- # @return [Boolean] The new value of the property
113
- def []=(uri, value)
114
- parse_options.addParserFeature(uri, value)
115
- self[uri]
116
- end
117
-
118
- # Iterate across each currently-set feature, in the same way as a hash
119
- #
120
- # @yield [uri, value]
121
- # @yieldparam uri [String] The URI of the feature as a String
122
- # @yieldparam value [Boolean] Whether the feature is on or off
123
- def each(&block)
124
- to_h.each(&block)
125
- end
126
- end
127
- end
@@ -1,102 +0,0 @@
1
- require 'saxon/s9api'
2
- require 'saxon/source'
3
- require 'saxon/configuration'
4
- require 'saxon/document_builder'
5
- require 'saxon/xpath'
6
- require 'saxon/xslt'
7
-
8
- module Saxon
9
- # Saxon::Processor wraps the S9API::Processor object. This is the object
10
- # responsible for creating an XSLT compiler or an XML Document object.
11
- #
12
- # The Processor is threadsafe, and can be shared between threads. But, most
13
- # importantly XSLT or XML objects created by a Processor can only be used
14
- # with other XSLT or XML objects created by the same Processor instance.
15
- class Processor
16
- # Provides a processor with default configuration. Essentially a singleton
17
- # instance
18
- # @return [Saxon::Processor]
19
- def self.default
20
- @processor ||= create(Saxon::Configuration.default)
21
- end
22
-
23
- # @param config [File, String, IO, Saxon::Configuration] an open File, or string,
24
- # containing a Saxon configuration file; an existing Saxon::Configuration
25
- # object
26
- # @return [Saxon::Processor]
27
- def self.create(config = nil)
28
- Saxon::Loader.load!
29
- case config
30
- when nil
31
- licensed_or_config_source = false
32
- when Saxon::Configuration
33
- licensed_or_config_source = config.to_java
34
- else
35
- licensed_or_config_source = Saxon::Source.create(config).to_java
36
- end
37
- s9_processor = S9API::Processor.new(licensed_or_config_source)
38
- new(s9_processor)
39
- end
40
-
41
- # @api private
42
- # @param [net.sf.saxon.s9api.Processor] s9_processor The Saxon Processor
43
- # instance to wrap
44
- def initialize(s9_processor)
45
- @s9_processor = s9_processor
46
- end
47
-
48
- # Generate a new DocumentBuilder that uses this Processor.
49
- # Sharing DocumentBuilders across threads is not recommended/
50
- #
51
- # @return [Saxon::DocumentBuilder] A new Saxon::DocumentBuilder
52
- def document_builder
53
- Saxon::DocumentBuilder.new(@s9_processor.newDocumentBuilder)
54
- end
55
-
56
- # Declare custom collations for use by XSLT, XPath, and XQuery processors
57
- #
58
- # @param collations [Hash<String => java.text.Collator>] collations to
59
- # declare, as a hash of URI => Collator
60
- def declare_collations(collations)
61
- collations.each do |uri, collation|
62
- @s9_processor.declareCollation(uri, collation)
63
- end
64
- end
65
- # Generate a new <tt>XPath::Compiler</tt> that uses this
66
- # <tt>Processor</tt>. Sharing <tt>XPath::Compiler</tt>s across threads is
67
- # fine as long as the static context is not changed.
68
- #
69
- # @yield An XPath compiler DSL block, see {Saxon::XPath::Compiler.create}
70
- # @return [Saxon::XPath::Compiler] a new XPath compiler
71
- def xpath_compiler(&block)
72
- Saxon::XPath::Compiler.create(self, &block)
73
- end
74
-
75
- # Generate a new <tt>XSLT::Compiler</tt> that uses this
76
- # <tt>Processor</tt>. Sharing <tt>XSLT::Compiler</tt>s across threads is
77
- # fine as long as the static context is not changed.
78
- #
79
- # @yield An XPath compiler DSL block, see {Saxon::XSLT::Compiler.create}
80
- # @return [Saxon::XSLT::Compiler] a new XSLT compiler
81
- def xslt_compiler(&block)
82
- Saxon::XSLT::Compiler.create(self, &block)
83
- end
84
-
85
- # @return [net.sf.saxon.s9api.Processor] The underlying Saxon processor
86
- def to_java
87
- @s9_processor
88
- end
89
-
90
- # compare equal if the underlying java processor is the same instance for
91
- # self and other
92
- # @param other object to compare against
93
- def ==(other)
94
- other.to_java === to_java
95
- end
96
-
97
- # @return [Saxon::Configuration] This processor's configuration instance
98
- def config
99
- @config ||= Saxon::Configuration.create(self)
100
- end
101
- end
102
- end
@@ -1,145 +0,0 @@
1
- require 'saxon/s9api'
2
-
3
- module Saxon
4
- # Represents QNames
5
- class QName
6
- def self.clark(clark_string)
7
- s9_qname = Saxon::S9API::QName.fromClarkName(clark_string)
8
- new(s9_qname)
9
- end
10
-
11
- def self.eqname(eqname_string)
12
- s9_qname = Saxon::S9API::QName.fromEQName(eqname_string)
13
- new(s9_qname)
14
- end
15
-
16
- def self.create(opts = {})
17
- prefix = opts[:prefix]
18
- uri = opts[:uri]
19
- begin
20
- local_name = opts.fetch(:local_name)
21
- rescue KeyError
22
- raise ArgumentError, "The :local_name option must be passed"
23
- end
24
-
25
- s9_qname = Saxon::S9API::QName.new(*[prefix, uri, local_name].compact)
26
- new(s9_qname)
27
- end
28
-
29
- # Resolve a QName string into a {Saxon::QName}.
30
- #
31
- # If the arg is a {Saxon::QName} already, it just gets returned. If
32
- # it's an instance of the underlying Saxon Java QName, it'll be wrapped
33
- # into a {Saxon::QName}
34
- #
35
- # If the arg is a string, it's resolved by using {resolve_variable_name}
36
- #
37
- # @param qname_or_string [String, Symbol, Saxon::QName] the qname to resolve
38
- # @param namespaces [Hash<String => String>] the set of namespaces as a hash of <tt>"prefix" => "namespace-uri"</tt>
39
- # @return [Saxon::QName]
40
- def self.resolve(qname_or_string, namespaces = {})
41
- case qname_or_string
42
- when String, Symbol
43
- resolve_qname_string(qname_or_string, namespaces)
44
- when self
45
- qname_or_string
46
- when Saxon::S9API::QName
47
- new(qname_or_string)
48
- end
49
- end
50
-
51
- # Resolve a QName string of the form <tt>"prefix:local-name"</tt> into a
52
- # {Saxon::QName} by looking up the namespace URI in a hash of
53
- # <tt>"prefix" => "namespace-uri"</tt>
54
- #
55
- # @param qname_string [String, Symbol] the QName as a <tt>"prefix:local-name"</tt> string
56
- # @param namespaces [Hash<String => String>] the set of namespaces as a hash of <tt>"prefix" => "namespace-uri"</tt>
57
- # @return [Saxon::QName]
58
- def self.resolve_qname_string(qname_string, namespaces = {})
59
- local_name, prefix = qname_string.to_s.split(':').reverse
60
- uri = nil
61
-
62
- if prefix
63
- uri = namespaces[prefix]
64
- raise self::PrefixedStringWithoutNSURIError.new(qname_string, prefix) if uri.nil?
65
- end
66
-
67
- create(prefix: prefix, uri: uri, local_name: local_name)
68
- end
69
-
70
- attr_reader :s9_qname
71
- private :s9_qname
72
-
73
- # @api private
74
- def initialize(s9_qname)
75
- @s9_qname = s9_qname
76
- end
77
-
78
- # @return [String] The local name part of the QName
79
- def local_name
80
- @s9_qname.getLocalName
81
- end
82
-
83
- # @return [String] The prefix part of the QName ('' if unset)
84
- def prefix
85
- @s9_qname.getPrefix
86
- end
87
-
88
- # @return [String] The namespace URI part of the QName ('' if unset)
89
- def uri
90
- @s9_qname.getNamespaceURI
91
- end
92
-
93
- # Return a Clark notation representation of the QName:
94
- #
95
- # "{http://ns.url}local-name"
96
- #
97
- # Note that the prefix is lost in Clark notation.
98
- # @return [String] The QName represented using Clark notation.
99
- def clark
100
- @s9_qname.getClarkName
101
- end
102
-
103
- # Return a Extended QName notation representation of the QName:
104
- #
105
- # "Q{http://ns.url}local-name"
106
- #
107
- # Note that the prefix is lost in EQName notation.
108
- # @return [String] The QName represented using EQName notation.
109
- def eqname
110
- @s9_qname.getEQName
111
- end
112
-
113
- def ==(other)
114
- return false unless other.is_a?(QName)
115
- s9_qname.equals(other.to_java)
116
- end
117
- alias_method :eql?, :==
118
-
119
- def hash
120
- @hash ||= (local_name + uri).hash
121
- end
122
-
123
- def to_java
124
- s9_qname
125
- end
126
-
127
- def to_s
128
- s9_qname.to_s
129
- end
130
-
131
- def inspect
132
- "<Saxon::QName @prefix=#{prefix} @uri=#{uri} @local_name=#{local_name}>"
133
- end
134
-
135
- class PrefixedStringWithoutNSURIError < RuntimeError
136
- def initialize(qname_string, prefix)
137
- @qname_string, @prefix = qname_string, prefix
138
- end
139
-
140
- def to_s
141
- "Namespace prefix ‘#{@prefix}’ for QName ‘#{@qname_string}’ is not bound to a URI"
142
- end
143
- end
144
- end
145
- end
@@ -1,16 +0,0 @@
1
- require 'saxon/loader'
2
-
3
- module Saxon
4
- module S9API
5
- def self.const_missing(name)
6
- Saxon::Loader.load!
7
- begin
8
- const_get(name)
9
- rescue NameError
10
- msg = "uninitialized constant Saxon::S9API::#{name}"
11
- e = NameError.new(msg, name)
12
- raise e
13
- end
14
- end
15
- end
16
- end
@@ -1,143 +0,0 @@
1
- require 'saxon/s9api'
2
- require 'stringio'
3
-
4
- module Saxon
5
- # Serialize XDM objects.
6
- class Serializer
7
- # Manage access to the serialization properties of this serializer, with
8
- # hash-like access.
9
- #
10
- # Properties can be set explicitly through this API, or via XSLT or XQuery
11
- # serialization options like `<xsl:output>`.
12
- #
13
- # Properties set explicitly here will override properties set through the
14
- # document like `<xsl:output>`.
15
- class OutputProperties
16
- # @api private
17
- # Provides mapping between symbols and the underlying Saxon property
18
- # instances
19
- def self.output_properties
20
- @output_properties ||= Hash[
21
- Saxon::S9API::Serializer::Property.values.map { |property|
22
- qname = property.getQName
23
- key = [
24
- qname.getPrefix,
25
- qname.getLocalName.tr('-', '_')
26
- ].reject { |str| str == '' }.join('_').to_sym
27
- [key, property]
28
- }
29
- ]
30
- end
31
-
32
- attr_reader :s9_serializer
33
-
34
- # @api private
35
- def initialize(s9_serializer)
36
- @s9_serializer = s9_serializer
37
- end
38
-
39
- # @param [Symbol, Saxon::S9API::Serializer::Property] property The property to fetch
40
- def [](property)
41
- s9_serializer.getOutputProperty(resolved_property(property))
42
- end
43
-
44
- # @param [Symbol, Saxon::S9API::Serializer::Property] property The property to set
45
- # @param [String] value The string value of the property
46
- def []=(property, value)
47
- s9_serializer.setOutputProperty(resolved_property(property), value)
48
- end
49
-
50
- # @overload fetch(property)
51
- # @param [Symbol, Saxon::S9API::Serializer::Property] property The property to fetch
52
- # @overload fetch(property, default)
53
- # @param property [Symbol, Saxon::S9API::Serializer::Property] The property to fetch
54
- # @param default [Object] The value to return if the property is unset
55
- def fetch(property, default = nil)
56
- explicit_value = self[property]
57
- if explicit_value.nil? && !default.nil?
58
- default
59
- else
60
- explicit_value
61
- end
62
- end
63
-
64
- private
65
- def resolved_property(property_key)
66
- case property_key
67
- when Symbol
68
- self.class.output_properties.fetch(property_key)
69
- else
70
- property_key
71
- end
72
- end
73
- end
74
-
75
- attr_reader :s9_serializer
76
- private :s9_serializer
77
-
78
- # @api private
79
- def initialize(s9_serializer)
80
- @s9_serializer = s9_serializer
81
- end
82
-
83
- # @return [Saxon::Serializer::OutputProperties] hash-like access to the Output Properties
84
- def output_property
85
- @output_property ||= OutputProperties.new(s9_serializer)
86
- end
87
-
88
- # @overload serialize(xdm_value, io)
89
- # Serialize an XdmValue to an IO
90
- # @param [Saxon::XdmValue] xdm_value The XdmValue to serialize
91
- # @param [File, IO] io The IO to serialize to
92
- # @return [nil]
93
- # @overload serialize(xdm_value, path)
94
- # Serialize an XdmValue to file <tt>path</tt>
95
- # @param [Saxon::XdmValue] xdm_value The XdmValue to serialize
96
- # @param [String, Pathname] path The path of the file to serialize to
97
- # @return [nil]
98
- # @overload serialize(xdm_value)
99
- # Serialize an XdmValue to a String
100
- # @param [Saxon::XdmValue] xdm_value The XdmValue to serialize
101
- # @return [String] The serialized XdmValue
102
- def serialize(xdm_value, io_or_path = nil)
103
- case io_or_path
104
- when nil
105
- serialize_to_string(xdm_value)
106
- when String, Pathname
107
- serialize_to_file(xdm_value, io_or_path)
108
- else
109
- serialize_to_io(xdm_value, io_or_path)
110
- end
111
- end
112
-
113
- # @return [Saxon::S9API::Serializer] The underlying Saxon Serializer object
114
- def to_java
115
- s9_serializer
116
- end
117
-
118
- private
119
-
120
- def serialize_to_io(xdm_value, io)
121
- s9_serializer.setOutputStream(io.to_outputstream)
122
- s9_serializer.serializeXdmValue(xdm_value.to_java)
123
- nil
124
- end
125
-
126
- def serialize_to_string(xdm_value)
127
- str_encoding = output_property.fetch(:encoding, Encoding.default_internal || Encoding.default_external)
128
- StringIO.open { |io|
129
- io.binmode
130
- serialize_to_io(xdm_value, io)
131
- io.string.force_encoding(str_encoding)
132
- }
133
- end
134
-
135
- def serialize_to_file(xdm_value, path)
136
- file = Java::JavaIO::File.new(path)
137
- s9_serializer.setOutputFile(file)
138
- s9_serializer.serializeXdmValue(xdm_value.to_java)
139
- nil
140
- end
141
- end
142
- end
143
-