Module: Shale::Adapter::REXML

Defined in:
lib/shale/adapter/rexml.rb,
lib/shale/adapter/rexml/node.rb,
lib/shale/adapter/rexml/document.rb

Overview

REXML adapter

Defined Under Namespace

Classes: Document, Node

Class Method Summary collapse

Class Method Details

.create_document(_version = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create Shale::Adapter::REXML::Document instance



75
76
77
# File 'lib/shale/adapter/rexml.rb', line 75

def self.create_document(_version = nil)
  Document.new
end

.dump(doc, pretty: false, declaration: false, encoding: false) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Serialize REXML document into XML

Parameters:

  • doc (::REXML::Document)

    REXML document

  • pretty (true, false) (defaults to: false)
  • declaration (true, false, String) (defaults to: false)
  • encoding (true, false, String) (defaults to: false)

Returns:

  • (String)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shale/adapter/rexml.rb', line 41

def self.dump(doc, pretty: false, declaration: false, encoding: false)
  if declaration
    ver = nil
    enc = nil

    if declaration.is_a?(String)
      ver = declaration
    end

    if encoding == true
      enc = 'UTF-8'
    else
      enc = encoding || nil
    end

    doc.add(::REXML::XMLDecl.new(ver, enc))
  end

  io = StringIO.new

  if pretty
    formatter = ::REXML::Formatters::Pretty.new
    formatter.compact = true
  else
    formatter = ::REXML::Formatters::Default.new
  end

  formatter.write(doc, io)
  io.string
end

.load(xml) ⇒ Shale::Adapter::REXML::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse XML into REXML document

Parameters:

  • xml (String)

    XML document

Returns:

Raises:



24
25
26
27
28
29
# File 'lib/shale/adapter/rexml.rb', line 24

def self.load(xml)
  doc = ::REXML::Document.new(xml, ignore_whitespace_nodes: :all)
  Node.new(doc.root)
rescue ::REXML::ParseException => e
  raise ParseError, "Document is invalid: #{e.message}"
end