-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathparser.rb
70 lines (61 loc) · 2.25 KB
/
parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
module AWS
module Core
module XML
class Parser
# @param [Hash] rules A has of xml parsing rules. Generally
# rules will come from an xml grammar.
def initialize rules = {}
@rules = rules
end
# @return [Hash] Returns the rules for this xml parser that define
# how it should transform the XMl into Ruby.
attr_reader :rules
# @param [String] xml An XML document string to parse.
# @return [Hash] Returns a hash of parsed xml data.
def parse xml
xml = '<xml/>' if xml.nil? or xml.empty?
sax_handler.parse(xml)
end
# @return [Hash] Returns a hash of mostly empty placeholder data.
def simulate
XML::Stub.simulate(rules)
end
# @param [String] xml An XML document string to parse.
# @param [Hash] rules A has of xml parsing rules. Generally
# rules will come from an xml grammar.
# @return [Hash] Returns a hash of parsed xml data.
def self.parse xml, rules = {}
self.new(rules).parse(xml)
end
protected
# There are three handlers, nokogiri is the fastest, followed
# by libxml-ruby. Lastly (by a long shot) is REXML. REXML
# is the only library that does not rely on a native
# extension.
#
# Currently you can not choose your xml sax handler, and the only
# we express a gem dependency on is nokogiri.
#
def sax_handler
begin
SaxHandlers::Nokogiri.new(rules)
rescue NameError, LoadError
SaxHandlers::REXML.new(rules)
end
end
end
end
end
end