saxon-rb 0.5.0-java → 0.7.3-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc90ab039f1169f43563e5036774ae66149c78c7b0027ea2b0cb72cafcd0b5e3
4
- data.tar.gz: 18490756712240e5d3869d73dd788ae980178ef3b79ffb41330a8e7db80c1b47
3
+ metadata.gz: c17f2a7367744b11c9ffdcf65e191dee9859cc3887902ce98291e52cea245080
4
+ data.tar.gz: 919c7e5a00c629ecc5d6193ce9f4836b0fb55ae1a4e80673347856d8ad4c5ce1
5
5
  SHA512:
6
- metadata.gz: d20fdd7e7b7fb7842d3d6059bb176903195fc911e46f4ffaecfa22d5dcc4e8919dadce8fe85feb5ab84045484d97f97ed3217330d77a4454a31c0b5efe1fc220
7
- data.tar.gz: c39dd5d7fa31cf2e58cddc002f51d218543f38eb270c10e618105eb559c1467cd338ca68abe317d4574427ef2461c1f3112c7f0acf0e2dd9f9463c67992b8660
6
+ metadata.gz: 5781e2bd70022ed23b2b649de4dcffef94abf400f9d0ab71ee28a8b711f74c7fd4efec02d5a88221ffcd14a99663f9be27413c12de312aa419c217ac26eaf250
7
+ data.tar.gz: 6d13e4918a6140693ec1e3dbcbb07bcefbcac9afa49cfd76edec287254b795bdf49ee91cdee2b08def37258c7e466675dbca6f56c04628c9b4bb1cd77b9e91da
@@ -33,8 +33,11 @@ jobs:
33
33
  command: |-
34
34
  ./cc-test-reporter before-build
35
35
  mkdir -p /tmp/test-results
36
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
37
+ if [ $? -eq 0 ]; then ./cc-test-reporter format-coverage -t simplecov -o "cc-coverage-jar-loading-alt-saxon.json"; fi
38
+ rm -rf coverage
36
39
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
37
- if [ $? -eq 0 ]; then ./cc-test-reporter format-coverage -t simplecov -o "cc-coverage-alt-saxon.json"; fi
40
+ if [ $? -eq 0 ]; then ./cc-test-reporter format-coverage -t simplecov -o "cc-coverage-main-alt-saxon.json"; fi
38
41
  - persist_to_workspace:
39
42
  root: "~/project"
40
43
  paths:
@@ -67,8 +70,11 @@ jobs:
67
70
  command: |-
68
71
  ./cc-test-reporter before-build
69
72
  mkdir -p /tmp/test-results
73
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
74
+ if [ $? -eq 0 ]; then ./cc-test-reporter format-coverage -t simplecov -o "cc-coverage-jar-loading.json"; fi
75
+ rm -rf coverage
70
76
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
71
- if [ $? -eq 0 ]; then ./cc-test-reporter format-coverage -t simplecov -o "cc-coverage.json"; fi
77
+ if [ $? -eq 0 ]; then ./cc-test-reporter format-coverage -t simplecov -o "cc-coverage-main.json"; fi
72
78
  - persist_to_workspace:
73
79
  root: "~/project"
74
80
  paths:
@@ -104,6 +110,8 @@ jobs:
104
110
  name: Run the tests
105
111
  command: |-
106
112
  mkdir -p /tmp/test-results
113
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
114
+ rm -rf coverage
107
115
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
108
116
  - store_test_results:
109
117
  path: "/tmp/test-results"
@@ -127,6 +135,8 @@ jobs:
127
135
  name: Run the tests
128
136
  command: |-
129
137
  mkdir -p /tmp/test-results
138
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
139
+ rm -rf coverage
130
140
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
131
141
  - store_test_results:
132
142
  path: "/tmp/test-results"
@@ -159,6 +169,8 @@ jobs:
159
169
  name: Run the tests
160
170
  command: |-
161
171
  mkdir -p /tmp/test-results
