Class: Libis::Metadata::DublinCoreRecord

Inherits:
Tools::XmlDocument
  • Object
show all
Defined in:
lib/libis/metadata/dublin_core_record.rb

Overview

Conveniece class to create and read DC records. Most of the functionality is derived from the Tools::XmlDocument base class. This class puts its focus on supporting the <dc:xxx> and <dcterms:xxx> namespaces. For most tags the namespaces are added automatically by checking which tag you want to add. In some cases the same tag exists in both namespaces and you may want to state the namespace explicitely. Even then things are made as easily as possible.

Constant Summary collapse

DC_ELEMENTS =

List of known tags in the DC namespace

%w'contributor coverage creator date description format identifier language' +
%w'publisher relation rights source subject title type'
DCTERMS_ELEMENTS =

List of known tags in the DCTERMS namespace

%w'abstract accessRights accrualMethod accrualPeriodicity accrualPolicy alternative' +
%w'audience available bibliographicCitation conformsTo contributor coverage created creator date' +
%w'dateAccepted dateCopyrighted dateSubmitted description educationLevel extent format hasFormat' +
%w'hasPart hasVersion identifier instructionalMethod isFormatOf isPartOf isReferencedBy isReplacedBy' +
%w'isRequiredBy issued isVersionOf language license mediator medium modified provenance publisher' +
%w'references relation replaces requires rights rightsHolder source spatial subject tableOfContents' +
%w'temporal title type valid'

Instance Method Summary collapse

Constructor Details

#initialize(doc = nil) ⇒ DublinCoreRecord

Note:

The input document is not checked if it is a valid DC record XML.

Create new DC document. If the doc parameter is nil a new empty DC document will be created with the dc:record root element and all required namespaces defined.

Parameters:

  • doc (::Libis::Tools::XmlDocument, String, IO, Hash) (defaults to: nil)

    optional document to read.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/libis/metadata/dublin_core_record.rb', line 33

def initialize(doc = nil)
  super()
  xml_doc = case doc
            when ::Libis::Tools::XmlDocument
              doc
            when String
              # noinspection RubyResolve
              File.exist?(doc) ? Libis::Tools::XmlDocument.open(doc) : Libis::Tools::XmlDocument.parse(doc)
            when IO
              Libis::Tools::XmlDocument.parse(doc.read)
            when Hash
              Libis::Tools::XmlDocument.from_hash(doc)
            when NilClass
              Libis::Tools::XmlDocument.new.build do |xml|
                xml[:dc].record('xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
                                'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
                                'xmlns:dcterms' => 'http://purl.org/dc/terms/') {
                  yield xml if block_given?
                }
              end
            else
              raise ArgumentError, "Invalid argument: #{doc.inspect}"
            end
  @document = xml_doc.document if xml_doc
  raise ArgumentError, 'XML document not valid.' if self.invalid?
end

Instance Method Details

#add_node(name, value = nil, parent = nil, attributes = {}) ⇒ Object

Add a node. You can omit the namespace in the name parameter. The method will add the correct namespace for you. If using symbols for name, an underscore (‘_’) can be used as separator instead of the colon (‘:’).

Parameters:

  • name (String, Symbol)

    tag name of the element

  • value (String) (defaults to: nil)

    content of the new element

  • parent (Nokogiri::XML::Node) (defaults to: nil)

    the new element will be attached to this node

  • attributes (Hash) (defaults to: {})

    list of <attribute_name>, <attribute_value> pairs for the new element



77
78
79
80
81
# File 'lib/libis/metadata/dublin_core_record.rb', line 77

def add_node(name, value = nil, parent = nil, attributes = {})
  ns, tag = get_namespace(name.to_s)
  (attributes[:namespaces] ||= {})[:node_ns] ||= ns if ns
  super tag, value, parent, **attributes
end

#xpath(path) ⇒ Object

Search the document with xpath. If no namespace is present, the ‘dc:’ namespace will be added.

Parameters:

  • path (String)

    any valid XPath expression



63
64
65
66
67
68
# File 'lib/libis/metadata/dublin_core_record.rb', line 63

def xpath(path)
  m = /^([\/.]*\/)?(dc(terms)?:)?(.*)/.match(path.to_s)
  return [] unless m[4]
  path = (m[1] || '') + ('dc:' || m[2]) + m[4]
  @document.xpath(path.to_s)
end