Class: MARC::FastXMLWriter
- Inherits:
-
XMLWriter
- Object
- XMLWriter
- MARC::FastXMLWriter
- Defined in:
- lib/marc/fastxmlwriter.rb
Constant Summary collapse
- XML_HEADER =
'<?xml version="1.0" encoding="UTF-8"?>'- OPEN_COLLECTION =
"<collection"> OPEN_COLLECTION_NAMESPACE = %Q{<collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">}
Class Method Summary collapse
- .encode(r, opts = {}) ⇒ Object
- .open_collection(use_ns) ⇒ Object
- .open_controlfield(tag) ⇒ Object
- .open_datafield(tag, ind1, ind2) ⇒ Object
- .open_subfield(code) ⇒ Object
- .single_record_document(r, opts = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(file, opts = {}) ⇒ FastXMLWriter
constructor
A new instance of FastXMLWriter.
- #write(record) ⇒ Object
Constructor Details
#initialize(file, opts = {}) ⇒ FastXMLWriter
Returns a new instance of FastXMLWriter.
15 16 17 |
# File 'lib/marc/fastxmlwriter.rb', line 15 def initialize(file, opts={}) super end |
Class Method Details
.encode(r, opts = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# 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 |
.open_collection(use_ns) ⇒ Object
26 27 28 29 30 31 32 |
# 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) ⇒ Object
54 55 56 57 |
# 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) ⇒ Object
44 45 46 47 |
# 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) ⇒ Object
49 50 51 52 |
# 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 = {}) ⇒ Object
35 36 37 38 39 40 41 42 |
# 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 |
Instance Method Details
#write(record) ⇒ Object
19 20 21 22 |
# File 'lib/marc/fastxmlwriter.rb', line 19 def write(record) @fh.write(self.class.encode(record)) # @fh.write("\n") end |