Class: Oga::XML::Document

Inherits:
Object
  • Object
show all
Includes:
Querying, Traversal
Defined in:
lib/oga/xml/document.rb

Overview

Class used for storing information about an entire XML document. This includes the doctype, XML declaration, child nodes and more.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Traversal

#each_node

Methods included from Querying

#at_css, #at_xpath, #css, #xpath

Constructor Details

#initialize(options = {}) ⇒ Document

Returns a new instance of Document

Options Hash (options):


33
34
35
36
37
38
39
# File 'lib/oga/xml/document.rb', line 33

def initialize(options = {})
  @doctype         = options[:doctype]
  @xml_declaration = options[:xml_declaration]
  @type            = options[:type] || :xml

  self.children = options[:children] if options[:children]
end

Instance Attribute Details

#doctypeOga::XML::Doctype

The doctype of the document.


19
20
21
22
23
24
25
26
27
28
29
30
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/oga/xml/document.rb', line 19

class Document
  include Querying
  include Traversal

  attr_accessor :doctype, :xml_declaration, :type

  ##
  # @param [Hash] options
  #
  # @option options [Oga::XML::NodeSet] :children
  # @option options [Oga::XML::Doctype] :doctype
  # @option options [Oga::XML::XmlDeclaration] :xml_declaration
  # @option options [Symbol] :type
  #
  def initialize(options = {})
    @doctype         = options[:doctype]
    @xml_declaration = options[:xml_declaration]
    @type            = options[:type] || :xml

    self.children = options[:children] if options[:children]
  end

  ##
  # @return [Oga::XML::NodeSet]
  #
  def children
    return @children ||= NodeSet.new([], self)
  end

  ##
  # Sets the child nodes of the document.
  #
  # @param [Oga::XML::NodeSet|Array] nodes
  #
  def children=(nodes)
    if nodes.is_a?(NodeSet)
      @children = nodes
    else
      @children = NodeSet.new(nodes, self)
    end
  end

  ##
  # Converts the document and its child nodes to XML.
  #
  # @return [String]
  #
  def to_xml
    xml = children.map(&:to_xml).join('')

    if doctype
      xml = doctype.to_xml + "\n" + xml.strip
    end

    if xml_declaration
      xml = xml_declaration.to_xml + "\n" + xml.strip
    end

    return xml
  end

  ##
  # Inspects the document and its child nodes. Child nodes are indented for
  # each nesting level.
  #
  # @return [String]
  #
  def inspect
    segments = []

    [:doctype, :xml_declaration, :children].each do |attr|
      value = send(attr)

      if value
        segments << "#{attr}: #{value.inspect}"
      end
    end

    return <<-EOF.strip
Document(
  #{segments.join("\n  ")}
)
    EOF
  end
end

#typeSymbol

The document type, either :xml or :html.


19
20
21
22
23
24
25
26
27
28
29
30
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/oga/xml/document.rb', line 19

class Document
  include Querying
  include Traversal

  attr_accessor :doctype, :xml_declaration, :type

  ##
  # @param [Hash] options
  #
  # @option options [Oga::XML::NodeSet] :children
  # @option options [Oga::XML::Doctype] :doctype
  # @option options [Oga::XML::XmlDeclaration] :xml_declaration
  # @option options [Symbol] :type
  #
  def initialize(options = {})
    @doctype         = options[:doctype]
    @xml_declaration = options[:xml_declaration]
    @type            = options[:type] || :xml

    self.children = options[:children] if options[:children]
  end

  ##
  # @return [Oga::XML::NodeSet]
  #
  def children
    return @children ||= NodeSet.new([], self)
  end

  ##
  # Sets the child nodes of the document.
  #
  # @param [Oga::XML::NodeSet|Array] nodes
  #
  def children=(nodes)
    if nodes.is_a?(NodeSet)
      @children = nodes
    else
      @children = NodeSet.new(nodes, self)
    end
  end

  ##
  # Converts the document and its child nodes to XML.
  #
  # @return [String]
  #
  def to_xml
    xml = children.map(&:to_xml).join('')

    if doctype
      xml = doctype.to_xml + "\n" + xml.strip
    end

    if xml_declaration
      xml = xml_declaration.to_xml + "\n" + xml.strip
    end

    return xml
  end

  ##
  # Inspects the document and its child nodes. Child nodes are indented for
  # each nesting level.
  #
  # @return [String]
  #
  def inspect
    segments = []

    [:doctype, :xml_declaration, :children].each do |attr|
      value = send(attr)

      if value
        segments << "#{attr}: #{value.inspect}"
      end
    end

    return <<-EOF.strip
