Class: MusicBrainz::Webservice::MBXML

Inherits:
Object
  • Object
show all
Defined in:
lib/rbrainz/webservice/mbxml.rb

Overview

Class to read the XML data returned by the MusicBrainz web service and create the corresponding model classes. The class understands the MusicBrainz XML Metadata Version 1.0 schema.

See http://musicbrainz.org/doc/MusicBrainzXMLMetaData for more information on the MusicBrainz XML Metadata schema.

Defined Under Namespace

Classes: ParseError

Instance Method Summary collapse

Constructor Details

#initialize(stream, factory = nil) ⇒ MBXML

Create a new MBXML instance to parse a MusicBrainz metadata document.

Parameters: [stream] An IO object to read the MusicBrainz metadata XML document from. [factory] A model factory. An instance of Model::DefaultFactory will be used if none is given.

Raises

MBXML::ParseError



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rbrainz/webservice/mbxml.rb', line 37

def initialize(stream, factory=nil)
  begin
    @document = REXML::Document.new(stream)
  rescue REXML::ParseException => e
    raise ParseError, e.to_s
  end
  
  # Set the model factory
  factory = Model::DefaultFactory.new unless factory
  @factory = factory
  
  # Already loaded artists, releases, tracks and labels will get cached
  # in these variables to link to them if they occure multiple times
  # inside the same document.
  @artists        = Hash.new
  @release_groups = Hash.new
  @releases       = Hash.new
  @tracks         = Hash.new
  @labels         = Hash.new
end

Instance Method Details

#get_entity(entity_type) ⇒ Object

Read the XML string and create an entity model for the given entity type if it is present in the document.

Returns nil if no entity of the given type is present.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbrainz/webservice/mbxml.rb', line 62

def get_entity(entity_type)
  # Search for the first occuring node of type entity which is a child node
  # of the metadata element.
  entity = @document.elements["//[local-name()='metadata' and namespace-uri()='%s']/%s[1]" %
           [Model::NS_MMD_1, Utils.entity_type_to_string(entity_type)]]
  
  unless entity.nil? or entity.is_a? REXML::Text
    create_method = method('create_' + entity_type.to_s)
    create_method.call(entity) if create_method
  else
    return nil
  end
end

#get_entity_list(entity_type, ns = Model::NS_MMD_1) ⇒ Object

Read the XML string and create a list of entity models for the given entity type. The list is returned as a Model::ScoredCollection. There must be an entity-list element as a child of the metadata element in the document.

Returns an empty Model::ScoredCollection if the list is empty or if no entity-list element can be found.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rbrainz/webservice/mbxml.rb', line 83

def get_entity_list(entity_type, ns=Model::NS_MMD_1)
  # Search for the first occuring node of type entity which is a child node
  # of the metadata element.
  entity_list = @document.elements[
    "//[local-name()='metadata' and namespace-uri()='%s']/[local-name()='%s-list' and namespace-uri()='%s'][1]" %
      [Model::NS_MMD_1, Utils.entity_type_to_string(entity_type), ns]]
  
  unless entity_list.nil? or entity_list.is_a? REXML::Text
    collection = Model::ScoredCollection.new(entity_list.attributes['count'],
                                             entity_list.attributes['offset'])
    # Select the method to use for reading the list.
    read_list_method = method('read_' + entity_list.name.gsub('-', '_'))
    
    # Read the entity list and store the entities in the collection.
    read_list_method.call(entity_list, collection, true) if read_list_method
    
    return collection
  else
    return Model::ScoredCollection.new
  end
end