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, 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
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
|
#namespaces ⇒ Object
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
|
#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
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
|
#serialize ⇒ Object
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_s ⇒ Object
23
24
25
|
# File 'lib/xml/smart_dom.rb', line 23
def to_s
@dom.to_s
end
|
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
|
68
|
# File 'lib/xml/smart_dom.rb', line 68
def unformated=(val); @unformated = (val.is_a?(TrueClass) ? true : false); end
|
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
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
|