/tools/Ruby/lib/ruby/1.8/xsd/mapping.rb

http://github.com/agross/netopenspace · Ruby · 42 lines · 26 code · 11 blank · 5 comment · 1 complexity · 9359a18232a16a4bfdf655a1b081a3a2 MD5 · raw file

  1. # XSD4R - XML Mapping for Ruby
  2. # Copyright (C) 2005 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
  3. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
  4. # redistribute it and/or modify it under the same terms of Ruby's license;
  5. # either the dual license version in 2003, or any later version.
  6. require "soap/parser"
  7. require 'soap/encodingstyle/literalHandler'
  8. require "soap/generator"
  9. require "soap/mapping"
  10. require "soap/mapping/wsdlliteralregistry"
  11. module XSD
  12. module Mapping
  13. MappingRegistry = SOAP::Mapping::WSDLLiteralRegistry.new
  14. MappingOpt = {:default_encodingstyle => SOAP::LiteralNamespace}
  15. def self.obj2xml(obj, elename = nil, io = nil)
  16. if !elename.nil? and !elename.is_a?(XSD::QName)
  17. elename = XSD::QName.new(nil, elename)
  18. end
  19. elename ||= XSD::QName.new(nil, SOAP::Mapping.name2elename(obj.class.to_s))
  20. soap = SOAP::Mapping.obj2soap(obj, MappingRegistry)
  21. soap.elename = elename
  22. generator = SOAP::SOAPGenerator.new(MappingOpt)
  23. generator.generate(soap, io)
  24. end
  25. def self.xml2obj(stream)
  26. parser = SOAP::Parser.new(MappingOpt)
  27. soap = parser.parse(stream)
  28. SOAP::Mapping.soap2obj(soap, MappingRegistry)
  29. end
  30. end
  31. end