lib/plist/parser.rb in plist-3.3.0 vs lib/plist/parser.rb in plist-3.4.0
- old
+ new
@@ -9,45 +9,45 @@
# Plist parses Mac OS X xml property list files into ruby data structures.
#
# === Load a plist file
# This is the main point of the library:
#
-# r = Plist::parse_xml( filename_or_xml )
+# r = Plist.parse_xml(filename_or_xml)
module Plist
-# Note that I don't use these two elements much:
-#
-# + Date elements are returned as DateTime objects.
-# + Data elements are implemented as Tempfiles
-#
-# Plist::parse_xml will blow up if it encounters a Date element.
-# If you encounter such an error, or if you have a Date element which
-# can't be parsed into a Time object, please send your plist file to
-# plist@hexane.org so that I can implement the proper support.
- def Plist::parse_xml( filename_or_xml )
+ # Note that I don't use these two elements much:
+ #
+ # + Date elements are returned as DateTime objects.
+ # + Data elements are implemented as Tempfiles
+ #
+ # Plist.parse_xml will blow up if it encounters a Date element.
+ # If you encounter such an error, or if you have a Date element which
+ # can't be parsed into a Time object, please create an issue
+ # attaching your plist file at https://github.com/patsplat/plist/issues
+ # so folks can implement the proper support.
+ def self.parse_xml(filename_or_xml)
listener = Listener.new
- #parser = REXML::Parsers::StreamParser.new(File.new(filename), listener)
+ # parser = REXML::Parsers::StreamParser.new(File.new(filename), listener)
parser = StreamParser.new(filename_or_xml, listener)
parser.parse
listener.result
end
class Listener
- #include REXML::StreamListener
+ # include REXML::StreamListener
attr_accessor :result, :open
def initialize
@result = nil
- @open = Array.new
+ @open = []
end
-
def tag_start(name, attributes)
- @open.push PTag::mappings[name].new
+ @open.push PTag.mappings[name].new
end
- def text( contents )
+ def text(contents)
@open.last.text = contents if @open.last
end
def tag_end(name)
last = @open.pop
@@ -58,15 +58,15 @@
end
end
end
class StreamParser
- def initialize( plist_data_or_file, listener )
+ def initialize(plist_data_or_file, listener)
if plist_data_or_file.respond_to? :read
@xml = plist_data_or_file.read
elsif File.exist? plist_data_or_file
- @xml = File.read( plist_data_or_file )
+ @xml = File.read(plist_data_or_file)
else
@xml = plist_data_or_file
end
@listener = listener
@@ -76,19 +76,18 @@
XMLDECL_PATTERN = /<\?xml\s+(.*?)\?>*/m
DOCTYPE_PATTERN = /\s*<!DOCTYPE\s+(.*?)(\[|>)/m
COMMENT_START = /\A<!--/
COMMENT_END = /.*?-->/m
-
def parse
- plist_tags = PTag::mappings.keys.join('|')
+ plist_tags = PTag.mappings.keys.join('|')
start_tag = /<(#{plist_tags})([^>]*)>/i
end_tag = /<\/(#{plist_tags})[^>]*>/i
require 'strscan'
- @scanner = StringScanner.new( @xml )
+ @scanner = StringScanner.new(@xml)
until @scanner.eos?
if @scanner.scan(COMMENT_START)
@scanner.scan(COMMENT_END)
elsif @scanner.scan(XMLDECL_PATTERN)
encoding = parse_encoding_from_xml_declaration(@scanner[1])
@@ -130,26 +129,25 @@
end
end
end
class PTag
- @@mappings = { }
- def PTag::mappings
- @@mappings
+ def self.mappings
+ @mappings ||= {}
end
- def PTag::inherited( sub_class )
+ def self.inherited(sub_class)
key = sub_class.to_s.downcase
- key.gsub!(/^plist::/, '' )
+ key.gsub!(/^plist::/, '')
key.gsub!(/^p/, '') unless key == "plist"
- @@mappings[key] = sub_class
+ mappings[key] = sub_class
end
attr_accessor :text, :children
def initialize
- @children = Array.new
+ @children = []
end
def to_ruby
raise "Unimplemented: " + self.class.to_s + "#to_ruby on #{self.inspect}"
end
@@ -161,11 +159,11 @@
end
end
class PDict < PTag
def to_ruby
- dict = Hash.new
+ dict = {}
key = nil
children.each do |c|
if key.nil?
key = c.to_ruby
@@ -179,16 +177,16 @@
end
end
class PKey < PTag
def to_ruby
- CGI::unescapeHTML(text || '')
+ CGI.unescapeHTML(text || '')
end
end
class PString < PTag
def to_ruby
- CGI::unescapeHTML(text || '')
+ CGI.unescapeHTML(text || '')
end
end
class PArray < PTag
def to_ruby