Fix up Rubinius specific library specs.
[rbx.git] / lib / rexml / xmldecl.rb
blob427eb78cf8e8d2a7e1645a1b479a3058fa52c487
1 require 'rexml/encoding'
2 require 'rexml/source'
4 module REXML
5         # NEEDS DOCUMENTATION
6         class XMLDecl < Child
7                 include Encoding
9                 DEFAULT_VERSION = "1.0";
10                 DEFAULT_ENCODING = "UTF-8";
11                 DEFAULT_STANDALONE = "no";
12                 START = '<\?xml';
13                 STOP = '\?>';
15                 attr_accessor :version, :standalone
16     attr_reader :writeencoding, :writethis
18                 def initialize(version=DEFAULT_VERSION, encoding=nil, standalone=nil)
19       @writethis = true
20       @writeencoding = !encoding.nil?
21                         if version.kind_of? XMLDecl
22                                 super()
23                                 @version = version.version
24                                 self.encoding = version.encoding
25         @writeencoding = version.writeencoding
26                                 @standalone = version.standalone
27                         else
28                                 super()
29                                 @version = version
30                                 self.encoding = encoding
31                                 @standalone = standalone
32                         end
33                         @version = DEFAULT_VERSION if @version.nil?
34                 end
36                 def clone
37                         XMLDecl.new(self)
38                 end
40     # indent::
41     #   Ignored.  There must be no whitespace before an XML declaration
42     # transitive::
43     #   Ignored
44     # ie_hack::
45     #   Ignored
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}"
51       else
52         writer << " #{content encoding}"
53       end
54                         writer << STOP.sub(/\\/u, '')
55                 end
57                 def ==( other )
58                   other.kind_of?(XMLDecl) and
59                   other.version == @version and
60                   other.encoding == self.encoding and
61                   other.standalone == @standalone
62                 end
64                 def xmldecl version, encoding, standalone
65                         @version = version
66                         self.encoding = encoding
67                         @standalone = standalone
68                 end
70                 def node_type
71                         :xmldecl
72                 end
74                 alias :stand_alone? :standalone
75     alias :old_enc= :encoding=
77     def encoding=( enc )
78       if enc.nil?
79         self.old_enc = "UTF-8"
80         @writeencoding = false
81       else
82         self.old_enc = enc
83         @writeencoding = true
84       end
85       self.dowrite
86     end
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.
91     #
92     # Note that XML 1.1 documents *must* include an XML declaration
93     def XMLDecl.default
94       rv = XMLDecl.new( "1.0" )
95       rv.nowrite
96       rv
97     end
99     def nowrite
100       @writethis = false
101     end
103     def dowrite
104       @writethis = true
105     end
107     def inspect
108       START.sub(/\\/u, '') + " ... " + STOP.sub(/\\/u, '')
109     end
111                 private
112                 def content(enc)
113                         rv = "version='#@version'"
114                         rv << " encoding='#{enc}'" if @writeencoding || enc !~ /utf-8/i
115                         rv << " standalone='#@standalone'" if @standalone
116                         rv
117                 end
118         end