tilia-vobject 4.0.0.pre.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rubocop.yml +32 -0
  4. data/.simplecov +4 -0
  5. data/.travis.yml +3 -0
  6. data/CHANGELOG.sabre.md +626 -0
  7. data/CONTRIBUTING.md +25 -0
  8. data/Gemfile +17 -0
  9. data/Gemfile.lock +68 -0
  10. data/LICENSE +27 -0
  11. data/LICENSE.sabre +27 -0
  12. data/README.md +63 -0
  13. data/Rakefile +17 -0
  14. data/bin/vobject +7 -0
  15. data/lib/tilia/v_object/birthday_calendar_generator.rb +142 -0
  16. data/lib/tilia/v_object/cli.rb +582 -0
  17. data/lib/tilia/v_object/component/available.rb +107 -0
  18. data/lib/tilia/v_object/component/v_alarm.rb +114 -0
  19. data/lib/tilia/v_object/component/v_availability.rb +128 -0
  20. data/lib/tilia/v_object/component/v_calendar.rb +468 -0
  21. data/lib/tilia/v_object/component/v_card.rb +457 -0
  22. data/lib/tilia/v_object/component/v_event.rb +127 -0
  23. data/lib/tilia/v_object/component/v_free_busy.rb +81 -0
  24. data/lib/tilia/v_object/component/v_journal.rb +75 -0
  25. data/lib/tilia/v_object/component/v_time_zone.rb +51 -0
  26. data/lib/tilia/v_object/component/v_todo.rb +147 -0
  27. data/lib/tilia/v_object/component.rb +591 -0
  28. data/lib/tilia/v_object/date_time_parser.rb +486 -0
  29. data/lib/tilia/v_object/document.rb +218 -0
  30. data/lib/tilia/v_object/element_list.rb +18 -0
  31. data/lib/tilia/v_object/eof_exception.rb +8 -0
  32. data/lib/tilia/v_object/free_busy_data.rb +149 -0
  33. data/lib/tilia/v_object/free_busy_generator.rb +465 -0
  34. data/lib/tilia/v_object/i_tip/broker.rb +909 -0
  35. data/lib/tilia/v_object/i_tip/i_tip_exception.rb +9 -0
  36. data/lib/tilia/v_object/i_tip/message.rb +109 -0
  37. data/lib/tilia/v_object/i_tip/same_organizer_for_all_components_exception.rb +13 -0
  38. data/lib/tilia/v_object/i_tip.rb +10 -0
  39. data/lib/tilia/v_object/node.rb +192 -0
  40. data/lib/tilia/v_object/parameter.rb +327 -0
  41. data/lib/tilia/v_object/parse_exception.rb +7 -0
  42. data/lib/tilia/v_object/parser/json.rb +149 -0
  43. data/lib/tilia/v_object/parser/mime_dir.rb +543 -0
  44. data/lib/tilia/v_object/parser/parser.rb +61 -0
  45. data/lib/tilia/v_object/parser/xml/element/key_value.rb +60 -0
  46. data/lib/tilia/v_object/parser/xml/element.rb +11 -0
  47. data/lib/tilia/v_object/parser/xml.rb +322 -0
  48. data/lib/tilia/v_object/parser.rb +10 -0
  49. data/lib/tilia/v_object/property/binary.rb +96 -0
  50. data/lib/tilia/v_object/property/boolean.rb +57 -0
  51. data/lib/tilia/v_object/property/flat_text.rb +52 -0
  52. data/lib/tilia/v_object/property/float_value.rb +107 -0
  53. data/lib/tilia/v_object/property/i_calendar/cal_address.rb +49 -0
  54. data/lib/tilia/v_object/property/i_calendar/date.rb +15 -0
  55. data/lib/tilia/v_object/property/i_calendar/date_time.rb +330 -0
  56. data/lib/tilia/v_object/property/i_calendar/duration.rb +65 -0
  57. data/lib/tilia/v_object/property/i_calendar/period.rb +124 -0
  58. data/lib/tilia/v_object/property/i_calendar/recur.rb +173 -0
  59. data/lib/tilia/v_object/property/i_calendar.rb +14 -0
  60. data/lib/tilia/v_object/property/integer_value.rb +60 -0
  61. data/lib/tilia/v_object/property/text.rb +352 -0
  62. data/lib/tilia/v_object/property/time.rb +85 -0
  63. data/lib/tilia/v_object/property/unknown.rb +30 -0
  64. data/lib/tilia/v_object/property/uri.rb +78 -0
  65. data/lib/tilia/v_object/property/utc_offset.rb +56 -0
  66. data/lib/tilia/v_object/property/v_card/date.rb +31 -0
  67. data/lib/tilia/v_object/property/v_card/date_and_or_time.rb +343 -0
  68. data/lib/tilia/v_object/property/v_card/date_time.rb +22 -0
  69. data/lib/tilia/v_object/property/v_card/language_tag.rb +41 -0
  70. data/lib/tilia/v_object/property/v_card/time_stamp.rb +74 -0
  71. data/lib/tilia/v_object/property/v_card.rb +13 -0
  72. data/lib/tilia/v_object/property.rb +532 -0
  73. data/lib/tilia/v_object/reader.rb +73 -0
  74. data/lib/tilia/v_object/recur/event_iterator.rb +417 -0
  75. data/lib/tilia/v_object/recur/no_instances_exception.rb +11 -0
  76. data/lib/tilia/v_object/recur/r_date_iterator.rb +138 -0
  77. data/lib/tilia/v_object/recur/r_rule_iterator.rb +717 -0
  78. data/lib/tilia/v_object/recur.rb +10 -0
  79. data/lib/tilia/v_object/settings.rb +32 -0
  80. data/lib/tilia/v_object/splitter/i_calendar.rb +95 -0
  81. data/lib/tilia/v_object/splitter/splitter_interface.rb +31 -0
  82. data/lib/tilia/v_object/splitter/v_card.rb +56 -0
  83. data/lib/tilia/v_object/splitter.rb +9 -0
  84. data/lib/tilia/v_object/string_util.rb +58 -0
  85. data/lib/tilia/v_object/time_zone_data/exchange_zones.rb +96 -0
  86. data/lib/tilia/v_object/time_zone_data/lotus_zones.rb +104 -0
  87. data/lib/tilia/v_object/time_zone_data/php_zones.rb +49 -0
  88. data/lib/tilia/v_object/time_zone_data/windows_zones.rb +121 -0
  89. data/lib/tilia/v_object/time_zone_data.rb +10 -0
  90. data/lib/tilia/v_object/time_zone_util.rb +213 -0
  91. data/lib/tilia/v_object/uuid_util.rb +51 -0
  92. data/lib/tilia/v_object/v_card_converter.rb +354 -0
  93. data/lib/tilia/v_object/version.rb +9 -0
  94. data/lib/tilia/v_object/writer.rb +56 -0
  95. data/lib/tilia/v_object.rb +45 -0
  96. data/lib/tilia/vobject.rb +1 -0
  97. data/resources/schema/xcal.rng +1192 -0
  98. data/resources/schema/xcard.rng +388 -0
  99. data/test/test_helper.rb +56 -0
  100. data/test/v_object/attach_issue_test.rb +19 -0
  101. data/test/v_object/birthday_calendar_generator_test.rb +463 -0
  102. data/test/v_object/cli_mock.rb +19 -0
  103. data/test/v_object/cli_test.rb +460 -0
  104. data/test/v_object/component/available_test.rb +59 -0
  105. data/test/v_object/component/v_alarm_test.rb +160 -0
  106. data/test/v_object/component/v_availability_test.rb +388 -0
  107. data/test/v_object/component/v_calendar_test.rb +646 -0
  108. data/test/v_object/component/v_card_test.rb +258 -0
  109. data/test/v_object/component/v_event_test.rb +85 -0
  110. data/test/v_object/component/v_free_busy_test.rb +59 -0
  111. data/test/v_object/component/v_journal_test.rb +85 -0
  112. data/test/v_object/component/v_time_zone_test.rb +47 -0
  113. data/test/v_object/component/v_todo_test.rb +172 -0
  114. data/test/v_object/component_test.rb +419 -0
  115. data/test/v_object/date_time_parser_test.rb +526 -0
  116. data/test/v_object/document_test.rb +71 -0
  117. data/test/v_object/element_list_test.rb +27 -0
  118. data/test/v_object/em_client_test.rb +53 -0
  119. data/test/v_object/empty_parameter_test.rb +65 -0
  120. data/test/v_object/empty_value_issue_test.rb +25 -0
  121. data/test/v_object/fake_component.rb +21 -0
  122. data/test/v_object/free_busy_data_test.rb +285 -0
  123. data/test/v_object/free_busy_generator_test.rb +637 -0
  124. data/test/v_object/google_colon_escaping_test.rb +27 -0
  125. data/test/v_object/i_calendar/attach_parse_test.rb +24 -0
  126. data/test/v_object/i_tip/broker_attendee_reply_test.rb +1042 -0
  127. data/test/v_object/i_tip/broker_delete_event_test.rb +175 -0
  128. data/test/v_object/i_tip/broker_new_event_test.rb +440 -0
  129. data/test/v_object/i_tip/broker_process_message_test.rb +153 -0
  130. data/test/v_object/i_tip/broker_process_reply_test.rb +402 -0
  131. data/test/v_object/i_tip/broker_tester.rb +71 -0
  132. data/test/v_object/i_tip/broker_update_event_test.rb +763 -0
  133. data/test/v_object/i_tip/evolution_test.rb +2644 -0
  134. data/test/v_object/i_tip/message_test.rb +25 -0
  135. data/test/v_object/issue153.vcf +352 -0
  136. data/test/v_object/issue153_test.rb +12 -0
  137. data/test/v_object/issue26_test.rb +25 -0
  138. data/test/v_object/issue36_work_around_test.rb +37 -0
  139. data/test/v_object/issue40_test.rb +26 -0
  140. data/test/v_object/issue64.vcf +351 -0
  141. data/test/v_object/issue64_test.rb +17 -0
  142. data/test/v_object/issue96_test.rb +22 -0
  143. data/test/v_object/issue_undefined_index_test.rb +24 -0
  144. data/test/v_object/j_cal_test.rb +150 -0
  145. data/test/v_object/j_card_test.rb +192 -0
  146. data/test/v_object/line_folding_issue_test.rb +19 -0
  147. data/test/v_object/mock_document.rb +6 -0
  148. data/test/v_object/parameter_test.rb +109 -0
  149. data/test/v_object/parser/json_test.rb +370 -0
  150. data/test/v_object/parser/mime_dir_test.rb +14 -0
  151. data/test/v_object/parser/quoted_printable_test.rb +78 -0
  152. data/test/v_object/parser/xml_test.rb +2563 -0
  153. data/test/v_object/property/binary_test.rb +12 -0
  154. data/test/v_object/property/boolean_test.rb +18 -0
  155. data/test/v_object/property/compound_test.rb +43 -0
  156. data/test/v_object/property/float_test.rb +20 -0
  157. data/test/v_object/property/i_calendar/cal_address_test.rb +26 -0
  158. data/test/v_object/property/i_calendar/date_time_test.rb +303 -0
  159. data/test/v_object/property/i_calendar/duration_test.rb +14 -0
  160. data/test/v_object/property/i_calendar/recur_test.rb +39 -0
  161. data/test/v_object/property/text_test.rb +81 -0
  162. data/test/v_object/property/v_card/date_and_or_time_test.rb +205 -0
  163. data/test/v_object/property/v_card/language_tag_test.rb +35 -0
  164. data/test/v_object/property_test.rb +338 -0
  165. data/test/v_object/reader_test.rb +403 -0
  166. data/test/v_object/recur/event_iterator/by_month_in_daily_test.rb +52 -0
  167. data/test/v_object/recur/event_iterator/by_set_pos_hang_test.rb +55 -0
  168. data/test/v_object/recur/event_iterator/expand_floating_times_test.rb +109 -0
  169. data/test/v_object/recur/event_iterator/fifth_tuesday_problem_test.rb +45 -0
  170. data/test/v_object/recur/event_iterator/incorrect_expand_test.rb +53 -0
  171. data/test/v_object/recur/event_iterator/infinite_loop_problem_test.rb +75 -0
  172. data/test/v_object/recur/event_iterator/issue48_test.rb +43 -0
  173. data/test/v_object/recur/event_iterator/issue50_test.rb +123 -0
  174. data/test/v_object/recur/event_iterator/main_test.rb +1222 -0
  175. data/test/v_object/recur/event_iterator/missing_overridden_test.rb +55 -0
  176. data/test/v_object/recur/event_iterator/no_instances_test.rb +32 -0
  177. data/test/v_object/recur/event_iterator/override_first_event_test.rb +106 -0
  178. data/test/v_object/recur/r_date_iterator_test.rb +44 -0
  179. data/test/v_object/recur/r_rule_iterator_test.rb +608 -0
  180. data/test/v_object/recurrence_iterator/UntilRespectsTimezoneTest.ics +39 -0
  181. data/test/v_object/slash_r_test.rb +15 -0
  182. data/test/v_object/splitter/i_calendar_test.rb +299 -0
  183. data/test/v_object/splitter/v_card_test.rb +173 -0
  184. data/test/v_object/string_util_test.rb +37 -0
  185. data/test/v_object/test_case.rb +42 -0
  186. data/test/v_object/time_zone_util_test.rb +271 -0
  187. data/test/v_object/uuid_util_test.rb +18 -0
  188. data/test/v_object/v_card21_test.rb +43 -0
  189. data/test/v_object/v_card_converter_test.rb +419 -0
  190. data/test/v_object/version_test.rb +15 -0
  191. data/test/v_object/writer_test.rb +33 -0
  192. data/tilia-vobject.gemspec +17 -0
  193. metadata +308 -0
