Class TaskJuggler::XMLDocument
In: lib/taskjuggler/XMLDocument.rb
Parent: Object

This class provides a rather simple XML document generator. It provides basic features to create a tree of XMLElements and to generate a XML String or file. It‘s much less powerful than REXML but provides a more efficient API to create XMLDocuments with lots of attributes.

Methods

<<   new   to_s   write  

Public Class methods

Create an empty XML document.

[Source]

# File lib/taskjuggler/XMLDocument.rb, line 25
    def initialize(&block)
      @elements = block ? yield(block) : []
    end

Public Instance methods

Add a top-level XMLElement.

[Source]

# File lib/taskjuggler/XMLDocument.rb, line 30
    def <<(arg)
      if arg.is_a?(Array)
        @elements += arg.flatten
      elsif arg.nil?
        # do nothing
      elsif arg.is_a?(XMLElement)
        @elements << arg
      else
        raise ArgumentError, "Unsupported argument of type #{arg.class}: " +
                             "#{arg.inspect}"
      end
    end

Produce the XMLDocument as String.

[Source]

# File lib/taskjuggler/XMLDocument.rb, line 44
    def to_s
      str = ''
      @elements.each do |element|
        str << element.to_s(0)
      end

      str
    end

Write the XMLDocument to the specified file.

[Source]

# File lib/taskjuggler/XMLDocument.rb, line 54
    def write(filename)
      f = filename == '.' ? $stdout : File.new(filename.untaint, 'w')
      @elements.each do |element|
        f.puts element.to_s(0)
      end
      f.close unless f == $stdout
    end

[Validate]