Module: MobyUtil::XML

Defined in:
lib/tdriver/util/xml/xml.rb,
lib/tdriver/util/xml/text.rb,
lib/tdriver/util/xml/error.rb,
lib/tdriver/util/xml/builder.rb,
lib/tdriver/util/xml/comment.rb,
lib/tdriver/util/xml/element.rb,
lib/tdriver/util/xml/nodeset.rb,
lib/tdriver/util/xml/document.rb,
lib/tdriver/util/xml/nil_node.rb,
lib/tdriver/util/xml/attribute.rb,
lib/tdriver/util/xml/abstraction.rb,
lib/tdriver/util/xml/parsers/libxml/libxml.rb,
lib/tdriver/util/xml/parsers/nokogiri/node.rb,
lib/tdriver/util/xml/parsers/nokogiri/text.rb,
lib/tdriver/util/xml/parsers/nokogiri/builder.rb,
lib/tdriver/util/xml/parsers/nokogiri/comment.rb,
lib/tdriver/util/xml/parsers/nokogiri/element.rb,
lib/tdriver/util/xml/parsers/nokogiri/nodeset.rb,
lib/tdriver/util/xml/parsers/nokogiri/document.rb,
lib/tdriver/util/xml/parsers/nokogiri/attribute.rb,
lib/tdriver/util/xml/parsers/nokogiri/abstraction.rb

Defined Under Namespace

Modules: Abstraction, LibXML, Nokogiri Classes: Attribute, Builder, BuilderError, Comment, Document, Element, NilNode, Nodeset, ParseError, Text

Class Method Summary collapse

Class Method Details

.buffer_sizeObject

Get current XML document cache buffering size

params

return

Integer

raises



75
76
77
78
79
# File 'lib/tdriver/util/xml/xml.rb', line 75

def self.buffer_size
  
  @document_cache_buffer_size

end

.buffer_size=(value) ⇒ Object

Set XML document cache buffering size, set 0 to disable

params

return

Integer

raises

TypeError


62
63
64
65
66
67
68
# File 'lib/tdriver/util/xml/xml.rb', line 62

def self.buffer_size=( value )

  value.check_type Integer, 'wrong argument type $1 for XML cache buffer size (expected $2)' 

  @document_cache_buffer_size = value

end

.build(&block) ⇒ Object

Create XML builder object dynamically

Usage:

MobyUtil::XML.build{
  root{
    element(:name => "element_name", :id => "0") {
      child(:name => "1st_child_of_element_0", :value => "123" )        
      child(:name => "2nd_child_of_element_0", :value => "456" )
    }
  }
}.to_xml

params

&block

return

MobyUtil::XML::Builder

raises



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/tdriver/util/xml/xml.rb', line 306

def self.build( &block )

  begin

    Builder.new.tap{ | builder | 

      builder.build( &block )

    }

  rescue

    raise MobyUtil::XML::BuilderError, "#{ $!.message } (#{ $!.class })"

  end
  
end

.current_parserObject

Get current XML parser

params

return

Module

raises



50
51
52
53
54
# File 'lib/tdriver/util/xml/xml.rb', line 50

def self.current_parser

  @parser

end

.current_parser=(parser) ⇒ Object

Set XML parser to be used

params

Module

return

nil

Document

XML document object

raises



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tdriver/util/xml/xml.rb', line 88

def self.current_parser=( parser )

  # set current parser
  @parser = parser

  # apply parser implementation to abstraction modules
  [ 
    :Document, 
    :Element, 
    :Nodeset, 
    :Attribute, 
    :Text, 
    :Builder,
    :Comment 
    
  ].each do | _module |

    const_get( _module ).module_exec{

      begin      

        # include parser behaviour
        include parser.const_get( _module ) 

      rescue NameError

        # raise proper exception if behaviour module not found
        raise NotImplementedError, "Required behaviour module #{ parser.name }::#{ _module } not found"

      end
    
    }
    
  end

  # return current parser as result
  parser

end

.parse_file(filename) ⇒ Object

Create XML Document object by parsing XML from file

Usage: MobyUtil::XML.parse_file(‘xml_dump.xml’)

==> Returns XML document object; default xml parser will be used.

params

filename

String containing path and filename of XML file.

return

Document

XML document object

raises

IOError

File <name> not found

Raises:

  • (IOError)


274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/tdriver/util/xml/xml.rb', line 274

def self.parse_file( filename )    
  
  # raise exception if file not found
  raise IOError, "File #{ filename.inspect } not found" unless File.exist?( filename )

  # parse file content
  parse_string( 
  
    IO.read( filename ) 
    
  )

end

.parse_string(xml_string, crc = nil) ⇒ Object

Create XML Document object by parsing XML from string

Usage: MobyUtil::XML.parse_string(‘<root>value</root>’)

==> Returns XML document object; default xml parser will be used.

params

xml_string

String containing XML

crc

Optional CRC16 checksum - used for cache/buffering if given

return

Document

XML document object

Array

Array containing XML document object and status was it already found from cache

raises



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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/tdriver/util/xml/xml.rb', line 140

def self.parse_string( xml_string, crc = nil )

  begin

=begin
    unless crc.nil?

      [ Document.new( xml_string ), false ]

    else

      Document.new( xml_string )
    
    end
=end

# JKo: disable xml caching for now, need more investigation why tests starts to fail
#=begin
    unless crc.nil?

      if @document_cache[ :cache ].include?( crc )

        # return cached object and status that object was found from cache
        [ @document_cache[ :objects ][ crc ], true ]

      else
  
        cache_enabled = ( @document_cache_buffer_size > 0 )

        # create new document object with given xml string
        document = Document.new( xml_string, { :cache_enabled => cache_enabled } )

        # verify that xml caching is enabled
        if cache_enabled

          # drop olders cached xml object
          if @document_cache[ :cache ].count == @document_cache_buffer_size

            # remove data object from cache
            @document_cache[ :objects ].delete( 

              # take (oldest) first from cache buffer
              @document_cache[ :cache ].shift

            )

          end

          # add new xml data object to cache
          @document_cache[ :cache ] << crc

          # add new xml data object to cache
          @document_cache[ :objects ][ crc ] = document 

        end

        # return document object and status that object was not found from cache
        [ document, false ]
        
      end

    else

      # create new document object with given xml string - no caching used
      Document.new( xml_string )

    end
#=end
  rescue

    if $TDRIVER_INITIALIZED == true
    
      # string for exception message
      dump_location = ""

      # check if xml parse error logging is enabled
      if $parameters[ :logging_xml_parse_error_dump, 'true' ].to_s.to_boolean

        # construct filename for xml dump
        filename = 'xml_error_dump'

        # add timestamp to filename if not overwriting the existing dump file 
        unless $parameters[ :logging_xml_parse_error_dump_overwrite, 'false' ].to_s.to_boolean

          filename << "_#{ Time.now.to_i }"

        end

        # add file extension
        filename << '.xml'

        # ... join filename with xml dump output path 
        path = File.join( MobyUtil::FileHelper.expand_path( $parameters[ :logging_xml_parse_error_dump_path ] ), filename )

        begin

          # write xml string to file
          File.open( path, "w" ){ | file | file << xml_string }

          dump_location = "Saved to #{ path }"

        rescue

          dump_location = "Error while saving to file #{ path }"

        end

      end

      # raise exception
      raise MobyUtil::XML::ParseError, "#{ $!.message.gsub("\n", '') } (#{ $!.class }). #{ dump_location }"

    else
    
      # raise exception
      raise MobyUtil::XML::ParseError, "#{ $!.message.gsub("\n", '') } (#{ $!.class })"
    
    end

  end

end