Class | REXML::Formatters::Default |
In: |
lib/rexml/formatters/default.rb
|
Parent: | Object |
Prints out the XML document with no formatting — except if id_hack is set.
ie_hack: | If set to true, then inserts whitespace before the close of an empty tag, so that IE‘s bad XML parser doesn‘t choke. |
# File lib/rexml/formatters/default.rb, line 10 10: def initialize( ie_hack=false ) 11: @ie_hack = ie_hack 12: end
Writes the node to some output.
node: | The node to write |
output: | A class implementing <<. Pass in an Output object to change the output encoding. |
# File lib/rexml/formatters/default.rb, line 21 21: def write( node, output ) 22: case node 23: 24: when Document 25: if node.xml_decl.encoding != "UTF-8" && !output.kind_of?(Output) 26: output = Output.new( output, node.xml_decl.encoding ) 27: end 28: write_document( node, output ) 29: 30: when Element 31: write_element( node, output ) 32: 33: when Declaration, ElementDecl, NotationDecl, ExternalEntity, Entity, 34: Attribute, AttlistDecl 35: node.write( output,-1 ) 36: 37: when Instruction 38: write_instruction( node, output ) 39: 40: when DocType, XMLDecl 41: node.write( output ) 42: 43: when Comment 44: write_comment( node, output ) 45: 46: when CData 47: write_cdata( node, output ) 48: 49: when Text 50: write_text( node, output ) 51: 52: else 53: raise Exception.new("XML FORMATTING ERROR") 54: 55: end 56: end
# File lib/rexml/formatters/default.rb, line 94 94: def write_cdata( node, output ) 95: output << CData::START 96: output << node.to_s 97: output << CData::STOP 98: end
# File lib/rexml/formatters/default.rb, line 88 88: def write_comment( node, output ) 89: output << Comment::START 90: output << node.to_s 91: output << Comment::STOP 92: end
# File lib/rexml/formatters/default.rb, line 59 59: def write_document( node, output ) 60: node.children.each { |child| write( child, output ) } 61: end
# File lib/rexml/formatters/default.rb, line 63 63: def write_element( node, output ) 64: output << "<#{node.expanded_name}" 65: 66: node.attributes.each_attribute do |attr| 67: output << " " 68: attr.write( output ) 69: end unless node.attributes.empty? 70: 71: if node.children.empty? 72: output << " " if @ie_hack 73: output << "/" 74: else 75: output << ">" 76: node.children.each { |child| 77: write( child, output ) 78: } 79: output << "</#{node.expanded_name}" 80: end 81: output << ">" 82: end
# File lib/rexml/formatters/default.rb, line 100 100: def write_instruction( node, output ) 101: output << Instruction::START.sub(/\\/u, '') 102: output << node.target 103: output << ' ' 104: output << node.content 105: output << Instruction::STOP.sub(/\\/u, '') 106: end