Class: Saxon::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/saxon/processor.rb

Overview

Saxon::Processor wraps the S9API::Processor object. This is the object responsible for creating an XSLT compiler or an XML Document object.

The Processor is threadsafe, and can be shared between threads. But, most importantly XSLT or XML objects created by a Processor can only be used with other XSLT or XML objects created by the same Processor instance.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s9_processor) ⇒ Processor

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 Processor.

Parameters:

  • s9_processor (net.sf.saxon.s9api.Processor)

    The Saxon Processor instance to wrap



45
46
47
# File 'lib/saxon/processor.rb', line 45

def initialize(s9_processor)
  @s9_processor = s9_processor
end

Class Method Details

.create(config = nil) ⇒ Saxon::Processor

Parameters:

  • config (File, String, IO, Saxon::Configuration) (defaults to: nil)

    an open File, or string, containing a Saxon configuration file; an existing Saxon::Configuration object

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/saxon/processor.rb', line 28

def self.create(config = nil)
  Saxon::Loader.load!
  case config
  when nil
    licensed_or_config_source = false
  when Saxon::Configuration
    licensed_or_config_source = config.to_java
  else
    licensed_or_config_source = Saxon::Source.create(config).to_java
  end
  s9_processor = S9API::Processor.new(licensed_or_config_source)
  new(s9_processor)
end

.defaultSaxon::Processor

Provides a processor with default configuration. Essentially a singleton instance

Returns:



20
21
22
# File 'lib/saxon/processor.rb', line 20

def self.default
  @processor ||= create(Saxon::Configuration.default)
end

Instance Method Details

#==(other) ⇒ Object

compare equal if the underlying java processor is the same instance for self and other

Parameters:

  • other

    object to compare against



107
108
109
# File 'lib/saxon/processor.rb', line 107

def ==(other)
  other.to_java === to_java
end

#configSaxon::Configuration

Returns This processor’s configuration instance.

Returns:



112
113
114
# File 'lib/saxon/processor.rb', line 112

def config
  @config ||= Saxon::Configuration.create(self)
end

#declare_collations(collations) ⇒ Object

Declare custom collations for use by XSLT, XPath, and XQuery processors

Parameters:

  • collations (Hash<String => java.text.Collator>)

    collations to declare, as a hash of URI => Collator



63
64
65
66
67
# File 'lib/saxon/processor.rb', line 63

def declare_collations(collations)
  collations.each do |uri, collation|
    @s9_processor.declareCollation(uri, collation)
  end
end

#document_builder { ... } ⇒ Saxon::DocumentBuilder

Generate a new DocumentBuilder that uses this Processor. Sharing DocumentBuilders across threads is not safe.

Yields:

Returns:



55
56
57
# File 'lib/saxon/processor.rb', line 55

def document_builder(&block)
  Saxon::DocumentBuilder.create(self, &block)
end

#serializer { ... } ⇒ Saxon::Serializer::Object

Generate a new Serializer for directly serializing XDM Values that uses this Processor. Serializers are effectively one-shot objects, and shouldn’t be reused.

Yields:

Returns:



95
96
97
# File 'lib/saxon/processor.rb', line 95

def serializer(&block)
  Saxon::Serializer::Object.create(self, &block)
end

#to_javanet.sf.saxon.s9api.Processor

Returns The underlying Saxon processor.

Returns:

  • (net.sf.saxon.s9api.Processor)

    The underlying Saxon processor



100
101
102
# File 'lib/saxon/processor.rb', line 100

def to_java
  @s9_processor
end

#XML(input, opts = {}) ⇒ Saxon::XDM::Node

Create a DocumentBuilder and construct a Source and parse some XML. The args are passed to Source.create, and the returned Source is parsed using DocumentBuilder#build. If a Source is passed in, parse that. Any options in opts will be ignored in that case.

Parameters:

  • input (Saxon::Source, IO, File, String, Pathname, URI)

    the input to be turned into a Source and parsed.

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

    for Source creation. See Source.create.

Returns:



125
126
127
128
# File 'lib/saxon/processor.rb', line 125

def XML(input, opts = {})
  source = Source.create(input, opts)
  document_builder.build(source)
end

#xpath_compiler { ... } ⇒ Saxon::XPath::Compiler

Generate a new XPath::Compiler that uses this Processor. Sharing XPath::Compilers across threads is fine as long as the static context is not changed.

Yields:

Returns:



74
75
76
# File 'lib/saxon/processor.rb', line 74

def xpath_compiler(&block)
  Saxon::XPath::Compiler.create(self, &block)
end

#XSLT(input, opts = {}) { ... } ⇒ Saxon::XSLT::Executable

Construct a Source containing an XSLT stylesheet, create an XSLT::Compiler, and compile the source, returning the XSLT::Executable produced. If a Source is passed as input, then it will be passed through to the compiler and any source-related options in opts will be ignored.

Parameters:

  • input (Saxon::Source, IO, File, String, Pathname, URI)

    the input to be turned into a Source and parsed.

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

    for Source creation. See Source.create.

Yields:

Returns:



142
143
144
145
# File 'lib/saxon/processor.rb', line 142

def XSLT(input, opts = {}, &block)
  source = Source.create(input, opts)
  xslt_compiler(&block).compile(source)
end

#xslt_compiler { ... } ⇒ Saxon::XSLT::Compiler

Generate a new XSLT::Compiler that uses this Processor. Sharing XSLT::Compilers across threads is fine as long as the static context is not changed.

Yields:

Returns:



84
85
86
# File 'lib/saxon/processor.rb', line 84

def xslt_compiler(&block)
  Saxon::XSLT::Compiler.create(self, &block)
end