flowplayer flash flv no longer work, move to mp4
[booh] / lib / booh / rexml / xmldecl.rb
1 require 'booh/rexml/encoding'
2 require 'booh/rexml/source'
3
4 module REXML
5         # NEEDS DOCUMENTATION
6         class XMLDecl < Child
7                 include Encoding
8
9                 DEFAULT_VERSION = "1.0";
10                 DEFAULT_ENCODING = "UTF-8";
11                 DEFAULT_STANDALONE = "no";
12                 START = '<\?xml';
13                 STOP = '\?>';
14
15                 attr_accessor :version, :standalone
16     attr_reader :writeencoding
17
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
35
36                 def clone
37                         XMLDecl.new(self)
38                 end
39
40                 def write writer, indent=-1, transitive=false, ie_hack=false
41       return nil unless @writethis or writer.kind_of? Output
42                         indent( writer, indent )
43                         writer << START.sub(/\\/u, '')
44       if writer.kind_of? Output
45         writer << " #{content writer.encoding}"
46       else
47         writer << " #{content encoding}"
48       end
49                         writer << STOP.sub(/\\/u, '')
50                 end
51
52                 def ==( other )
53                   other.kind_of?(XMLDecl) and
54                   other.version == @version and
55                   other.encoding == self.encoding and
56                   other.standalone == @standalone
57                 end
58
59                 def xmldecl version, encoding, standalone
60                         @version = version
61                         self.encoding = encoding
62                         @standalone = standalone
63                 end
64
65                 def node_type
66                         :xmldecl
67                 end
68
69                 alias :stand_alone? :standalone
70     alias :old_enc= :encoding=
71
72     def encoding=( enc )
73       if enc.nil?
74         self.old_enc = "UTF-8"
75         @writeencoding = false
76       else
77         self.old_enc = enc
78         @writeencoding = true
79       end
80       self.dowrite
81     end
82
83     # Only use this if you do not want the XML declaration to be written;
84     # this object is ignored by the XML writer.  Otherwise, instantiate your
85     # own XMLDecl and add it to the document.
86     #
87     # Note that XML 1.1 documents *must* include an XML declaration
88     def XMLDecl.default
89       rv = XMLDecl.new( "1.0" )
90       rv.nowrite
91       rv
92     end
93
94     def nowrite
95       @writethis = false
96     end
97
98     def dowrite
99       @writethis = true
100     end
101
102     def inspect
103       START.sub(/\\/u, '') + " ... " + STOP.sub(/\\/u, '')
104     end
105
106                 private
107                 def content(enc)
108                         rv = "version='#@version'"
109                         rv << " encoding='#{enc}'" if @writeencoding || enc !~ /utf-8/i
110                         rv << " standalone='#@standalone'" if @standalone
111                         rv
112                 end
113         end
114 end