~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to lib/rexml/formatters/transitive.rb

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'rexml/formatters/pretty'
 
2
 
 
3
module REXML
 
4
  module Formatters
 
5
    # The Transitive formatter writes an XML document that parses to an
 
6
    # identical document as the source document.  This means that no extra
 
7
    # whitespace nodes are inserted, and whitespace within text nodes is
 
8
    # preserved.  Within these constraints, the document is pretty-printed,
 
9
    # with whitespace inserted into the metadata to introduce formatting.
 
10
    #
 
11
    # Note that this is only useful if the original XML is not already
 
12
    # formatted.  Since this formatter does not alter whitespace nodes, the
 
13
    # results of formatting already formatted XML will be odd.
 
14
    class Transitive < Default
 
15
      def initialize( indentation=2 )
 
16
        @indentation = indentation
 
17
        @level = 0
 
18
      end
 
19
 
 
20
      protected
 
21
      def write_element( node, output )
 
22
        output << "<#{node.expanded_name}"
 
23
 
 
24
        node.attributes.each_attribute do |attr|
 
25
          output << " "
 
26
          attr.write( output )
 
27
        end unless node.attributes.empty?
 
28
 
 
29
        output << "\n"
 
30
        output << ' '*@level
 
31
        if node.children.empty?
 
32
          output << "/" 
 
33
        else
 
34
          output << ">"
 
35
          # If compact and all children are text, and if the formatted output
 
36
          # is less than the specified width, then try to print everything on
 
37
          # one line
 
38
          skip = false
 
39
          @level += @indentation
 
40
          node.children.each { |child|
 
41
            write( child, output )
 
42
          }
 
43
          @level -= @indentation
 
44
          output << "</#{node.expanded_name}"
 
45
          output << "\n"
 
46
          output << ' '*@level
 
47
        end
 
48
        output << ">"
 
49
      end
 
50
 
 
51
      def write_text( node, output )
 
52
        output << node.to_s()
 
53
      end
 
54
    end
 
55
  end
 
56
end