rcommons 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/COPYING +202 -0
  2. data/Changes.rdoc +4 -0
  3. data/README.rdoc +21 -0
  4. data/Rakefile +277 -0
  5. data/doc/jamis.rb +589 -0
  6. data/lib/commons.rb +44 -0
  7. data/lib/commons/io/filename_utils.rb +54 -0
  8. data/lib/commons/lang/builder/to_s_builder.rb +124 -0
  9. data/lib/commons/lang/builder/to_s_style.rb +493 -0
  10. data/lib/commons/lang/class_loader.rb +77 -0
  11. data/lib/commons/lang/class_utils.rb +50 -0
  12. data/lib/commons/lang/object_utils.rb +61 -0
  13. data/lib/commons/lang/state_error.rb +41 -0
  14. data/lib/commons/lang/system_utils.rb +113 -0
  15. data/lib/commons/lang/time/date_utils.rb +47 -0
  16. data/lib/commons/lang/time/duration_format_utils.rb +279 -0
  17. data/lib/commons/lang/time/stop_watch.rb +207 -0
  18. data/lib/commons/logging/impl/log4r_logger.rb +159 -0
  19. data/lib/commons/logging/impl/log_factory_impl.rb +253 -0
  20. data/lib/commons/logging/impl/no_op_log.rb +59 -0
  21. data/lib/commons/logging/impl/simple_log.rb +251 -0
  22. data/lib/commons/logging/log.rb +106 -0
  23. data/lib/commons/logging/log_configuration_error.rb +40 -0
  24. data/lib/commons/logging/log_factory.rb +177 -0
  25. data/lib/commons/ruby/exception.rb +46 -0
  26. data/lib/commons/ruby/log4r.rb +34 -0
  27. data/lib/commons/ruby/log4r/logger.rb +116 -0
  28. data/lib/commons/ruby/string.rb +55 -0
  29. data/lib/commons/ruby/test/unit.rb +33 -0
  30. data/lib/commons/ruby/test/unit/assertions.rb +50 -0
  31. data/lib/commons/util/properties.rb +84 -0
  32. data/test/commons/io/filename_utils_test.rb +69 -0
  33. data/test/commons/lang/builder/to_s_builder_test.rb +101 -0
  34. data/test/commons/lang/system_utils_test.rb +75 -0
  35. data/test/commons/lang/time/duration_format_utils_test.rb +261 -0
  36. data/test/commons/lang/time/stop_watch_test.rb +232 -0
  37. data/test/commons/logging/impl/log4r_logger_test.rb +133 -0
  38. data/test/commons/logging/log_factory_test.rb +80 -0
  39. data/test/commons/ruby/string_test.rb +64 -0
  40. data/test/commons/util/properties_test.rb +92 -0
  41. data/test/log4r/logger_test.rb +92 -0
  42. metadata +123 -0
