XProperties 1.1.6 → 2.0.10

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.
data/lib/xproperties.rb CHANGED
@@ -1,293 +1 @@
1
- require 'json'
2
- require 'rexml/document'
3
-
4
-
5
- # Parse properties file.
6
- class Properties
7
- def initialize
8
- @properties = { }
9
- end
10
-
11
- # Setting the value of a property.
12
- #
13
- # @param key Property name.
14
- # @param value Value to set for the property.
15
- # @return [NilClass]
16
- def []=(key, value)
17
- set_property key, value
18
- nil?
19
- end
20
-
21
- # Getting the value of a property.
22
- #
23
- # @param key Property name.
24
- # @return [String]
25
- def [](key)
26
- get_property key
27
- end
28
-
29
- # Get an iterator object.
30
- #
31
- # @return [Array]
32
- def each
33
- items.each do |item|
34
- yield item
35
- end
36
- end
37
-
38
- # Convert the current object to string.
39
- #
40
- # @return [String]
41
- def to_s
42
- to_string
43
- end
44
-
45
- # Loads a property list(key and element pairs) from the .properties file.
46
- # The file is assumed to use the ISO-8859-1(Latin1) character encoding.
47
- #
48
- # @param file_name .properties file name.
49
- # @raise IOError When an error occurs while reading the file.
50
- # @raise ArgumentError When the file contains a malformed
51
- # Unicode escape sequence.
52
- # @return [void]
53
- def load(file_name)
54
- temp = File.read file_name, :encoding => "iso-8859-1"
55
- temp = temp.gsub /^[ \t]*[#!].*[\r\n\f]+/, ""
56
- temp = temp.gsub /(?<!\\)\\[ \t]*[\r\n\f]+[ \t]*/, ""
57
- raw_data = temp.split /[\r\n\f]+/
58
- raw_data.each do |i|
59
- pair = i.split /(?<!\\)[ \t]*(?<!\\)[=:][ \t]*/, 2
60
- if pair[0] != nil and pair[0].strip != ""
61
- key = load_convert pair[0], :is_convert_key => true
62
- if pair.length == 2
63
- value = load_convert pair[1]
64
- @properties[key] = value
65
- else
66
- @properties[key] = ""
67
- end
68
- end
69
- end
70
- nil?
71
- end
72
-
73
- # Saves a property list(key and element pairs) to the .properties file.
74
- # The file will be written in ISO-8859-1(Latin1) character encoding.
75
- #
76
- # @param file_name .properties file name.
77
- # @raise IOError When an error occurs while writing the file.
78
- # @return [NilClass]
79
- def save(file_name)
80
- file = File.open file_name, "w", :encoding => "iso-8859-1"
81
- @properties.each_pair do |k, v|
82
- key = save_convert k, :is_convert_key => true
83
- value = save_convert v
84
- pair = key + "=" + value
85
- file.write pair + "\n"
86
- end
87
- file.close
88
- nil?
89
- end
90
-
91
- # Loads a property list(key and element pairs) from the .xml file.
92
- #
93
- # The XML document will have the following DOCTYPE declaration:
94
- # <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
95
- #
96
- # @param file_name .xml file name.
97
- # @raise IOError When an error occurs while reading the file.
98
- # @raise ParseException When the XML file is malformed.
99
- # @return [NilClass]
100
- def load_from_xml(file_name)
101
- raw = File.read file_name, :encoding => "utf-8"
102
- doc = REXML::Document.new raw
103
- pairs = doc.root.elements
104
- pairs.each "entry" do |pair|
105
- if pair.attributes.key? "key"
106
- key = pair.attributes["key"]
107
- value = pair.text
108
- @properties[key] = value
109
- else
110
- raise ParseException "Malformed XML format."
111
- end
112
- end
113
- end
114
-
115
- # Saves a property list(key and element pairs) from the .xml file.
116
- #
117
- # The XML document will have the following DOCTYPE declaration:
118
- # <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
119
- #
120
- # @param file_name .xml file name.
121
- # @raise IOError When an error occurs while writing the file.
122
- # @return [NilClass]
123
- def save_to_xml(file_name)
124
- xml_declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
125
- doctype = "<!DOCTYPE properties SYSTEM " +
126
- "\"http://java.sun.com/dtd/properties.dtd\">\n"
127
- root = REXML::Element.new "properties"
128
- @properties.each do |k, v|
129
- entry = REXML::Element.new "entry"
130
- entry.add_attribute "key", k
131
- entry.text = v
132
- root.add_element entry
133
- end
134
- file = File.open file_name, "w", :encoding => "utf-8"
135
- file.write xml_declaration
136
- file.write doctype
137
- doc = REXML::Document.new
138
- doc.add_element root
139
- doc.write file, 2, :encoding => "utf-8"
140
- end
141
-
142
- # Converts escape sequences to chars.
143
- #
144
- # @param value String to convert.
145
- # @raise ArgumentError When the file contains a malformed
146
- # Unicode escape sequence.
147
- # @return [String]
148
- def load_convert(value, is_convert_key = false)
149
- if is_convert_key
150
- value = value.gsub /\\ /, " "
151
- value = value.gsub /\\=/, "="
152
- value = value.gsub /\\:/, ":"
153
- end
154
- value = value.gsub /\\\\/, "\\"
155
- value = value.gsub /\\t/, "\t"
156
- value = value.gsub /\\r/, "\r"
157
- value = value.gsub /\\n/, "\n"
158
- value = value.gsub /\\f/, "\f"
159
- escapes = value.scan(/\\u[A-F0-9]{4}/)
160
- escapes.each do |escape|
161
- temp = 0
162
- escape.chars[2..].each do |i|
163
- if "0123456789".include? i
164
- temp = (temp << 4) + i.ord - "0".ord
165
- elsif "abcdef".include? i
166
- temp = (temp << 4) + 10 + i.ord - "a".ord
167
- elsif "ABCDEF".include? i
168
- temp = (temp << 4) + 10 + i.ord - "A".ord
169
- else
170
- raise ArgumentError "Malformed \\uxxxx encoding."
171
- end
172
- end
173
- char = temp.chr "utf-8"
174
- value = value.gsub escape, char
175
- end
176
- value
177
- end
178
- private :load_convert
179
-
180
- # Converts chars to escape sequences.
181
- #
182
- # @param value String to convert.
183
- # @return [String]
184
- def save_convert(value, is_convert_key = false)
185
- buffer = []
186
- if is_convert_key
187
- value = value.gsub /[ ]/, "\\ "
188
- value = value.gsub /[=]/, "\\="
189
- value = value.gsub /:/, "\\:"
190
- end
191
- value = value.gsub /\\/, "\\\\"
192
- value = value.gsub /\t/, "\\t"
193
- value = value.gsub /\r/, "\\r"
194
- value = value.gsub /\n/, "\\n"
195
- value = value.gsub /\f/, "\\f"
196
- value.chars.each do |char|
197
- if char.ord < 0x20 or char.ord > 0x7e
198
- char = "\\u" + "%04x" % char.ord
199
- end
200
- buffer.append char
201
- end
202
- buffer.join
203
- end
204
- private :save_convert
205
-
206
- # Setting the value of a property.
207
- #
208
- # @param key Property name.
209
- # @param value Value to set for the property.
210
- # @return [NilClass]
211
- def set_property(key, value)
212
- @properties[key] = value
213
- end
214
-
215
- # Getting the value of a property.
216
- #
217
- # @param key Property name.
218
- # @param default Default value if property does not exist.
219
- # @raise KeyError When property does not exist.
220
- # @return [String]
221
- def get_property(key, default = "")
222
- if @properties.key? key
223
- return @properties[key]
224
- end
225
- default
226
- end
227
-
228
- # Deleting the value of a property.
229
- #
230
- # @param key Property name.
231
- # @raise KeyError When property does not exist.
232
- # @return [NilClass]
233
- def delete_property(key)
234
- @properties.delete key
235
- end
236
-
237
- # Remove all properties
238
- #
239
- # @return [NilClass]
240
- def clear
241
- @properties.clear
242
- end
243
-
244
- # Getting the list of properties name.
245
- #
246
- # @return [Array]
247
- def keys
248
- @properties.keys
249
- end
250
-
251
- # Getting the list of properties value.
252
- #
253
- # @return [Array]
254
- def values
255
- @properties.values
256
- end
257
-
258
- # Getting the list of properties key-value pair.
259
- #
260
- # @return [Array]
261
- def items
262
- @properties.keys.zip @properties.values
263
- end
264
-
265
- # Get the number of properties.
266
- #
267
- # @return [Integer]
268
- def count
269
- @properties.count
270
- end
271
-
272
- # Returns a value whether the key exists.
273
- #
274
- # @return [Boolean]
275
- def contains?(key)
276
- @properties.key? key
277
- end
278
-
279
- # Returns a value whether two object instances are equal.
280
- #
281
- # @param other Other object to compare.
282
- # @return [Boolean]
283
- def equals?(other)
284
- self == other
285
- end
286
-
287
- # Convert the current object to string.
288
- #
289
- # @return [String]
290
- def to_string
291
- JSON.dump @properties
292
- end
293
- end
1
+ require 'xproperties/properties'