Class: Xml2Go::Parser
- Inherits:
-
Object
- Object
- Xml2Go::Parser
- Defined in:
- lib/xml2go/parser.rb
Constant Summary collapse
- SUPPORTED_TYPES =
["bool", "int", "float64", "string"]
- INVALID_CHARS =
["-"]
Instance Attribute Summary collapse
-
#structs ⇒ Object
readonly
Returns the value of attribute structs.
Instance Method Summary collapse
-
#add_attrs_to_struct(element, struct) ⇒ Object
adds the XML attrs of element to the Go struct.
- #add_field_to_struct(struct, var_name, type, xml_tag) ⇒ Object
-
#add_xml_node(element, struct) ⇒ Object
Add XML node, which contains more child nodes, as a member struct.
-
#add_xml_primitive(element, struct) ⇒ Object
Add XML node, which contains no child nodes, as a primitive type if it contains attrs, add it as a struct.
-
#cap(s) ⇒ Object
ruby’s .capitalize ignores Camel case.
- #get_struct_member(element) ⇒ Object
- #get_type(string) ⇒ Object
-
#get_type_from_elem(element) ⇒ Object
analyses the xml element to try and fetch the type if can’t figure it out, returns ‘string’.
-
#initialize(config) ⇒ Parser
constructor
A new instance of Parser.
-
#low(s) ⇒ Object
ruby’s .capitalize ignores Camel case.
-
#normalize(s) ⇒ Object
remove invalid chars, capitalize and camelcase it.
- #numeric?(str) ⇒ Boolean
- #parse_element(element) ⇒ Object
-
#singularize(string) ⇒ Object
take ‘s’ out of string and return if it was plural or not.
Constructor Details
#initialize(config) ⇒ Parser
Returns a new instance of Parser.
11 12 13 14 |
# File 'lib/xml2go/parser.rb', line 11 def initialize(config) @config = config @structs = {} end |
Instance Attribute Details
#structs ⇒ Object (readonly)
Returns the value of attribute structs.
9 10 11 |
# File 'lib/xml2go/parser.rb', line 9 def structs @structs end |
Instance Method Details
#add_attrs_to_struct(element, struct) ⇒ Object
adds the XML attrs of element to the Go struct
80 81 82 83 84 85 86 87 |
# File 'lib/xml2go/parser.rb', line 80 def add_attrs_to_struct(element, struct) if element.respond_to?(:attributes) then element.attributes.each do |attri, value| var_name = attri.dup add_field_to_struct(struct, normalize(var_name), get_type(value.text), "#{attri},attr") end end end |
#add_field_to_struct(struct, var_name, type, xml_tag) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/xml2go/parser.rb', line 62 def add_field_to_struct(struct, var_name, type, xml_tag) if struct.fields.has_key?(var_name) && !struct.fields[var_name].type.include?("[]") then type, sing = singularize(struct.fields[var_name].type) struct.fields[var_name].type = "[]" + type if @config[:plural_arrays] then struct.fields[var_name].name << "s" if struct.fields[var_name].name[-1] != "s" end else struct.add_field(var_name, type, xml_tag) end end |
#add_xml_node(element, struct) ⇒ Object
Add XML node, which contains more child nodes, as a member struct
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/xml2go/parser.rb', line 90 def add_xml_node(element, struct) var_name, type, xml_tag = get_struct_member(element) type, plural = singularize(type) type = "[]" + type if plural begin xml_tag = element.namespace.prefix << ":" << xml_tag rescue => e # :) end add_field_to_struct(struct, var_name, type, xml_tag) end |
#add_xml_primitive(element, struct) ⇒ Object
Add XML node, which contains no child nodes, as a primitive type if it contains attrs, add it as a struct.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/xml2go/parser.rb', line 107 def add_xml_primitive(element, struct) var_name, type, xml_tag = get_struct_member(element) # is this a primitive with attrs? prim_attrs = element.attributes.select{ |k,v| !k.include?("type") } if prim_attrs.length > 0 then add_field_to_struct(struct, var_name, type, xml_tag) parse_element(element) else type = get_type_from_elem(element) add_field_to_struct(struct, var_name, type, xml_tag) end end |
#cap(s) ⇒ Object
ruby’s .capitalize ignores Camel case
17 18 19 20 |
# File 'lib/xml2go/parser.rb', line 17 def cap(s) s[0] = s[0].upcase return s end |
#get_struct_member(element) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/xml2go/parser.rb', line 55 def get_struct_member(element) type = normalize(element.name) var_name = type xml_tag = element.name [var_name, type, xml_tag] end |
#get_type(string) ⇒ Object
168 169 170 171 172 173 174 175 176 |
# File 'lib/xml2go/parser.rb', line 168 def get_type(string) # try to figure out the type if numeric?(string) then return "float64" if Float(string).to_s.length == string.length return "int" end return "bool" if ["true", "false"].include?(string) return "string" end |
#get_type_from_elem(element) ⇒ Object
analyses the xml element to try and fetch the type if can’t figure it out, returns ‘string’
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/xml2go/parser.rb', line 153 def get_type_from_elem(element) if @config[:detect_type] then # check and see if the type is provided in the attributes element.attributes.each do |k,v| if k.include?("type") then type = v.value.split(":").last return type if SUPPORTED_TYPES.include?(type) end end end return get_type(element.child.to_s) end |
#low(s) ⇒ Object
ruby’s .capitalize ignores Camel case
24 25 26 27 28 |
# File 'lib/xml2go/parser.rb', line 24 def low(s) dup = s.dup dup[0] = dup[0].downcase return dup end |
#normalize(s) ⇒ Object
remove invalid chars, capitalize and camelcase it
32 33 34 35 36 37 38 39 |
# File 'lib/xml2go/parser.rb', line 32 def normalize(s) s = cap(s) INVALID_CHARS.each do |char| s.gsub!(char, "_") end return s.gsub(/(?<=_|^)(\w)/){$1.upcase}.gsub(/(?:_)(\w)/,'\1') end |
#numeric?(str) ⇒ Boolean
41 42 43 |
# File 'lib/xml2go/parser.rb', line 41 def numeric?(str) Float(str) != nil rescue false end |
#parse_element(element) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/xml2go/parser.rb', line 123 def parse_element(element) struct_name = normalize(element.name) struct_name, s = singularize(struct_name) # TODO: maybe we DO want to process repeated structs # to capture arrays don't process to structs return if @structs.has_key?(struct_name) struct = Xml2Go::Struct.new(struct_name) # add attributes as properties if @config[:add_attrs] then add_attrs_to_struct(element, struct) end element.elements.each do |child| # this is a struct if child.elements.count > 0 then add_xml_node(child, struct) parse_element(child) else # this is an XML primitive add_xml_primitive(child, struct) end end @structs[struct.type] = struct end |
#singularize(string) ⇒ Object
take ‘s’ out of string and return if it was plural or not
46 47 48 49 50 51 52 53 |
# File 'lib/xml2go/parser.rb', line 46 def singularize(string) # I need a singulizer util :( if string[-1] == "s" && string[-1] != string[-2] then s = string[0..-2] return [s, true] end return [string, false] end |