Document(
  #{segments.join("\n  ")}
)
    EOF
  end
end

#xml_declarationOga::XML::XmlDeclaration

The XML declaration of the document.


19
20
21
22
23
24
25
26
27
28
29
30
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/oga/xml/document.rb', line 19

class Document
  include Querying
  include Traversal

  attr_accessor :doctype, :xml_declaration, :type

  ##
  # @param [Hash] options
  #
  # @option options [Oga::XML::NodeSet] :children
  # @option options [Oga::XML::Doctype] :doctype
  # @option options [Oga::XML::XmlDeclaration] :xml_declaration
  # @option options [Symbol] :type
  #
  def initialize(options = {})
    @doctype         = options[:doctype]
    @xml_declaration = options[:xml_declaration]
    @type            = options[:type] || :xml

    self.children = options[:children] if options[:children]
  end

  ##
  # @return [Oga::XML::NodeSet]
  #
  def children
    return @children ||= NodeSet.new([], self)
  end

  ##
  # Sets the child nodes of the document.
  #
  # @param [Oga::XML::NodeSet|Array] nodes
  #
  def children=(nodes)
    if nodes.is_a?(NodeSet)
      @children = nodes
    else
      @children = NodeSet.new(nodes, self)
    end
  end

  ##
  # Converts the document and its child nodes to XML.
  #
  # @return [String]
  #
  def to_xml
    xml = children.map(&:to_xml).join('')

    if doctype
      xml = doctype.to_xml + "\n" + xml.strip
    end

    if xml_declaration
      xml = xml_declaration.to_xml + "\n" + xml.strip
    end

    return xml
  end

  ##
  # Inspects the document and its child nodes. Child nodes are indented for
  # each nesting level.
  #
  # @return [String]
  #
  def inspect
    segments = []

    [:doctype, :xml_declaration, :children].each do |attr|
      value = send(attr)

      if value
        segments << "#{attr}: #{value.inspect}"
      end
    end

    return <<-EOF.strip
Document(
  #{segments.join("\n  ")}
)
    EOF
  end
end

Instance Method Details

#childrenOga::XML::NodeSet


44
45
46
# File 'lib/oga/xml/document.rb', line 44

def children
  return @children ||= NodeSet.new([], self)
end

#children=(nodes) ⇒ Object

Sets the child nodes of the document.


53
54
55
56
57
58
59
# File 'lib/oga/xml/document.rb', line 53

def children=(nodes)
  if nodes.is_a?(NodeSet)
    @children = nodes
  else
    @children = NodeSet.new(nodes, self)
  end
end

#inspectString

Inspects the document and its child nodes. Child nodes are indented for each nesting level.


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/oga/xml/document.rb', line 86

def inspect
  segments = []

  [:doctype, :xml_declaration, :children].each do |attr|
    value = send(attr)

    if value
      segments << "#{attr}: #{value.inspect}"
    end
  end

  return <<-EOF.strip
Document(
  #{segments.join("\n  ")}
)
  EOF
end

#to_xmlString

Converts the document and its child nodes to XML.


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/oga/xml/document.rb', line 66

def to_xml
  xml = children.map(&:to_xml).join('')

  if doctype
    xml = doctype.to_xml + "\n" + xml.strip
  end

  if xml_declaration
    xml = xml_declaration.to_xml + "\n" + xml.strip
  end

  return xml
end