XProperties 1.1.4 → 2.0.11

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,291 +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
-
179
- # Converts chars to escape sequences.
180
- #
181
- # @param value String to convert.
182
- # @return [String]
183
- def save_convert(value, is_convert_key = false)
184
- buffer = []
185
- if is_convert_key
186
- value = value.gsub /[ ]/, "\\ "
187
- value = value.gsub /[=]/, "\\="
188
- value = value.gsub /:/, "\\:"
189
- end
190
- value = value.gsub /\\/, "\\\\"
191
- value = value.gsub /\t/, "\\t"
192
- value = value.gsub /\r/, "\\r"
193
- value = value.gsub /\n/, "\\n"
194
- value = value.gsub /\f/, "\\f"
195
- value.chars.each do |char|
196
- if char.ord < 0x20 or char.ord > 0x7e
197
- char = "\\u" + "%04x" % char.ord
198
- end
199
- buffer.append char
200
- end
201
- buffer.join
202
- end
203
-
204
- # Setting the value of a property.
205
- #
206
- # @param key Property name.
207
- # @param value Value to set for the property.
208
- # @return [NilClass]
209
- def set_property(key, value)
210
- @properties[key] = value
211
- end
212
-
213
- # Getting the value of a property.
214
- #
215
- # @param key Property name.
216
- # @param default Default value if property does not exist.
217
- # @raise KeyError When property does not exist.
218
- # @return [String]
219
- def get_property(key, default = "")
220
- if @properties.key? key
221
- return @properties[key]
222
- end
223
- default
224
- end
225
-
226
- # Deleting the value of a property.
227
- #
228
- # @param key Property name.
229
- # @raise KeyError When property does not exist.
230
- # @return [NilClass]
231
- def delete_property(key)
232
- @properties.delete key
233
- end
234
-
235
- # Remove all properties
236
- #
237
- # @return [NilClass]
238
- def clear
239
- @properties.clear
240
- end
241
-
242
- # Getting the list of properties name.
243
- #
244
- # @return [Array]
245
- def keys
246
- @properties.keys
247
- end
248
-
249
- # Getting the list of properties value.
250
- #
251
- # @return [Array]
252
- def values
253
- @properties.values
254
- end
255
-
256
- # Getting the list of properties key-value pair.
257
- #
258
- # @return [Array]
259
- def items
260
- @properties.keys.zip @properties.values
261
- end
262
-
263
- # Get the number of properties.
264
- #
265
- # @return [Integer]
266
- def count
267
- @properties.count
268
- end
269
-
270
- # Returns a value whether the key exists.
271
- #
272
- # @return [Boolean]
273
- def contains?(key)
274
- @properties.key? key
275
- end
276
-
277
- # Returns a value whether two object instances are equal.
278
- #
279
- # @param other Other object to compare.
280
- # @return [Boolean]
281
- def equals?(other)
282
- self == other
283
- end
284
-
285
- # Convert the current object to string.
286
- #
287
- # @return [String]
288
- def to_string
289
- JSON.dump @properties
290
- end
291
- end
1
+ require 'xproperties/properties'