172
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
173
+ rm -rf coverage
162
174
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
163
175
  - store_test_results:
164
176
  path: "/tmp/test-results"
@@ -182,6 +194,8 @@ jobs:
182
194
  name: Run the tests
183
195
  command: |-
184
196
  mkdir -p /tmp/test-results
197
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
198
+ rm -rf coverage
185
199
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
186
200
  - store_test_results:
187
201
  path: "/tmp/test-results"
@@ -214,6 +228,8 @@ jobs:
214
228
  name: Run the tests
215
229
  command: |-
216
230
  mkdir -p /tmp/test-results
231
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
232
+ rm -rf coverage
217
233
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
218
234
  - store_test_results:
219
235
  path: "/tmp/test-results"
@@ -237,6 +253,8 @@ jobs:
237
253
  name: Run the tests
238
254
  command: |-
239
255
  mkdir -p /tmp/test-results
256
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
257
+ rm -rf coverage
240
258
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
241
259
  - store_test_results:
242
260
  path: "/tmp/test-results"
@@ -269,6 +287,8 @@ jobs:
269
287
  name: Run the tests
270
288
  command: |-
271
289
  mkdir -p /tmp/test-results
290
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
291
+ rm -rf coverage
272
292
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
273
293
  - store_test_results:
274
294
  path: "/tmp/test-results"
@@ -292,6 +312,8 @@ jobs:
292
312
  name: Run the tests
293
313
  command: |-
294
314
  mkdir -p /tmp/test-results
315
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
316
+ rm -rf coverage
295
317
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
296
318
  - store_test_results:
297
319
  path: "/tmp/test-results"
@@ -324,6 +346,8 @@ jobs:
324
346
  name: Run the tests
325
347
  command: |-
326
348
  mkdir -p /tmp/test-results
349
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
350
+ rm -rf coverage
327
351
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
328
352
  - store_test_results:
329
353
  path: "/tmp/test-results"
@@ -347,6 +371,8 @@ jobs:
347
371
  name: Run the tests
348
372
  command: |-
349
373
  mkdir -p /tmp/test-results
374
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
375
+ rm -rf coverage
350
376
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
351
377
  - store_test_results:
352
378
  path: "/tmp/test-results"
@@ -379,6 +405,8 @@ jobs:
379
405
  name: Run the tests
380
406
  command: |-
381
407
  mkdir -p /tmp/test-results
408
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
409
+ rm -rf coverage
382
410
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
383
411
  - store_test_results:
384
412
  path: "/tmp/test-results"
@@ -402,6 +430,8 @@ jobs:
402
430
  name: Run the tests
403
431
  command: |-
404
432
  mkdir -p /tmp/test-results
433
+ VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml
434
+ rm -rf coverage
405
435
  bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress
406
436
  - store_test_results:
407
437
  path: "/tmp/test-results"
@@ -0,0 +1,2 @@
1
+ --format progress
2
+ --color
@@ -1 +1 @@
1
- jruby-9.2.6.0
1
+ jruby-9.2.9.0
@@ -0,0 +1 @@
1
+ -e docs/templates/plugin.rb
data/README.md CHANGED
@@ -78,7 +78,7 @@ result_2 = transformer.call_template('main-template')
78
78
  processor = Saxon::Processor.create
79
79
  xpath = processor.xpath_compiler.compile('//element[@attr = $a:var]')
80
80
 
