2009-01-09 22:27:31 +00:00
|
|
|
# --
|
|
|
|
# Copyright (C) 2008-2009 10gen Inc.
|
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2009-01-09 22:27:31 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-09 22:27:31 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2009-01-09 22:27:31 +00:00
|
|
|
# ++
|
|
|
|
|
|
|
|
require 'rexml/document'
|
|
|
|
require 'mongo'
|
|
|
|
|
2009-01-12 14:48:24 +00:00
|
|
|
# Converts a .xson file (an XML file that describes a Mongo-type document) to
|
2009-01-09 22:27:31 +00:00
|
|
|
# an OrderedHash.
|
|
|
|
class XMLToRuby
|
|
|
|
|
|
|
|
include XGen::Mongo::Driver
|
|
|
|
|
|
|
|
def xml_to_ruby(io)
|
|
|
|
doc = REXML::Document.new(io)
|
|
|
|
doc_to_ruby(doc.root.elements['doc'])
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def element_to_ruby(e)
|
|
|
|
type = e.name
|
|
|
|
child = e.elements[1]
|
|
|
|
case type
|
|
|
|
when 'oid'
|
|
|
|
ObjectID.from_string(e.text)
|
|
|
|
when 'ref'
|
|
|
|
dbref_to_ruby(e.elements)
|
|
|
|
when 'int'
|
|
|
|
e.text.to_i
|
|
|
|
when 'number'
|
|
|
|
e.text.to_f
|
2009-03-12 19:34:15 +00:00
|
|
|
when 'string'
|
2009-01-09 22:27:31 +00:00
|
|
|
e.text.to_s
|
2009-03-12 19:34:15 +00:00
|
|
|
when 'code'
|
2009-03-12 20:01:13 +00:00
|
|
|
Code.new(e.text.to_s)
|
2009-01-10 00:58:53 +00:00
|
|
|
when 'binary'
|
2009-02-02 15:07:01 +00:00
|
|
|
bin = Binary.new
|
|
|
|
decoded = Base64.decode64(e.text.to_s)
|
|
|
|
decoded.each_byte { |b| bin.put(b) }
|
|
|
|
bin
|
2009-01-09 23:25:36 +00:00
|
|
|
when 'symbol'
|
|
|
|
e.text.to_s.intern
|
2009-01-09 22:27:31 +00:00
|
|
|
when 'boolean'
|
|
|
|
e.text.to_s == 'true'
|
|
|
|
when 'array'
|
|
|
|
array_to_ruby(e.elements)
|
|
|
|
when 'date'
|
2009-07-16 20:32:57 +00:00
|
|
|
Time.at(e.text.to_f / 1000.0)
|
2009-01-09 22:27:31 +00:00
|
|
|
when 'regex'
|
|
|
|
regex_to_ruby(e.elements)
|
2009-01-12 17:08:02 +00:00
|
|
|
when 'null'
|
2009-01-09 22:27:31 +00:00
|
|
|
nil
|
2009-01-12 17:08:02 +00:00
|
|
|
when 'undefined'
|
|
|
|
Undefined.new
|
2009-01-09 22:27:31 +00:00
|
|
|
when 'doc'
|
|
|
|
doc_to_ruby(e)
|
|
|
|
else
|
|
|
|
raise "Unknown type #{type} in element with name #{e.attributes['name']}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def doc_to_ruby(element)
|
|
|
|
oh = OrderedHash.new
|
|
|
|
element.elements.each { |e| oh[e.attributes['name']] = element_to_ruby(e) }
|
|
|
|
oh
|
|
|
|
end
|
|
|
|
|
|
|
|
def array_to_ruby(elements)
|
|
|
|
a = []
|
|
|
|
elements.each { |e|
|
|
|
|
index_str = e.attributes['name']
|
|
|
|
a[index_str.to_i] = element_to_ruby(e)
|
|
|
|
}
|
|
|
|
a
|
|
|
|
end
|
|
|
|
|
|
|
|
def regex_to_ruby(elements)
|
|
|
|
pattern = elements['pattern'].text
|
|
|
|
options_str = elements['options'].text || ''
|
|
|
|
|
|
|
|
options = 0
|
|
|
|
options |= Regexp::IGNORECASE if options_str.include?('i')
|
|
|
|
options |= Regexp::MULTILINE if options_str.include?('m')
|
|
|
|
options |= Regexp::EXTENDED if options_str.include?('x')
|
|
|
|
Regexp.new(pattern, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def dbref_to_ruby(elements)
|
|
|
|
ns = elements['ns'].text
|
|
|
|
oid_str = elements['oid'].text
|
2009-03-10 18:17:01 +00:00
|
|
|
DBRef.new(ns, ObjectID.from_string(oid_str))
|
2009-01-09 22:27:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|