Class: Nori::XMLUtilityNode

Inherits:
Object
  • Object
show all
Defined in:
lib/nori/xml_utility_node.rb

Overview

This is a slighly modified version of the XMLUtilityNode from merb.devjavu.com/projects/merb/ticket/95 ([email protected])

John Nunemaker: It’s mainly just adding vowels, as I ht cd wth n vwls :) This represents the hard part of the work, all I did was change the underlying parser.

Constant Summary collapse

XS_TIME =

Simple xs:time Regexp. Valid xs:time formats 13:20:00 1:20 PM 13:20:30.5555 1:20 PM and 30.5555 seconds 13:20:00-05:00 1:20 PM, US Eastern Standard Time 13:20:00+02:00 1:20 PM, Central European Standard Time 13:20:00Z 1:20 PM, Coordinated Universal Time (UTC) 00:00:00 midnight 24:00:00 midnight

/^\d{2}:\d{2}:\d{2}[Z\.\-\+]?\d*:?\d*$/
XS_DATE =

Simple xs:date Regexp. Valid xs:date formats 2004-04-12 April 12, 2004 -0045-01-01 January 1, 45 BC 12004-04-12 April 12, 12004 2004-04-12-05:00 April 12, 2004, US Eastern Standard Time, which is 5 hours behind Coordinated Universal Time (UTC) 2004-04-12+02:00 April 12, 2004, Central European Summer Time, which is 2 hours ahead of Coordinated Universal Time (UTC) 2004-04-12Z April 12, 2004, Coordinated Universal Time (UTC)

/^[-]?\d{4}-\d{2}-\d{2}[Z\-\+]?\d*:?\d*$/
XS_DATE_TIME =

Simple xs:dateTime Regexp. Valid xs:dateTime formats 2004-04-12T13:20:00 1:20 pm on April 12, 2004 2004-04-12T13:20:15.5 1:20 pm and 15.5 seconds on April 12, 2004 2004-04-12T13:20:00-05:00 1:20 pm on April 12, 2004, US Eastern Standard Time 2004-04-12T13:20:00+02:00 1:20 pm on April 12, 2004, Central European Summer Time 2004-04-12T13:20:15.5-05:00 1:20 pm and 15.5 seconds on April 12, 2004, US Eastern Standard Time 2004-04-12T13:20:00Z 1:20 pm on April 12, 2004, Coordinated Universal Time (UTC)

/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[\.Z]?\d*[\-\+]?\d*:?\d*$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nori, name, normalized_attributes = {}) ⇒ XMLUtilityNode

Returns a new instance of XMLUtilityNode.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/nori/xml_utility_node.rb', line 87

def initialize(nori, name, normalized_attributes = {})
  # unnormalize attribute values
  attributes = Hash[* normalized_attributes.map do |key, value|
    [ key, unnormalize_xml_entities(value) ]
  end.flatten]

  @nori = nori
  @name = name.tr("-", "_")
  @name = @name.split(":").last if @nori.strip_namespaces?
  @name = @nori.convert_tag(@name) if @nori.convert_tags?

  # leave the type alone if we don't know what it is
  @type = self.class.available_typecasts.include?(attributes["type"]) ? attributes.delete("type") : attributes["type"]

  @nil_element = false
  attributes.keys.each do |key|
    if result = /^((.*):)?nil$/.match(key)
      @nil_element = attributes.delete(key) == "true"
      attributes.delete("xmlns:#{result[2]}") if result[1]
    end
  end
  @attributes = undasherize_keys(attributes)
  @children = []
  @text = false
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



113
114
115
# File 'lib/nori/xml_utility_node.rb', line 113

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



113
114
115
# File 'lib/nori/xml_utility_node.rb', line 113

def children
  @children
end

#nameObject

Returns the value of attribute name.



113
114
115
# File 'lib/nori/xml_utility_node.rb', line 113

def name
  @name
end

#typeObject

Returns the value of attribute type.



113
114
115
# File 'lib/nori/xml_utility_node.rb', line 113

def type
  @type
end

Class Method Details

.available_typecastsObject



63
64
65
# File 'lib/nori/xml_utility_node.rb', line 63

def self.available_typecasts
  @@available_typecasts
end

.available_typecasts=(obj) ⇒ Object



67
68
69
# File 'lib/nori/xml_utility_node.rb', line 67

def self.available_typecasts=(obj)
  @@available_typecasts = obj
end

.typecastsObject



55
56
57
# File 'lib/nori/xml_utility_node.rb', line 55

