Class: Properties
- Inherits:
-
Object
- Object
- Properties
- Defined in:
- lib/xproperties.rb
Overview
Parse properties file.
Instance Method Summary collapse
-
#[](key) ⇒ String
Getting the value of a property.
-
#[]=(key, value) ⇒ NilClass
Setting the value of a property.
-
#clear ⇒ NilClass
Remove all properties.
-
#contains?(key) ⇒ Boolean
Returns a value whether the key exists.
-
#count ⇒ Integer
Get the number of properties.
-
#delete_property(key) ⇒ NilClass
Deleting the value of a property.
-
#each ⇒ Array
Get an iterator object.
-
#equals?(other) ⇒ Boolean
Returns a value whether two object instances are equal.
-
#get_property(key, default = "") ⇒ String
Getting the value of a property.
-
#initialize ⇒ Properties
constructor
A new instance of Properties.
-
#items ⇒ Array
Getting the list of properties key-value pair.
-
#keys ⇒ Array
Getting the list of properties name.
-
#load(file_name) ⇒ void
Loads a property list(key and element pairs) from the .properties file.
- #load_convert(value, is_convert_key = false) ⇒ Object
-
#load_from_xml(file_name) ⇒ NilClass
Loads a property list(key and element pairs) from the .xml file.
-
#save(file_name) ⇒ NilClass
Saves a property list(key and element pairs) to the .properties file.
- #save_convert(value, is_convert_key = false) ⇒ Object
-
#save_to_xml(file_name) ⇒ NilClass
Saves a property list(key and element pairs) from the .xml file.
-
#set_property(key, value) ⇒ NilClass
Setting the value of a property.
-
#to_s ⇒ String
Convert the current object to string.
-
#to_string ⇒ String
Convert the current object to string.
-
#values ⇒ Array
Getting the list of properties value.
Constructor Details
#initialize ⇒ Properties
Returns a new instance of Properties.
7 8 9 |
# File 'lib/xproperties.rb', line 7 def initialize @properties = { } end |
Instance Method Details
#[](key) ⇒ String
Getting the value of a property.
25 26 27 |
# File 'lib/xproperties.rb', line 25 def [](key) get_property key end |
#[]=(key, value) ⇒ NilClass
Setting the value of a property.
16 17 18 19 |
# File 'lib/xproperties.rb', line 16 def []=(key, value) set_property key, value nil? end |
#clear ⇒ NilClass
Remove all properties
240 241 242 |
# File 'lib/xproperties.rb', line 240 def clear @properties.clear end |
#contains?(key) ⇒ Boolean
Returns a value whether the key exists.
275 276 277 |
# File 'lib/xproperties.rb', line 275 def contains?(key) @properties.key? key end |
#count ⇒ Integer
Get the number of properties.
268 269 270 |
# File 'lib/xproperties.rb', line 268 def count @properties.count end |
#delete_property(key) ⇒ NilClass
Deleting the value of a property.
233 234 235 |
# File 'lib/xproperties.rb', line 233 def delete_property(key) @properties.delete key end |
#each ⇒ Array
Get an iterator object.
32 33 34 35 36 |
# File 'lib/xproperties.rb', line 32 def each items.each do |item| yield item end end |
#equals?(other) ⇒ Boolean
Returns a value whether two object instances are equal.
283 284 285 |
# File 'lib/xproperties.rb', line 283 def equals?(other) self == other end |
#get_property(key, default = "") ⇒ String
Getting the value of a property.
221 222 223 224 225 226 |
# File 'lib/xproperties.rb', line 221 def get_property(key, default = "") if @properties.key? key return @properties[key] end default end |
#items ⇒ Array
Getting the list of properties key-value pair.
261 262 263 |
# File 'lib/xproperties.rb', line 261 def items @properties.keys.zip @properties.values end |
#keys ⇒ Array
Getting the list of properties name.
247 248 249 |
# File 'lib/xproperties.rb', line 247 def keys @properties.keys end |
#load(file_name) ⇒ void
This method returns an undefined value.
Loads a property list(key and element pairs) from the .properties file. The file is assumed to use the ISO-8859-1(Latin1) character encoding.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/xproperties.rb', line 53 def load(file_name) temp = File.read file_name, :encoding => "iso-8859-1" temp = temp.gsub /^[ \t]*[#!].*[\r\n\f]+/, "" temp = temp.gsub /(?<!\\)\\[ \t]*[\r\n\f]+[ \t]*/, "" raw_data = temp.split /[\r\n\f]+/ raw_data.each do |i| pair = i.split /(?<!\\)[ \t]*(?<!\\)[=:][ \t]*/, 2 if pair[0] != nil and pair[0].strip != "" key = load_convert pair[0], :is_convert_key => true if pair.length == 2 value = load_convert pair[1] @properties[key] = value else @properties[key] = "" end end end nil? end |
#load_convert(value, is_convert_key = false) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/xproperties.rb', line 149 def load_convert(value, is_convert_key = false) if is_convert_key value = value.gsub /\\ /, " " value = value.gsub /\\=/, "=" value = value.gsub /\\:/, ":" end value = value.gsub /\\\\/, "\\" value = value.gsub /\\t/, "\t" value = value.gsub /\\r/, "\r" value = value.gsub /\\n/, "\n" value = value.gsub /\\f/, "\f" escapes = value.scan(/\\u[A-F0-9]{4}/) escapes.each do |escape| temp = 0 escape.chars[2..].each do |i| if "0123456789".include? i temp = (temp << 4) + i.ord - "0".ord elsif "abcdef".include? i temp = (temp << 4) + 10 + i.ord - "a".ord elsif "ABCDEF".include? i temp = (temp << 4) + 10 + i.ord - "A".ord else raise ArgumentError "Malformed \\uxxxx encoding." end end char = temp.chr "utf-8" value = value.gsub escape, char end value end |
#load_from_xml(file_name) ⇒ NilClass
Loads a property list(key and element pairs) from the .xml file.
The XML document will have the following DOCTYPE declaration: <!DOCTYPE properties SYSTEM “java.sun.com/dtd/properties.dtd”>
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/xproperties.rb', line 100 def load_from_xml(file_name) raw = File.read file_name, :encoding => "utf-8" doc = REXML::Document.new raw pairs = doc.root.elements pairs.each "entry" do |pair| if pair.attributes.key? "key" key = pair.attributes["key"] value = pair.text @properties[key] = value else raise ParseException "Malformed XML format." end end end |
#save(file_name) ⇒ NilClass
Saves a property list(key and element pairs) to the .properties file. The file will be written in ISO-8859-1(Latin1) character encoding.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/xproperties.rb', line 79 def save(file_name) file = File.open file_name, "w", :encoding => "iso-8859-1" @properties.each_pair do |k, v| key = save_convert k, :is_convert_key => true value = save_convert v pair = key + "=" + value file.write pair + "\n" end file.close nil? end |
#save_convert(value, is_convert_key = false) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/xproperties.rb', line 185 def save_convert(value, is_convert_key = false) buffer = [] if is_convert_key value = value.gsub /[ ]/, "\\ " value = value.gsub /[=]/, "\\=" value = value.gsub /:/, "\\:" end value = value.gsub /\\/, "\\\\" value = value.gsub /\t/, "\\t" value = value.gsub /\r/, "\\r" value = value.gsub /\n/, "\\n" value = value.gsub /\f/, "\\f" value.chars.each do |char| if char.ord < 0x20 or char.ord > 0x7e char = "\\u" + "%04x" % char.ord end buffer.append char end buffer.join end |
#save_to_xml(file_name) ⇒ NilClass
Saves a property list(key and element pairs) from the .xml file.
The XML document will have the following DOCTYPE declaration: <!DOCTYPE properties SYSTEM “java.sun.com/dtd/properties.dtd”>
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/xproperties.rb', line 123 def save_to_xml(file_name) xml_declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" doctype = "<!DOCTYPE properties SYSTEM " + "\"http://java.sun.com/dtd/properties.dtd\">\n" root = REXML::Element.new "properties" @properties.each do |k, v| entry = REXML::Element.new "entry" entry.add_attribute "key", k entry.text = v root.add_element entry end file = File.open file_name, "w", :encoding => "utf-8" file.write xml_declaration file.write doctype doc = REXML::Document.new doc.add_element root doc.write file, 2, :encoding => "utf-8" end |
#set_property(key, value) ⇒ NilClass
Setting the value of a property.
211 212 213 |
# File 'lib/xproperties.rb', line 211 def set_property(key, value) @properties[key] = value end |
#to_s ⇒ String
Convert the current object to string.
41 42 43 |
# File 'lib/xproperties.rb', line 41 def to_s to_string end |
#to_string ⇒ String
Convert the current object to string.
290 291 292 |
# File 'lib/xproperties.rb', line 290 def to_string JSON.dump @properties end |
#values ⇒ Array
Getting the list of properties value.
254 255 256 |
# File 'lib/xproperties.rb', line 254 def values @properties.values end |