Class: Tilia::VObject::Parser::Xml

Inherits:
Tilia::VObject::Parser show all
Defined in:
lib/tilia/v_object/parser/xml.rb,
lib/tilia/v_object/parser/xml/element.rb,
lib/tilia/v_object/parser/xml/element/key_value.rb

Overview

XML Parser.

This parser parses both the xCal and xCard formats.

Defined Under Namespace

Modules: Element

Constant Summary collapse

XCAL_NAMESPACE =
'urn:ietf:params:xml:ns:icalendar-2.0'
XCARD_NAMESPACE =
'urn:ietf:params:xml:ns:vcard-4.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil, options = 0) ⇒ void

Creates the parser.

Optionally, it’s possible to parse the input stream here.

Parameters:

  • input (defaults to: nil)
  • options (Fixnum) (defaults to: 0)

    Any parser options (OPTION constants).



21
22
23
24
25
26
27
28
29
# File 'lib/tilia/v_object/parser/xml.rb', line 21

def initialize(input = nil, options = 0)
  @input = nil
  @pointer = nil
  @root = nil

  options = OPTION_FORGIVING if options == 0

  super(input, options)
end

Class Method Details

.tag_name(clarked_tag_name) ⇒ String

Get tag name from a Clark notation.

Parameters:

  • clarked_tag_name (String)

Returns:

  • (String)


298
299
300
301
# File 'lib/tilia/v_object/parser/xml.rb', line 298

def self.tag_name(clarked_tag_name)
  (_, tag_name) = Tilia::Xml::Service.parse_clark_notation(clarked_tag_name)
  tag_name
end

Instance Method Details

#input=(input) ⇒ void

This method returns an undefined value.

Sets the input data.

Parameters:

  • input (resource|string)


279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/tilia/v_object/parser/xml.rb', line 279

def input=(input)
  input = input.read unless input.is_a?(String)

  if input.is_a?(String)
    reader = Tilia::Xml::Reader.new
    reader.element_map["{#{XCAL_NAMESPACE}}period"] = Tilia::VObject::Parser::Xml::Element::KeyValue
    reader.element_map["{#{XCAL_NAMESPACE}}recur"] = Tilia::VObject::Parser::Xml::Element::KeyValue
    reader.xml(input)
    input = reader.parse
  end

  @input = input
end

#parse(input = nil, options = 0) ⇒ Document

Parse xCal or xCard.

Parameters:

  • input (String, IO) (defaults to: nil)
  • options (Fixnum) (defaults to: 0)

Returns:



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
# File 'lib/tilia/v_object/parser/xml.rb', line 39

def parse(input = nil, options = 0)
  self.input = input unless input.nil?
  @options = options if options != 0
  if @input.nil?
    fail Tilia::VObject::EofException, 'End of input stream, or no input supplied'
  end

  case @input['name']
  when "{#{XCAL_NAMESPACE}}icalendar"
    @root = Tilia::VObject::Component::VCalendar.new({}, false)
    @pointer = @input['value'][0]
    parse_v_calendar_components(@root)
  when "{#{XCARD_NAMESPACE}}vcards"
    @input['value'].each do |v_card|
      @root = Tilia::VObject::Component::VCard.new({ 'version' => '4.0' }, false)
      @pointer = v_card
      parse_v_card_components(@root)

      # We just parse the first <vcard /> element.
      break
    end
  else
    fail Tilia::VObject::ParseException, 'Unsupported XML standard'
  end

  @root
end