class LANSA::XML::Writer

Public Class Methods

new(output, strip_space = false, indent = nil) click to toggle source
# File xml/writer.rb, line 11
def initialize output, strip_space = false, indent = nil
  if output.respond_to?(:print)
    @stream = output
  else
    @java_sax = output
    begin
      # Due to problems with the stupid XML Java libraries interfering with the Ruby class loading,
      # we are using the class loader of the Java output to load an appropriate Attributes implementation
      attributes_class = output.get_class.get_class_loader.load_class('org.xml.sax.helpers.AttributesImpl')
      @java_attrs = attributes_class.new_instance
    rescue
      @java_attrs = org.xml.sax.helpers.AttributesImpl.new
    end
  end
  @strip_space = strip_space
  @indent = indent
  @depth = -1
  @should_indent = false
end

Public Instance Methods

cdata_block(content) click to toggle source
# File xml/writer.rb, line 114
def cdata_block content
  if @java_sax
    chars = content.to_java.to_char_array
    @java_sax.characters chars, 0, chars.size
  else
    @stream.print "<![CDATA[#{content}]]>"
  end
end
characters(s) click to toggle source
# File xml/writer.rb, line 93
def characters s
  return if s.nil?
  s = s.content if s.kind_of? Nokogiri::XML::Text
  if @strip_space and s.strip.empty?
    return
  end
  if @java_sax
    chars = s.to_java.to_char_array
    @java_sax.characters chars, 0, chars.size
  else
    @should_indent = false
    @stream.print escape_string(s)
  end
end
comment(content) click to toggle source
# File xml/writer.rb, line 108
def comment content
  if not @java_sax
    @stream.print "<!--#{content}-->"
  end
end
depth() click to toggle source
# File xml/writer.rb, line 123
def depth
  @depth
end
end_document() click to toggle source
# File xml/writer.rb, line 41
def end_document
  @java_sax.end_document if @java_sax
end
end_element_namespace(name, prefix = nil, uri = nil) click to toggle source
# File xml/writer.rb, line 76
def end_element_namespace name, prefix = nil, uri = nil

  raise "Element must not be empty" if not name or name.size == 0

  if @java_sax
    @java_sax.end_element uri, name, name
  else
    print_indent if @last_element != name
    @should_indent = true
    @stream.print "</#{name}>"
    @stream.print "\n" if @depth == 0
  end

  @depth -= 1

end
start_document() click to toggle source
# File xml/writer.rb, line 37
def start_document
  @java_sax.start_document if @java_sax
end
start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) click to toggle source
# File xml/writer.rb, line 45
def start_element_namespace name, attrs = [], prefix = nil, uri = nil, ns = []

  raise "Element must not be empty" if not name or name.size == 0

  @depth += 1

  if @java_sax
    @java_attrs.clear
    attrs.each do |a|
      raise "Attribute must not be empty" if a.localname.size == 0
      value = a.value
      value = value.content if value.kind_of? Nokogiri::XML::Text
      @java_attrs.add_attribute('', a.localname, a.localname, nil, value)
    end
    @java_sax.start_element uri, name, name, @java_attrs
  else
    @should_indent = true
    print_indent
    @stream.print "<#{name}"
    attrs.each do |a|
      raise "Attribute must not be empty" if a.localname.size == 0
      value = a.value
      value = value.content if value.kind_of? Nokogiri::XML::Text
      @stream.print " #{a.localname}=\"#{escape_string(value)}\""
    end
    @stream.print '>'
    @last_element = name
  end

end
xmldecl(version = DEFAULT_XML_VERSION, encoding = DEFAULT_XML_ENCODING, standalone = DEFAULT_XML_STANDALONE) click to toggle source
# File xml/writer.rb, line 31
def xmldecl version = DEFAULT_XML_VERSION, encoding = DEFAULT_XML_ENCODING, standalone = DEFAULT_XML_STANDALONE
  if not @java_sax
    @stream.print "<?xml version=\"#{version}\" encoding=\"UTF-8\"?>"
  end
end