Class: XML::Smart::Dom

Inherits:
Object
  • Object
show all
Defined in:
lib/xml/smart_dom.rb,
lib/xml/smart_domtext.rb,
lib/xml/smart_domother.rb,
lib/xml/smart_domelement.rb,
lib/xml/smart_domnodeset.rb,
lib/xml/smart_domattribute.rb,
lib/xml/smart_domnamespace.rb,
lib/xml/smart_domattributeset.rb,
lib/xml/smart_domnamespaceset.rb

Defined Under Namespace

Classes: Attribute, AttributeSet, Element, Namespace, NamespaceSet, NodeSet, Other, Text

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dom, basepath = nil) ⇒ Dom

Returns a new instance of Dom.



4
5
6
7
8
# File 'lib/xml/smart_dom.rb', line 4

def initialize(dom,basepath=nil)
  @dom = dom
  @dom.basepath = basepath
  @unformated = false
end

Class Method Details

.smart_helper(node) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/xml/smart_dom.rb', line 75

def self::smart_helper(node)
  if node.instance_of? Nokogiri::XML::Element
    XML::Smart::Dom::Element.new(node)
  elsif node.instance_of? Nokogiri::XML::Attr
    XML::Smart::Dom::Attribute.new(node)
  elsif node.instance_of? Nokogiri::XML::NodeSet
    XML::Smart::Dom::NodeSet.new(node)
  elsif node.instance_of?(String) || node.instance_of?(TrueClass) || node.instance_of?(FalseClass) || node.instance_of?(Float)
    node
  elsif node.instance_of? Nokogiri::XML::Text
    XML::Smart::Dom::Text.new(node)
  elsif node.instance_of? Nokogiri::XML::Namespace
    XML::Smart::Dom::Namespace.new(node)
  elsif node.instance_of? Nokogiri::XML::Document
    XML::Smart::Dom.new(node)
  elsif node.instance_of? Nokogiri::XML::ProcessingInstruction
    XML::Smart::ProcessingInstruction.new(node)
  elsif node.nil?
    nil
  else
    XML::Smart::Dom::Other.new(node)
  end
end

Instance Method Details

#===(cls) ⇒ Object



10
# File 'lib/xml/smart_dom.rb', line 10

def ===(cls); self.is_a? cls; end

#find(path) ⇒ Object



19
20
21
# File 'lib/xml/smart_dom.rb', line 19

def find(path)
  Dom::smart_helper(@dom.xpath_fast(path))
end

#namespacesObject



27
28
29
# File 'lib/xml/smart_dom.rb', line 27

def namespaces
  @dom.custom_namespace_prefixes.merge @dom.user_custom_namespace_prefixes
end

#register_namespace(a, b) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/xml/smart_dom.rb', line 30

def register_namespace(a,b)
  if a.respond_to?(:to_s) && b.respond_to?(:to_s) && !@dom.custom_namespace_prefixes.key?(a.to_s) && @dom.custom_namespace_prefixes.value?(b.to_s)
    @dom.user_custom_namespace_prefixes[a.to_s] = b.to_s
    @dom.ns_update
    true
  else
    false
  end
end

#rootObject



12
13
14
# File 'lib/xml/smart_dom.rb', line 12

def root
  Element.new(@dom.root)
end

#root=(nr) ⇒ Object



15
16
17
# File 'lib/xml/smart_dom.rb', line 15

def root=(nr)
  @dom.root.replace(nr.instance_variable_get(:@node)) if nr.instance_of? Element
end

#save_as(name) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
# File 'lib/xml/smart_dom.rb', line 49

def save_as(name)
  raise Error, 'first parameter has to be a filename or filehandle' unless name.is_a?(String) || name.is_a?(IO) || name.is_a?(Tempfile)
  begin
    io = name.is_a?(String) ? ::URI::open(name,'w') : name
  rescue
    raise Error, "could not open #{name}"
  end
  io.write serialize
  io.close unless name == io
end

#serializeObject



60
61
62
63
64
65
66
# File 'lib/xml/smart_dom.rb', line 60

def serialize
  if @unformated
    @dom.root.serialize(:encoding => 'UTF-8', :save_with => Nokogiri::XML::Node::SaveOptions::NO_DECLARATION | Nokogiri::XML::Node::SaveOptions::AS_XML)
  else
    @dom.root.serialize(:encoding => 'UTF-8', :save_with => Nokogiri::XML::Node::SaveOptions::FORMAT | Nokogiri::XML::Node::SaveOptions::AS_XML)
  end
end

#to_sObject



23
24
25
# File 'lib/xml/smart_dom.rb', line 23

def to_s
  @dom.to_s
end

#transform_with(doc, params = nil) ⇒ Object

Raises:



112
113
114
115
116
117
118
119
120
# File 'lib/xml/smart_dom.rb', line 112

def transform_with(doc,params=nil)
  raise Error, 'first parameter has to XML::Smart::Dom document' unless doc.instance_of? Dom
  res = Nokogiri::XSLT::Stylesheet.parse_stylesheet_doc(doc.instance_variable_get(:@dom)).transform(@dom,params)
  if res.children.length != 0 && res.children.first.class == Nokogiri::XML::Text
    Text.new(res.children.first).text
  else
    Dom::smart_helper(res)
  end
end

#unformated=(val) ⇒ Object



68
# File 'lib/xml/smart_dom.rb', line 68

def unformated=(val); @unformated = (val.is_a?(TrueClass) ? true : false); end

#unformated?Boolean

Returns:

  • (Boolean)


69
# File 'lib/xml/smart_dom.rb', line 69

def unformated?; @unformated; end

#unregister_namespace(a) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/xml/smart_dom.rb', line 39

def unregister_namespace(a)
  if a.respond_to?(:to_s) && @dom.user_custom_namespace_prefixes.key?(a.to_s)
    @dom.user_custom_namespace_prefixes.delete(a.to_s)
    @dom.ns_update
    true
  else
    false
  end
end

#validate_against(doc, &errbl) ⇒ Object

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/xml/smart_dom.rb', line 99

def validate_against(doc,&errbl)
  raise Error, 'first parameter has to XML::Smart::Dom document' unless doc.instance_of? Dom
  res = if doc.root.namespaces.has_ns?("http://relaxng.org/ns/structure/1.0")
    Nokogiri::XML::RelaxNG.from_document(doc.instance_variable_get(:@dom)).validate(@dom)
  elsif doc.root.namespaces.has_ns?("http://www.w3.org/2001/XMLSchema")
    tdoc = Nokogiri::XSLT.parse(File.read(File.expand_path(File.dirname(__FILE__) + '/XSDtoRNG.xsl')))
    rdoc = tdoc.transform(doc.instance_variable_get(:@dom))
    Nokogiri::XML::RelaxNG.from_document(rdoc).validate(@dom)
  end
  res.each { |err| errbl.call err } if block_given?
  res.empty?
end

#xinclude!(basedir = nil) ⇒ Object



71
72
73
# File 'lib/xml/smart_dom.rb', line 71

def xinclude!(basedir=nil)
  Element.new(@dom.root).xinclude!(basedir)
end