Class: Mappum::XmlTransform

Inherits:
Object show all
Defined in:
lib/mappum/xml_transform.rb

Overview

SOAP4r based xml to xml transforming class.

Instance Method Summary collapse

Constructor Details

#initialize(map_catalogue = nil, force_open_struct = false) ⇒ XmlTransform



108
109
110
111
# File 'lib/mappum/xml_transform.rb', line 108

def initialize(map_catalogue = nil, force_open_struct=false)
  @default_mapper =  XSD::Mapping::Mapper.new(SOAP::Mapping::LiteralRegistry.new)
  @ruby_transform = RubyXmlTransform.new(map_catalogue, OpenXmlObject, force_open_struct)
end

Instance Method Details

#to_xml_string(transformed, map = nil, to_qname = nil, soap = false) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/mappum/xml_transform.rb', line 175

def to_xml_string(transformed, map=nil, to_qname=nil, soap=false)
   to_mapper = XSD::Mapping::Mapper.find_mapper_for_class(transformed.class)
  if to_mapper.nil?
    to_mapper = @default_mapper
  end

  if transformed.kind_of?(OpenXmlObject) and to_qname.nil? and not map.nil?
    to = map.to
    if to.clazz.kind_of?(XSD::QName)
      to_qname = to.clazz
    else
      to_qname = XSD::QName.new(nil, XSD::CodeGen::GenSupport.safeconstname(to.clazz.to_s))
    end
  end
  to_preparsed = to_mapper.obj2soap(transformed,to_qname)
  if soap == true 
    to_preparsed = SOAP::SOAPEnvelope.new(SOAP::SOAPHeader.new, SOAP::SOAPBody.new(to_preparsed))
  end
  generator = SOAP::Generator.new(XSD::Mapping::Mapper::MAPPING_OPT)
  return generator.generate(to_preparsed, nil)
end

#transform(from_xml, map = nil, from_qname = nil, to_qname = nil, handle_soap = true) ⇒ Object

Transforms given from_xml using map xml transformation from and to Ruby objects can be controled via from_qname and to_qname. And soap envelope will be striped when handle_soap=true.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
# File 'lib/mappum/xml_transform.rb', line 116

def transform(from_xml, map=nil, from_qname=nil, to_qname=nil, handle_soap=true)
  soap = false
  
  parser = SOAP::Parser.new(XSD::Mapping::Mapper::MAPPING_OPT)
  preparsed = parser.parse(from_xml)

  if from_qname.nil?
    from_qname = preparsed.elename
  end
  
  if handle_soap and from_qname == XSD::QName.new("http://schemas.xmlsoap.org/soap/envelope/","Envelope")
    soap = true
    #for soap remove envelope
    preparsed = preparsed.body.root_node
    from_qname = preparsed.elename
  end
  
        
  from_mapper = XSD::Mapping::Mapper.find_mapper_for_type(from_qname)
  
  if from_mapper.nil?
     from_mapper = @default_mapper
  end
 
  from_clazz = XSD::Mapping::Mapper.get_class_from_qname(from_qname) unless from_qname.nil?
  begin
    parsed =SOAP::Mapping.soap2obj(preparsed, from_mapper.registry, from_clazz)
  rescue NoMethodError => e
    raise ParsingFailedException.new("Parsing failed for xml with root element: #{from_qname}")
  end

  map ||= @ruby_transform.map_catalogue[from_qname]
  map ||= @ruby_transform.map_catalogue[from_qname.name.to_sym] unless from_qname.name.nil?
  if not map.nil? and not map.kind_of?(Map)
    map = @ruby_transform.map_catalogue[map.to_sym]
  end

  begin
    transformed = @ruby_transform.transform(parsed, map)
  rescue MapMissingException => e
    if e.from == parsed
      raise MapMissingException.new(e.from,"Map for element \"#{from_qname}\" not found!")
    else
      e.from = to_xml_string(e.from)
      e.to = to_xml_string(e.to)
      e.from_root = to_xml_string(e.from_root)
      e.to_root = to_xml_string(e.to_root)
      raise e
    end
  rescue MappumException => e
      e.from = to_xml_string(e.from)
      e.to = to_xml_string(e.to)
      e.from_root = to_xml_string(e.from_root)
      e.to_root = to_xml_string(e.to_root)
      raise e
  end
  
return to_xml_string(transformed, map, to_qname, soap)
end