|
7 | 7 | require 'rexml/xmltokens'
|
8 | 8 |
|
9 | 9 | module REXML
|
| 10 | + class ReferenceWriter |
| 11 | + def initialize(id_type, |
| 12 | + public_id_literal, |
| 13 | + system_literal) |
| 14 | + @id_type = id_type |
| 15 | + @public_id_literal = public_id_literal |
| 16 | + @system_literal = system_literal |
| 17 | + @default_quote = "\"" |
| 18 | + end |
| 19 | + |
| 20 | + def write(output) |
| 21 | + output << " #{@id_type}" |
| 22 | + if @public_id_literal |
| 23 | + if @public_id_literal.include?("'") |
| 24 | + quote = "\"" |
| 25 | + else |
| 26 | + quote = @default_quote |
| 27 | + end |
| 28 | + output << " #{quote}#{@public_id_literal}#{quote}" |
| 29 | + end |
| 30 | + if @system_literal |
| 31 | + if @system_literal.include?("'") |
| 32 | + quote = "\"" |
| 33 | + elsif @system_literal.include?("\"") |
| 34 | + quote = "'" |
| 35 | + else |
| 36 | + quote = @default_quote |
| 37 | + end |
| 38 | + output << " #{quote}#{@system_literal}#{quote}" |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + |
10 | 43 | # Represents an XML DOCTYPE declaration; that is, the contents of <!DOCTYPE
|
11 | 44 | # ... >. DOCTYPES can be used to declare the DTD of a document, as well as
|
12 | 45 | # being used to declare entities used in the document.
|
@@ -50,6 +83,8 @@ def initialize( first, parent=nil )
|
50 | 83 | super( parent )
|
51 | 84 | @name = first.name
|
52 | 85 | @external_id = first.external_id
|
| 86 | + @long_name = first.instance_variable_get(:@long_name) |
| 87 | + @uri = first.instance_variable_get(:@uri) |
53 | 88 | elsif first.kind_of? Array
|
54 | 89 | super( parent )
|
55 | 90 | @name = first[0]
|
@@ -112,9 +147,12 @@ def write( output, indent=0, transitive=false, ie_hack=false )
|
112 | 147 | output << START
|
113 | 148 | output << ' '
|
114 | 149 | output << @name
|
115 |
| - output << " #@external_id" if @external_id |
116 |
| - output << " #{@long_name.inspect}" if @long_name |
117 |
| - output << " #{@uri.inspect}" if @uri |
| 150 | + if @external_id |
| 151 | + reference_writer = ReferenceWriter.new(@external_id, |
| 152 | + @long_name, |
| 153 | + @uri) |
| 154 | + reference_writer.write(output) |
| 155 | + end |
118 | 156 | unless @children.empty?
|
119 | 157 | output << ' ['
|
120 | 158 | @children.each { |child|
|
@@ -249,9 +287,9 @@ def initialize name, middle, pub, sys
|
249 | 287 | end
|
250 | 288 |
|
251 | 289 | def to_s
|
252 |
| - notation = "<!NOTATION #{@name} #{@middle}" |
253 |
| - notation << " #{@public.inspect}" if @public |
254 |
| - notation << " #{@system.inspect}" if @system |
| 290 | + notation = "<!NOTATION #{@name}" |
| 291 | + reference_writer = ReferenceWriter.new(@middle, @public, @system) |
| 292 | + reference_writer.write(notation) |
255 | 293 | notation << ">"
|
256 | 294 | notation
|
257 | 295 | end
|
|
0 commit comments