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 =
%(<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) ⇒ Object
- .open_collection(use_ns) ⇒ Object
- .open_controlfield(tag) ⇒ Object
- .open_datafield(tag, ind1, ind2) ⇒ Object
- .open_subfield(code) ⇒ Object
- .single_record_document(r, include_namespace: true) ⇒ 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.
12 13 14 |
# File 'lib/marc/fastxmlwriter.rb', line 12 def initialize(file, opts = {}) super end |
Class Method Details
.encode(r) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/marc/fastxmlwriter.rb', line 52 def encode(r) 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.instance_of?(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.instance_of?(MARC::ControlField) xml << open_controlfield(f.tag) << f.value.encode(xml: :text) << "</controlfield>" end end xml << "</record>" xml.force_encoding("utf-8") end |
.open_collection(use_ns) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/marc/fastxmlwriter.rb', line 22 def open_collection(use_ns) if use_ns OPEN_COLLECTION_NAMESPACE.dup else OPEN_COLLECTION.dup end end |
.open_controlfield(tag) ⇒ Object
47 48 49 50 |
# File 'lib/marc/fastxmlwriter.rb', line 47 def open_controlfield(tag) # return "\n<controlfield tag=\"#{tag}\">" "<controlfield tag=\"#{tag}\">" end |
.open_datafield(tag, ind1, ind2) ⇒ Object
38 39 40 |
# File 'lib/marc/fastxmlwriter.rb', line 38 def open_datafield(tag, ind1, ind2) "<datafield tag=\"#{tag}\" ind1=\"#{ind1}\" ind2=\"#{ind2}\">" end |
.open_subfield(code) ⇒ Object
42 43 44 45 |
# File 'lib/marc/fastxmlwriter.rb', line 42 def open_subfield(code) # return "\n <subfield code=\"#{code}\">" "<subfield code=\"#{code}\">" end |
.single_record_document(r, include_namespace: true) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/marc/fastxmlwriter.rb', line 30 def single_record_document(r, include_namespace: true) xml = XML_HEADER.dup xml << open_collection(include_namespace) xml << encode(r) xml << "</collection>" xml end |
Instance Method Details
#write(record) ⇒ Object
16 17 18 19 |
# File 'lib/marc/fastxmlwriter.rb', line 16 def write(record) @fh.write(self.class.encode(record)) # @fh.write("\n") end |