rcommons 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
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,77 @@
1
+ # = Class Loader
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: class_loader.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
+ require 'commons/ruby/string'
35
+
36
+ module Commons
37
+ module Lang
38
+
39
+ class ClassLoader
40
+
41
+ # Load the class definition file (feature).
42
+ # Feature guess pattern:
43
+ # `Commons::Lang::ClassLoader' -> `commons/lang/class_loader'
44
+ # raise:: LoadError if the class definition file is not found.
45
+ def self.load(class_name)
46
+ begin
47
+ eval(class_name)
48
+ rescue NameError
49
+ require class_name \
50
+ .replace_all(/([a-z0-9])([A-Z])/, '\\1_\\2') \
51
+ .replace_all(/::/, '/') \
52
+ .downcase
53
+ end
54
+ end
55
+
56
+
57
+ # Get the absolute path of the given resource name.
58
+ # Resource name is relative path (delimiter: '/')on $LOAD_PATH.
59
+ # return:: the absolute path, or nil if the resource is not found.
60
+ def self.get_resource(name)
61
+ if name == nil || name == ''
62
+ return nil
63
+ end
64
+
65
+ file_separator = SystemUtils.file_separator
66
+ name = name.replace_all(/\//, file_separator)
67
+ matched_path = $LOAD_PATH.find {|path|
68
+ FileTest.exist?(path + file_separator + name)
69
+ }
70
+
71
+ return matched_path == nil ? nil : matched_path + file_separator + name
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,50 @@
1
+ # = Class 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: class_utils.rb 74 2008-10-12 02:17:15Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ module Commons
34
+ module Lang
35
+
36
+ class ClassUtils
37
+
38
+ def initialize
39
+ super()
40
+ end
41
+
42
+
43
+ def self.get_short_class_name(klass)
44
+ return klass.name.split(/::/).last
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,61 @@
1
+ # = Object 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: object_utils.rb 74 2008-10-12 02:17:15Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ module Commons
34
+ module Lang
35
+
36
+ class ObjectUtils
37
+
38
+ def initialize
39
+ super()
40
+ end
41
+
42
+
43
+ # Append the object id (hex string) to the given string.
44
+ # +buffer+:: string buffer.
45
+ # +object+:: a instance.
46
+ def self.append_identity_to_s(buffer, object)
47
+ if object == nil
48
+ return nil
49
+ end
50
+ if buffer == nil
51
+ buffer = String.new('')
52
+ end
53
+
54
+ return buffer.concat(
55
+ '#<' + object.class.name + ':0x' + object.__id__.to_s(16) + '>')
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,41 @@
1
+ # = State Error
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: state_error.rb 74 2008-10-12 02:17:15Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ module Commons
34
+ module Lang
35
+
36
+ class StateError < RuntimeError
37
+ end
38
+
39
+ end
40
+ end
41
+
@@ -0,0 +1,113 @@
1
+ # = System 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: system_utils.rb 74 2008-10-12 02:17:15Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'facets/rbsystem'
34
+
35
+ module Commons
36
+ module Lang
37
+
38
+ class SystemUtils
39
+ @@current_platform = nil
40
+
41
+ @@line_separator = nil
42
+
43
+ @@file_separator = nil
44
+
45
+ @@path_separator = nil
46
+
47
+
48
+ def initialize
49
+ super()
50
+ end
51
+
52
+
53
+ def self.current_platform
54
+ if @@current_platform == nil
55
+ @@current_platform = System.current_platform
56
+ end
57
+
58
+ return @@current_platform
59
+ end
60
+
61
+
62
+ def self.line_separator
63
+ if @@line_separator == nil
64
+ if os_windows?
65
+ @@line_separator = "\r\n"
66
+ else
67
+ @@line_separator = "\n"
68
+ end
69
+ end
70
+
71
+ return @@line_separator
72
+ end
73
+
74
+
75
+ def self.file_separator
76
+ if @@file_separator == nil
77
+ if File::ALT_SEPARATOR == nil
78
+ @@file_separator = File::SEPARATOR
79
+ else
80
+ @@file_separator = File::ALT_SEPARATOR
81
+ end
82
+ =begin
83
+ if os_windows?
84
+ @@file_separator = '\\'
85
+ else
86
+ @@file_separator = '/'
87
+ end
88
+ =end
89
+ end
90
+
91
+ return @@file_separator
92
+ end
93
+
94
+
95
+ def self.path_separator
96
+ if @@path_separator == nil
97
+ @@path_separator = File::PATH_SEPARATOR
98
+ end
99
+
100
+ return @@path_separator
101
+ end
102
+
103
+
104
+ def self.os_windows?
105
+ return current_platform.include?('mswin32')
106
+ end
107
+
108
+ # TODO
109
+
110
+ end
111
+
112
+ end
113
+ end
@@ -0,0 +1,47 @@
1
+ # = Date 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: date_utils.rb 76 2008-10-12 11:45:33Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ module Commons
34
+ module Lang
35
+ module Time
36
+
37
+ class DateUtils
38
+ MILLIS_PER_SECOND = 1000
39
+ MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND
40
+ MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE
41
+ MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR
42
+
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,279 @@
1
+ # = Duration Format Utilities
2
+ # (based on org.apache.commons.lang.time.DurationFormatUtils)
3
+ #
4
+ #--
5
+ # Ruby version 1.8
6
+ #
7
+ # == Authors
8
+ # * Yomei Komiya
9
+ #
10
+ # == Copyright
11
+ # 2008 the original author or authors.
12
+ #
13
+ # == License
14
+ # Apache License 2.0
15
+ #
16
+ # Licensed under the Apache License, Version 2.0 (the "License");
17
+ # you may not use this file except in compliance with the License.
18
+ # You may obtain a copy of the License at
19
+ #
20
+ # http://www.apache.org/licenses/LICENSE-2.0
21
+ #
22
+ # Unless required by applicable law or agreed to in writing, software
23
+ # distributed under the License is distributed on an "AS IS" BASIS,
24
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25
+ # See the License for the specific language governing permissions and
26
+ # limitations under the License.
27
+ #++
28
+ # == Version
29
+ # SVN: $Id: duration_format_utils.rb 76 2008-10-12 11:45:33Z whitestar $
30
+ #
31
+ # == Since
32
+ # File available since Release 0.8.0
33
+
34
+ require 'commons/lang/time/date_utils'
35
+
36
+ module Commons
37
+ module Lang
38
+ module Time
39
+
40
+ class DurationFormatUtils
41
+ ISO_EXTENDED_FORMAT_PATTERN = "'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'"
42
+
43
+ TOKEN_y = :y
44
+ TOKEN_M = :M
45
+ TOKEN_d = :d
46
+ TOKEN_H = :H
47
+ TOKEN_m = :m
48
+ TOKEN_s = :s
49
+ TOKEN_S = :S
50
+
51
+
52
+ def initialize
53
+ super()
54
+ end
55
+
56
+
57
+ def self.format_duration_hms(duration_millis)
58
+ return format_duration(duration_millis, 'H:mm:ss.SSS')
59
+ end
60
+
61
+
62
+ def self.format_duration(duration_millis, format, pad_with_zeros = true)
63
+ tokens = lexx(format)
64
+
65
+ days = 0
66
+ hours = 0
67
+ minutes = 0
68
+ seconds = 0
69
+ milliseconds = 0
70
+
71
+ if Token.contains_token_with_value?(tokens, TOKEN_d)
72
+ days = duration_millis.div(DateUtils::MILLIS_PER_DAY)
73
+ duration_millis = duration_millis - (days * DateUtils::MILLIS_PER_DAY)
74
+ end
75
+ if Token.contains_token_with_value?(tokens, TOKEN_H)
76
+ hours = duration_millis.div(DateUtils::MILLIS_PER_HOUR)
77
+ duration_millis = duration_millis - (hours * DateUtils::MILLIS_PER_HOUR)
78
+ end
79
+ if Token.contains_token_with_value?(tokens, TOKEN_m)
80
+ minutes = duration_millis.div(DateUtils::MILLIS_PER_MINUTE)
81
+ duration_millis = duration_millis - (minutes * DateUtils::MILLIS_PER_MINUTE)
82
+ end
83
+ if Token.contains_token_with_value?(tokens, TOKEN_s)
84
+ seconds = duration_millis.div(DateUtils::MILLIS_PER_SECOND)
85
+ duration_millis = duration_millis - (seconds * DateUtils::MILLIS_PER_SECOND)
86
+ end
87
+ if Token.contains_token_with_value?(tokens, TOKEN_S)
88
+ milliseconds = duration_millis
89
+ end
90
+
91
+ return format(
92
+ tokens, 0, 0, days, hours, minutes, seconds, milliseconds, pad_with_zeros)
93
+ end
94
+
95
+
96
+ def self.format(
97
+ tokens,
98
+ years, months, days, hours, minutes, seconds, milliseconds,
99
+ pad_with_zeros)
100
+
101
+ buffer = String.new('')
102
+ last_output_seconds = false
103
+ tokens.each {|token|
104
+ value = token.value
105
+ count = token.count
106
+ if value.kind_of?(String)
107
+ buffer.concat(value.to_s)
108
+ else
109
+ if value.equal?(TOKEN_y)
110
+ buffer.concat(
111
+ pad_with_zeros ? years.to_s.rjust(count, '0') : years.to_s)
112
+ last_output_seconds = false
113
+ elsif value.equal?(TOKEN_M)
114
+ buffer.concat(
115
+ pad_with_zeros ? months.to_s.rjust(count, '0') : months.to_s)
116
+ last_output_seconds = false
117
+ elsif value.equal?(TOKEN_d)
118
+ buffer.concat(
119
+ pad_with_zeros ? days.to_s.rjust(count, '0') : days.to_s)
120
+ last_output_seconds = false
121
+ elsif value.equal?(TOKEN_H)
122
+ buffer.concat(
123
+ pad_with_zeros ? hours.to_s.rjust(count, '0') : hours.to_s)
124
+ last_output_seconds = false
125
+ elsif value.equal?(TOKEN_m)
126
+ buffer.concat(
127
+ pad_with_zeros ? minutes.to_s.rjust(count, '0') : minutes.to_s)
128
+ last_output_seconds = false
129
+ elsif value.equal?(TOKEN_s)
130
+ buffer.concat(
131
+ pad_with_zeros ? seconds.to_s.rjust(count, '0') : seconds.to_s)
132
+ last_output_seconds = true
133
+ elsif value.equal?(TOKEN_S)
134
+ if last_output_seconds
135
+ milliseconds += 1000
136
+ str = pad_with_zeros \
137
+ ? milliseconds.to_s.rjust(count, '0') : milliseconds.to_s
138
+ buffer.concat(str[1..-1])
139
+ else
140
+ buffer.concat(pad_with_zeros \
141
+ ? milliseconds.to_s.rjust(count, '0') : milliseconds.to_s)
142
+ end
143
+ last_output_seconds = false
144
+ end
145
+ end
146
+ }
147
+
148
+ return buffer
149
+ end
150
+
151
+
152
+ # Parses a classic date format string into Tokens
153
+ def self.lexx(format)
154
+ chars = format.split(//)
155
+ tokens = Array.new
156
+
157
+ in_literal = false
158
+ buffer = nil
159
+ previous = nil
160
+ chars.each {|char|
161
+ if in_literal && char != "'"
162
+ buffer.concat(char)
163
+ next
164
+ end
165
+ value = nil
166
+ case char
167
+ # TODO: Need to handle escaping of '
168
+ when "'"
169
+ if in_literal
170
+ buffer = nil
171
+ in_literal = false
172
+ else
173
+ buffer = String.new('')
174
+ tokens.push(Token.new(buffer))
175
+ in_literal = true
176
+ end
177
+ when 'y' then value = TOKEN_y
178
+ when 'M' then value = TOKEN_M
179
+ when 'd' then value = TOKEN_d
180
+ when 'H' then value = TOKEN_H
181
+ when 'm' then value = TOKEN_m
182
+ when 's' then value = TOKEN_s
183
+ when 'S' then value = TOKEN_S
184
+ else
185
+ if buffer == nil
186
+ buffer = String.new('')
187
+ tokens.push(Token.new(buffer))
188
+ end
189
+ buffer.concat(char)
190
+ end
191
+
192
+ if value != nil
193
+ if previous != nil && previous.value.equal?(value)
194
+ previous.increment
195
+ else
196
+ token = Token.new(value)
197
+ tokens.push(token)
198
+ previous = token
199
+ end
200
+ buffer = nil
201
+ end
202
+ }
203
+
204
+ return tokens
205
+ end
206
+
207
+
208
+ class Token
209
+ attr_reader :value, :count
210
+
211
+ def self.contains_token_with_value?(tokens, value)
212
+ tokens.each {|token|
213
+ if token.value.equal?(value)
214
+ return true
215
+ end
216
+ }
217
+ return false
218
+ end
219
+
220
+
221
+ def initialize(value, count = 1)
222
+ super()
223
+
224
+ @value = value
225
+ @count = count
226
+ end
227
+
228
+
229
+ def increment
230
+ @count += 1
231
+ end
232
+
233
+
234
+ def ==(other)
235
+ return self.eql?(other)
236
+ end
237
+
238
+
239
+ def eql?(other)
240
+ if other.kind_of?(Token)
241
+ if !@value.class.equal?(other.value.class)
242
+ return false
243
+ end
244
+ if @count != other.count
245
+ return false
246
+ end
247
+ if @value.kind_of?(String)
248
+ return @value.to_s == other.value.to_s
249
+ elsif @value.kind_of?(Numeric)
250
+ return @value == other.value
251
+ else
252
+ return @value.equal?(other.value)
253
+ end
254
+ end
255
+
256
+ return false
257
+ end
258
+
259
+
260
+ def hash
261
+ return @value.hash
262
+ end
263
+
264
+
265
+ def to_s
266
+ return @value.to_s * @count
267
+ end
268
+
269
+
270
+ def to_str
271
+ return self.to_s
272
+ end
273
+ end # Token
274
+
275
+ end # DurationFormatUtils
276
+
277
+ end
278
+ end
279
+ end