mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-22 18:50:08 +03:00
bug #1117: Format Templates preserving double qoutes. It checks for empty elements (e.g. <CLUSTER/>) to preserve syntax correctness
This commit is contained in:
parent
308469a824
commit
f7b7f0cc4a
@ -225,6 +225,8 @@ module OpenNebula
|
||||
end
|
||||
end
|
||||
|
||||
# Returns wheter there are elements for a given XPath
|
||||
# xpath_str:: _String_ XPath expression to locate the element
|
||||
def has_elements?(xpath_str)
|
||||
if NOKOGIRI
|
||||
element = @xml.xpath(xpath_str.to_s.upcase)
|
||||
@ -235,10 +237,13 @@ module OpenNebula
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the <TEMPLATE> element in text form
|
||||
# indent:: _Boolean_ indents the resulting string, default true
|
||||
def template_str(indent=true)
|
||||
template_like_str('TEMPLATE', indent)
|
||||
end
|
||||
|
||||
# Returns the <TEMPLATE> element in XML form
|
||||
def template_xml
|
||||
if NOKOGIRI
|
||||
@xml.xpath('TEMPLATE').to_s
|
||||
@ -247,47 +252,54 @@ module OpenNebula
|
||||
end
|
||||
end
|
||||
|
||||
# Returns elements in text form
|
||||
# root_element:: _String_ base element
|
||||
# indent:: _Boolean_ indents the resulting string, default true
|
||||
# xpath_exp:: _String_ filter elements with a XPath
|
||||
def template_like_str(root_element, indent=true, xpath_exp=nil)
|
||||
if NOKOGIRI
|
||||
xml_template=@xml.xpath(root_element).to_s
|
||||
rexml=REXML::Document.new(xml_template).root
|
||||
xml_template = @xml.xpath(root_element).to_s
|
||||
rexml = REXML::Document.new(xml_template).root
|
||||
else
|
||||
rexml=@xml.elements[root_element]
|
||||
rexml = @xml.elements[root_element]
|
||||
end
|
||||
|
||||
if indent
|
||||
ind_enter="\n"
|
||||
ind_tab=' '
|
||||
ind_enter = "\n"
|
||||
ind_tab = ' '
|
||||
else
|
||||
ind_enter=''
|
||||
ind_tab=' '
|
||||
ind_enter = ''
|
||||
ind_tab = ' '
|
||||
end
|
||||
|
||||
str=rexml.elements.collect(xpath_exp) {|n|
|
||||
if n.class==REXML::Element
|
||||
str_line=""
|
||||
if n.has_elements?
|
||||
str_line << n.name << "=[" << ind_enter
|
||||
str = rexml.elements.collect(xpath_exp) {|n|
|
||||
next if n.class != REXML::Element
|
||||
|
||||
str_line << n.collect {|n2|
|
||||
if n2 && n2.class==REXML::Element
|
||||
str = ""
|
||||
str << ind_tab << n2.name << '='
|
||||
str << attr_to_str(n2.text) if n2.text
|
||||
str
|
||||
end
|
||||
}.compact.join(','+ind_enter)
|
||||
str_line<<" ]"
|
||||
else
|
||||
str_line << n.name << '=' << attr_to_str(n.text.to_s)
|
||||
end
|
||||
str_line
|
||||
str_line = ""
|
||||
|
||||
if n.has_elements?
|
||||
str_line << "#{n.name}=[#{ind_enter}" << n.collect { |n2|
|
||||
|
||||
next if n2.class != REXML::Element or !n2.has_text?
|
||||
|
||||
str = "#{ind_tab}#{n2.name}=#{attr_to_str(n2.text)}"
|
||||
|
||||
}.compact.join(",#{ind_enter}") << " ]"
|
||||
else
|
||||
next if !n.has_text?
|
||||
|
||||
str_line << "#{n.name}=#{attr_to_str(n.text)}"
|
||||
end
|
||||
|
||||
str_line
|
||||
}.compact.join("\n")
|
||||
|
||||
str
|
||||
return str
|
||||
end
|
||||
|
||||
#
|
||||
#
|
||||
#
|
||||
def to_xml(pretty=false)
|
||||
if NOKOGIRI && pretty
|
||||
str = @xml.to_xml
|
||||
@ -305,6 +317,9 @@ module OpenNebula
|
||||
return str
|
||||
end
|
||||
|
||||
#
|
||||
#
|
||||
#
|
||||
def to_hash(hash={}, element=nil)
|
||||
element ||= @xml.document.root
|
||||
|
||||
@ -344,12 +359,14 @@ module OpenNebula
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
#
|
||||
#
|
||||
#
|
||||
def attr_to_str(attr)
|
||||
attr.gsub!('"',"\\\"")
|
||||
|
||||
if attr.match(/[=,' ']/)
|
||||
return '"' + attr + '"'
|
||||
end
|
||||
attr.prepend('"')
|
||||
attr << '"'
|
||||
|
||||
return attr
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user