class MARC::FastXMLWriter
Constants
- OPEN_COLLECTION
- XML_HEADER
Public Class Methods
encode(r, opts={})
click to toggle source
# File lib/marc/fastxmlwriter.rb, line 59 def encode(r, opts={}) xml = "<record>" # MARCXML only allows alphanumerics or spaces in the leader lead = r.leader.gsub(/[^\w|^\s]/, 'Z').encode(:xml=>:text) # MARCXML is particular about last four characters; ILSes aren't lead.ljust(23, ' ')[20..23] = "4500" # MARCXML doesn't like a space here so we need a filler character: Z if (lead[6..6] == " ") lead[6..6] = "Z" end xml << "<leader>" << lead.encode(:xml => :text) << '</leader>' r.each do |f| if f.class == MARC::DataField xml << open_datafield(f.tag, f.indicator1, f.indicator2) f.each do |sf| xml << open_subfield(sf.code) << sf.value.encode(:xml => :text) << '</subfield>' end xml << '</datafield>' elsif f.class == MARC::ControlField xml << open_controlfield(f.tag) << f.value.encode(:xml=>:text) << '</controlfield>' end end xml << '</record>' return xml.force_encoding('utf-8') end
new(file, opts={})
click to toggle source
Calls superclass method
# File lib/marc/fastxmlwriter.rb, line 15 def initialize(file, opts={}) super end
open_collection(use_ns)
click to toggle source
# File lib/marc/fastxmlwriter.rb, line 26 def open_collection(use_ns) if use_ns %Q{<collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:marc="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">}.dup else "<collection>".dup end end
open_controlfield(tag)
click to toggle source
# File lib/marc/fastxmlwriter.rb, line 54 def open_controlfield(tag) # return "\n<controlfield tag=\"#{tag}\">" return "<controlfield tag=\"#{tag}\">" end
open_datafield(tag, ind1, ind2)
click to toggle source
# File lib/marc/fastxmlwriter.rb, line 44 def open_datafield(tag, ind1, ind2) # return "\n <datafield tag=\"#{tag}\" ind1=\"#{ind1}\" ind2=\"#{ind2}\">" return "<datafield tag=\"#{tag}\" ind1=\"#{ind1}\" ind2=\"#{ind2}\">" end
open_subfield(code)
click to toggle source
# File lib/marc/fastxmlwriter.rb, line 49 def open_subfield(code) # return "\n <subfield code=\"#{code}\">" return "<subfield code=\"#{code}\">" end
single_record_document(r, opts={})
click to toggle source
# File lib/marc/fastxmlwriter.rb, line 35 def single_record_document(r, opts={}) xml = XML_HEADER.dup xml << open_collection(opts[:include_namespace]) xml << encode(r, opts) xml << '</collection>' xml end
Public Instance Methods
write(record)
click to toggle source
# File lib/marc/fastxmlwriter.rb, line 19 def write(record) @fh.write(self.class.encode(record)) # @fh.write("\n") end