@@ -0,0 +1,44 @@
1
+ # = Commons
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: commons.rb 74 2008-10-12 02:17:15Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ # = Commons module
34
+ #
35
+ # This top level module contains the version number constant, pseudo
36
+ # environment constant for rCommons and so on.
37
+ module Commons
38
+ VERSION = '0.8.0'
39
+
40
+ ENV = Hash.new
41
+
42
+ # The property name used to identify the Log4r configuration file.
43
+ LOG4R_CONF_PROPERTY = 'log4r.configuration'
44
+ end
@@ -0,0 +1,54 @@
1
+ # = Filename Utilities
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: filename_utils.rb 74 2008-10-12 02:17:15Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons/lang/system_utils'
34
+
35
+ module Commons
36
+ module IO
37
+
38
+ # This class contains utility methods for file name string.
39
+ class FilenameUtils
40
+ def initialize
41
+ super()
42
+ end
43
+
44
+
45
+ # Test whether the given path string is absolute path or not.
46
+ def self.absolute?(path)
47
+ return true if path =~ /^(?:\w:)?(?:\/|\\)/
48
+ return true if path[0].chr == '/'
49
+ return false
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,124 @@
1
+ # = to_s Builder
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: to_s_builder.rb 75 2008-10-12 10:35:52Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons/lang/builder/to_s_style'
34
+
35
+ module Commons
36
+ module Lang
37
+ module Builder
38
+
39
+ # This class supports the implementation of to_s method.
40
+ #
41
+ # Example:
42
+ # require 'commons/lang/builder/to_s_builder'
43
+ #
44
+ # class Person
45
+ # def initialize
46
+ # @name = nil
47
+ # @age = nil
48
+ # end
49
+ # # ...
50
+ # def to_s
51
+ # return Commons::Lang::Builder::ToSBuilder.new(self).
52
+ # append('name', @name).
53
+ # append('age', @age).
54
+ # to_s
55
+ # end
56
+ # end
57
+ class ToSBuilder
58
+ @@default_style = ToSStyle::DEFAULT_STYLE
59
+
60
+ attr_reader :buffer, :style, :object
61
+
62
+ def self.get_default_style
63
+ return @@default_style
64
+ end
65
+
66
+
67
+ def self.set_default_style(style)
68
+ if style == nil
69
+ raise ArgumentError, 'The style must not be nil'
70
+ end
71
+ @@default_style = style
72
+ end
73
+
74
+
75
+ def initialize(object, style = nil, buffer = nil)
76
+ if style == nil
77
+ style = get_default_style
78
+ end
79
+ if buffer == nil
80
+ buffer = String.new('')
81
+ end
82
+ @buffer = buffer
83
+ @style = style
84
+ @object = object
85
+
86
+ style.append_start(buffer, object)
87
+ end
88
+
89
+
90
+ def append(field_name, value, full_detail = true)
91
+ @style.append(@buffer, field_name, value, full_detail)
92
+ return self
93
+ end
94
+
95
+
96
+ def append_super(super_to_s)
97
+ if super_to_s != nil
98
+ @style.append_super(@buffer, super_to_s)
99
+ end
100
+ return self
101
+ end
102
+
103
+
104
+ def append_to_s(to_s)
105
+ if to_s != nil
106
+ @style.append_to_s(@buffer, to_s)
107
+ end
108
+ return self
109
+ end
110
+
111
+
112
+ def to_s
113
+ if @object == nil
114
+ @buffer.append(@style.get_nil_text)
115
+ else
116
+ @style.append_end(@buffer, @object)
117
+ end
118
+ return @buffer.to_s
119
+ end
120
+ end
121
+
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,493 @@
1
+ # = to_s Style
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: to_s_style.rb 75 2008-10-12 10:35:52Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons/lang/class_utils'
34
+ require 'commons/lang/object_utils'
35
+ require 'commons/lang/system_utils'
36
+
37
+ module Commons
38
+ module Lang
39
+ module Builder
40
+
41
+ class ToSStyle
42
+
43
+ attr_accessor \
44
+ :use_class_name, :use_short_className, :use_identity_hash_code,
45
+ :use_field_names, :default_full_detail, :array_content_detail,
46
+ :array_start, :array_end, :array_separator, :content_start, :content_end,
47
+ :field_name_value_separator, :field_separator, :field_separator_at_start,
48
+ :field_separator_at_end, :nil_text, :size_start_text, :size_end_text,
49
+ :summary_object_start_text, :summary_object_end_text
50
+
51
+ attr_reader :registry
52
+
53
+ protected \
54
+ :registry, :use_class_name, :use_short_className, :use_identity_hash_code,
55
+ :use_field_names, :default_full_detail, :array_content_detail,
56
+ :array_start, :array_end, :array_separator, :content_start, :content_end,
57
+ :field_name_value_separator, :field_separator, :field_separator_at_start,
58
+ :field_separator_at_end, :nil_text, :size_start_text, :size_end_text,
59
+ :summary_object_start_text, :summary_object_end_text
60
+
61
+
62
+ def initialize
63
+ super()
64
+
65
+ @registry = Hash.new
66
+
67
+ # Whether to use the field names, the default is true.
68
+ @use_field_names = true
69
+
70
+ # Whether to use the class name, the default is true.
71
+ @use_class_name = true
72
+
73
+ # Whether to use short class names, the default is false.
74
+ @use_short_className = false
75
+
76
+ # Whether to use the identity hash code, the default is true.
77
+ @use_identity_hash_code = true
78
+
79
+ # The content start '{'.
80
+ @content_start = '{'
81
+
82
+ # The content end '}'.
83
+ @content_end = '}'
84
+
85
+ # The field name value separator '='.
86
+ @field_name_value_separator = '='
87
+
88
+ # Whether the field separator should be added before any other fields.
89
+ @field_separator_at_start = false
90
+
91
+ # Whether the field separator should be added after any other fields.
92
+ @field_separator_at_end = false
93
+
94
+ # The field separator ','.
95
+ @field_separator = ','
96
+
97
+ # The array start '['.
98
+ @array_start = '['
99
+
100
+ # The array separator ','.
101
+ @array_separator = ','
102
+
103
+ # The detail for array content.
104
+ @array_content_detail = true
105
+
106
+ # The array end ']'.
107
+ @array_end = ']'
108
+
109
+ # The value to use when fullDetail is nil,
110
+ # the default value is true.
111
+ @default_full_detail = true
112
+
113
+ # The nil text '<nil>'.
114
+ @nil_text = '<nil>'
115
+
116
+ # The summary size text start '<size'.
117
+ @size_start_text = '<size='
118
+
119
+ # The summary size text start '&gt;'.
120
+ @size_end_text = '>'
121
+
122
+ # The summary object text start '&lt;'.
123
+ @summary_object_start_text = '<'
124
+
125
+ # The summary object text start '&gt;'.
126
+ @summary_object_end_text = '>'
127
+ end
128
+
129
+
130
+ def registered?(value)
131
+ return @registry.has_key?(value.__id__)
132
+ end
133
+
134
+
135
+ def register(value)
136
+ if value != nil
137
+ @registry[value.__id__] = value
138
+ end
139
+ end
140
+
141
+
142
+ def unregister(value)
143
+ @registry.delete(value.__id__);
144
+ end
145
+
146
+
147
+ def append_super(buffer, super_to_s)
148
+ append_to_s(buffer, super_to_s)
149
+ end
150
+
151
+
152
+ def append_to_s(buffer, to_s)
153
+ if to_s != nil
154
+ pos1 = to_s.index(@content_start) + @content_start.length
155
+ pos2 = to_s.rindex(@content_end)
156
+ if pos1 != pos2 && pos1 >= 0 && pos2 >= 0
157
+ data = to_s[pos1 ... pos2]
158
+ if @field_separator_at_start
159
+ remove_last_field_separator(buffer)
160
+ end
161
+ buffer.concat(data)
162
+ append_field_separator(buffer)
163
+ end
164
+ end
165
+ end
166
+
167
+
168
+ def append_start(buffer, object)
169
+ if object != nil
170
+ append_class_name(buffer, object)
171
+ append_identity_hash_code(buffer, object)
172
+ append_content_start(buffer)
173
+ if @field_separator_at_start
174
+ append_field_separator(buffer)
175
+ end
176
+ end
177
+ end
178
+
179
+
180
+ def append_end(buffer, object)
181
+ if @field_separator_at_end == false
182
+ remove_last_field_separator(buffer)
183
+ end
184
+ append_content_end(buffer)
185
+ end
186
+
187
+
188
+ def remove_last_field_separator(buffer)
189
+ len = buffer.length
190
+ sep_len = @field_separator.length
191
+ if len > 0 && sep_len > 0 && len >= sep_len
192
+ match = true
193
+ (0 ... sep_len).each {|i|
194
+ if buffer[len - 1 - i] != @field_separator[sep_len - 1 - i]
195
+ match = false
196
+ break
197
+ end
198
+ }
199
+ if match
200
+ buffer[len - sep_len .. -1] = ''
201
+ end
202
+ end
203
+ end
204
+ protected :remove_last_field_separator
205
+
206
+ #-- ------------------------------------------------------------------------------
207
+ #++
208
+
209
+ def append(buffer, field_name, value, full_detail)
210
+ append_field_start(buffer, field_name)
211
+
212
+ if value == nil
213
+ append_nil_text(buffer, field_name)
214
+ else
215
+ append_internal(buffer, field_name, value, full_detail)
216
+ end
217
+
218
+ append_field_end(buffer, field_name)
219
+ end
220
+
221
+
222
+ def append_internal(buffer, field_name, value, detail)
223
+ if registered?(value)
224
+ append_cyclic_object(buffer, field_name, value)
225
+ return
226
+ end
227
+
228
+ register(value)
229
+
230
+ begin
231
+ case value
232
+ when Hash
233
+ if detail
234
+ append_hash_detail(buffer, field_name, value)
235
+ else
236
+ append_summary_size(buffer, field_name, value.size)
237
+ end
238
+ when Array
239
+ if detail
240
+ # We do not use Array#to_s
241
+ append_array_detail(buffer, field_name, value)
242
+ else
243
+ append_summary_size(buffer, field_name, value.size)
244
+ end
245
+ else
246
+ if detail
247
+ append_detail(buffer, field_name, value)
248
+ else
249
+ append_summary(buffer, field_name, value)
250
+ end
251
+ end
252
+ ensure
253
+ unregister(value)
254
+ end
255
+ end
256
+ protected :append_internal
257
+
258
+
259
+ def append_cyclic_object(buffer, field_name, value)
260
+ ObjectUtils.append_identity_to_s(buffer, value)
261
+ buffer.concat('...')
262
+ end
263
+ protected :append_cyclic_object
264
+
265
+
266
+ def append_summary(buffer, field_name, value)
267
+ buffer.concat(@summary_object_start_text)
268
+ buffer.concat(get_short_class_name(value.class))
269
+ buffer.concat(@summary_object_end_text)
270
+ end
271
+ protected :append_summary
272
+
273
+
274
+ def append_detail(buffer, field_name, value)
275
+ buffer.concat(value.to_s)
276
+ end
277
+ protected :append_detail
278
+
279
+ #-- ------------------------------------------------------------------------------
280
+ #++
281
+
282
+ def append_hash_detail(buffer, field_name, hash)
283
+ buffer.concat(@array_start)
284
+ i = 0
285
+ hash.each_pair {|key, value|
286
+ if i > 0
287
+ buffer.concat(@array_separator)
288
+ end
289
+ if value == nil
290
+ append_nil_text(buffer, field_name)
291
+ else
292
+ buffer.concat(key.to_s + '=>')
293
+ append_internal(buffer, field_name, value, @array_content_detail)
294
+ end
295
+ i += 1
296
+ }
297
+ buffer.concat(@array_end)
298
+ end
299
+ protected :append_hash_detail
300
+
301
+
302
+ def append_array_detail(buffer, field_name, array)
303
+ buffer.concat(@array_start)
304
+ array.each_index {|i|
305
+ item = array[i]
306
+ if i > 0
307
+ buffer.concat(@array_separator)
308
+ end
309
+ if item == nil
310
+ append_nil_text(buffer, field_name)
311
+ else
312
+ append_internal(buffer, field_name, item, @array_content_detail)
313
+ end
314
+ }
315
+ buffer.concat(@array_end)
316
+ end
317
+ protected :append_array_detail
318
+
319
+ #-- ------------------------------------------------------------------------------
320
+ #++
321
+
322
+ def append_class_name(buffer, object)
323
+ if @use_class_name && object != nil
324
+ if @use_short_className
325
+ buffer.concat(get_short_class_name(object.class))
326
+ else
327
+ buffer.concat(object.class.name)
328
+ end
329
+ end
330
+ end
331
+ protected :append_class_name
332
+
333
+
334
+ def append_identity_hash_code(buffer, object)
335
+ if @use_identity_hash_code && object != nil
336
+ buffer.concat(':')
337
+ buffer.concat('0x' + object.__id__.to_s(16))
338
+ end
339
+ end
340
+ protected :append_identity_hash_code
341
+
342
+
343
+ def append_content_start(buffer)
344
+ buffer.concat(@content_start)
345
+ end
346
+ protected :append_content_start
347
+
348
+
349
+ def append_content_end(buffer)
350
+ buffer.concat(@content_end)
351
+ end
352
+ protected :append_content_end
353
+
354
+
355
+ def append_nil_text(buffer, field_name)
356
+ buffer.concat(@nil_text)
357
+ end
358
+ protected :append_nil_text
359
+
360
+
361
+ def append_field_separator(buffer)
362
+ buffer.concat(@field_separator)
363
+ end
364
+ protected :append_field_separator
365
+
366
+
367
+ def append_field_start(buffer, field_name)
368
+ if @use_field_names && field_name != nil
369
+ buffer.concat(field_name)
370
+ buffer.concat(@field_name_value_separator)
371
+ end
372
+ end
373
+ protected :append_field_start
374
+
375
+
376
+ def append_field_end(buffer, field_name)
377
+ append_field_separator(buffer)
378
+ end
379
+ protected :append_field_end
380
+
381
+
382
+ def append_summary_size(buffer, field_name, size)
383
+ buffer.concat(@size_start_text)
384
+ buffer.concat(size.to_s)
385
+ buffer.concat(@size_end_text)
386
+ end
387
+ protected :append_summary_size
388
+
389
+
390
+ def get_short_class_name(clazz)
391
+ return ClassUtils.get_short_class_name(clazz)
392
+ end
393
+ protected :get_short_class_name
394
+
395
+ #-- ------------------------------------------------------------------------------
396
+ #++
397
+
398
+ class DefaultToSStyle < ToSStyle
399
+ def initialize
400
+ super()
401
+ end
402
+
403
+
404
+ def read_resolve
405
+ return ToSStyle::DEFAULT_STYLE
406
+ end
407
+ private :read_resolve
408
+ end
409
+
410
+
411
+ # ToSStyle that does not print out the field names.
412
+ class NoFieldNameToSStyle < ToSStyle
413
+ def initialize
414
+ super()
415
+ self.use_field_names = false
416
+ end
417
+
418
+
419
+ def read_resolve
420
+ return ToSStyle::NO_FIELD_NAMES_STYLE
421
+ end
422
+ private :read_resolve
423
+ end
424
+
425
+
426
+ # ToSStyle that prints out the short
427
+ # class name and no identity hashcode.
428
+ class ShortPrefixToSStyle < ToSStyle
429
+ def initialize
430
+ super()
431
+ self.use_short_className = true
432
+ self.use_identity_hash_code = false
433
+ end
434
+
435
+
436
+ def read_resolve
437
+ return ToSStyle::SHORT_PREFIX_STYLE
438
+ end
439
+ private :read_resolve
440
+ end
441
+
442
+
443
+ # ToSStyle that does not print out the
444
+ # classname, identity hashcode, content start or field name.
445
+ class SimpleToSStyle < ToSStyle
446
+ def initialize
447
+ super()
448
+ self.use_class_name = false
449
+ self.use_identity_hash_code = false
450
+ self.use_field_names = false
451
+ self.content_start = ''
452
+ self.content_end = ''
453
+ end
454
+
455
+
456
+ def read_resolve
457
+ return ToSStyle::SIMPLE_STYLE
458
+ end
459
+ private :read_resolve
460
+ end
461
+
462
+
463
+ # ToSStyle that outputs on multiple lines.
464
+ class MultiLineToSStyle < ToSStyle
465
+ def initialize
466
+ super()
467
+ self.content_start = '{'
468
+ self.field_separator = SystemUtils.line_separator + ' '
469
+ self.field_separator_at_start = true
470
+ self.content_end = SystemUtils.line_separator + '}'
471
+ end
472
+
473
+ def read_resolve
474
+ return ToSStyle::MULTI_LINE_STYLE
475
+ end
476
+ private :read_resolve
477
+ end
478
+
479
+
480
+ DEFAULT_STYLE = DefaultToSStyle.new
481
+
482
+ MULTI_LINE_STYLE = MultiLineToSStyle.new
483
+
484
+ NO_FIELD_NAMES_STYLE = NoFieldNameToSStyle.new
485
+
486
+ SHORT_PREFIX_STYLE = ShortPrefixToSStyle.new
487
+
488
+ SIMPLE_STYLE = SimpleToSStyle.new
489
+ end
490
+
491
+ end
492
+ end
493
+ end