java-properties 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -17
- data/lib/java-properties.rb +7 -5
- data/lib/java-properties/generating/generator.rb +28 -6
- data/lib/java-properties/version.rb +1 -1
- data/spec/fixtures/test_out_skip_separators.properties +11 -0
- data/spec/fixtures/test_out_skip_special_chars.properties +14 -0
- data/spec/fixtures/test_out_skip_unicode.properties +11 -0
- metadata +13 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa01303d1449ae0f74ddd1a56b9da076f232335e
|
4
|
+
data.tar.gz: 9704efbe011c991ddede20ea11cc5d63da9897d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4df80543ad68a53c37dd7cc6aea82b256591c11813e30737a2a65ee340ec1c4eb877436f265466f47f93b1ac68c46a0c7b9fa77529cc061cb4965b5ce38f960
|
7
|
+
data.tar.gz: ea06f6daad8d178218f6bb227f080263c3b7341b1c2d128993b8b9b5734e00f2e9eee85b87a51f240dbd2f4aa35780f6c2e8add55d004fb1affb5c8e40e4a54a
|
data/README.md
CHANGED
@@ -11,46 +11,62 @@ A ruby library to read and write [Java properties files](http://en.wikipedia.org
|
|
11
11
|
|
12
12
|
Install via Rubygems
|
13
13
|
|
14
|
-
|
14
|
+
```bash
|
15
|
+
$ gem install java-properties
|
16
|
+
```
|
15
17
|
|
16
18
|
... or add to your Gemfile
|
17
19
|
|
18
|
-
|
20
|
+
```ruby
|
21
|
+
gem "java-properties"
|
22
|
+
```
|
19
23
|
|
20
24
|
## Loading files
|
21
25
|
|
22
26
|
You can load a valid Java properties file from the file system using a path:
|
23
27
|
|
24
|
-
|
25
|
-
|
28
|
+
```ruby
|
29
|
+
properties = JavaProperties.load("path/to/my.properties")
|
30
|
+
properties[:foo] # => "bar"
|
31
|
+
```
|
26
32
|
|
27
33
|
If have already the content of the properties file at hand than parse the content as:
|
28
34
|
|
29
|
-
|
30
|
-
|
35
|
+
```ruby
|
36
|
+
properties = JavaProperties.load("foo=bar")
|
37
|
+
properties[:foo] # => "bar"
|
38
|
+
```
|
31
39
|
|
32
40
|
## Writing files
|
33
41
|
|
34
42
|
You can write any Hash-like structure as a properties file:
|
35
43
|
|
36
|
-
|
37
|
-
|
44
|
+
```ruby
|
45
|
+
hash = {:foo => "bar"}
|
46
|
+
JavaProperties.write(hash, "path/to/my.properties")
|
47
|
+
```
|
38
48
|
|
39
49
|
Or if you want to omit the file you can receive the content directly:
|
40
50
|
|
41
|
-
|
42
|
-
|
51
|
+
```ruby
|
52
|
+
hash = {:foo => "bar"}
|
53
|
+
JavaProperties.generate(hash) # => "foo=bar"
|
54
|
+
```
|
43
55
|
|
44
56
|
## Encodings and special chars
|
45
57
|
|
46
58
|
As Java properties files normally hold UTF-8 chars in their escaped representation this tool tries to convert them:
|
47
59
|
|
48
|
-
|
60
|
+
```
|
61
|
+
"ה" <=> "\u05d4"
|
62
|
+
```
|
49
63
|
|
50
64
|
The tool also escaped every '=', ' ' and ':' in the name part of a property line:
|
51
65
|
|
52
|
-
|
53
|
-
|
66
|
+
```ruby
|
67
|
+
JavaProperties.generate({"i : like=strange" => "bar"})
|
68
|
+
# => "i\ \:\ like\=strange=bar"
|
69
|
+
```
|
54
70
|
|
55
71
|
## Multi line and line breaks
|
56
72
|
|
@@ -58,12 +74,16 @@ In Java properties files a string can be multi line but line breaks have to be e
|
|
58
74
|
|
59
75
|
Assume the following input:
|
60
76
|
|
61
|
-
|
62
|
-
|
77
|
+
```ini
|
78
|
+
my=This is a multi \
|
79
|
+
line content with only \n one line break
|
80
|
+
```
|
63
81
|
|
64
82
|
The parses would read:
|
65
83
|
|
66
|
-
|
84
|
+
```ruby
|
85
|
+
{:my => "This is a multi line content which only \n one line break"}
|
86
|
+
```
|
67
87
|
|
68
88
|
In the opposite direction line breaks will be correctly escaped but the generator will never use multi line values.
|
69
89
|
|
@@ -85,4 +105,4 @@ For more information about the properties file format have a look at the [Java P
|
|
85
105
|
|
86
106
|
## License
|
87
107
|
|
88
|
-
This gem is released under the MIT License. See the LICENSE file for further details.
|
108
|
+
This gem is released under the MIT License. See the LICENSE file for further details.
|
data/lib/java-properties.rb
CHANGED
@@ -18,10 +18,11 @@ module JavaProperties
|
|
18
18
|
|
19
19
|
# Generates the content of a Java properties file
|
20
20
|
# @see Generating::Generator
|
21
|
-
# @param hash [Hash]
|
21
|
+
# @param hash [Hash]
|
22
|
+
# @param options [Hash] options for the generator
|
22
23
|
# @return [String]
|
23
|
-
def self.generate(hash)
|
24
|
-
Generating::Generator.generate(hash)
|
24
|
+
def self.generate(hash, options = {})
|
25
|
+
Generating::Generator.generate(hash, options)
|
25
26
|
end
|
26
27
|
|
27
28
|
# Loads and parses a Java properties file
|
@@ -36,8 +37,9 @@ module JavaProperties
|
|
36
37
|
# @see Generating::Generator
|
37
38
|
# @param hash [Hash]
|
38
39
|
# @param path [String]
|
39
|
-
|
40
|
-
|
40
|
+
# @param options [Hash] options for the generator
|
41
|
+
def self.write(hash, path, options = {})
|
42
|
+
File.write(path, generate(hash, options))
|
41
43
|
end
|
42
44
|
|
43
45
|
end
|
@@ -11,23 +11,45 @@ module JavaProperties
|
|
11
11
|
# @return [String]
|
12
12
|
KEY_VALUE_SEPARATOR = '='
|
13
13
|
|
14
|
+
# Default options
|
15
|
+
# @return [Hash]
|
16
|
+
DEFAULT_OPTIONS = {
|
17
|
+
:skip_encode_unicode => false,
|
18
|
+
:skip_encode_separators => false,
|
19
|
+
:skip_encode_special_chars => false
|
20
|
+
}.freeze
|
21
|
+
|
14
22
|
# Generates a properties file content based on a hash
|
15
23
|
# @param properties [Properties] or simple hash
|
24
|
+
# @param options [Hash]
|
25
|
+
# @option options skip_encode_unicode [Boolean] Skip unicode encoding
|
26
|
+
# @option options skip_encode_separators [Boolean] Skip seperators encoding
|
27
|
+
# @option options skip_encode_special_chars [Boolean] Skip special char encoding
|
16
28
|
# @return [String]
|
17
|
-
def self.generate(properties)
|
29
|
+
def self.generate(properties, options = {})
|
30
|
+
options = DEFAULT_OPTIONS.merge(options)
|
18
31
|
lines = []
|
19
32
|
properties.each do |key, value|
|
20
|
-
lines << build_line(key, value)
|
33
|
+
lines << build_line(key, value, options)
|
21
34
|
end
|
22
35
|
lines.join("\n")
|
23
36
|
end
|
24
37
|
|
25
38
|
private
|
26
39
|
|
27
|
-
def self.build_line(key, value
|
28
|
-
Encoding.encode!(key.to_s.dup)
|
29
|
-
|
30
|
-
|
40
|
+
def self.build_line(key, value, options)
|
41
|
+
encoded_key = Encoding.encode!(key.to_s.dup, *encoding_skips(false, options))
|
42
|
+
encoded_value = Encoding.encode!(value.to_s.dup, *encoding_skips(true, options))
|
43
|
+
|
44
|
+
encoded_key + KEY_VALUE_SEPARATOR + encoded_value
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.encoding_skips(is_value, options)
|
48
|
+
skips = []
|
49
|
+
skips << Encoding::SKIP_SEPARATORS if is_value || options[:skip_encode_separators]
|
50
|
+
skips << Encoding::SKIP_UNICODE if options[:skip_encode_unicode]
|
51
|
+
skips << Encoding::SKIP_SPECIAL_CHARS if options[:skip_encode_special_chars]
|
52
|
+
skips
|
31
53
|
end
|
32
54
|
end
|
33
55
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: java-properties
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Thiel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Tool for loading and writing Java properties files
|
14
14
|
email:
|
@@ -21,21 +21,24 @@ files:
|
|
21
21
|
- README.md
|
22
22
|
- Rakefile
|
23
23
|
- java-properties.gemspec
|
24
|
+
- lib/java-properties.rb
|
25
|
+
- lib/java-properties/encoding.rb
|
24
26
|
- lib/java-properties/encoding/separators.rb
|
25
27
|
- lib/java-properties/encoding/special_chars.rb
|
26
28
|
- lib/java-properties/encoding/unicode.rb
|
27
|
-
- lib/java-properties/encoding.rb
|
28
|
-
- lib/java-properties/generating/generator.rb
|
29
29
|
- lib/java-properties/generating.rb
|
30
|
+
- lib/java-properties/generating/generator.rb
|
31
|
+
- lib/java-properties/parsing.rb
|
30
32
|
- lib/java-properties/parsing/normalizer.rb
|
31
33
|
- lib/java-properties/parsing/parser.rb
|
32
|
-
- lib/java-properties/parsing.rb
|
33
34
|
- lib/java-properties/properties.rb
|
34
35
|
- lib/java-properties/version.rb
|
35
|
-
- lib/java-properties.rb
|
36
36
|
- spec/fixtures/test.properties
|
37
37
|
- spec/fixtures/test_normalized.properties
|
38
38
|
- spec/fixtures/test_out.properties
|
39
|
+
- spec/fixtures/test_out_skip_separators.properties
|
40
|
+
- spec/fixtures/test_out_skip_special_chars.properties
|
41
|
+
- spec/fixtures/test_out_skip_unicode.properties
|
39
42
|
homepage: https://github.com/jnbt/java-properties
|
40
43
|
licenses:
|
41
44
|
- MIT
|
@@ -56,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
59
|
version: 1.3.5
|
57
60
|
requirements: []
|
58
61
|
rubyforge_project:
|
59
|
-
rubygems_version: 2.1
|
62
|
+
rubygems_version: 2.2.1
|
60
63
|
signing_key:
|
61
64
|
specification_version: 4
|
62
65
|
summary: Loader and writer for *.properties files
|
@@ -64,4 +67,7 @@ test_files:
|
|
64
67
|
- spec/fixtures/test.properties
|
65
68
|
- spec/fixtures/test_normalized.properties
|
66
69
|
- spec/fixtures/test_out.properties
|
70
|
+
- spec/fixtures/test_out_skip_separators.properties
|
71
|
+
- spec/fixtures/test_out_skip_special_chars.properties
|
72
|
+
- spec/fixtures/test_out_skip_unicode.properties
|
67
73
|
has_rdoc:
|