Class: XmlWriteStream::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/xml-write-stream/base.rb

Direct Known Subclasses

StatefulWriter, YieldingWriter

Constant Summary collapse

DEFAULT_INDENT =
4
TAG_NAME_REGEX =

these are probably fairly incorrect, but good enough for now

/\A[a-zA-Z:_][\w.\-_:]*/
ATTRIBUTE_KEY_REGEX =
/\A[a-zA-Z_][\w\-_]*/
TEXT_ESCAPE_CHARS =
/["'<>&]/
ATTRIBUTE_ESCAPE_CHARS =
/["'<>&\n\r\t]/
TEXT_ESCAPE_HASH =
{
  '"' => '&quot;',
  "'" => '&apos;',
  '<' => '&lt;',
  '>' => '&gt;',
  '&' => '&amp;'
}
ATTRIBUTE_ESCAPE_HASH =
TEXT_ESCAPE_HASH.merge({
  "\n" => '&#xA;',
  "\r" => '&#xD;',
  "\t" => '&#x9;'
})
DEFAULT_HEADER_ATTRIBUTES =
{
  version: '1.0',
  encoding: 'utf-8'
}

Instance Method Summary collapse

Instance Method Details

#write_header(attributes = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/xml-write-stream/base.rb', line 41

def write_header(attributes = {})
  stream.write('<?xml ')

  write_attributes(
    DEFAULT_HEADER_ATTRIBUTES.merge(attributes)
  )

  stream.write('?>')
  write_newline
end

#write_text(text, options = {}) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/xml-write-stream/base.rb', line 33

def write_text(text, options = {})
  escape = options.fetch(:escape, true)

  stream.write(
    escape ? escape_text(text) : text
  )
end