~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to lib/rexml/document.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
                def add( child )
67
67
                        if child.kind_of? XMLDecl
68
68
                                @children.unshift child
 
69
        child.parent = self
69
70
                        elsif child.kind_of? DocType
70
71
        # Find first Element or DocType node and insert the decl right 
71
72
        # before it.  If there is no such node, just insert the child at the
139
140
                        xml_decl().stand_alone?
140
141
                end
141
142
 
142
 
    # Write the XML tree out.  This writes the entire XML document, including
143
 
    # declarations and processing instructions.
144
 
    #
145
 
                # A controversial point is whether Document should always write the XML
146
 
                # declaration (<?xml version='1.0'?>) whether or not one is given by the
147
 
                # user (or source document).  REXML does not write one if one was not
148
 
                # specified, because it adds unneccessary bandwidth to applications such
149
 
                # as XML-RPC.
 
143
    # Write the XML tree out, optionally with indent.  This writes out the
 
144
    # entire XML document, including XML declarations, doctype declarations,
 
145
    # and processing instructions (if any are given).
 
146
    #
 
147
    # A controversial point is whether Document should always write the XML
 
148
    # declaration (<?xml version='1.0'?>) whether or not one is given by the
 
149
    # user (or source document).  REXML does not write one if one was not
 
150
    # specified, because it adds unneccessary bandwidth to applications such
 
151
    # as XML-RPC.
 
152
    #
 
153
    # See also the classes in the rexml/formatters package for the proper way
 
154
    # to change the default formatting of XML output
150
155
    #
151
156
    # _Examples_
152
157
    #   Document.new("<a><b/></a>").serialize
155
160
    #   tr = Transitive.new( output_string )
156
161
    #   Document.new("<a><b/></a>").serialize( tr )
157
162
    #
158
 
    # formatter::
159
 
    #   One of the rexml/formatters classes.  If none is given, then the Pretty
160
 
    #   formatter will be used to dump the XML to the STDOUT.
161
 
    def serialize( formatter = nil )
162
 
      if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output) 
163
 
        output = Output.new( output, xml_decl.encoding )
164
 
      end
165
 
 
166
 
      formatter = REXML::Pretty.new( $stdout ) if (formatter.nil?)
167
 
 
168
 
                        @children.each { |node|
169
 
        puts "node = #{node.inspect}"
170
 
                                indent( output, indent ) if node.node_type == :element
171
 
                                if node.write( output, indent, transitive, ie_hack )
172
 
          output << "\n" unless indent<0 or node == @children[-1]
173
 
        end
174
 
                        }
175
 
    end
176
 
 
177
 
    # Write the XML tree out, optionally with indent.  This writes out the
178
 
                # entire XML document, including XML declarations, doctype declarations,
179
 
                # and processing instructions (if any are given).
180
 
    #
181
 
                # A controversial point is whether Document should always write the XML
182
 
                # declaration (<?xml version='1.0'?>) whether or not one is given by the
183
 
                # user (or source document).  REXML does not write one if one was not
184
 
                # specified, because it adds unneccessary bandwidth to applications such
185
 
                # as XML-RPC.
186
 
    #
187
 
    # See also the classes in the rexml/formatters package for the proper way
188
 
    # to change the default formatting of XML output
189
 
                #
190
 
                #
191
 
                # output::
192
 
                #         output an object which supports '<< string'; this is where the
193
 
                #   document will be written.
194
 
                # indent::
195
 
                #   An integer.  If -1, no indenting will be used; otherwise, the
196
 
                #   indentation will be twice this number of spaces, and children will be
197
 
                #   indented an additional amount.  For a value of 3, every item will be 
 
163
    # output::
 
164
    #     output an object which supports '<< string'; this is where the
 
165
    #   document will be written.
 
166
    # indent::
 
167
    #   An integer.  If -1, no indenting will be used; otherwise, the
 
168
    #   indentation will be twice this number of spaces, and children will be
 
169
    #   indented an additional amount.  For a value of 3, every item will be 
198
170
    #   indented 3 more levels, or 6 more spaces (2 * 3). Defaults to -1
199
 
                # trans::
200
 
                #   If transitive is true and indent is >= 0, then the output will be
201
 
                #   pretty-printed in such a way that the added whitespace does not affect
202
 
                #   the absolute *value* of the document -- that is, it leaves the value
203
 
                #   and number of Text nodes in the document unchanged.
204
 
                # ie_hack::
205
 
                #   Internet Explorer is the worst piece of crap to have ever been
206
 
                #   written, with the possible exception of Windows itself.  Since IE is
207
 
                #   unable to parse proper XML, we have to provide a hack to generate XML
208
 
                #   that IE's limited abilities can handle.  This hack inserts a space 
209
 
                #   before the /> on empty tags.  Defaults to false
210
 
                def write( output=$stdout, indent=-1, trans=false, ie_hack=false )
 
171
    # trans::
 
172
    #   If transitive is true and indent is >= 0, then the output will be
 
173
    #   pretty-printed in such a way that the added whitespace does not affect
 
174
    #   the absolute *value* of the document -- that is, it leaves the value
 
175
    #   and number of Text nodes in the document unchanged.
 
176
    # ie_hack::
 
177
    #   Internet Explorer is the worst piece of crap to have ever been
 
178
    #   written, with the possible exception of Windows itself.  Since IE is
 
179
    #   unable to parse proper XML, we have to provide a hack to generate XML
 
180
    #   that IE's limited abilities can handle.  This hack inserts a space 
 
181
    #   before the /> on empty tags.  Defaults to false
 
182
    def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
211
183
      if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
212
184
        output = Output.new( output, xml_decl.encoding )
213
185
      end
214
186
      formatter = if indent > -1
215
 
          if transitive
 
187
          if trans
216
188
            REXML::Formatters::Transitive.new( indent, ie_hack )
217
189
          else
218
190
            REXML::Formatters::Pretty.new( indent, ie_hack )