1 require 'rexml/encoding'
9 DEFAULT_VERSION = "1.0";
10 DEFAULT_ENCODING = "UTF-8";
11 DEFAULT_STANDALONE = "no";
15 attr_accessor :version, :standalone
16 attr_reader :writeencoding, :writethis
18 def initialize(version=DEFAULT_VERSION, encoding=nil, standalone=nil)
20 @writeencoding = !encoding.nil?
21 if version.kind_of? XMLDecl
23 @version = version.version
24 self.encoding = version.encoding
25 @writeencoding = version.writeencoding
26 @standalone = version.standalone
30 self.encoding = encoding
31 @standalone = standalone
33 @version = DEFAULT_VERSION if @version.nil?
41 # Ignored. There must be no whitespace before an XML declaration
46 def write(writer, indent=-1, transitive=false, ie_hack=false)
47 return nil unless @writethis or writer.kind_of? Output
48 writer << START.sub(/\\/u, '')
49 if writer.kind_of? Output
50 writer << " #{content writer.encoding}"
52 writer << " #{content encoding}"
54 writer << STOP.sub(/\\/u, '')
58 other.kind_of?(XMLDecl) and
59 other.version == @version and
60 other.encoding == self.encoding and
61 other.standalone == @standalone
64 def xmldecl version, encoding, standalone
66 self.encoding = encoding
67 @standalone = standalone
74 alias :stand_alone? :standalone
75 alias :old_enc= :encoding=
79 self.old_enc = "UTF-8"
80 @writeencoding = false
88 # Only use this if you do not want the XML declaration to be written;
89 # this object is ignored by the XML writer. Otherwise, instantiate your
90 # own XMLDecl and add it to the document.
92 # Note that XML 1.1 documents *must* include an XML declaration
94 rv = XMLDecl.new( "1.0" )
108 START.sub(/\\/u, '') + " ... " + STOP.sub(/\\/u, '')
113 rv = "version='#@version'"
114 rv << " encoding='#{enc}'" if @writeencoding || enc !~ /utf-8/i
115 rv << " standalone='#@standalone'" if @standalone