81
- matches = xpath.run(document_node)
81
+ matches = xpath.evaluate(document_node)
82
82
  ```
83
83
 
84
84
  ## Migrating from `saxon-xslt` (or Nokogiri)
@@ -194,6 +194,47 @@ result = xslt.call_template('template-name', {
194
194
 
195
195
  Global and initial template parameters can be set at compiler creation time, compile time, or execution time. See [Setting parameters](#label-Setting+parameters) for details.
196
196
 
197
+ To serialize the document you can, of course, just call `#to_s` on the result:
198
+
199
+ ```ruby
200
+ result = xslt.apply_templates(input)
201
+ puts result.to_s #=> '<?xml version="1.0"...'
202
+ ```
203
+
204
+ You can also serialize directly to a file path or to any IO instance.
205
+
206
+ ```ruby
207
+ result = xslt.apply_templates(input)
208
+ result.serialize('/path/to/output.xml')
209
+
210
+ result_2 = xslt.apply_templates(input)
211
+ result_2.serialize($stderr)
212
+ ```
213
+
214
+ You can override serialization options that were set by `<xsl:output/>` in your XSLT:
215
+
216
+ ```ruby
217
+ result = xslt.apply_templates(input)
218
+ result.serialize('/path/to/output.xml') {
219
+ output_property[:indent] = 'yes'
220
+ }
221
+ ```
222
+
223
+ You can also obtain the result of the transform as an XDM Value:
224
+
225
+ ```ruby
226
+ result = xslt.apply_templates(input)
227
+ result.xdm_value #=> #<Saxon::XDM::Node...>
228
+ ```
229
+
230
+ You also have easy access to provide an instance of a class implementing Saxon's `net.sf.saxon.s9api.Destination` interface:
231
+
232
+ ```ruby
233
+ dom_document = javax.xml.parsers.DocumentBuilderFactory.newInstance.builder.newDocument
234
+ destination = Saxon::S9API::DOMDestination.new(dom_document)
235
+ result = xslt.apply_templates(input).to_destination(destination)
236
+ ```
237
+
197
238
  #### Setting parameters
198
239
 
199
240
  There are four kinds of parameters you can set: *Static parameters*, which are
data/Rakefile CHANGED
@@ -190,12 +190,18 @@ task :circleci do
190
190
  def run_tests_step(opts)
191
191
  command = [
192
192
  "mkdir -p /tmp/test-results",
193
- "bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress"
193
+ "VERIFY_SAXON_LAZY_LOADING=1 bundle exec rspec spec/jar_loading_spec.rb --options .rspec-jar-loading --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec-jar-loading.xml"
194
194
  ]
195
195
  if opts.fetch(:run_codeclimate)
196
196
  command.prepend("./cc-test-reporter before-build")
197
- command.append("if [ $? -eq 0 ]; then ./cc-test-reporter format-coverage -t simplecov -o \"cc-coverage#{"-alt-saxon" if opts.fetch(:alt_saxon_url)}.json\"; fi")
197
+ command.append("if [ $? -eq 0 ]; then ./cc-test-reporter format-coverage -t simplecov -o \"cc-coverage-jar-loading#{"-alt-saxon" if opts.fetch(:alt_saxon_url)}.json\"; fi")
198
198
  end
