plistr 0.1.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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +12 -0
- data/Rakefile +2 -0
- data/lib/plistr.rb +147 -0
- data/lib/plistr/version.rb +3 -0
- data/plistr.gemspec +18 -0
- metadata +100 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Norbert Crombach
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
data/lib/plistr.rb
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'date'
|
3
|
+
require 'base64'
|
4
|
+
|
5
|
+
module Plistr
|
6
|
+
class Document < Nokogiri::XML::SAX::Document
|
7
|
+
VALUES = %w(string real integer date data key)
|
8
|
+
BOOLEANS = %w(true false)
|
9
|
+
|
10
|
+
attr_reader :value
|
11
|
+
|
12
|
+
def start_document
|
13
|
+
@stack = []
|
14
|
+
@text = ""
|
15
|
+
end
|
16
|
+
|
17
|
+
def start_element(name, attributes)
|
18
|
+
return if name == "plist"
|
19
|
+
|
20
|
+
if VALUES.include?(name)
|
21
|
+
@stack.push Value.new(name)
|
22
|
+
elsif BOOLEANS.include?(name)
|
23
|
+
@stack.push Boolean.new(name)
|
24
|
+
elsif name == "array"
|
25
|
+
@stack.push Array.new
|
26
|
+
elsif name == "dict"
|
27
|
+
@stack.push Dict.new
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def characters(string)
|
32
|
+
@text.concat string
|
33
|
+
end
|
34
|
+
|
35
|
+
def end_element(name)
|
36
|
+
return unless value = @stack.pop
|
37
|
+
|
38
|
+
if value.is_a?(Value)
|
39
|
+
value.text.concat @text.strip
|
40
|
+
end
|
41
|
+
@text = ""
|
42
|
+
|
43
|
+
if @stack.last.is_a?(Struct)
|
44
|
+
@stack.last.push value
|
45
|
+
elsif @stack.empty?
|
46
|
+
@value = value.parse
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class Element
|
52
|
+
end
|
53
|
+
|
54
|
+
class Value < Element
|
55
|
+
attr_reader :name, :text
|
56
|
+
|
57
|
+
def initialize(name)
|
58
|
+
@name = name
|
59
|
+
@text = ""
|
60
|
+
end
|
61
|
+
|
62
|
+
def parse
|
63
|
+
case @name
|
64
|
+
when "string"
|
65
|
+
@text
|
66
|
+
when "real"
|
67
|
+
@text.to_f
|
68
|
+
when "integer"
|
69
|
+
@text.to_i
|
70
|
+
when "date"
|
71
|
+
DateTime.parse(@text)
|
72
|
+
when "data"
|
73
|
+
Base64.decode64(@text.gsub(/\s/, ''))
|
74
|
+
when "key"
|
75
|
+
@text
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class Boolean < Element
|
81
|
+
def initialize(value)
|
82
|
+
@value = value
|
83
|
+
end
|
84
|
+
|
85
|
+
def parse
|
86
|
+
@value == "true"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class Struct < Element
|
91
|
+
end
|
92
|
+
|
93
|
+
class Array < Struct
|
94
|
+
def initialize
|
95
|
+
@array = []
|
96
|
+
end
|
97
|
+
|
98
|
+
def parse
|
99
|
+
@array
|
100
|
+
end
|
101
|
+
|
102
|
+
def push(value)
|
103
|
+
@array.push value.parse
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
class Dict < Struct
|
108
|
+
def initialize
|
109
|
+
@hash = {}
|
110
|
+
end
|
111
|
+
|
112
|
+
def parse
|
113
|
+
@hash
|
114
|
+
end
|
115
|
+
|
116
|
+
def push(value)
|
117
|
+
if !@key
|
118
|
+
@key = value.text
|
119
|
+
else
|
120
|
+
@hash[@key] = value.parse
|
121
|
+
@key = nil
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
class Reader
|
127
|
+
def self.open(filename)
|
128
|
+
data = File.open(filename)
|
129
|
+
new(data)
|
130
|
+
end
|
131
|
+
|
132
|
+
def initialize(data)
|
133
|
+
@data = data
|
134
|
+
end
|
135
|
+
|
136
|
+
def value
|
137
|
+
@value || parse
|
138
|
+
end
|
139
|
+
|
140
|
+
def parse
|
141
|
+
@document = Document.new
|
142
|
+
parser = Nokogiri::XML::SAX::Parser.new(@document)
|
143
|
+
parser.parse(@data)
|
144
|
+
@value = @document.value
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
data/plistr.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path('../lib/plistr/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ["Norbert Crombach"]
|
5
|
+
gem.email = ["norbert.crombach@primetheory.org"]
|
6
|
+
gem.summary = %q{Fast XML property list reader.}
|
7
|
+
gem.homepage = "https://github.com/norbert/plistr"
|
8
|
+
|
9
|
+
gem.files = `git ls-files`.split($\)
|
10
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
11
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
12
|
+
gem.name = "plistr"
|
13
|
+
gem.require_paths = ["lib"]
|
14
|
+
gem.version = Plistr::VERSION
|
15
|
+
|
16
|
+
gem.add_dependency 'nokogiri'
|
17
|
+
gem.add_development_dependency 'rake'
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: plistr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Norbert Crombach
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-05-01 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
prerelease: false
|
22
|
+
type: :runtime
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
version_requirements: *id001
|
33
|
+
name: nokogiri
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
prerelease: false
|
36
|
+
type: :development
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
version_requirements: *id002
|
47
|
+
name: rake
|
48
|
+
description:
|
49
|
+
email:
|
50
|
+
- norbert.crombach@primetheory.org
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- .gitignore
|
59
|
+
- Gemfile
|
60
|
+
- LICENSE
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- lib/plistr.rb
|
64
|
+
- lib/plistr/version.rb
|
65
|
+
- plistr.gemspec
|
66
|
+
homepage: https://github.com/norbert/plistr
|
67
|
+
licenses: []
|
68
|
+
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
requirements: []
|
93
|
+
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.10
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Fast XML property list reader.
|
99
|
+
test_files: []
|
100
|
+
|