XProperties 1.0.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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/XProperties/xproperties.rb +289 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 109bc45ec217298a983afcf67f24ec3e553a5382925eca641cd3dd80f1a0e602
4
+ data.tar.gz: f97746330aea41e11e73294b69658c3a760b66f06898628aec685853446e22de
5
+ SHA512:
6
+ metadata.gz: e5a0bf2fec81d7b7e78e8a69691b388adfe6410c8ee125045fff34d47c5b4e6c2abf55196081a33b965511384b05cddd6d15d1f092fe553473ba718d5e7bd985
7
+ data.tar.gz: 5aa99ef231b2feb6b5e98066449b6534339cb4cd370f5fced34b968cc3c7e00f106911991f9e3290e749296188dc64ed15b6a01d1405cb26a2e15413108b20ba
@@ -0,0 +1,289 @@
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
+ root = doc.root
104
+ pairs = root.elements
105
+ pairs.each("entry") do |pair|
106
+ if pair.attributes.key? "key"
107
+ key = pair.attributes["key"]
108
+ value = pair.text
109
+ @properties[key] = value
110
+ else
111
+ raise ParseException("Malformed XML format.")
112
+ end
113
+ end
114
+ end
115
+
116
+ # Saves a property list(key and element pairs) from the .xml file.
117
+ #
118
+ # The XML document will have the following DOCTYPE declaration:
119
+ # <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
120
+ #
121
+ # @param file_name .xml file name.
122
+ # @raise IOError When an error occurs while writing the file.
123
+ # @return [NilClass]
124
+ def save_to_xml(file_name)
125
+ xml_declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
126
+ doctype = "<!DOCTYPE properties SYSTEM " +
127
+ "\"http://java.sun.com/dtd/properties.dtd\">\n"
128
+ root = REXML::Element.new "properties"
129
+ @properties.each do |k, v|
130
+ entry = REXML::Element.new "entry"
131
+ entry.add_attribute "key", k
132
+ entry.text = v
133
+ root.add_element entry
134
+ end
135
+ file = File.open file_name, "w", :encoding => "utf-8"
136
+ file.write xml_declaration
137
+ file.write doctype
138
+ doc = REXML::Document.new
139
+ doc.add_element root
140
+ doc.write file, 2, :encoding => "utf-8"
141
+ end
142
+
143
+ # Converts escape sequences to chars.
144
+ #
145
+ # @param value String to convert.
146
+ # @raise ArgumentError When the file contains a malformed
147
+ # Unicode escape sequence.
148
+ # @return [String]
149
+ def load_convert(value, is_convert_key = false)
150
+ if is_convert_key
151
+ value = value.gsub /\\ /, " "
152
+ end
153
+ value = value.gsub /\\t/, "\t"
154
+ value = value.gsub /\\r/, "\r"
155
+ value = value.gsub /\\n/, "\n"
156
+ value = value.gsub /\\f/, "\f"
157
+ escapes = value.scan(/\\u[A-F0-9]{4}/)
158
+ escapes.each do |escape|
159
+ temp = 0
160
+ escape.chars[2..].each do |i|
161
+ if "0123456789".include? i
162
+ temp = (temp << 4) + i.ord - "0".ord
163
+ elsif "abcdef".include? i
164
+ temp = (temp << 4) + 10 + i.ord - "a".ord
165
+ elsif "ABCDEF".include? i
166
+ temp = (temp << 4) + 10 + i.ord - "A".ord
167
+ else
168
+ raise ArgumentError("Malformed \\uxxxx encoding.")
169
+ end
170
+ end
171
+ char = temp.chr "utf-8"
172
+ value = value.gsub escape, char
173
+ end
174
+ value
175
+ end
176
+
177
+ # Converts chars to escape sequences.
178
+ #
179
+ # @param value String to convert.
180
+ # @return [String]
181
+ def save_convert(value, is_convert_key = false)
182
+ buffer = []
183
+ if is_convert_key
184
+ value = value.gsub /[ ]/, "\\ "
185
+ value = value.gsub /[=]/, "\\="
186
+ value = value.gsub /:/, "\\:"
187
+ end
188
+ value = value.gsub /\\/, "\\\\"
189
+ value = value.gsub /\t/, "\\t"
190
+ value = value.gsub /\r/, "\\r"
191
+ value = value.gsub /\n/, "\\n"
192
+ value = value.gsub /\f/, "\\f"
193
+ value.chars.each do |char|
194
+ if char.ord < 0x20 or char.ord > 0x7e
195
+ char = "\\u" + "%04x" % char.ord
196
+ end
197
+ buffer.append char
198
+ end
199
+ buffer.join
200
+ end
201
+
202
+ # Setting the value of a property.
203
+ #
204
+ # @param key Property name.
205
+ # @param value Value to set for the property.
206
+ # @return [NilClass]
207
+ def set_property(key, value)
208
+ @properties[key] = value
209
+ end
210
+
211
+ # Getting the value of a property.
212
+ #
213
+ # @param key Property name.
214
+ # @param default Default value if property does not exist.
215
+ # @raise KeyError When property does not exist.
216
+ # @return [String]
217
+ def get_property(key, default = "")
218
+ if @properties.key? key
219
+ return @properties[key]
220
+ end
221
+ default
222
+ end
223
+
224
+ # Deleting the value of a property.
225
+ #
226
+ # @param key Property name.
227
+ # @raise KeyError When property does not exist.
228
+ # @return [NilClass]
229
+ def delete_property(key)
230
+ @properties.delete(key)
231
+ end
232
+
233
+ # Remove all properties
234
+ #
235
+ # @return [NilClass]
236
+ def clear
237
+ @properties.clear
238
+ end
239
+
240
+ # Getting the list of properties name.
241
+ #
242
+ # @return [Array]
243
+ def keys
244
+ @properties.keys
245
+ end
246
+
247
+ # Getting the list of properties value.
248
+ #
249
+ # @return [Array]
250
+ def values
251
+ @properties.values
252
+ end
253
+
254
+ # Getting the list of properties key-value pair.
255
+ #
256
+ # @return [Array]
257
+ def items
258
+ @properties.keys.zip(@properties.values)
259
+ end
260
+
261
+ # Get the number of properties.
262
+ #
263
+ # @return [Integer]
264
+ def count
265
+ @properties.count
266
+ end
267
+
268
+ # Returns a value whether the key exists.
269
+ #
270
+ # @return [Boolean]
271
+ def contains?(key)
272
+ @properties.key? key
273
+ end
274
+
275
+ # Returns a value whether two object instances are equal.
276
+ #
277
+ # @param other Other object to compare.
278
+ # @return [Boolean]
279
+ def equals?(other)
280
+ self == other
281
+ end
282
+
283
+ # Convert the current object to string.
284
+ #
285
+ # @return [String]
286
+ def to_string
287
+ JSON.dump(@properties).to_s
288
+ end
289
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: XProperties
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Duelit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-02-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Cross-Language .properties file parser
14
+ email: jyoon07dev@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - XProperties/xproperties.rb
20
+ homepage: https://github.com/Duelit/XProperties
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.3.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Cross-Language .properties file parser
43
+ test_files: []