Class: BerkeleyLibrary::TIND::MARC::XMLWriter

Inherits:
Object
  • Object
show all
Includes:
Logging, Util::Files
Defined in:
lib/berkeley_library/tind/marc/xml_writer.rb

Constant Summary collapse

UTF_8 =

Constants

Encoding::UTF_8.name
EMPTY_COLLECTION_DOC =
Nokogiri::XML::Builder.new(encoding: UTF_8) do |xml|
  xml.collection(xmlns: ::MARC::MARC_NS)
end.doc.freeze
COLLECTION_CLOSING_TAG =
'</collection>'.freeze
DEFAULT_NOKOGIRI_OPTS =
{ encoding: UTF_8 }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out, **nokogiri_options) ⇒ XMLWriter

Initializes a new BerkeleyLibrary::TIND::MARC::XMLWriter.

File.open('marc.xml', 'wb') do |f|
  w = XMLWriter.new(f)
  marc_records.each { |r| w.write(r) }
  w.close
end

Parameters:

  • out (IO, String)

    an IO, or the name of a file

  • nokogiri_options (Hash)

    Options passed to Nokogiri::XML::Node#write_to Note that the encoding option is ignored, except insofar as passing an encoding other than UTF-8 will raise an ArgumentError.

Raises:

  • ArgumentError if out is not an IO or a string, or is a string referencing a file path that cannot be opened for writing; or if an encoding other than UTF-8 is specified in nokogiri-options

See Also:

  • #open


53
54
55
56
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 53

def initialize(out, **nokogiri_options)
  @nokogiri_options = valid_nokogiri_options(nokogiri_options)
  @out = ensure_io(out)
end

Instance Attribute Details

#nokogiri_optionsObject (readonly)

Returns the value of attribute nokogiri_options.



29
30
31
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 29

def nokogiri_options
  @nokogiri_options
end

#outObject (readonly)


Fields



28
29
30
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 28

def out
  @out
end

Class Method Details

.open(out, **nokogiri_options) {|writer| ... } ⇒ Object

Opens a new BerkeleyLibrary::TIND::MARC::XMLWriter with the specified output destination and Nokogiri options, writes the XML prolog and opening <collection> tag, yields the writer to write one or more MARC records, and closes the writer.

XMLWriter.open('marc.xml') do |w|
  marc_records.each { |r| w.write(r) }
end

Note that unlike initializing a writer with #new and closing it immediately, this will write an XML document with an empty <collection></collection> tag even if no records are written.

Yield Parameters:

See Also:



81
82
83
84
85
86
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 81

def open(out, **nokogiri_options)
  writer = new(out, **nokogiri_options)
  writer.send(:ensure_open!)
  yield writer if block_given?
  writer.close
end

Instance Method Details

#closeObject

Closes the underlying stream. If the XML prolog and opening <collection> tag have already been written, the closing <collection/> tag is written first.



108
109
110
111
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 108

def close
  out.write(COLLECTION_CLOSING_TAG) if @open
  out.close
end

#write(record) ⇒ Object

Writes the specified record to the underlying stream, writing the XML prolog and opening <collection> tag if they have not yet been written.

Parameters:

  • record (::MARC::Record)

    the MARC record to write.

Raises:

  • IOError if the underlying stream has already been closed.



98
99
100
101
102
103
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 98

def write(record)
  ensure_open!
  record_element = XMLBuilder.new(record).build
  record_element.write_to(out, nokogiri_options)
  out.write("\n")
end