Class: FinalCutPro::XMLParser::Common

Inherits:
Object
  • Object
show all
Defined in:
lib/final_cut_pro/xml_parser/common.rb

Direct Known Subclasses

Document, FCPXML::Version1, XMEML::Version5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, options = { }) ⇒ Common

xml_as_document



17
18
19
20
# File 'lib/final_cut_pro/xml_parser/common.rb', line 17

def initialize(xml, options = { })
  @logger = FinalCutPro.process_options_for_logger(options)
  @xml_document = xml_as_document(xml)
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



11
12
13
# File 'lib/final_cut_pro/xml_parser/common.rb', line 11

def files
  @files
end

#loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/final_cut_pro/xml_parser/common.rb', line 10

def logger
  @logger
end

#sequencesObject

is_sequence?



115
116
117
# File 'lib/final_cut_pro/xml_parser/common.rb', line 115

def sequences
  @sequences
end

Class Method Details

.xml_as_document(xml, options = { }) ⇒ Object



13
14
15
# File 'lib/final_cut_pro/xml_parser/common.rb', line 13

def self.xml_as_document(xml, options = { })
  AXML.xml_as_document(xml)
end

Instance Method Details

#is_clip?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/final_cut_pro/xml_parser/common.rb', line 103

def is_clip?
  top_level_container == 'clip'
end

#is_fcpxml?Boolean

is_xmeml?

Returns:

  • (Boolean)


88
89
90
# File 'lib/final_cut_pro/xml_parser/common.rb', line 88

def is_fcpxml?
  root_type == 'fcpxml'
end

#is_project?Boolean

is_clip?

Returns:

  • (Boolean)


107
108
109
# File 'lib/final_cut_pro/xml_parser/common.rb', line 107

def is_project?
  top_level_container == 'project'
end

#is_sequence?Boolean

is_project?

Returns:

  • (Boolean)


111
112
113
# File 'lib/final_cut_pro/xml_parser/common.rb', line 111

def is_sequence?
  top_level_container == 'sequence'
end

#is_xmeml?Boolean

type

Returns:

  • (Boolean)


84
85
86
# File 'lib/final_cut_pro/xml_parser/common.rb', line 84

def is_xmeml?
  root_type == 'xmeml'
end

#rootObject



75
76
77
# File 'lib/final_cut_pro/xml_parser/common.rb', line 75

def root
  xml_document.root
end

#root_typeObject

Gets the



80
81
82
# File 'lib/final_cut_pro/xml_parser/common.rb', line 80

def root_type
  @root_type ||= root.name
end

#to_hash(keys_to_symbols = true) ⇒ Object

xml_as_document



26
27
28
29
# File 'lib/final_cut_pro/xml_parser/common.rb', line 26

def to_hash(keys_to_symbols = true)
  rt = keys_to_symbols ? root_type.to_sym : root_type
  { rt => xml_node_to_hash(root, keys_to_symbols) }
end

#top_level_containerObject

The tag inside of the xmeml or fcpxml tag



99
100
101
# File 'lib/final_cut_pro/xml_parser/common.rb', line 99

def top_level_container
  @top_level_container ||= root.children.first.name
end

#versionObject

The fcpxml or xmeml version



94
95
96
# File 'lib/final_cut_pro/xml_parser/common.rb', line 94

def version
  @version ||= root.attributes['version']
end

#xml_as_document(xml) ⇒ Object

initialize



22
23
24
# File 'lib/final_cut_pro/xml_parser/common.rb', line 22

def xml_as_document(xml)
  self.class.xml_as_document(xml)
end

#xml_documentObject Also known as: xmldoc

xml_node_to_hash



70
71
72
# File 'lib/final_cut_pro/xml_parser/common.rb', line 70

def xml_document
  @xml_document
end

#xml_node_to_hash(node, keys_to_symbols = true) ⇒ Object

to_hash



31
32
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
59
60
61
62
63
64
65
66
67
68
# File 'lib/final_cut_pro/xml_parser/common.rb', line 31

def xml_node_to_hash(node, keys_to_symbols = true)
  # If we are at the root of the document, start the hash
  return node.content.to_s unless node.element?
  result_hash = {}

  # Add the attributes for the node to the hash
  node.each_attr { |a| a_name = keys_to_symbols ? a.name.to_sym : a.name; result_hash[a_name] = a.value }
  return result_hash.empty? ? nil : result_hash unless node.children?

  node.each_child do |child|
    result = xml_node_to_hash(child, keys_to_symbols)

    if child.name == 'text' or child.cdata?
      return result if !child.next? and !child.prev?
      next
    end

    begin
      key = keys_to_symbols ? child.name.to_sym : child.name
    rescue
      # FCP CDATA Fields usually fail to convert to a sym.
      logger.error { "Error Converting #{child.name} to symbol.\nCHILD: #{child.inspect}\nNODE: #{node}" }
      key = child.name
    end
    if result_hash[key]
      # We don't want to overwrite a value for an existing key so the value needs to be an array
      if result_hash[key].is_a?(Array)
        result_hash[key] << result
      else
        # Create an array
        result_hash[key] = [ result_hash[key], result ]
      end
    else
      result_hash[key] = result
    end
  end
  return result_hash
end