199
+ command.append("rm -rf coverage")
200
+ command.append("bundle exec rspec spec --profile 10 --format RspecJunitFormatter --out /tmp/test-results/rspec.xml --format progress")
201
+ if opts.fetch(:run_codeclimate)
202
+ command.append("if [ $? -eq 0 ]; then ./cc-test-reporter format-coverage -t simplecov -o \"cc-coverage-main#{"-alt-saxon" if opts.fetch(:alt_saxon_url)}.json\"; fi")
203
+ end
204
+
199
205
  {
200
206
  "run" => {
201
207
  "name" => "Run the tests" + (opts.fetch(:run_codeclimate) ? ", and upload coverage data to Code Climate" : ""),
@@ -0,0 +1,73 @@
1
+ include YARD
2
+ include Templates
3
+
4
+ module JavadocHtmlHelper
5
+ JAVA_TYPE_MATCHER = /\A(?:[a-z_$](?:[a-z0-9_$]*)\.)+[A-Z][A-Za-z_$]*/
6
+ RUBY_COLLECTION_TYPE_MATCHER = /\A(?:[A-Z][A-Za-z0-9_])(?:::[A-Z][A-Za-z0-9_]*)*</
7
+ SAXON_TYPE_MATCHER = /\A(?:net\.sf\.saxon|com\.saxonica)/
8
+
9
+ def format_types(typelist, brackets = true)
10
+ return unless typelist.is_a?(Array)
11
+ list = typelist.map { |type|
12
+ case type
13
+ when JAVA_TYPE_MATCHER
14
+ format_java_type(type)
15
+ else
16
+ super([type], false)
17
+ end
18
+ }
19
+ list.empty? ? "" : (brackets ? "(#{list.join(", ")})" : list.join(", "))
20
+ end
21
+
22
+ def format_java_type(type)
23
+ "<tt>" + linkify_saxon_type(type) + "</tt>"
24
+ end
25
+
26
+ def linkify_saxon_type(type)
27
+ case type
28
+ when SAXON_TYPE_MATCHER
29
+ link = url_for_java_object(type)
30
+ else
31
+ link = nil
32
+ end
33
+ link ? link_url(link, type, :title => h(type)) : type
34
+ end
35
+
36
+ def linkify(*args)
37
+ if args.first.is_a?(String)
38
+ case args.first
39
+ when JAVA_TYPE_MATCHER
40
+ link = url_for_java_object(args.first)
41
+ title = args.first
42
+ link ? link_url(link, title, :title => h(title)) : title
43
+ else
44
+ super
45
+ end
46
+ else
47
+ super
48
+ end
49
+ end
50
+
51
+ def url_for(obj, anchor = nil, relative = true)
52
+ case obj
53
+ when JAVA_TYPE_MATCHER
54
+ url_for_java_object(obj, anchor, relative)
55
+ else
56
+ super
57
+ end
58
+ end
59
+
60
+ def url_for_java_object(obj, anchor = nil, relative = nil)
61
+ case obj
62
+ when SAXON_TYPE_MATCHER
63
+ package, _, klass = obj.rpartition(".")
64
+ "http://saxonica.com/documentation/index.html#!javadoc/#{package}/#{klass}"
65
+ else
66
+ path = obj.split(".").join("/")
67
+ "https://docs.oracle.com/javase/8/docs/api/index.html?#{path}.html"
68
+ end
69
+ end
70
+ end
71
+
72
+ Template.extra_includes << proc { |opts| JavadocHtmlHelper if opts.format == :html }
73
+ # Engine.register_template_path(File.dirname(__FILE__))
@@ -1 +0,0 @@
1
- require_relative 'saxon'
@@ -2,12 +2,13 @@ require 'saxon/s9api'
2
2
  require 'saxon/parse_options'
3
3
 
4
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
5
+ # Wraps the <tt>net.sf.saxon.Configuration</tt> class.
6
+ #
7
+ # See {net.sf.saxon.Configuration} for details of what configuration options
8
+ # are available and what values they accept.
9
+ #
10
+ # See {net.sf.saxon.lib.FeatureKeys} for details of the constant names used to
11
+ # access the values
11
12
  class Configuration
12
13
  DEFAULT_SEMAPHORE = Mutex.new
13
14
  private_constant :DEFAULT_SEMAPHORE
@@ -60,22 +61,23 @@ module Saxon
60
61
  end
61
62
 
62
63
  # Get a configuration option value
63
- # See https://www.saxonica.com/html/documentation/javadoc/net/sf/saxon/lib/FeatureKeys.html
64
- # for details of the available options. Use the constant name as a string
65
- # or symbol as the option
64
+ #
65
+ # See {net.sf.saxon.lib.FeatureKeys} for details of the available options.
66
+ # Use the constant name as a string or symbol as the option, e.g.
67
+ # +:allow_multhreading+, +'ALLOW_MULTITHREADING'+, +'allow_multithreading'+.
66
68
  #
67
69
  # @param option [String, Symbol]
68
70
  # @return [Object] the value of the configuration option
69
71
  # @raise [NameError] if the option name does not exist
72
+ # @see net.sf.saxon.lib.FeatureKeys
70
73
  def [](option)
71
74
  @config.getConfigurationProperty(option_url(option))
72
75
  end
73
76
 
74
- # Get a configuration option value
75
- # See http://saxonica.com/documentation9.5/javadoc/net/sf/saxon/lib/FeatureKeys.html
76
- # for details of the available options. Use the constant name as a string
77
- # or symbol as the option
77
+ # Set a configuration option value. See {#[]} for details about the option
78
+ # names.
78
79
  #
80
+ # @see #[]
79
81
  # @param option [String, Symbol]
80
82
  # @param value [Object] the value of the configuration option
81
83
  # @return [Object] the value you passed in
@@ -4,11 +4,217 @@ module Saxon
4
4
  # Builds XDM objects from XML sources, for use in XSLT or for query and
5
5
  # access
6
6
  class DocumentBuilder
7
+ # Provides a simple configuraion DSL for DocumentBuilders.
8
+ # @see DocumentBuilder.create
9
+ class ConfigurationDSL
10
+ # @api private
11
+ #
12
+ # Create a new instance and +instance_exec+ the passed-in block against it
13
+ def self.define(document_builder, block)
14
+ new(document_builder).instance_exec(&block)
15
+ end
16
+
17
+ # @api private
18
+ def initialize(document_builder)
19
+ @document_builder = document_builder
20
+ end
21
+
22
+ # Sets line numbering on or off
23
+ #
24
+ # @see DocumentBuilder#line_numbering=
25
+ #
26
+ # @param value [Boolean] on (true) or off (false)
27
+ def line_numbering(value)
28
+ @document_builder.line_numbering = value
29
+ end
30
+
31
+ # Sets the base URI of documents created using this instance.
32
+ #
33
+ # @see DocumentBuilder.base_uri=
34
+ #
35
+ # @param value [String, URI::File, URI::HTTP] The (absolute) base URI to use
36
+ def base_uri(value)
37
+ @document_builder.base_uri = value
38
+ end
39
+
40
+ # Sets the base URI of documents created using this instance.
41
+ #
42
+ # @see DocumentBuilder.base_uri=
43
+ #
44
+ # @param value [String, URI::File, URI::HTTP] The (absolute) base URI to use
45
+ def whitespace_stripping_policy(value)
46
+ @document_builder.whitespace_stripping_policy = value
47
+ end
48
+
49
+ # Sets the base URI of documents created using this instance.
50
+ #
51
+ # @see DocumentBuilder.base_uri=
52
+ #
53
+ # @param value [String, URI::File, URI::HTTP] The (absolute) base URI to use
54
+ def dtd_validation(value)
55
+ @document_builder.dtd_validation = value
56
+ end
57
+ end
58
+
59
+
60
+ # Create a new DocumentBuilder that can be used to build new XML documents
61
+ # with the passed-in {Saxon::Processor}. If a block is passed in it's
62
+ # executed as a DSL for configuring the builder instance.
63
+ #
64
+ # @param processor [Saxon::Processor] the Processor
65
+ # @yield An DocumentBuilder configuration DSL block
66
+ # @return [Saxon::DocumentBuilder] the new instance
67
+ def self.create(processor, &block)
68
+ new(processor.to_java.newDocumentBuilder, &block)
69
+ end
70
+
71
+ attr_reader :s9_document_builder
72
+ private :s9_document_builder
73
+
7
74
  # @api private
8
75
  # @param [net.sf.saxon.s9api.DocumentBuilder] s9_document_builder The
9
- # Saxon DocumentBuilder instance to wrap
10
- def initialize(s9_document_builder)
76
+ # Saxon DocumentBuilder instance to wrap
77
+ def initialize(s9_document_builder, &block)
11
78
  @s9_document_builder = s9_document_builder
79
+ if block_given?
80
+ ConfigurationDSL.define(self, block)
81
+ end
82
+ end
83
+
84
+ # Report whether documents created using this instance will keep track of
85
+ # the line and column numbers of elements.
86
+ #
87
+ # @return [Boolean] whether line numbering will be tracked
88
+ def line_numbering?
89
+ s9_document_builder.isLineNumbering
90
+ end
91
+
92
+
93
+ # Switch tracking of line and column numbers for elements in documents
94
+ # created by this instance on or off
95
+ #
96
+ # @see https://www.saxonica.com/documentation9.9/index.html#!javadoc/net.sf.saxon.s9api/DocumentBuilder@setLineNumbering
97
+ #
98
+ # @param on_or_not [Boolean] whether or not to track line numbering
99
+ def line_numbering=(on_or_not)
100
+ s9_document_builder.setLineNumbering(on_or_not)
101
+ end
102
+
103
+ # Return the default base URI to be used when building documents using this
104
+ # instance. This value will be ignored if the source being parsed has an
105
+ # intrinsic base URI (e.g. a File).
106
+ #
107
+ # Returns +nil+ if no URI is set (the default).
108
+ #
109
+ # @return [nil, URI::File, URI::HTTP] the default base URI (or nil)
110
+ def base_uri
111
+ uri = s9_document_builder.getBaseURI
112
+ uri.nil? ? uri : URI(uri.to_s)
113
+ end
114
+
115
+ # Set the base URI of documents created using this instance. This value will
116
+ # be ignored if the source being parsed has an intrinsic base URI (e.g. a
117
+ # File)
118
+ #
119
+ # @see https://www.saxonica.com/documentation9.9/index.html#!javadoc/net.sf.saxon.s9api/DocumentBuilder@setBaseURI
120
+ #
121
+ # @param uri [String, URI::File, URI::HTTP] The (absolute) base URI to use
122
+ def base_uri=(uri)
123
+ s9_document_builder.setBaseURI(java.net.URI.new(uri.to_s))
124
+ end
125
+
126
+ # Return the Whitespace stripping policy for this instance. Returns one of
127
+ # the standard policy names as a symbol, or the custom Java
128
+ # WhitespaceStrippingPolicy if one was defined using
129
+ # +#whitespace_stripping_policy = ->(qname) { ... }+. (See
130
+ # {#whitespace_stripping_policy=} for more.)
131
+ #
132
+ # +:all+: All whitespace-only nodes will be discarded
133
+ #
134
+ # +:none+: No whitespace-only nodes will be discarded (the default if DTD or
135
+ # schema validation is not in effect)
136
+ #
137
+ # +:ignorable+: Whitespace-only nodes inside elements defined as
138
+ # element-only in the DTD or schema being used will be discarded (the
139
+ # default if DTD or schema validation is in effect)
140
+ #
141
+ # +:unspecified+: the default, which in practice means :ignorable if DTD or
142
+ # schema validation is in effect, and :none otherwise.
143
+ #
144
+ # @return [:all, :none, :ignorable, :unspecified, Proc]
145
+ def whitespace_stripping_policy
146
+ s9_policy = s9_document_builder.getWhitespaceStrippingPolicy
147
+ case s9_policy
148
+ when Saxon::S9API::WhitespaceStrippingPolicy::UNSPECIFIED
149
+ :unspecified
150
+ when Saxon::S9API::WhitespaceStrippingPolicy::NONE
151
+ :none
152
+ when Saxon::S9API::WhitespaceStrippingPolicy::IGNORABLE
153
+ :ignorable
154
+ when Saxon::S9API::WhitespaceStrippingPolicy::ALL
155
+ :all
156
+ else
157
+ s9_policy
158
+ end
159
+ end
160
+
161
+ # Set the whitespace stripping policy to be used for documents built with
162
+ # this instance.
163
+ #
164
+ # Possible values are:
165
+ #
166
+ # * One of the standard policies, as a symbol (+:all+, +:none+,
167
+ # +:ignorable+, +:unspecified+, see {#whitespace_stripping_policy}).
168
+ # * A Java +net.sf.saxon.s9api.WhitesapceStrippingPolicy+ instance
169
+ # * A Proc/lambda that is handed an element name as a {Saxon::QName}, and
170
+ # should return true (if whitespace should be stripped for this element)
171
+ # or false (it should not).
172
+ # @example
173
+ # whitespace_stripping_policy = ->(element_qname) {
174
+ # element_qname == Saxon::QName.clark("{http://example.org/}element-name")
175
+ # }
176
+ #
177
+ # @see https://www.saxonica.com/documentation9.9/index.html#!javadoc/net.sf.saxon.s9api/DocumentBuilder@setWhitespaceStrippingPolicy
178
+ # @see https://www.saxonica.com/documentation9.9/index.html#!javadoc/net.sf.saxon.s9api/WhitespaceStrippingPolicy
179
+ # @param policy [Symbol, Proc, Saxon::S9API::WhitespaceStrippingPolicy] the
180
+ # policy to use
181
+ def whitespace_stripping_policy=(policy)
182
+ case policy
183
+ when :unspecified, :none, :ignorable, :all
184
+ s9_policy = Saxon::S9API::WhitespaceStrippingPolicy.const_get(policy.to_s.upcase.to_sym)
185
+ when Proc
186
+ wrapped_policy = ->(s9_qname) {
187
+ policy.call(Saxon::QName.new(s9_qname))
188
+ }
189
+ s9_policy = Saxon::S9API::WhitespaceStrippingPolicy.makeCustomPolicy(wrapped_policy)
190
+ when Saxon::S9API::WhitespaceStrippingPolicy
191
+ s9_policy = policy
192
+ else
193
+ raise InvalidWhitespaceStrippingPolicyError, "#{policy.inspect} is not one of the allowed Symbols, or a custom policy"
194
+ end
195
+ s9_document_builder.setWhitespaceStrippingPolicy(s9_policy)
196
+ end
197
+
198
+ # @return [Boolean] whether DTD Validation is enabled
199
+ def dtd_validation?
200
+ s9_document_builder.isDTDValidation
201
+ end
202
+
203
+ # Switches DTD validation on or off.
204
+ #
205
+ # It's important to note that DTD validation only applies to documents that
206
+ # contain a +<!doctype>+, but switching DTD validation off doesn't stop the
207
+ # XML parser Saxon uses from trying to retrieve the DTD that's referenced,
208
+ # which can mean network requests. By default, the SAX parser Saxon uses
209
+ # (Xerces) doesn't make use of XML catalogs, which causes problems when documents reference a DTD with a relative path as in:
210
+ # <!DOCTYPE root-element SYSTEM "example.dtd">
211
+ # This can be controlled through a configuration option, however.
212
+ #
213
+ # @see https://www.saxonica.com/documentation9.9/index.html#!javadoc/net.sf.saxon.s9api/DocumentBuilder@setDTDValidation
214
+ # @see https://www.saxonica.com/documentation9.9/index.html#!sourcedocs/controlling-parsing
215
+ # @param on [Boolean] whether DTD Validation should be enabled
216
+ def dtd_validation=(on)
217
+ s9_document_builder.setDTDValidation(on)
12
218
  end
13
219
 
14
220
  # @param [Saxon::Source] source The Saxon::Source containing the source
@@ -16,13 +222,18 @@ module Saxon
16
222
  # @return [Saxon::XDM::Node] The Saxon::XDM::Node representing the root of the
17
223
  # document tree
18
224
  def build(source)
19
- XDM::Node.new(@s9_document_builder.build(source.to_java))
225
+ XDM::Node.new(s9_document_builder.build(source.to_java))
20
226
  end
21
227
 
22
- # @return [net.sf.saxon.s9api.DocumentBuilder] The underlying Java Saxon
228
+ # @return [Java::NetSfSaxonS9api::DocumentBuilder] The underlying Java Saxon
23
229
  # DocumentBuilder instance
24
230
  def to_java
25
- @s9_document_builder
231
+ s9_document_builder
26
232
  end
27
233
  end
234
+
235
+ # Error raised when someone tries to set an invalid whitespace stripping
236
+ # policy on a {DocumentBuilder}
237
+ class InvalidWhitespaceStrippingPolicyError < RuntimeError
238
+ end
28
239
  end