Class: REXML::Object::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/voruby/misc/rexml_ext.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defn = nil) ⇒ Base

Returns a new instance of Base.



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/voruby/misc/rexml_ext.rb', line 134

def initialize(defn=nil)
  @node = case defn
    when Document      then defn.root
    when Element       then defn
    when Hash          then Element.new(self.xml_element_name)
    when NilClass      then Element.new(self.xml_element_name)
    else
      Document.new(defn).root
  end

  initialize_members(defn) if defn.is_a?(Hash)
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



128
129
130
# File 'lib/voruby/misc/rexml_ext.rb', line 128

def node
  @node
end

#xml_element_nameObject (readonly)

Returns the value of attribute xml_element_name.



128
129
130
# File 'lib/voruby/misc/rexml_ext.rb', line 128

def xml_element_name
  @xml_element_name
end

Class Method Details

.xml_typeObject



132
# File 'lib/voruby/misc/rexml_ext.rb', line 132

def self.xml_type; self.class.to_s.split('::').last end

.xsd_namespaceObject



131
# File 'lib/voruby/misc/rexml_ext.rb', line 131

def self.xsd_namespace; 'http://www.w3.org/2001/XMLSchema' end

.xsi_namespaceObject



130
# File 'lib/voruby/misc/rexml_ext.rb', line 130

def self.xsi_namespace; 'http://www.w3.org/2001/XMLSchema-instance' end

Instance Method Details

#find_namespace_by_href(href) ⇒ Object



149
150
151
# File 'lib/voruby/misc/rexml_ext.rb', line 149

def find_namespace_by_href(href)
  self.node.namespaces.find{ |prefix, uri| uri == href }
end

#get_attribute(name, ns = nil) ⇒ Object



153
154
155
156
# File 'lib/voruby/misc/rexml_ext.rb', line 153

def get_attribute(name, ns=nil)
  attribute = self.node.attributes.get_attribute_ns(ns, name)
  attribute ? attribute.value : nil
end

#get_element(name, ns = nil) ⇒ Object



162
163
164
# File 'lib/voruby/misc/rexml_ext.rb', line 162

def get_element(name, ns=nil)
  XPath.first(self.node, *element_xpath_for(name, ns))
end

#get_elements(name, ns = nil) ⇒ Object



183
184
185
# File 'lib/voruby/misc/rexml_ext.rb', line 183

def get_elements(name, ns=nil)
  ReflectiveElementList.new(self.node, *element_xpath_for(name, ns))
end

#set_attribute(name, value, ns = nil) ⇒ Object



158
159
160
# File 'lib/voruby/misc/rexml_ext.rb', line 158

def set_attribute(name, value, ns=nil)
  self.node.attributes[attribute_xpath_for(name, ns)] = value.to_s
end

#set_element(name, value, ns) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/voruby/misc/rexml_ext.rb', line 166

def set_element(name, value, ns)
  el = get_element(name, ns)

  # The element doesn't exist...
  if !el
    el = REXML::Element.new(name)
    @node << el # so insert it
  end
  
  case value
    when String               then el.text = value.to_s
    when REXML::Element       then value.name = name; el.replace_with(value)
  end
  
  el.add_namespace(ns) if !find_namespace_by_href(ns)
end

#set_elements(name, values, ns = nil) ⇒ Object



187
188
189
# File 'lib/voruby/misc/rexml_ext.rb', line 187

def set_elements(name, values, ns=nil)
  get_elements(name, ns).replace(values)
end

#to_sObject



191
192
193
194
195
196
# File 'lib/voruby/misc/rexml_ext.rb', line 191

def to_s
  formatter = Formatters::Default.new
  xml = ''
  formatter.write(self.node, xml)
  xml
end