Class: XML::Smart::Dom
- Inherits:
-
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) ⇒ Dom
Returns a new instance of Dom.
4
5
6
7
|
# File 'lib/xml/smart_dom.rb', line 4
def initialize(dom)
@dom = dom
@save_unformated = false
end
|
Class Method Details
.smart_helper(node) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/xml/smart_dom.rb', line 71
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
9
|
# File 'lib/xml/smart_dom.rb', line 9
def ===(cls); self.is_a? cls; end
|
#find(path) ⇒ Object
18
19
20
|
# File 'lib/xml/smart_dom.rb', line 18
def find(path)
Dom::smart_helper(@dom.xpath_fast(path))
end
|
#namespaces ⇒ Object
26
27
28
|
# File 'lib/xml/smart_dom.rb', line 26
def namespaces
@dom.custom_namespace_prefixes.merge @dom.user_custom_namespace_prefixes
end
|
#register_namespace(a, b) ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'lib/xml/smart_dom.rb', line 29
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
|
#root ⇒ Object
11
12
13
|
# File 'lib/xml/smart_dom.rb', line 11
def root
Element.new(@dom.root)
end
|
#root=(nr) ⇒ Object
14
15
16
|
# File 'lib/xml/smart_dom.rb', line 14
def root=(nr)
@dom.root.replace(nr.instance_variable_get(:@element)) if nr.instance_of? Element
end
|
#save_as(name) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/xml/smart_dom.rb', line 48
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) ? ::Kernel::open(name,'w') : name
rescue
raise Error, "could not open #{name}"
end
ftext = if @save_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
io.write ftext
io.close unless name == io
end
|
64
|
# File 'lib/xml/smart_dom.rb', line 64
def save_unformated=(val); @save_unformated = (val.is_a?(TrueClass) ? true : false); end
|
65
|
# File 'lib/xml/smart_dom.rb', line 65
def save_unformated?; @save_unformated; end
|
#to_s ⇒ Object
22
23
24
|
# File 'lib/xml/smart_dom.rb', line 22
def to_s
@dom.to_s
end
|
108
109
110
111
112
113
114
115
116
|
# File 'lib/xml/smart_dom.rb', line 108
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
|
#unregister_namespace(a) ⇒ Object
38
39
40
41
42
43
44
45
46
|
# File 'lib/xml/smart_dom.rb', line 38
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
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/xml/smart_dom.rb', line 95
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
67
68
69
|
# File 'lib/xml/smart_dom.rb', line 67
def xinclude!(basedir=nil)
Element.new(@dom.root).xinclude!(basedir)
end
|