class IsoBibItem::XMLParser

Public Class Methods

from_xml(xml) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 6
def from_xml(xml)
  doc = Nokogiri::XML(xml)
  IsoBibliographicItem.new( 
    docid:        fetch_docid(doc),
    edition:      doc.at('/bibitem/edition')&.text,
    language:     doc.xpath('/bibitem/language').map(&:text),
    script:       doc.xpath('/bibitem/script').map(&:text),
    titles:       fetch_titles(doc),
    type:         doc.at('bibitem')&.attr(:type),
    docstatus:    fetch_status(doc),
    ics:          fetch_ics(doc),
    dates:        fetch_dates(doc),
    contributors: fetch_contributors(doc),
    workgroup:    fetch_workgroup(doc),
    abstract:     fetch_abstract(doc),
    copyright:    fetch_copyright(doc),
    link:         fetch_link(doc),
    relations:    fetch_relations(doc)
  )
end

Private Class Methods

fetch_abstract(doc) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 174
def fetch_abstract(doc)
  doc.xpath('/bibitem/abstract').map do |a|
    FormattedString.new(content: a.text, language: a[:language],
                        script: a[:script], type: a[:format])
  end
end
fetch_contributors(doc) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 145
def fetch_contributors(doc)
  doc.xpath("/bibitem/contributor").map do |c|
    entity = if (org = c.at "./organization") then get_org(org)
             elsif (person = c.at "./person") then get_person(person)
             end
    role_descr = c.xpath("./role/description").map &:text
    IsoBibItem::ContributionInfo.new entity: entity,
                                     role: [[c.at("role")[:type], role_descr]]
  end
end
fetch_dates(doc) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 89
def fetch_dates(doc)
  doc.xpath('/bibitem/date').map do |d|
    BibliographicDate.new(type: d[:type], on: d.at('on')&.text,
                          from: d.at('from')&.text,
                          to: d.at('to')&.text)
  end
end
fetch_docid(doc) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 33
def fetch_docid(doc)
  ret = []
  doc.xpath("/bibitem/docidentifier").each do |did|
    #did = doc.at('/bibitem/docidentifier')
    type = did.at("./@type")
    if did.text == "IEV" then ret << IsoBibItem::IsoDocumentId.new(project_number: "IEV", part_number: nil, prefix: nil)
    else
      id = get_id did
      ret << IsoBibItem::IsoDocumentId.new(project_number: id.nil? ? did.text : id[:project],
                                           part_number:    (id.nil? || !id.names.include?("part")) ? nil : id[:part],
                                           prefix:         nil,
                                           id:             did.text,
                                           type:           type&.text)
    end
  end
  ret
end
fetch_ics(doc) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 85
def fetch_ics(doc)
  doc.xpath('/bibitem/ics/code').map { |ics| Ics.new ics.text }
end
fetch_relations(doc) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 200
def fetch_relations(doc)
  doc.xpath("/bibitem/relation").map do |r|
    localities = r.xpath("./locality").map do |l|
      ref_to = (rt = l.at("./referenceTo")) ? LocalizedString.new(rt.text) : nil
      BibItemLocality.new(
        l[:type],
        LocalizedString.new(l.at("./referenceFrom").text),
        ref_to
      )
    end
    DocumentRelation.new(
      type: r[:type],
      identifier: r&.at("./bibitem/formattedref | ./bibitem/docidentifier")&.text,
      bib_locality: localities,
    )
  end
end
fetch_status(doc) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 76
def fetch_status(doc)
  status    = doc.at('/bibitem/status')
  stage     = status&.at('stage')&.text
  substage  = status&.at('substage')&.text
  iteration = status&.at('iterarion')&.text&.to_i
  IsoDocumentStatus.new(status: status&.text, stage: stage,
                        substage: substage, iteration: iteration)
end
fetch_titles(doc) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 51
def fetch_titles(doc)
  doc.xpath("/bibitem/title").map do |t|
    titl = t.text.sub("[ -- ]", "").split " -- "
    case titl.size
    when 0
      intro, main, part = nil, "", nil
    when 1
      intro, main, part = nil, titl[0], nil
    when 2
      if /^(Part|Partie) \d+:/.match titl[1]
        intro, main, part = nil, titl[0], titl[1]
      else
        intro, main, part = titl[0], titl[1], nil
      end
    when 3
      intro, main, part = titl[0], titl[1], titl[2]
    else
      intro, main, part = titl[0], titl[1], titl[2..-1]&.join(" -- ")
    end
    IsoLocalizedTitle.new(title_intro: intro, title_main: main,
                          title_part: part, language: t[:language],
                          script: t[:script])
  end
end
fetch_workgroup(doc) click to toggle source

@TODO Organization doesn't recreated

# File lib/iso_bib_item/xml_parser.rb, line 157
def fetch_workgroup(doc)
  eg = doc.at('/bibitem/editorialgroup')
  tc = eg&.at('technical_committee')
  sc = eg&.at('subcommittee')
  scom = iso_subgroup(sc)
  wg   = eg&.at('workgroup')
  wgrp = iso_subgroup(wg)
  IsoProjectGroup.new(technical_committee: iso_subgroup(tc),
                      subcommittee: scom, workgroup: wgrp)
end
get_id(did) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 29
def get_id(did)
  did.text.match(/^(?<project>.*?\d+)(?<hyphen>-)?(?(<hyphen>)(?<part>\d*))/)
end
get_org(org) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 97
def get_org(org)
  names = org.xpath("name").map do |n|
    { content: n.text, language: n[:language], script: n[:script] }
  end
  identifiers = org.xpath("./identifier").map do |i|
    IsoBibItem::OrgIdentifier.new(i[:type], i.text)
  end
  IsoBibItem::Organization.new(name: names,
                               abbreviation: org.at("abbreviation")&.text,
                               url: org.at("uri")&.text,
                               identifiers: identifiers)
end
get_person(person) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 110
def get_person(person)
  name = person.at "./name/completename"
  affilations = person.xpath("./affiliation").map do |a|
    org = a.at "./organization"
    IsoBibItem::Affilation.new get_org(org)
  end

  contacts = person.xpath("./address | ./phone | ./email | ./uri").map do |contact|
    if contact.name == "address"
      streets = contact.xpath("./street").map(&:text)
      IsoBibItem::Address.new(
        street: streets,
        city: contact.at("./city").text,
        state: contact.at("./state").text,
        country: contact.at("./country").text,
        postcode: contact.at("./postcode").text,
      )
    else
      IsoBibItem::Contact.new(type: contact.name, value: contact.text)
    end
  end

  identifiers = person.xpath("./identifier").map do |pi|
    IsoBibItem::PersonIdentifier.new pi[:type], pi.text
  end

  completename = IsoBibItem::LocalizedString.new(name.text, name[:language])
  IsoBibItem::Person.new(
    name: IsoBibItem::FullName.new(completename: completename),
    affiliation: affilations,
    contacts: contacts,
    identifiers: identifiers,
  )
end
iso_subgroup(com) click to toggle source
# File lib/iso_bib_item/xml_parser.rb, line 168
def iso_subgroup(com)
  return nil if com.nil?
  IsoSubgroup.new(name: com.text, type: com[:type],
                  number: com[:number].to_i)
end