@@ -0,0 +1,532 @@
1
+ module Tilia
2
+ module VObject
3
+ # Property.
4
+ #
5
+ # A property is always in a KEY:VALUE structure, and may optionally contain
6
+ # parameters.
7
+ class Property < Node
8
+ require 'tilia/v_object/property/binary'
9
+ require 'tilia/v_object/property/boolean'
10
+ require 'tilia/v_object/property/text'
11
+ require 'tilia/v_object/property/flat_text'
12
+ require 'tilia/v_object/property/float_value'
13
+ require 'tilia/v_object/property/integer_value'
14
+ require 'tilia/v_object/property/time'
15
+ require 'tilia/v_object/property/unknown'
16
+ require 'tilia/v_object/property/uri'
17
+ require 'tilia/v_object/property/utc_offset'
18
+ require 'tilia/v_object/property/i_calendar'
19
+ require 'tilia/v_object/property/v_card'
20
+
21
+ # Property name.
22
+ #
23
+ # This will contain a string such as DTSTART, SUMMARY, FN.
24
+ #
25
+ # @var string
26
+ attr_accessor :name
27
+
28
+ # Property group.
29
+ #
30
+ # This is only used in vcards
31
+ #
32
+ # @var string
33
+ attr_accessor :group
34
+
35
+ # List of parameters.
36
+ #
37
+ # @var array
38
+ attr_accessor :parameters
39
+
40
+ # Current value.
41
+ #
42
+ # @var mixed
43
+ # RUBY: attr_accessor :value
44
+
45
+ # In case this is a multi-value property. This string will be used as a
46
+ # delimiter.
47
+ #
48
+ # @var string|null
49
+ attr_accessor :delimiter
50
+
51
+ # Creates the generic property.
52
+ #
53
+ # Parameters must be specified in key=>value syntax.
54
+ #
55
+ # @param Component root The root document
56
+ # @param string name
57
+ # @param string|array|null value
58
+ # @param array parameters List of parameters
59
+ # @param string group The vcard property group
60
+ #
61
+ # @return void
62
+ def initialize(root, name, value = nil, parameters = {}, group = nil)
63
+ @parameters = {}
64
+ @delimiter = ';'
65
+ @name = name
66
+ @group = group
67
+ @root = root
68
+
69
+ parameters.each do |k, v|
70
+ add(k, v)
71
+ end
72
+
73
+ self.value = value unless value.nil?
74
+ end
75
+
76
+ # Updates the current value.
77
+ #
78
+ # This may be either a single, or multiple strings in an array.
79
+ #
80
+ # @param string|array value
81
+ #
82
+ # @return void
83
+ attr_writer :value
84
+
85
+ # Returns the current value.
86
+ #
87
+ # This method will always return a singular value. If this was a
88
+ # multi-value object, some decision will be made first on how to represent
89
+ # it as a string.
90
+ #
91
+ # To get the correct multi-value version, use getParts.
92
+ #
93
+ # @return string
94
+ def value
95
+ if @value.is_a?(Array)
96
+ if @value.empty?
97
+ nil
98
+ elsif @value.size == 1
99
+ @value[0]
100
+ else
101
+ raw_mime_dir_value
102
+ end
103
+ else
104
+ @value
105
+ end
106
+ end
107
+
108
+ # Sets a multi-valued property.
109
+ #
110
+ # @param array parts
111
+ #
112
+ # @return void
113
+ def parts=(parts)
114
+ @value = parts
115
+ end
116
+
117
+ # Returns a multi-valued property.
118
+ #
119
+ # This method always returns an array, if there was only a single value,
120
+ # it will still be wrapped in an array.
121
+ #
122
+ # @return array
123
+ def parts
124
+ if @value.nil?
125
+ []
126
+ elsif @value.is_a?(Array)
127
+ @value.clone
128
+ else
129
+ [@value]
130
+ end
131
+ end
132
+
133
+ # Adds a new parameter.
134
+ #
135
+ # If a parameter with same name already existed, the values will be
136
+ # combined.
137
+ # If nameless parameter is added, we try to guess it's name.
138
+ #
139
+ # @param string name
140
+ # @param string|null|array value
141
+ def add(name, value = nil)
142
+ no_name = false
143
+ if name.nil?
144
+ name = Parameter.guess_parameter_name_by_value(value)
145
+ no_name = true
146
+ end
147
+
148
+ if @parameters.key?(name.upcase)
149
+ @parameters[name.upcase].add_value(value)
150
+ else
151
+ param = Parameter.new(@root, name, value)
152
+ param.no_name = no_name
153
+ @parameters[param.name] = param
154
+ end
155
+ end
156
+
157
+ # Returns an iterable list of children.
158
+ #
159
+ # @return array
160
+ attr_reader :parameters
161
+
162
+ # Returns the type of value.
163
+ #
164
+ # This corresponds to the VALUE= parameter. Every property also has a
165
+ # 'default' valueType.
166
+ #
167
+ # @return string
168
+ def value_type
169
+ end
170
+
171
+ # Sets a raw value coming from a mimedir (iCalendar/vCard) file.
172
+ #
173
+ # This has been 'unfolded', so only 1 line will be passed. Unescaping is
174
+ # not yet done, but parameters are not included.
175
+ #
176
+ # @param string val
177
+ #
178
+ # @return void
179
+ def raw_mime_dir_value=(_val)
180
+ end
181
+
182
+ # Returns a raw mime-dir representation of the value.
183
+ #
184
+ # @return string
185
+ def raw_mime_dir_value
186
+ end
187
+
188
+ # Turns the object back into a serialized blob.
189
+ #
190
+ # @return string
191
+ def serialize
192
+ str = @name
193
+ str = "#{@group}.#{@name}" if @group
194
+
195
+ @parameters.each do |_name, param|
196
+ str += ";#{param.serialize}"
197
+ end
198
+
199
+ str += ":#{raw_mime_dir_value}"
200
+
201
+ out = ''
202
+ while str.size > 0
203
+ if str.bytesize > 75
204
+ tmp = StringUtil.mb_strcut(str, 75)
205
+ out += tmp + "\r\n"
206
+ str = ' ' + str[tmp.length..-1]
207
+ else
208
+ out += str + "\r\n"
209
+ str = ''
210
+ break
211
+ end
212
+ end
213
+
214
+ out
215
+ end
216
+
217
+ # Returns the value, in the format it should be encoded for JSON.
218
+ #
219
+ # This method must always return an array.
220
+ #
221
+ # @return array
222
+ def json_value
223
+ parts
224
+ end
225
+
226
+ # Sets the JSON value, as it would appear in a jCard or jCal object.
227
+ #
228
+ # The value must always be an array.
229
+ #
230
+ # @param array value
231
+ #
232
+ # @return void
233
+ def json_value=(value)
234
+ value = value.values if value.is_a?(Hash)
235
+ if value.size == 1
236
+ self.value = value[0]
237
+ else
238
+ self.value = value
239
+ end
240
+ end
241
+
242
+ # This method returns an array, with the representation as it should be
243
+ # encoded in JSON. This is used to create jCard or jCal documents.
244
+ #
245
+ # @return array
246
+ def json_serialize
247
+ parameters = {}
248
+
249
+ @parameters.each do |_, parameter|
250
+ next if parameter.name == 'VALUE'
251
+
252
+ parameters[parameter.name.downcase] = parameter.json_serialize
253
+ end
254
+
255
+ # In jCard, we need to encode the property-group as a separate 'group'
256
+ # parameter.
257
+ parameters['group'] = @group if @group
258
+
259
+ result = [
260
+ @name.downcase,
261
+ parameters,
262
+ value_type.downcase
263
+ ]
264
+ result.concat json_value
265
+ end
266
+
267
+ # Hydrate data from a XML subtree, as it would appear in a xCard or xCal
268
+ # object.
269
+ #
270
+ # @param array value
271
+ #
272
+ # @return void
273
+ def xml_value=(value)
274
+ self.json_value = value
275
+ end
276
+
277
+ # This method serializes the data into XML. This is used to create xCard or
278
+ # xCal documents.
279
+ #
280
+ # @param Xml\Writer writer XML writer.
281
+ #
282
+ # @return void
283
+ def xml_serialize(writer)
284
+ parameters = []
285
+
286
+ @parameters.each do |_, parameter|
287
+ next if parameter.name == 'VALUE'
288
+
289
+ parameters << parameter
290
+ end
291
+
292
+ writer.start_element(@name.downcase)
293
+
294
+ if parameters.any?
295
+ writer.start_element('parameters')
296
+
297
+ parameters.each do |parameter|
298
+ writer.start_element(parameter.name.downcase)
299
+ writer.write(parameter)
300
+ writer.end_element
301
+ end
302
+
303
+ writer.end_element
304
+ end
305
+
306
+ xml_serialize_value(writer)
307
+ writer.end_element
308
+ end
309
+
310
+ protected
311
+
312
+ # This method serializes only the value of a property. This is used to
313
+ # create xCard or xCal documents.
314
+ #
315
+ # @param Xml\Writer writer XML writer.
316
+ #
317
+ # @return void
318
+ def xml_serialize_value(writer)
319
+ value_type = self.value_type.downcase
320
+
321
+ json_value.each do |values|
322
+ values = [values] unless values.is_a?(Array)
323
+ values.each do |value|
324
+ writer.write_element(value_type, value)
325
+ end
326
+ end
327
+ end
328
+
329
+ public
330
+
331
+ # Called when this object is being cast to a string.
332
+ #
333
+ # If the property only had a single value, you will get just that. In the
334
+ # case the property had multiple values, the contents will be escaped and
335
+ # combined with ,.
336
+ #
337
+ # @return string
338
+ def to_s
339
+ value.to_s
340
+ end
341
+
342
+ # Checks if an array element exists.
343
+ #
344
+ # @param mixed name
345
+ #
346
+ # @return bool
347
+ def key?(name)
348
+ name = name.upcase
349
+
350
+ @parameters.each do |_name, parameter|
351
+ return true if parameter.name == name
352
+ end
353
+ false
354
+ end
355
+
356
+ # Returns a parameter.
357
+ #
358
+ # If the parameter does not exist, null is returned.
359
+ #
360
+ # @param string name
361
+ #
362
+ # @return Node
363
+ def [](name)
364
+ return super(name) if name.is_a?(Fixnum)
365
+
366
+ @parameters[name.upcase]
367
+ end
368
+
369
+ # Creates a new parameter.
370
+ #
371
+ # @param string name
372
+ # @param mixed value
373
+ #
374
+ # @return void
375
+ def []=(name, value)
376
+ if name.is_a?(Fixnum)
377
+ super(name, value)
378
+ # @codeCoverageIgnoreStart
379
+ # This will never be reached, because an exception is always
380
+ # thrown.
381
+ return nil
382
+ # @codeCoverageIgnoreEnd
383
+ end
384
+
385
+ param = Parameter.new(@root, name, value)
386
+ @parameters[param.name] = param
387
+ end
388
+
389
+ # Removes one or more parameters with the specified name.
390
+ #
391
+ # @param string name
392
+ #
393
+ # @return void
394
+ def delete(name)
395
+ if name.is_a?(Fixnum)
396
+ super(name)
397
+ # @codeCoverageIgnoreStart
398
+ # This will never be reached, because an exception is always
399
+ # thrown.
400
+ return nil
401
+ # @codeCoverageIgnoreEnd
402
+ end
403
+
404
+ @parameters.delete(name.upcase)
405
+ end
406
+
407
+ # This method is automatically called when the object is cloned.
408
+ # Specifically, this will ensure all child elements are also cloned.
409
+ #
410
+ # @return void
411
+ def initialize_copy(_other)
412
+ new_params = {}
413
+ @parameters.each do |key, child|
414
+ new_params[key] = child.clone
415
+ new_params[key].parent = self
416
+ end
417
+ @parameters = new_params
418
+ end
419
+
420
+ # Validates the node for correctness.
421
+ #
422
+ # The following options are supported:
423
+ # - Node::REPAIR - If something is broken, and automatic repair may
424
+ # be attempted.
425
+ #
426
+ # An array is returned with warnings.
427
+ #
428
+ # Every item in the array has the following properties:
429
+ # * level - (number between 1 and 3 with severity information)
430
+ # * message - (human readable message)
431
+ # * node - (reference to the offending node)
432
+ #
433
+ # @param int options
434
+ #
435
+ # @return array
436
+ def validate(options = 0)
437
+ warnings = []
438
+
439
+ # Checking if our value is UTF-8
440
+ if raw_mime_dir_value.is_a?(String) && !StringUtil.utf8?(raw_mime_dir_value)
441
+ old_value = raw_mime_dir_value
442
+ level = 3
443
+
444
+ if options & self.class::REPAIR > 0
445
+ new_value = StringUtil.convert_to_utf8(old_value)
446
+
447
+ self.raw_mime_dir_value = new_value
448
+ level = 1
449
+ end
450
+
451
+ matches = /([\x00-\x08\x0B-\x0C\x0E-\x1F\x7F])/.match(old_value)
452
+ if matches
453
+ message = format('Property contained a control character (0x%02x)', matches[1].ord)
454
+ else
455
+ message = "Property is not valid UTF-8! #{old_value}"
456
+ end
457
+
458
+ warnings << {
459
+ 'level' => level,
460
+ 'message' => message,
461
+ 'node' => self
462
+ }
463
+ end
464
+
465
+ # Checking if the propertyname does not contain any invalid bytes.
466
+ unless @name =~ /^([A-Z0-9-]+)$/
467
+ warnings << {
468
+ 'level' => 1,
469
+ 'message' => "The propertyname: #{@name} contains invalid characters. Only A-Z, 0-9 and - are allowed",
470
+ 'node' => self
471
+ }
472
+
473
+ if options & self.class::REPAIR > 0
474
+ # Uppercasing and converting underscores to dashes.
475
+ @name = @name.upcase.tr('_', '-')
476
+
477
+ # Removing every other invalid character
478
+ @name = @name.gsub(/([^A-Z0-9-])/u, '')
479
+ end
480
+ end
481
+
482
+ encoding = self['ENCODING']
483
+ if encoding
484
+ if @root.document_type == Document::VCARD40
485
+ warnings << {
486
+ 'level' => 1,
487
+ 'message' => 'ENCODING parameter is not valid in vCard 4.',
488
+ 'node' => self
489
+ }
490
+ else
491
+ encoding = encoding.to_s
492
+
493
+ allowed_encoding = []
494
+
495
+ case @root.document_type
496
+ when Document::ICALENDAR20
497
+ allowed_encoding = ['8BIT', 'BASE64']
498
+ when Document::VCARD21
499
+ allowed_encoding = ['QUOTED-PRINTABLE', 'BASE64', '8BIT']
500
+ when Document::VCARD30
501
+ allowed_encoding = ['B']
502
+ end
503
+
504
+ if allowed_encoding.any? && !allowed_encoding.include?(encoding.upcase)
505
+ warnings << {
506
+ 'level' => 1,
507
+ 'message' => "ENCODING=#{encoding.upcase} is not valid for this document type.",
508
+ 'node' => self
509
+ }
510
+ end
511
+ end
512
+ end
513
+
514
+ # Validating inner parameters
515
+ @parameters.each do |_name, param|
516
+ warnings.concat param.validate(options)
517
+ end
518
+
519
+ warnings
520
+ end
521
+
522
+ # TODO: document
523
+ def ==(other)
524
+ if other.is_a?(String)
525
+ to_s == other
526
+ else
527
+ super(other)
528
+ end
529
+ end
530
+ end
531
+ end
532
+ end
@@ -0,0 +1,73 @@
1
+ module Tilia
2
+ module VObject
3
+ # iCalendar/vCard/jCal/jCard/xCal/xCard reader object.
4
+ #
5
+ # This object provides a few (static) convenience methods to quickly access
6
+ # the parsers.
7
+ class Reader
8
+ # If this option is passed to the reader, it will be less strict about the
9
+ # validity of the lines.
10
+ OPTION_FORGIVING = 1
11
+
12
+ # If this option is turned on, any lines we cannot parse will be ignored
13
+ # by the reader.
14
+ OPTION_IGNORE_INVALID_LINES = 2
15
+
16
+ # Parses a vCard or iCalendar object, and returns the top component.
17
+ #
18
+ # The options argument is a bitfield. Pass any of the OPTIONS constant to
19
+ # alter the parsers' behaviour.
20
+ #
21
+ # You can either supply a string, or a readable stream for input.
22
+ #
23
+ # @param string|resource data
24
+ # @param int options
25
+ #
26
+ # @return Document
27
+ def self.read(data, options = 0)
28
+ parser = Parser::MimeDir.new
29
+ result = parser.parse(data, options)
30
+
31
+ result
32
+ end
33
+
34
+ # Parses a jCard or jCal object, and returns the top component.
35
+ #
36
+ # The options argument is a bitfield. Pass any of the OPTIONS constant to
37
+ # alter the parsers' behaviour.
38
+ #
39
+ # You can either a string, a readable stream, or an array for it's input.
40
+ # Specifying the array is useful if json_decode was already called on the
41
+ # input.
42
+ #
43
+ # @param string|resource|array data
44
+ # @param int options
45
+ #
46
+ # @return Document
47
+ def self.read_json(data, options = 0)
48
+ parser = Parser::Json.new
49
+ result = parser.parse(data, options)
50
+
51
+ result
52
+ end
53
+
54
+ # Parses a xCard or xCal object, and returns the top component.
55
+ #
56
+ # The options argument is a bitfield. Pass any of the OPTIONS constant to
57
+ # alter the parsers' behaviour.
58
+ #
59
+ # You can either supply a string, or a readable stream for input.
60
+ #
61
+ # @param string|resource data
62
+ # @param int options
63
+ #
64
+ # @return Document
65
+ def self.read_xml(data, options = 0)
66
+ parser = Parser::Xml.new
67
+ result = parser.parse(data, options)
68
+
69
+ result
70
+ end
71
+ end
72
+ end
73
+ end