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.
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
3
- require 'jars/installer'
4
-
5
- RSpec::Core::RakeTask.new(:spec)
6
-
7
- task :default => :spec
8
-
9
- desc "Install JAR dependencies"
10
- task :install_jars do
11
- Jars::Installer.vendor_jars!
12
- end
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "saxon"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,6 +0,0 @@
1
- require_relative 'saxon/version'
2
- require_relative 'saxon/processor'
3
-
4
- # An idiomatic Ruby wrapper around the Saxon XML processing library.
5
- module Saxon
6
- end
@@ -1,32 +0,0 @@
1
- require 'saxon/s9api'
2
- require 'saxon/xdm_node'
3
-
4
- module Saxon
5
- # An XPath Data Model Node object, representing an XML document, or an element or one of the other node chunks in the XDM.
6
- class AxisIterator
7
- include Enumerable
8
-
9
- attr_reader :s9_xdm_node, :s9_axis
10
- private :s9_xdm_node, :s9_axis
11
-
12
- def initialize(xdm_node, axis)
13
- @s9_xdm_node = xdm_node.to_java
14
- @s9_axis = Saxon::S9API::Axis.const_get(axis.to_s.upcase.to_sym)
15
- end
16
-
17
- # @return [Saxon::S9API::XdmSequenceIterator] A new Saxon Java XDM sequence iterator.
18
- def to_java
19
- s9_sequence_iterator
20
- end
21
-
22
- def each(&block)
23
- s9_sequence_iterator.lazy.map { |s9_xdm_node| Saxon::XdmNode.new(s9_xdm_node) }.each(&block)
24
- end
25
-
26
- private
27
-
28
- def s9_sequence_iterator
29
- s9_xdm_node.axisIterator(s9_axis)
30
- end
31
- end
32
- end
@@ -1,116 +0,0 @@
1
- require 'saxon/s9api'
2
- require 'saxon/parse_options'
3
-
4
- module Saxon
5
- # Wraps the <tt>net.saxon.Configuration</tt> class. See
6
- # http://saxonica.com/documentation9.5/javadoc/net/sf/saxon/Configuration.html
7
- # for details of what configuration options are available and what values
8
- # they accept. See
9
- # http://saxonica.com/documentation9.5/javadoc/net/sf/saxon/lib/FeatureKeys.html
10
- # for details of the constant names used to access the values
11
- class Configuration
12
- DEFAULT_SEMAPHORE = Mutex.new
13
-
14
- # Provides a processor with default configuration. Essentially a singleton
15
- # instance
16
- # @return [Saxon::Configuration]
17
- def self.default
18
- DEFAULT_SEMAPHORE.synchronize do
19
- @config ||= create
20
- end
21
- end
22
-
23
- # @param processor [Saxon::Processor] a Saxon::Processor instance
24
- # @return [Saxon::Configuration]
25
- def self.create(processor = nil)
26
- Saxon::Loader.load!
27
- if processor
28
- config = processor.to_java.underlying_configuration
29
- else
30
- config = Saxon::S9API::Configuration.new
31
- end
32
- new(config)
33
- end
34
-
35
- # @param license_path [String] the absolute path to a Saxon PE or EE
36
- # license file
37
- # @return [Saxon::Configuration]
38
- def self.create_licensed(license_path)
39
- Saxon::Loader.load!
40
- java_config = Saxon::S9API::Configuration.makeLicensedConfiguration(nil, nil)
41
- config = new(java_config)
42
- config[:LICENSE_FILE_LOCATION] = license_path
43
- config
44
- end
45
-
46
- # Set a already-created Licensed Configuration object as the default
47
- # configuration
48
- def self.set_licensed_default!(licensed_configuration)
49
- DEFAULT_SEMAPHORE.synchronize do
50
- @config = licensed_configuration
51
- end
52
- end
53
-
54
- # @api private
55
- # @param config [net.sf.saxon.Configuration] The Saxon Configuration
56
- # instance to wrap
57
- def initialize(config)
58
- @config = config
59
- end
60
-
61
- # Get a configuration option value
62
- # See https://www.saxonica.com/html/documentation/javadoc/net/sf/saxon/lib/FeatureKeys.html
63
- # for details of the available options. Use the constant name as a string
64
- # or symbol as the option
65
- #
66
- # @param option [String, Symbol]
67
- # @return [Object] the value of the configuration option
68
- # @raise [NameError] if the option name does not exist
69
- def [](option)
70
- @config.getConfigurationProperty(option_url(option))
71
- end
72
-
73
- # Get a configuration option value
74
- # See http://saxonica.com/documentation9.5/javadoc/net/sf/saxon/lib/FeatureKeys.html
75
- # for details of the available options. Use the constant name as a string
76
- # or symbol as the option
77
- #
78
- # @param option [String, Symbol]
79
- # @param value [Object] the value of the configuration option
80
- # @return [Object] the value you passed in
81
- # @raise [NameError] if the option name does not exist
82
- def []=(option, value)
83
- url = option_url(option)
84
- @config.setConfigurationProperty(url, value)
85
- end
86
-
87
- # Return the current ParseOptions object
88
- # See https://www.saxonica.com/documentation/index.html#!javadoc/net.sf.saxon.lib/ParseOptions
89
- # for details. This allows you to set JAXP features and properties to be
90
- # passed to the parser
91
- #
92
- # @return [Saxon::ParseOptions] the ParseOptions object
93
- def parse_options
94
- ParseOptions.new(@config.getParseOptions)
95
- end
96
-
97
- # @return [net.sf.saxon.Configuration] The underlying Saxon Configuration
98
- def to_java
99
- @config
100
- end
101
-
102
- private
103
-
104
- def feature_keys
105
- @feature_keys ||= Saxon::S9API::FeatureKeys.java_class
106
- end
107
-
108
- def option_url(option)
109
- feature_keys.field(normalize_option_name(option)).static_value
110
- end
111
-
112
- def normalize_option_name(option)
113
- option.to_s.upcase
114
- end
115
- end
116
- end
@@ -1,28 +0,0 @@
1
- require 'saxon/xdm_node'
2
-
3
- module Saxon
4
- # Builds XDM objects from XML sources, for use in XSLT or for query and
5
- # access
6
- class DocumentBuilder
7
- # @api private
8
- # @param [net.sf.saxon.s9api.DocumentBuilder] s9_document_builder The
9
- # Saxon DocumentBuilder instance to wrap
10
- def initialize(s9_document_builder)
11
- @s9_document_builder = s9_document_builder
12
- end
13
-
14
- # @param [Saxon::Source] source The Saxon::Source containing the source
15
- # IO/string
16
- # @return [Saxon::XdmNode] The Saxon::XdmNode representing the root of the
17
- # document tree
18
- def build(source)
19
- XdmNode.new(@s9_document_builder.build(source.to_java))
20
- end
21
-
22
- # @return [net.sf.saxon.s9api.DocumentBuilder] The underlying Java Saxon
23
- # DocumentBuilder instance
24
- def to_java
25
- @s9_document_builder
26
- end
27
- end
28
- end
@@ -1,200 +0,0 @@
1
- require_relative 's9api'
2
-
3
- module Saxon
4
- # Represent XDM types abstractly
5
- class ItemType
6
- # A mapping of Ruby types to XDM type constants
7
- TYPE_MAPPING = {
8
- 'String' => :STRING,
9
- 'Array' => :ANY_ARRAY,
10
- 'Hash' => :ANY_MAP,
11
- 'TrueClass' => :BOOLEAN,
12
- 'FalseClass' => :BOOLEAN,
13
- 'Date' => :DATE,
14
- 'DateTime' => :DATE_TIME,
15
- 'Time' => :DATE_TIME,
16
- 'BigDecimal' => :DECIMAL,
17
- 'Integer' => :INTEGER,
18
- 'Float' => :FLOAT,
19
- 'Numeric' => :NUMERIC
20
- }.freeze
21
-
22
- # A mapping of type names/QNames to XDM type constants
23
- STR_MAPPING = {
24
- 'array(*)' => :ANY_ARRAY,
25
- 'xs:anyAtomicType' => :ANY_ATOMIC_VALUE,
26
- 'item()' => :ANY_ITEM,
27
- 'map(*)' => :ANY_MAP,
28
- 'node()' => :ANY_NODE,
29
- 'xs:anyURI' => :ANY_URI,
30
- 'xs:base64Binary' => :BASE64_BINARY,
31
- 'xs:boolean' => :BOOLEAN,
32
- 'xs:byte' => :BYTE,
33
- 'xs:date' => :DATE,
34
- 'xs:dateTime' => :DATE_TIME,
35
- 'xs:dateTimeStamp' => :DATE_TIME_STAMP,
36
- 'xs:dayTimeDuration' => :DAY_TIME_DURATION,
37
- 'xs:decimal' => :DECIMAL,
38
- 'xs:double' => :DOUBLE,
39
- 'xs:duration' => :DURATION,
40
- 'xs:ENTITY' => :ENTITY,
41
- 'xs:float' => :FLOAT,
42
- 'xs:gDay' => :G_DAY,
43
- 'xs:gMonth' => :G_MONTH,
44
- 'xs:gMonthDay' => :G_MONTH_DAY,
45
- 'xs:gYear' => :G_YEAR,
46
- 'xs:gYearMonth' => :G_YEAR_MONTH,
47
- 'xs:hexBinary' => :HEX_BINARY,
48
- 'xs:ID' => :ID,
49
- 'xs:IDREF' => :IDREF,
50
- 'xs:int' => :INT,
51
- 'xs:integer' => :INTEGER,
52
- 'xs:language' => :LANGUAGE,
53
- 'xs:long' => :LONG,
54
- 'xs:Name' => :NAME,
55
- 'xs:NCName' => :NCNAME,
56
- 'xs:negativeInteger' => :NEGATIVE_INTEGER,
57
- 'xs:NMTOKEN' => :NMTOKEN,
58
- 'xs:nonNegativeInteger' => :NON_NEGATIVE_INTEGER,
59
- 'xs:nonPositiveInteger' => :NON_POSITIVE_INTEGER,
60
- 'xs:normalizedString' => :NORMALIZED_STRING,
61
- 'xs:NOTATION' => :NOTATION,
62
- 'xs:numeric' => :NUMERIC,
63
- 'xs:positiveInteger' => :POSITIVE_INTEGER,
64
- 'xs:QName' => :QNAME,
65
- 'xs:short' => :SHORT,
66
- 'xs:string' => :STRING,
67
- 'xs:time' => :TIME,
68
- 'xs:token' => :TOKEN,
69
- 'xs:unsignedByte' => :UNSIGNED_BYTE,
70
- 'xs:unsignedInt' => :UNSIGNED_INT,
71
- 'xs:unsignedLong' => :UNSIGNED_LONG,
72
- 'xs:unsignedShort' => :UNSIGNED_SHORT,
73
- 'xs:untypedAtomic' => :UNTYPED_ATOMIC,
74
- 'xs:yearMonthDuration' => :YEAR_MONTH_DURATION
75
- }.freeze
76
-
77
- class << self
78
- # Get an appropriate {ItemType} for a Ruby type or given a type name as a
79
- # string
80
- #
81
- # @return [Saxon::ItemType]
82
- # @overload get_type(ruby_class)
83
- # Get an appropriate {ItemType} for object of a given Ruby class
84
- # @param ruby_class [Class] The Ruby class to get a type for
85
- # @overload get_type(type_name)
86
- # Get the {ItemType} for the name
87
- # @param type_name [String] name of the built-in {ItemType} to fetch
88
- def get_type(arg)
89
- new(get_s9_type(arg))
90
- end
91
-
92
- private
93
-
94
- def get_s9_type(arg)
95
- case arg
96
- when Class
97
- get_s9_class_mapped_type(arg)
98
- when String
99
- get_s9_str_mapped_type(arg)
100
- end
101
- end
102
-
103
- def get_s9_class_mapped_type(klass)
104
- class_name = klass.name
105
- if mapped_type = TYPE_MAPPING.fetch(class_name, false)
106
- S9API::ItemType.const_get(mapped_type)
107
- else
108
- raise UnmappedRubyTypeError, class_name
109
- end
110
- end
111
-
112
- def get_s9_str_mapped_type(type_str)
113
- if mapped_type = STR_MAPPING.fetch(type_str, false)
114
- # ANY_ITEM is a method, not a constant, for reasons not entirely
115
- # clear to me
116
- return S9API::ItemType.ANY_ITEM if mapped_type == :ANY_ITEM
117
- S9API::ItemType.const_get(mapped_type)
118
- else
119
- raise UnmappedXSDTypeNameError, type_str
120
- end
121
- end
122
- end
123
-
124
- attr_reader :s9_item_type
125
- private :s9_item_type
126
-
127
- # @api private
128
- def initialize(s9_item_type)
129
- @s9_item_type = s9_item_type
130
- end
131
-
132
- # Return the {QName} which represents this type
133
- #
134
- # @return [Saxon::QName] the {QName} of the type
135
- def type_name
136
- @type_name ||= Saxon::QName.new(s9_item_type.getTypeName)
137
- end
138
-
139
- # @return [Saxon::S9API::ItemType] The underlying Saxon Java ItemType object
140
- def to_java
141
- s9_item_type
142
- end
143
-
144
- # compares two {ItemType}s using the underlying Saxon and XDM comparision rules
145
- # @param other [Saxon::ItemType]
146
- # @return [Boolean]
147
- def ==(other)
148
- return false unless other.is_a?(ItemType)
149
- s9_item_type.equals(other.to_java)
150
- end
151
-
152
- alias_method :eql?, :==
153
-
154
- def hash
155
- @hash ||= s9_item_type.hashCode
156
- end
157
-
158
- # Error raised when a Ruby class has no equivalent XDM type to be converted
159
- # into
160
- class UnmappedRubyTypeError < StandardError
161
- def initialize(class_name)
162
- @class_name = class_name
163
- end
164
-
165
- def to_s
166
- "Ruby class <#{@class_name}> has no XDM type equivalent"
167
- end
168
- end
169
-
170
- # Error raise when an attempt to reify an <tt>xs:*</tt> type string is
171
- # made, but the type string doesn't match any of the built-in <tt>xs:*</tt>
172
- # types
173
- class UnmappedXSDTypeNameError < StandardError
174
- def initialize(type_str)
175
- @type_str = type_str
176
- end
177
-
178
- def to_s
179
- "'#{@type_str}' is not recognised as an XSD built-in type"
180
- end
181
- end
182
-
183
- class Factory
184
- DEFAULT_SEMAPHORE = Mutex.new
185
-
186
- attr_reader :processor
187
-
188
- def initialize(processor)
189
- @processor = processor
190
- end
191
-
192
- def s9_factory
193
- return @s9_factory if instance_variable_defined?(:@s9_factory)
194
- DEFAULT_SEMAPHORE.synchronize do
195
- @s9_factory = S9API::ItemTypeFactory.new(processor.to_java)
196
- end
197
- end
198
- end
199
- end
200
- end
@@ -1,8 +0,0 @@
1
- require 'java'
2
-
3
- module Saxon
4
- # an easy-to-access place to keep JAXP-related Java classes
5
- module JAXP
6
- include_package 'javax.xml.transform.stream'
7
- end
8
- end
@@ -1,103 +0,0 @@
1
- require 'pathname'
2
- require 'java'
3
-
4
- module Saxon
5
- # A sensible namespace to put Saxon Java classes into
6
- module S9API
7
- end
8
-
9
- module Loader
10
- LOAD_SEMAPHORE = Mutex.new
11
-
12
- # Error raised if Saxon::Loader.load! is called but the path handed
13
- # in does not exist or is not a directory
14
- class NoJarsError < StandardError
15
- def initialize(path)
16
- @path = path
17
- end
18
-
19
- def to_s
20
- "The path ('#{@path}') you supplied for the Saxon .jar files doesn't exist, sorry"
21
- end
22
- end
23
-
24
- # Error raised if Saxon::Loader.load! is called but the path handed
25
- # in does not contain the Saxon .jars
26
- class MissingJarError < StandardError
27
- def initialize(path)
28
- @path = path
29
- end
30
-
31
- def to_s
32
- "One of saxon9he.jar, saxon9pe.jar, or saxon9ee.jar must be present in the path ('#{@path}') you supplied, sorry"
33
- end
34
- end
35
-
36
- # @param saxon_home [String, Pathname] the path to the dir containing
37
- # Saxon's .jars. Defaults to the vendored Saxon HE
38
- # @return [true, false] Returns true if Saxon had not been loaded and
39
- # is now, and false if Saxon was already loaded
40
- def self.load!(saxon_home = nil)
41
- return false if @saxon_loaded
42
- LOAD_SEMAPHORE.synchronize do
43
- if Saxon::S9API.const_defined?(:Processor)
44
- false
45
- else
46
- if jars_not_on_classpath?
47
- if saxon_home.nil?
48
- require 'saxon_jars'
49
- else
50
- saxon_home = Pathname.new(saxon_home)
51
- raise NoJarsError, saxon_home unless saxon_home.directory?
52
- jars = [main_jar(saxon_home)].compact
53
- raise MissingJarError if jars.empty?
54
- jars += extra_jars(saxon_home)
55
-
56
- add_jars_to_classpath!(saxon_home, jars)
57
- end
58
- end
59
- import_classes_to_namespace!
60
-
61
- @saxon_loaded = true
62
- true
63
- end
64
- end
65
- end
66
-
67
- private
68
-
69
- def self.main_jar(path)
70
- ['saxon9he.jar', 'saxon9pe.jar', 'saxon9ee.jar'].map { |jar| path.join(jar) }.find { |jar| jar.file? }
71
- end
72
-
73
- def self.extra_jars(path)
74
- optional = ['saxon9-unpack.jar', 'saxon9-sql.jar'].map { |jar| path.join(jar) }.select { |jar| jar.file? }
75
- icu = path.children.find { |jar| jar.extname == '.jar' && !jar.basename.to_s.match(/^saxon-icu|^icu4j/).nil? }
76
- ([icu] + optional).compact
77
- end
78
-
79
- def self.jars_not_on_classpath?
80
- begin
81
- Java::net.sf.saxon.s9api.Processor
82
- false
83
- rescue
84
- true
85
- end
86
- end
87
-
88
- def self.add_jars_to_classpath!(saxon_home, jars)
89
- jars.each do |jar|
90
- $CLASSPATH << jar.to_s
91
- end
92
- end
93
-
94
- def self.import_classes_to_namespace!
95
- Saxon::S9API.class_eval do
96
- include_package 'net.sf.saxon.s9api'
97
- java_import 'net.sf.saxon.Configuration'
98
- java_import 'net.sf.saxon.lib.FeatureKeys'
99
- java_import 'net.sf.saxon.lib.ParseOptions'
100
- end
101
- end
102
- end
103
- end