Module: StixRuby::Marshall::InstanceMethods

Defined in:
lib/ruby_stix/marshall.rb

Instance Method Summary collapse

Instance Method Details

#to_java_io(io) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_stix/marshall.rb', line 49

def to_java_io(io)
  if io.kind_of?(StringIO) || io.kind_of?(File)
    io.to_outputstream
  elsif io.kind_of?(String)
    sio = StringIO.new(io)
    sio.to_outputstream
  else
    io
  end
end

#to_xmlObject



60
61
62
63
64
# File 'lib/ruby_stix/marshall.rb', line 60

def to_xml
  io = StringIO.new
  write(io)
  io.string
end

#write(io, args = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_stix/marshall.rb', line 11

def write(io, args = {})
  clean_namespaces = args[:clean_namespaces] != false

  if clean_namespaces
    document = self.class.new_dom
    self.class.marshaller.marshal(self, document)

    namespaces = {}
    namespaces[StixRuby::NAMESPACE_MAPPINGS[document.document_element.namespace_uri]] = true
    for i in 0...document.document_element.child_nodes.length
      collect_namespaces(document.document_element.child_nodes.item(i), namespaces, )
    end

    to_delete = []

    for i in 0...document.document_element.attributes.length
      attribute = document.document_element.attributes.item(i)
      if attribute.name =~ /xmlns/
        if !(namespaces[attribute.name.split(':').last] || namespaces[attribute.value])
          to_delete.push(attribute.name)
        end
      end
    end

    to_delete.each {|i| document.document_element.remove_attribute(i)}
    io = to_java_io(io)

    self.class.dom_writer.set_output_property(javax.xml.transform.OutputKeys::INDENT, args[:no_formatting] == true ? 'no' : 'yes');
    self.class.dom_writer.transform(javax.xml.transform.dom.DOMSource.new(document), javax.xml.transform.stream.StreamResult.new(io))
  else
    io = to_java_io(io)
    self.class.marshaller.set_property(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, !(args[:no_formatting] == true))
    self.class.marshaller.marshal(self, io)
  end

  return io
end