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
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)
Element.new(@dom.root).find(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=(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)
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
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
|
106
107
108
109
110
111
112
113
114
|
# File 'lib/xml/smart_dom.rb', line 106
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
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/xml/smart_dom.rb', line 93
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
|