class NXML

Andrej Gelenberg <andrej.gelenberg@udo.edu> © 2019

Constants

ATTR_ESCAPE
ATTR_ESCAPE_HASH
NAME_CHAR
NAME_RE
NAME_START
TEXT_ESCAPE
TEXT_ESCAPE_HASH

Attributes

encoding[RW]
root[RW]
standalone[RW]
version[RW]

Public Class Methods

check_name(name) click to toggle source
# File lib/nxml.rb, line 24
def self.check_name(name)
  if not name.match? NAME_RE
    raise ArgumentError.new "invalid XML name \"#{name}\""
  end
end
escape_attr_raw(value) click to toggle source
# File lib/nxml.rb, line 34
def self.escape_attr_raw(value)
  value.gsub /["]/, '&quot;'
end
escape_attr_value(value) click to toggle source
# File lib/nxml.rb, line 30
def self.escape_attr_value(value)
  value.gsub ATTR_ESCAPE, ATTR_ESCAPE_HASH
end
escape_text(value) click to toggle source
# File lib/nxml.rb, line 38
def self.escape_text(value)
  value.gsub TEXT_ESCAPE, TEXT_ESCAPE_HASH
end
escape_text_raw(value) click to toggle source
# File lib/nxml.rb, line 42
def self.escape_text_raw(value)
  value.gsub /[<]/, '&lt;'
end
new(&block) click to toggle source
# File lib/nxml.rb, line 157
def initialize(&block)
  version = "1.0"
  @encoding = Encoding::UTF_8

  instance_eval &block
end

Public Instance Methods

to_str() click to toggle source
# File lib/nxml.rb, line 168
def to_str
  ret = "<?xml"

  if @root.nil?
  end

  if not @version.nil?
    ret += " version=\"#{@version}\""
  end

  if not @encoding.nil?
    ret += " encoding=\"#{@encoding.name}\""
  end

  if not @standalone.nil?
    ret += " standalone=\"#{@standalone ? "yes" : "no"}\""
  end

  ret += "?>\n"

  ret += @root

  ret.encode @encoding
end