| 1 |
# XSD4R - XML Mapping for Ruby |
|---|
| 2 |
# Copyright (C) 2000-2007 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. |
|---|
| 3 |
|
|---|
| 4 |
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can |
|---|
| 5 |
# redistribute it and/or modify it under the same terms of Ruby's license; |
|---|
| 6 |
# either the dual license version in 2003, or any later version. |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
require "soap/parser" |
|---|
| 10 |
require 'soap/encodingstyle/literalHandler' |
|---|
| 11 |
require "soap/generator" |
|---|
| 12 |
require "soap/mapping" |
|---|
| 13 |
require "soap/mapping/wsdlliteralregistry" |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
module XSD |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
module Mapping |
|---|
| 20 |
MappingRegistry = SOAP::Mapping::LiteralRegistry.new |
|---|
| 21 |
|
|---|
| 22 |
def self.obj2xml(obj, elename = nil, io = nil) |
|---|
| 23 |
Mapper.new(MappingRegistry).obj2xml(obj, elename, io) |
|---|
| 24 |
end |
|---|
| 25 |
|
|---|
| 26 |
def self.xml2obj(stream, klass = nil) |
|---|
| 27 |
Mapper.new(MappingRegistry).xml2obj(stream, klass) |
|---|
| 28 |
end |
|---|
| 29 |
|
|---|
| 30 |
class Mapper |
|---|
| 31 |
MAPPING_OPT = { |
|---|
| 32 |
:default_encodingstyle => SOAP::LiteralNamespace, |
|---|
| 33 |
:generate_explicit_type => false, |
|---|
| 34 |
:root_type_hint => true |
|---|
| 35 |
}.freeze |
|---|
| 36 |
|
|---|
| 37 |
def initialize(registry) |
|---|
| 38 |
@registry = registry |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
def obj2xml(obj, elename = nil, io = nil) |
|---|
| 42 |
opt = MAPPING_OPT.dup |
|---|
| 43 |
unless elename |
|---|
| 44 |
if definition = @registry.elename_schema_definition_from_class(obj.class) |
|---|
| 45 |
elename = definition.elename |
|---|
| 46 |
opt[:root_type_hint] = false |
|---|
| 47 |
end |
|---|
| 48 |
end |
|---|
| 49 |
elename = SOAP::Mapping.to_qname(elename) if elename |
|---|
| 50 |
soap = SOAP::Mapping.obj2soap(obj, @registry, elename, opt) |
|---|
| 51 |
if soap.elename.nil? or soap.elename == XSD::QName::EMPTY |
|---|
| 52 |
soap.elename = |
|---|
| 53 |
XSD::QName.new(nil, SOAP::Mapping.name2elename(obj.class.to_s)) |
|---|
| 54 |
end |
|---|
| 55 |
generator = SOAP::Generator.new(opt) |
|---|
| 56 |
generator.generate(soap, io) |
|---|
| 57 |
end |
|---|
| 58 |
|
|---|
| 59 |
def xml2obj(stream, klass = nil) |
|---|
| 60 |
parser = SOAP::Parser.new(MAPPING_OPT) |
|---|
| 61 |
soap = parser.parse(stream) |
|---|
| 62 |
SOAP::Mapping.soap2obj(soap, @registry, klass) |
|---|
| 63 |
end |
|---|
| 64 |
end |
|---|
| 65 |
end |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
end |
|---|