Class: Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/xproperties/properties.rb

Overview

Parse properties file.

Instance Method Summary collapse

Constructor Details

#initializeProperties

Returns a new instance of Properties.



513
514
515
# File 'lib/xproperties/properties.rb', line 513

def initialize
  @properties = { }
end

Instance Method Details

#[](key) ⇒ String

Getting the value of a property.

Parameters:

  • key

    Property name.

Returns:

  • (String)


531
532
533
# File 'lib/xproperties/properties.rb', line 531

def [](key)
  get_property key
end

#[]=(key, value) ⇒ NilClass

Setting the value of a property.

Parameters:

  • key

    Property name.

  • value

    Value to set for the property.

Returns:

  • (NilClass)


522
523
524
525
# File 'lib/xproperties/properties.rb', line 522

def []=(key, value)
  set_property key, value
  nil?
end

#clearNilClass

Remove all properties

Returns:

  • (NilClass)


750
751
752
# File 'lib/xproperties/properties.rb', line 750

def clear
  @properties.clear
end

#contains?(key) ⇒ Boolean

Returns a value whether the key exists.

Returns:

  • (Boolean)


785
786
787
# File 'lib/xproperties/properties.rb', line 785

def contains?(key)
  @properties.key? key
end

#countInteger

Get the number of properties.

Returns:

  • (Integer)


778
779
780
# File 'lib/xproperties/properties.rb', line 778

def count
  @properties.count
end

#delete_property(key) ⇒ NilClass

Deleting the value of a property.

Parameters:

  • key

    Property name.

Returns:

  • (NilClass)

Raises:

  • KeyError When property does not exist.



743
744
745
# File 'lib/xproperties/properties.rb', line 743

def delete_property(key)
  @properties.delete key
end

#eachArray

Get an iterator object.

Returns:

  • (Array)


538
539
540
541
542
# File 'lib/xproperties/properties.rb', line 538

def each
  items.each do |item|
    yield item
  end
end

#equals?(other) ⇒ Boolean

Returns a value whether two object instances are equal.

Parameters:

  • other

    Other object to compare.

Returns:

  • (Boolean)


793
794
795
# File 'lib/xproperties/properties.rb', line 793

def equals?(other)
  self == other
end

#get_property(key, default = "") ⇒ String

Getting the value of a property.

Parameters:

  • key

    Property name.

  • default (defaults to: "")

    Default value if property does not exist.

Returns:

  • (String)

Raises:

  • KeyError When property does not exist.



731
732
733
734
735
736
# File 'lib/xproperties/properties.rb', line 731

def get_property(key, default = "")
  if not @properties.key? key and default != ""
    return default
  end
  @properties[key]
end

#itemsArray

Getting the list of properties key-value pair.

Returns:

  • (Array)


771
772
773
# File 'lib/xproperties/properties.rb', line 771

def items
  @properties.keys.zip @properties.values
end

#keysArray

Getting the list of properties name.

Returns:

  • (Array)


757
758
759
# File 'lib/xproperties/properties.rb', line 757

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.

Parameters:

  • file_name

    .properties file name.

Raises:

  • IOError When an error occurs while reading the file.

  • ArgumentError When the file contains a malformed Unicode escape sequence.



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/xproperties/properties.rb', line 559

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_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”>

Parameters:

  • file_name

    .xml file name.

Returns:

  • (NilClass)

Raises:

  • IOError When an error occurs while reading the file.

  • ParseException When the XML file is malformed.



606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/xproperties/properties.rb', line 606

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.

Parameters:

  • file_name

    .properties file name.

Returns:

  • (NilClass)

Raises:

  • IOError When an error occurs while writing the file.



585
586
587
588
589
590
591
592
593
594
595
# File 'lib/xproperties/properties.rb', line 585

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_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”>

Parameters:

  • file_name

    .xml file name.

Returns:

  • (NilClass)

Raises:

  • IOError When an error occurs while writing the file.



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/xproperties/properties.rb', line 629

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.

Parameters:

  • key

    Property name.

  • value

    Value to set for the property.

Returns:

  • (NilClass)


721
722
723
# File 'lib/xproperties/properties.rb', line 721

def set_property(key, value)
  @properties[key] = value
end

#to_sString

Convert the current object to string.

Returns:

  • (String)


547
548
549
# File 'lib/xproperties/properties.rb', line 547

def to_s
  to_string
end

#to_stringString

Convert the current object to string.

Returns:

  • (String)


800
801
802
# File 'lib/xproperties/properties.rb', line 800

def to_string
  JSON.dump @properties
end

#valuesArray

Getting the list of properties value.

Returns:

  • (Array)


764
765
766
# File 'lib/xproperties/properties.rb', line 764

def values
  @properties.values
end