def self.typecasts
  @@typecasts
end

.typecasts=(obj) ⇒ Object



59
60
61
# File 'lib/nori/xml_utility_node.rb', line 59

def self.typecasts=(obj)
  @@typecasts = obj
end

Instance Method Details

#add_node(node) ⇒ Object



126
127
128
129
# File 'lib/nori/xml_utility_node.rb', line 126

def add_node(node)
  @text = true if node.is_a? String
  @children << node
end

#advanced_typecasting(value) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/nori/xml_utility_node.rb', line 215

def advanced_typecasting(value)
  split = value.split
  return value if split.size > 1

  case split.first
    when "true"       then true
    when "false"      then false
    when XS_DATE_TIME then try_to_convert(value) {|x| DateTime.parse(x)}
    when XS_DATE      then try_to_convert(value) {|x| Date.parse(x)}
    when XS_TIME      then try_to_convert(value) {|x| Time.parse(x)}
    else                   value
  end
end

#inner_htmlObject

Get the inner_html of the REXML node.



238
239
240
# File 'lib/nori/xml_utility_node.rb', line 238

def inner_html
  @children.join
end

#prefixed_attribute_name(attribute) ⇒ Object



122
123
124
# File 'lib/nori/xml_utility_node.rb', line 122

def prefixed_attribute_name(attribute)
  @nori.convert_tags? ? @nori.convert_tag(attribute) : attribute
end

#prefixed_attributesObject



115
116
117
118
119
120
# File 'lib/nori/xml_utility_node.rb', line 115

def prefixed_attributes
  attributes.inject({}) do |memo, (key, value)|
    memo[prefixed_attribute_name("@#{key}")] = value
    memo
  end
end

#to_hashObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/nori/xml_utility_node.rb', line 131

def to_hash
  if @type == "file"
    f = StringIOFile.new((@children.first || '').unpack('m').first)
    f.original_filename = attributes['name'] || 'untitled'
    f.content_type = attributes['content_type'] || 'application/octet-stream'
    return { name => f }
  end

  if @text
    t = typecast_value unnormalize_xml_entities(inner_html)
    t = advanced_typecasting(t) if t.is_a?(String) && @nori.advanced_typecasting?

    if t.is_a?(String)
      t = StringWithAttributes.new(t)
      t.attributes = attributes
    end

    return { name => t }
  else
    #change repeating groups into an array
    groups = @children.inject({}) { |s,e| (s[e.name] ||= []) << e; s }

    out = nil
    if @type == "array"
      out = []
      groups.each do |k, v|
        if v.size == 1
          out << v.first.to_hash.entries.first.last
        else
          out << v.map{|e| e.to_hash[k]}
        end
      end
      out = out.flatten

    else # If Hash
      out = {}
      groups.each do |k,v|
        if v.size == 1
          out.merge!(v.first)
        else
          out.merge!( k => v.map{|e| e.to_hash[k]})
        end
      end
      out.merge! prefixed_attributes unless attributes.empty?
      out = out.empty? ? nil : out
    end

    if @type && out.nil?
      { name => typecast_value(out) }
    else
      { name => out }
    end
  end
end

#to_htmlString Also known as: to_s

Converts the node into a readable HTML node.

Returns:

  • (String)

    The HTML node in text form.



245
246
247
248
# File 'lib/nori/xml_utility_node.rb', line 245

def to_html
  attributes.merge!(:type => @type ) if @type
  "<#{name}#{attributes.to_xml_attributes}>#{@nil_element ? '' : inner_html}</#{name}>"
end

#typecast_value(value) ⇒ Integer, ...

Note:

If self does not have a “type” key, or if it’s not one of the options specified above, the raw value will be returned.

Typecasts a value based upon its type. For instance, if node has #type == “integer”, #=> 12]}

Parameters:

  • value (String)

    The value that is being typecast.

Returns:

  • (Integer, TrueClass, FalseClass, Time, Date, Object)

    The result of typecasting value.



209
210
211
212
213
# File 'lib/nori/xml_utility_node.rb', line 209

def typecast_value(value)
  return value unless @type
  proc = self.class.typecasts[@type]
  proc.nil? ? value : proc.call(value)
end

#undasherize_keys(params) ⇒ Object

Take keys of the form foo-bar and convert them to foo_bar



230
231
232
233
234
235
# File 'lib/nori/xml_utility_node.rb', line 230

def undasherize_keys(params)
  params.keys.each do |key, value|
    params[key.tr("-", "_")] = params.delete(key)
  end
  params
end