Sha256: 91c59361bdce2e7c272a5118262ff1fb7ac527890fd563ae58a4783f986290bd

Contents?: true

Size: 1.65 KB

Versions: 6

Compression:

Stored size: 1.65 KB

Contents

#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = XMLDocument.rb -- The TaskJuggler III Project Management Software
#
# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011
#               by Chris Schlaeger <chris@linux.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

require 'taskjuggler/XMLElement'

class TaskJuggler

  # 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.
  class XMLDocument

    # Create an empty XML document.
    def initialize(&block)
      @elements = block ? yield(block) : []
    end

    # Add a top-level XMLElement.
    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.
    def to_s
      str = ''
      @elements.each do |element|
        str << element.to_s(0)
      end

      str
    end

    # Write the XMLDocument to the specified file.
    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

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
taskjuggler-3.0.0 lib/taskjuggler/XMLDocument.rb
taskjuggler-0.2.2 lib/taskjuggler/XMLDocument.rb
taskjuggler-0.2.1 lib/taskjuggler/XMLDocument.rb
taskjuggler-0.2.0 lib/taskjuggler/XMLDocument.rb
taskjuggler-0.1.1 lib/taskjuggler/XMLDocument.rb
taskjuggler-0.1.0 lib/taskjuggler/XMLDocument.rb