Revision e3b9eb63 src/oca/ruby/OpenNebula/XMLUtils.rb

View differences:

src/oca/ruby/OpenNebula/XMLUtils.rb
7 7
    rescue LoadError
8 8
        NOKOGIRI=false
9 9
    end
10
    
10

  
11 11
    # Require crack library if present, otherwise don't bother
12 12
    # This is just for OCCI use
13 13
    begin
......
17 17

  
18 18
    ###########################################################################
19 19
    # The XMLUtilsElement module provides an abstraction of the underlying
20
    # XML parser engine. It provides XML-related methods for the Pool Elements  
20
    # XML parser engine. It provides XML-related methods for the Pool Elements
21 21
    ###########################################################################
22 22
    module XMLUtilsElement
23 23
        # Initialize a XML document for the element
......
34 34

  
35 35
        # Extract an element from the XML description of the PoolElement.
36 36
        # key::_String_ The name of the element
37
        # [return] _String_ the value of the element 
37
        # [return] _String_ the value of the element
38 38
        # Examples:
39 39
        #   ['VID'] # gets VM id
40 40
        #   ['HISTORY/HOSTNAME'] # get the hostname from the history
......
47 47
            else
48 48
                element=@xml.elements[key.to_s.upcase]
49 49
            end
50
            
51
            if element 
50

  
51
            if element
52 52
                element.text
53 53
            end
54 54
        end
55
        
55

  
56 56
        def template_str(indent=true)
57 57
            template_like_str('TEMPLATE', indent)
58 58
        end
59
            
59

  
60 60
        def template_like_str(root_element, indent=true)
61 61
            if NOKOGIRI
62 62
                xml_template=@xml.xpath(root_element).to_s
......
64 64
            else
65 65
                rexml=@xml.elements[root_element]
66 66
            end
67
            
67

  
68 68
            if indent
69 69
                ind_enter="\n"
70 70
                ind_tab='  '
......
72 72
                ind_enter=''
73 73
                ind_tab=' '
74 74
            end
75
            
75

  
76 76
            str=rexml.collect {|n|
77 77
                if n.class==REXML::Element
78 78
                    str_line=""
......
93 93
                    str_line
94 94
                end
95 95
            }.compact.join("\n")
96
            
96

  
97 97
            str
98 98
        end
99
        
100
        def to_hash 
99

  
100
        def to_hash
101 101
            if !@hash && @xml
102 102
                begin
103 103
                   @hash = Crack::XML.parse(to_xml)
......
116 116
                str = ""
117 117
                if pretty
118 118
                    REXML::Formatters::Pretty.new(1).write(@xml,str)
119
                else 
119
                else
120 120
                    REXML::Formatters::Default.new.write(@xml,str)
121 121
                end
122 122
                str
123 123
            end
124 124
        end
125
        
125

  
126
        def XMLUtilsElement.xml_to_hash(xml)
127
            begin
128
                hash = Crack::XML.parse(xml)
129
            rescue Exception => e
130
                error = OpenNebula::Error.new(e.message)
131
                return error
132
            end
133

  
134
            return hash
135
        end
136

  
126 137
    end
127
        
138

  
128 139
    ###########################################################################
129 140
    # The XMLUtilsPool module provides an abstraction of the underlying
130
    # XML parser engine. It provides XML-related methods for the Pools  
141
    # XML parser engine. It provides XML-related methods for the Pools
131 142
    ###########################################################################
132 143
    module XMLUtilsPool
133 144

  
......
141 152
                xml=REXML::Document.new(xml).root
142 153
            end
143 154
        end
144
        
155

  
145 156
        #Executes the given block for each element of the Pool
146
        #block:: _Block_ 
157
        #block:: _Block_
147 158
        def each_element(block)
148 159
            if NOKOGIRI
149 160
                @xml.xpath(
......
165 176
                str = ""
166 177
                if pretty
167 178
                    REXML::Formatters::Pretty.new(1).write(@xml,str)
168
                else 
179
                else
169 180
                    REXML::Formatters::Default.new.write(@xml,str)
170 181
                end
171 182
                str
172 183
            end
173 184
        end
174
        
175
        def to_hash 
185

  
186
        def to_hash
176 187
            if !@hash && @xml
177 188
                @hash=Crack::XML.parse(to_xml)
178 189
            end

Also available in: Unified diff