rhodes 2.0.0.beta1 → 2.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/LICENSE +19 -674
- data/Manifest.txt +21 -4
- data/README.textile +5 -36
- data/Rakefile +5 -0
- data/lib/build/jake.rb +3 -1
- data/lib/extensions/rexml/rexml/document.rb +2 -0
- data/lib/extensions/rexml/rexml/parsers/baseparser.rb +0 -6
- data/lib/extensions/rhoxml/rexml/child.rb +96 -0
- data/lib/extensions/rhoxml/rexml/document.rb +527 -0
- data/lib/extensions/rhoxml/rexml/element.rb +987 -0
- data/lib/extensions/rhoxml/rexml/encoding.rb +71 -0
- data/lib/extensions/rhoxml/rexml/encodings/US-ASCII.rb +30 -0
- data/lib/extensions/rhoxml/rexml/encodings/UTF-16.rb +35 -0
- data/lib/extensions/rhoxml/rexml/encodings/UTF-8.rb +18 -0
- data/lib/extensions/rhoxml/rexml/namespace.rb +47 -0
- data/lib/extensions/rhoxml/rexml/node.rb +76 -0
- data/lib/extensions/rhoxml/rexml/parent.rb +166 -0
- data/lib/extensions/rhoxml/rexml/parseexception.rb +51 -0
- data/lib/extensions/rhoxml/rexml/parsers/baseparser.rb +531 -0
- data/lib/extensions/rhoxml/rexml/parsers/treeparser.rb +100 -0
- data/lib/extensions/rhoxml/rexml/parsers/xpathparser.rb +698 -0
- data/lib/extensions/rhoxml/rexml/set.rb +1274 -0
- data/lib/extensions/rhoxml/rexml/source.rb +258 -0
- data/lib/extensions/rhoxml/rexml/xmltokens.rb +18 -0
- data/lib/extensions/rhoxml/rexml/xpath.rb +77 -0
- data/lib/extensions/rhoxml/rexml/xpath_parser.rb +806 -0
- data/lib/framework/builtinME.rb +2 -0
- data/lib/framework/dateME.rb +5 -1
- data/lib/framework/rho/render.rb +10 -2
- data/lib/framework/rhom/rhom_object_factory.rb +2 -1
- data/lib/framework/singleton.rb +1 -1
- data/platform/android/Rhodes/jni/src/rhodes.cpp +2 -4
- data/platform/android/Rhodes/src/com/rhomobile/rhodes/NativeBar.java +23 -18
- data/platform/android/Rhodes/src/com/rhomobile/rhodes/Rhodes.java +42 -69
- data/platform/android/Rhodes/src/com/rhomobile/rhodes/SplashScreen.java +59 -7
- data/platform/android/Rhodes/src/com/rhomobile/rhodes/camera/Camera.java +1 -1
- data/platform/android/Rhodes/src/com/rhomobile/rhodes/mapview/Annotation.java +1 -0
- data/platform/android/Rhodes/src/com/rhomobile/rhodes/mapview/MapView.java +97 -85
- data/platform/android/Rhodes/src/com/rhomobile/rhodes/uri/SmsUriHandler.java +52 -0
- data/platform/android/build/RhodesSRC_build.files +1 -0
- data/platform/android/build/android.rake +38 -14
- data/platform/bb/RubyVM/RubyVM.jdp +1 -0
- data/platform/bb/build/RubyVM_build.files +1 -0
- data/platform/bb/build/bb.rake +44 -2
- data/platform/bb/rhodes/platform/5.0/com/rho/BrowserAdapter5.java +1 -1
- data/platform/bb/rhodes/rhodes.jdp +4 -4
- data/platform/bb/rhodes/src/com/rho/BrowserAdapter.java +8 -4
- data/platform/bb/rhodes/src/com/rho/rubyext/Alert.java +149 -17
- data/platform/bb/rhodes/src/rhomobile/PushListeningThread.java +20 -17
- data/platform/bb/rhodes/src/rhomobile/RhodesApplication.java +54 -28
- data/platform/bb/rhodes/src/rhomobile/mapview/Annotation.java +1 -0
- data/platform/bb/rhodes/src/rhomobile/mapview/GoogleMapField.java +37 -11
- data/platform/bb/rhodes/src/rhomobile/mapview/MapView.java +49 -19
- data/platform/bb/rhodes/src/rhomobile/mapview/MapViewScreen.java +6 -0
- data/platform/iphone/Classes/MapView/GoogleGeocoder.h +6 -7
- data/platform/iphone/Classes/MapView/GoogleGeocoder.m +70 -70
- data/platform/iphone/Classes/MapView/MapAnnotation.h +5 -3
- data/platform/iphone/Classes/MapView/MapAnnotation.m +10 -8
- data/platform/iphone/Classes/MapView/MapViewController.h +13 -10
- data/platform/iphone/Classes/MapView/MapViewController.m +131 -72
- data/platform/iphone/Classes/Rhodes.h +2 -0
- data/platform/iphone/Classes/Rhodes.m +13 -1
- data/platform/iphone/Classes/SimpleMainView.m +0 -1
- data/platform/iphone/Classes/TabbedMainView.m +5 -6
- data/platform/shared/common/RhoTime.h +2 -2
- data/platform/shared/common/RhodesApp.cpp +1 -1
- data/platform/shared/db/DBAdapter.cpp +6 -0
- data/platform/shared/net/CURLNetRequest.cpp +23 -1
- data/platform/shared/ruby/thread_win32.c +9 -1
- data/platform/shared/rubyJVM/src/com/rho/Capabilities.java +6 -0
- data/platform/shared/rubyJVM/src/com/rho/RhodesApp.java +1 -1
- data/platform/shared/rubyJVM/src/com/rho/sync/ClientRegister.java +1 -1
- data/platform/shared/rubyJVM/src/com/xruby/GeneratedMethods/RubySymbol_Methods.java +6 -1
- data/platform/shared/rubyJVM/src/com/xruby/GeneratedMethods/RubyTime_Methods.java +3 -3
- data/platform/shared/rubyJVM/src/com/xruby/runtime/builtin/InputStreamExecutor.java +1 -1
- data/platform/shared/rubyJVM/src/com/xruby/runtime/builtin/RubyArray.java +15 -3
- data/platform/shared/rubyJVM/src/com/xruby/runtime/builtin/RubyString.java +10 -2
- data/platform/shared/rubyJVM/src/com/xruby/runtime/builtin/RubyTime.java +12 -1
- data/platform/shared/rubyJVM/src/com/xruby/runtime/lang/RubyKernelModule.java +18 -9
- data/platform/shared/rubyJVM/src/com/xruby/runtime/lang/RubySymbol.java +5 -0
- data/platform/shared/rubyJVM/src/org/json/me/JSONArray.java +2 -1
- data/platform/shared/unzip/unzip.cpp +1 -1
- data/platform/wm/build/wm.rake +27 -6
- data/platform/wm/rhodes/Alert.cpp +335 -5
- data/platform/wm/rhodes/Alert.h +84 -1
- data/platform/wm/rhodes/MainWindow.cpp +28 -6
- data/platform/wm/rhodes/MainWindow.h +7 -2
- data/platform/wm/rhodes/Rhodes.cpp +23 -0
- data/platform/wm/rhodes/rho/rubyext/SystemImpl.cpp +2 -1
- data/platform/wm/rhodes/stdafx.h +1 -0
- data/platform/wm/tools/detool/detool.cpp +405 -14
- data/rakefile.rb +5 -0
- data/res/build-tools/detool.exe +0 -0
- data/res/generators/rhogen.rb +2 -0
- data/rhodes.gemspec +1 -1
- data/spec/framework_spec/app/spec/fixtures/object_values.txt +1 -1
- data/spec/framework_spec/app/spec/pagination/fixtures/object_values.txt +1 -1
- data/spec/framework_spec/app/spec/rhom_object_spec.rb +12 -12
- metadata +23 -6
- data/LICENSING_OPTIONS +0 -1
- data/platform/bb/build/rhodesApp.rapc +0 -9
- data/res/generators/templates/source/source_adapter.rb +0 -48
- data/rhobuild.yml +0 -37
@@ -0,0 +1,987 @@
|
|
1
|
+
require "rexml/parent"
|
2
|
+
require "rexml/namespace"
|
3
|
+
#require "rexml/attribute"
|
4
|
+
#require "rexml/cdata"
|
5
|
+
require "rexml/xpath"
|
6
|
+
#require "rexml/parseexception"
|
7
|
+
|
8
|
+
module REXML
|
9
|
+
# An implementation note about namespaces:
|
10
|
+
# As we parse, when we find namespaces we put them in a hash and assign
|
11
|
+
# them a unique ID. We then convert the namespace prefix for the node
|
12
|
+
# to the unique ID. This makes namespace lookup much faster for the
|
13
|
+
# cost of extra memory use. We save the namespace prefix for the
|
14
|
+
# context node and convert it back when we write it.
|
15
|
+
@@namespaces = {}
|
16
|
+
|
17
|
+
# Represents a tagged XML element. Elements are characterized by
|
18
|
+
# having children, attributes, and names, and can themselves be
|
19
|
+
# children.
|
20
|
+
class Element < Parent
|
21
|
+
include Namespace
|
22
|
+
|
23
|
+
UNDEFINED = "UNDEFINED"; # The default name
|
24
|
+
|
25
|
+
# Mechanisms for accessing attributes and child elements of this
|
26
|
+
# element.
|
27
|
+
attr_reader :attributes, :elements
|
28
|
+
# The context holds information about the processing environment, such as
|
29
|
+
# whitespace handling.
|
30
|
+
attr_accessor :context
|
31
|
+
|
32
|
+
# Constructor
|
33
|
+
# arg::
|
34
|
+
# if not supplied, will be set to the default value.
|
35
|
+
# If a String, the name of this object will be set to the argument.
|
36
|
+
# If an Element, the object will be shallowly cloned; name,
|
37
|
+
# attributes, and namespaces will be copied. Children will +not+ be
|
38
|
+
# copied.
|
39
|
+
# parent::
|
40
|
+
# if supplied, must be a Parent, and will be used as
|
41
|
+
# the parent of this object.
|
42
|
+
# context::
|
43
|
+
# If supplied, must be a hash containing context items. Context items
|
44
|
+
# include:
|
45
|
+
# * <tt>:respect_whitespace</tt> the value of this is :+all+ or an array of
|
46
|
+
# strings being the names of the elements to respect
|
47
|
+
# whitespace for. Defaults to :+all+.
|
48
|
+
# * <tt>:compress_whitespace</tt> the value can be :+all+ or an array of
|
49
|
+
# strings being the names of the elements to ignore whitespace on.
|
50
|
+
# Overrides :+respect_whitespace+.
|
51
|
+
# * <tt>:ignore_whitespace_nodes</tt> the value can be :+all+ or an array
|
52
|
+
# of strings being the names of the elements in which to ignore
|
53
|
+
# whitespace-only nodes. If this is set, Text nodes which contain only
|
54
|
+
# whitespace will not be added to the document tree.
|
55
|
+
# * <tt>:raw</tt> can be :+all+, or an array of strings being the names of
|
56
|
+
# the elements to process in raw mode. In raw mode, special
|
57
|
+
# characters in text is not converted to or from entities.
|
58
|
+
def initialize( arg = UNDEFINED, parent=nil, context=nil )
|
59
|
+
super(parent)
|
60
|
+
|
61
|
+
@elements = Elements.new(self)
|
62
|
+
@attributes = Attributes.new(self)
|
63
|
+
@context = context
|
64
|
+
|
65
|
+
if arg.kind_of? String
|
66
|
+
self.name = arg
|
67
|
+
elsif arg.kind_of? Element
|
68
|
+
self.name = arg.expanded_name
|
69
|
+
arg.attributes.each_attribute{ |attribute|
|
70
|
+
@attributes << attribute #Attribute.new( attribute )
|
71
|
+
}
|
72
|
+
@context = arg.context
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def inspect
|
77
|
+
rv = "<#@expanded_name"
|
78
|
+
|
79
|
+
@attributes.each_attribute do |attr|
|
80
|
+
rv << " "
|
81
|
+
attr.write( rv, 0 )
|
82
|
+
end
|
83
|
+
|
84
|
+
if children.size > 0
|
85
|
+
rv << "> ... </>"
|
86
|
+
else
|
87
|
+
rv << "/>"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
# Creates a shallow copy of self.
|
93
|
+
# d = Document.new "<a><b/><b/><c><d/></c></a>"
|
94
|
+
# new_a = d.root.clone
|
95
|
+
# puts new_a # => "<a/>"
|
96
|
+
def clone
|
97
|
+
self.class.new self
|
98
|
+
end
|
99
|
+
|
100
|
+
# Evaluates to the root node of the document that this element
|
101
|
+
# belongs to. If this element doesn't belong to a document, but does
|
102
|
+
# belong to another Element, the parent's root will be returned, until the
|
103
|
+
# earliest ancestor is found.
|
104
|
+
#
|
105
|
+
# Note that this is not the same as the document element.
|
106
|
+
# In the following example, <a> is the document element, and the root
|
107
|
+
# node is the parent node of the document element. You may ask yourself
|
108
|
+
# why the root node is useful: consider the doctype and XML declaration,
|
109
|
+
# and any processing instructions before the document element... they
|
110
|
+
# are children of the root node, or siblings of the document element.
|
111
|
+
# The only time this isn't true is when an Element is created that is
|
112
|
+
# not part of any Document. In this case, the ancestor that has no
|
113
|
+
# parent acts as the root node.
|
114
|
+
# d = Document.new '<a><b><c/></b></a>'
|
115
|
+
# a = d[1] ; c = a[1][1]
|
116
|
+
# d.root_node == d # TRUE
|
117
|
+
# a.root_node # namely, d
|
118
|
+
# c.root_node # again, d
|
119
|
+
def root_node
|
120
|
+
parent.nil? ? self : parent.root_node
|
121
|
+
end
|
122
|
+
|
123
|
+
def root
|
124
|
+
return elements[1] if self.kind_of? Document
|
125
|
+
return self if parent.kind_of? Document or parent.nil?
|
126
|
+
return parent.root
|
127
|
+
end
|
128
|
+
|
129
|
+
# Evaluates to the document to which this element belongs, or nil if this
|
130
|
+
# element doesn't belong to a document.
|
131
|
+
def document
|
132
|
+
rt = root
|
133
|
+
rt.parent if rt
|
134
|
+
end
|
135
|
+
|
136
|
+
# Evaluates to +true+ if whitespace is respected for this element. This
|
137
|
+
# is the case if:
|
138
|
+
# 1. Neither :+respect_whitespace+ nor :+compress_whitespace+ has any value
|
139
|
+
# 2. The context has :+respect_whitespace+ set to :+all+ or
|
140
|
+
# an array containing the name of this element, and
|
141
|
+
# :+compress_whitespace+ isn't set to :+all+ or an array containing the
|
142
|
+
# name of this element.
|
143
|
+
# The evaluation is tested against +expanded_name+, and so is namespace
|
144
|
+
# sensitive.
|
145
|
+
def whitespace
|
146
|
+
@whitespace = nil
|
147
|
+
if @context
|
148
|
+
if @context[:respect_whitespace]
|
149
|
+
@whitespace = (@context[:respect_whitespace] == :all or
|
150
|
+
@context[:respect_whitespace].include? expanded_name)
|
151
|
+
end
|
152
|
+
@whitespace = false if (@context[:compress_whitespace] and
|
153
|
+
(@context[:compress_whitespace] == :all or
|
154
|
+
@context[:compress_whitespace].include?( expanded_name ))
|
155
|
+
)
|
156
|
+
end
|
157
|
+
@whitespace = true unless @whitespace == false
|
158
|
+
@whitespace
|
159
|
+
end
|
160
|
+
|
161
|
+
def ignore_whitespace_nodes
|
162
|
+
@ignore_whitespace_nodes = false
|
163
|
+
if @context
|
164
|
+
if @context[:ignore_whitespace_nodes]
|
165
|
+
@ignore_whitespace_nodes =
|
166
|
+
(@context[:ignore_whitespace_nodes] == :all or
|
167
|
+
@context[:ignore_whitespace_nodes].include? expanded_name)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# Evaluates to +true+ if raw mode is set for this element. This
|
173
|
+
# is the case if the context has :+raw+ set to :+all+ or
|
174
|
+
# an array containing the name of this element.
|
175
|
+
#
|
176
|
+
# The evaluation is tested against +expanded_name+, and so is namespace
|
177
|
+
# sensitive.
|
178
|
+
def raw
|
179
|
+
@raw = (@context and @context[:raw] and
|
180
|
+
(@context[:raw] == :all or
|
181
|
+
@context[:raw].include?( expanded_name )))
|
182
|
+
@raw
|
183
|
+
end
|
184
|
+
|
185
|
+
#once :whitespace, :raw, :ignore_whitespace_nodes
|
186
|
+
|
187
|
+
#################################################
|
188
|
+
# Namespaces #
|
189
|
+
#################################################
|
190
|
+
|
191
|
+
# Evaluates to an +Array+ containing the prefixes (names) of all defined
|
192
|
+
# namespaces at this context node.
|
193
|
+
# doc = Document.new("<a xmlns:x='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
|
194
|
+
# doc.elements['//b'].prefixes # -> ['x', 'y']
|
195
|
+
def prefixes
|
196
|
+
prefixes = []
|
197
|
+
prefixes = parent.prefixes if parent
|
198
|
+
prefixes |= attributes.prefixes
|
199
|
+
return prefixes
|
200
|
+
end
|
201
|
+
|
202
|
+
def namespaces
|
203
|
+
namespaces = {}
|
204
|
+
namespaces = parent.namespaces if parent
|
205
|
+
namespaces = namespaces.merge( attributes.namespaces )
|
206
|
+
return namespaces
|
207
|
+
end
|
208
|
+
|
209
|
+
# Evalutas to the URI for a prefix, or the empty string if no such
|
210
|
+
# namespace is declared for this element. Evaluates recursively for
|
211
|
+
# ancestors. Returns the default namespace, if there is one.
|
212
|
+
# prefix::
|
213
|
+
# the prefix to search for. If not supplied, returns the default
|
214
|
+
# namespace if one exists
|
215
|
+
# Returns::
|
216
|
+
# the namespace URI as a String, or nil if no such namespace
|
217
|
+
# exists. If the namespace is undefined, returns an empty string
|
218
|
+
# doc = Document.new("<a xmlns='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
|
219
|
+
# b = doc.elements['//b']
|
220
|
+
# b.namespace # -> '1'
|
221
|
+
# b.namespace("y") # -> '2'
|
222
|
+
def namespace(prefix=nil)
|
223
|
+
if prefix.nil?
|
224
|
+
prefix = prefix()
|
225
|
+
end
|
226
|
+
if prefix == ''
|
227
|
+
prefix = "xmlns"
|
228
|
+
else
|
229
|
+
prefix = "xmlns:#{prefix}" unless prefix[0,5] == 'xmlns'
|
230
|
+
end
|
231
|
+
ns = attributes[ prefix ]
|
232
|
+
ns = parent.namespace(prefix) if ns.nil? and parent
|
233
|
+
ns = '' if ns.nil? and prefix == 'xmlns'
|
234
|
+
return ns
|
235
|
+
end
|
236
|
+
|
237
|
+
# Adds a namespace to this element.
|
238
|
+
# prefix::
|
239
|
+
# the prefix string, or the namespace URI if +uri+ is not
|
240
|
+
# supplied
|
241
|
+
# uri::
|
242
|
+
# the namespace URI. May be nil, in which +prefix+ is used as
|
243
|
+
# the URI
|
244
|
+
# Evaluates to: this Element
|
245
|
+
# a = Element.new("a")
|
246
|
+
# a.add_namespace("xmlns:foo", "bar" )
|
247
|
+
# a.add_namespace("foo", "bar") # shorthand for previous line
|
248
|
+
# a.add_namespace("twiddle")
|
249
|
+
# puts a #-> <a xmlns:foo='bar' xmlns='twiddle'/>
|
250
|
+
def add_namespace( prefix, uri=nil )
|
251
|
+
unless uri
|
252
|
+
@attributes["xmlns"] = prefix
|
253
|
+
else
|
254
|
+
prefix = "xmlns:#{prefix}" unless prefix =~ /^xmlns:/
|
255
|
+
@attributes[ prefix ] = uri
|
256
|
+
end
|
257
|
+
self
|
258
|
+
end
|
259
|
+
|
260
|
+
# Removes a namespace from this node. This only works if the namespace is
|
261
|
+
# actually declared in this node. If no argument is passed, deletes the
|
262
|
+
# default namespace.
|
263
|
+
#
|
264
|
+
# Evaluates to: this element
|
265
|
+
# doc = Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
|
266
|
+
# doc.root.delete_namespace
|
267
|
+
# puts doc # -> <a xmlns:foo='bar'/>
|
268
|
+
# doc.root.delete_namespace 'foo'
|
269
|
+
# puts doc # -> <a/>
|
270
|
+
def delete_namespace namespace="xmlns"
|
271
|
+
namespace = "xmlns:#{namespace}" unless namespace == 'xmlns'
|
272
|
+
attribute = attributes.get_attribute(namespace)
|
273
|
+
attribute.remove unless attribute.nil?
|
274
|
+
self
|
275
|
+
end
|
276
|
+
|
277
|
+
#################################################
|
278
|
+
# Elements #
|
279
|
+
#################################################
|
280
|
+
|
281
|
+
# Adds a child to this element, optionally setting attributes in
|
282
|
+
# the element.
|
283
|
+
# element::
|
284
|
+
# optional. If Element, the element is added.
|
285
|
+
# Otherwise, a new Element is constructed with the argument (see
|
286
|
+
# Element.initialize).
|
287
|
+
# attrs::
|
288
|
+
# If supplied, must be a Hash containing String name,value
|
289
|
+
# pairs, which will be used to set the attributes of the new Element.
|
290
|
+
# Returns:: the Element that was added
|
291
|
+
# el = doc.add_element 'my-tag'
|
292
|
+
# el = doc.add_element 'my-tag', {'attr1'=>'val1', 'attr2'=>'val2'}
|
293
|
+
# el = Element.new 'my-tag'
|
294
|
+
# doc.add_element el
|
295
|
+
def add_element element, attrs=nil
|
296
|
+
raise "First argument must be either an element name, or an Element object" if element.nil?
|
297
|
+
el = @elements.add(element)
|
298
|
+
attrs.each do |key, value|
|
299
|
+
el.attributes[key]=value
|
300
|
+
end if attrs.kind_of? Hash
|
301
|
+
el
|
302
|
+
end
|
303
|
+
|
304
|
+
# Deletes a child element.
|
305
|
+
# element::
|
306
|
+
# Must be an +Element+, +String+, or +Integer+. If Element,
|
307
|
+
# the element is removed. If String, the element is found (via XPath)
|
308
|
+
# and removed. <em>This means that any parent can remove any
|
309
|
+
# descendant.<em> If Integer, the Element indexed by that number will be
|
310
|
+
# removed.
|
311
|
+
# Returns:: the element that was removed.
|
312
|
+
# doc.delete_element "/a/b/c[@id='4']"
|
313
|
+
# doc.delete_element doc.elements["//k"]
|
314
|
+
# doc.delete_element 1
|
315
|
+
def delete_element element
|
316
|
+
@elements.delete element
|
317
|
+
end
|
318
|
+
|
319
|
+
# Evaluates to +true+ if this element has at least one child Element
|
320
|
+
# doc = Document.new "<a><b/><c>Text</c></a>"
|
321
|
+
# doc.root.has_elements # -> true
|
322
|
+
# doc.elements["/a/b"].has_elements # -> false
|
323
|
+
# doc.elements["/a/c"].has_elements # -> false
|
324
|
+
def has_elements?
|
325
|
+
!@elements.empty?
|
326
|
+
end
|
327
|
+
|
328
|
+
# Iterates through the child elements, yielding for each Element that
|
329
|
+
# has a particular attribute set.
|
330
|
+
# key::
|
331
|
+
# the name of the attribute to search for
|
332
|
+
# value::
|
333
|
+
# the value of the attribute
|
334
|
+
# max::
|
335
|
+
# (optional) causes this method to return after yielding
|
336
|
+
# for this number of matching children
|
337
|
+
# name::
|
338
|
+
# (optional) if supplied, this is an XPath that filters
|
339
|
+
# the children to check.
|
340
|
+
#
|
341
|
+
# doc = Document.new "<a><b @id='1'/><c @id='2'/><d @id='1'/><e/></a>"
|
342
|
+
# # Yields b, c, d
|
343
|
+
# doc.root.each_element_with_attribute( 'id' ) {|e| p e}
|
344
|
+
# # Yields b, d
|
345
|
+
# doc.root.each_element_with_attribute( 'id', '1' ) {|e| p e}
|
346
|
+
# # Yields b
|
347
|
+
# doc.root.each_element_with_attribute( 'id', '1', 1 ) {|e| p e}
|
348
|
+
# # Yields d
|
349
|
+
# doc.root.each_element_with_attribute( 'id', '1', 0, 'd' ) {|e| p e}
|
350
|
+
def each_element_with_attribute( key, value=nil, max=0, name=nil, &block ) # :yields: Element
|
351
|
+
each_with_something( proc {|child|
|
352
|
+
if value.nil?
|
353
|
+
child.attributes[key] != nil
|
354
|
+
else
|
355
|
+
child.attributes[key]==value
|
356
|
+
end
|
357
|
+
}, max, name, &block )
|
358
|
+
end
|
359
|
+
|
360
|
+
# Iterates through the children, yielding for each Element that
|
361
|
+
# has a particular text set.
|
362
|
+
# text::
|
363
|
+
# the text to search for. If nil, or not supplied, will iterate
|
364
|
+
# over all +Element+ children that contain at least one +Text+ node.
|
365
|
+
# max::
|
366
|
+
# (optional) causes this method to return after yielding
|
367
|
+
# for this number of matching children
|
368
|
+
# name::
|
369
|
+
# (optional) if supplied, this is an XPath that filters
|
370
|
+
# the children to check.
|
371
|
+
#
|
372
|
+
# doc = Document.new '<a><b>b</b><c>b</c><d>d</d><e/></a>'
|
373
|
+
# # Yields b, c, d
|
374
|
+
# doc.each_element_with_text {|e|p e}
|
375
|
+
# # Yields b, c
|
376
|
+
# doc.each_element_with_text('b'){|e|p e}
|
377
|
+
# # Yields b
|
378
|
+
# doc.each_element_with_text('b', 1){|e|p e}
|
379
|
+
# # Yields d
|
380
|
+
# doc.each_element_with_text(nil, 0, 'd'){|e|p e}
|
381
|
+
def each_element_with_text( text=nil, max=0, name=nil, &block ) # :yields: Element
|
382
|
+
each_with_something( proc {|child|
|
383
|
+
if text.nil?
|
384
|
+
child.has_text?
|
385
|
+
else
|
386
|
+
child.text == text
|
387
|
+
end
|
388
|
+
}, max, name, &block )
|
389
|
+
end
|
390
|
+
|
391
|
+
# Synonym for Element.elements.each
|
392
|
+
def each_element( xpath=nil, &block ) # :yields: Element
|
393
|
+
@elements.each( xpath, &block )
|
394
|
+
end
|
395
|
+
|
396
|
+
# Synonym for Element.to_a
|
397
|
+
# This is a little slower than calling elements.each directly.
|
398
|
+
# xpath:: any XPath by which to search for elements in the tree
|
399
|
+
# Returns:: an array of Elements that match the supplied path
|
400
|
+
def get_elements( xpath )
|
401
|
+
@elements.to_a( xpath )
|
402
|
+
end
|
403
|
+
|
404
|
+
# Returns the next sibling that is an element, or nil if there is
|
405
|
+
# no Element sibling after this one
|
406
|
+
# doc = Document.new '<a><b/>text<c/></a>'
|
407
|
+
# doc.root.elements['b'].next_element #-> <c/>
|
408
|
+
# doc.root.elements['c'].next_element #-> nil
|
409
|
+
def next_element
|
410
|
+
element = next_sibling
|
411
|
+
element = element.next_sibling until element.nil? or element.kind_of? Element
|
412
|
+
return element
|
413
|
+
end
|
414
|
+
|
415
|
+
# Returns the previous sibling that is an element, or nil if there is
|
416
|
+
# no Element sibling prior to this one
|
417
|
+
# doc = Document.new '<a><b/>text<c/></a>'
|
418
|
+
# doc.root.elements['c'].previous_element #-> <b/>
|
419
|
+
# doc.root.elements['b'].previous_element #-> nil
|
420
|
+
def previous_element
|
421
|
+
element = previous_sibling
|
422
|
+
element = element.previous_sibling until element.nil? or element.kind_of? Element
|
423
|
+
return element
|
424
|
+
end
|
425
|
+
|
426
|
+
|
427
|
+
#################################################
|
428
|
+
# Text #
|
429
|
+
#################################################
|
430
|
+
|
431
|
+
# Evaluates to +true+ if this element has at least one Text child
|
432
|
+
def has_text?
|
433
|
+
not text().nil?
|
434
|
+
end
|
435
|
+
|
436
|
+
# A convenience method which returns the String value of the _first_
|
437
|
+
# child text element, if one exists, and +nil+ otherwise.
|
438
|
+
#
|
439
|
+
# <em>Note that an element may have multiple Text elements, perhaps
|
440
|
+
# separated by other children</em>. Be aware that this method only returns
|
441
|
+
# the first Text node.
|
442
|
+
#
|
443
|
+
# This method returns the +value+ of the first text child node, which
|
444
|
+
# ignores the +raw+ setting, so always returns normalized text. See
|
445
|
+
# the Text::value documentation.
|
446
|
+
#
|
447
|
+
# doc = Document.new "<p>some text <b>this is bold!</b> more text</p>"
|
448
|
+
# # The element 'p' has two text elements, "some text " and " more text".
|
449
|
+
# doc.root.text #-> "some text "
|
450
|
+
def text( path = nil )
|
451
|
+
rv = get_text(path)
|
452
|
+
return rv.value unless rv.nil?
|
453
|
+
nil
|
454
|
+
end
|
455
|
+
|
456
|
+
# Returns the first child Text node, if any, or +nil+ otherwise.
|
457
|
+
# This method returns the actual +Text+ node, rather than the String content.
|
458
|
+
# doc = Document.new "<p>some text <b>this is bold!</b> more text</p>"
|
459
|
+
# # The element 'p' has two text elements, "some text " and " more text".
|
460
|
+
# doc.root.get_text.value #-> "some text "
|
461
|
+
def get_text path = nil
|
462
|
+
rv = nil
|
463
|
+
if path
|
464
|
+
element = @elements[ path ]
|
465
|
+
rv = element.get_text unless element.nil?
|
466
|
+
else
|
467
|
+
rv = @children.find { |node| node.kind_of? Text }
|
468
|
+
end
|
469
|
+
return rv
|
470
|
+
end
|
471
|
+
|
472
|
+
# Sets the first Text child of this object. See text() for a
|
473
|
+
# discussion about Text children.
|
474
|
+
#
|
475
|
+
# If a Text child already exists, the child is replaced by this
|
476
|
+
# content. This means that Text content can be deleted by calling
|
477
|
+
# this method with a nil argument. In this case, the next Text
|
478
|
+
# child becomes the first Text child. In no case is the order of
|
479
|
+
# any siblings disturbed.
|
480
|
+
# text::
|
481
|
+
# If a String, a new Text child is created and added to
|
482
|
+
# this Element as the first Text child. If Text, the text is set
|
483
|
+
# as the first Child element. If nil, then any existing first Text
|
484
|
+
# child is removed.
|
485
|
+
# Returns:: this Element.
|
486
|
+
# doc = Document.new '<a><b/></a>'
|
487
|
+
# doc.root.text = 'Sean' #-> '<a><b/>Sean</a>'
|
488
|
+
# doc.root.text = 'Elliott' #-> '<a><b/>Elliott</a>'
|
489
|
+
# doc.root.add_element 'c' #-> '<a><b/>Elliott<c/></a>'
|
490
|
+
# doc.root.text = 'Russell' #-> '<a><b/>Russell<c/></a>'
|
491
|
+
# doc.root.text = nil #-> '<a><b/><c/></a>'
|
492
|
+
def text=( text )
|
493
|
+
if text.kind_of? String
|
494
|
+
text = Text.new( text, whitespace(), nil, raw() )
|
495
|
+
elsif text and !text.kind_of? Text
|
496
|
+
text = Text.new( text.to_s, whitespace(), nil, raw() )
|
497
|
+
end
|
498
|
+
old_text = get_text
|
499
|
+
if text.nil?
|
500
|
+
old_text.remove unless old_text.nil?
|
501
|
+
else
|
502
|
+
if old_text.nil?
|
503
|
+
self << text
|
504
|
+
else
|
505
|
+
old_text.replace_with( text )
|
506
|
+
end
|
507
|
+
end
|
508
|
+
return self
|
509
|
+
end
|
510
|
+
|
511
|
+
# A helper method to add a Text child. Actual Text instances can
|
512
|
+
# be added with regular Parent methods, such as add() and <<()
|
513
|
+
# text::
|
514
|
+
# if a String, a new Text instance is created and added
|
515
|
+
# to the parent. If Text, the object is added directly.
|
516
|
+
# Returns:: this Element
|
517
|
+
# e = Element.new('a') #-> <e/>
|
518
|
+
# e.add_text 'foo' #-> <e>foo</e>
|
519
|
+
# e.add_text Text.new(' bar') #-> <e>foo bar</e>
|
520
|
+
# Note that at the end of this example, the branch has <b>3</b> nodes; the 'e'
|
521
|
+
# element and <b>2</b> Text node children.
|
522
|
+
def add_text( text )
|
523
|
+
if text.kind_of? String
|
524
|
+
if @children[-1].kind_of? Text
|
525
|
+
@children[-1] << text
|
526
|
+
return
|
527
|
+
end
|
528
|
+
text = Text.new( text, whitespace(), nil, raw() )
|
529
|
+
end
|
530
|
+
self << text unless text.nil?
|
531
|
+
return self
|
532
|
+
end
|
533
|
+
|
534
|
+
def node_type
|
535
|
+
:element
|
536
|
+
end
|
537
|
+
|
538
|
+
def xpath
|
539
|
+
path_elements = []
|
540
|
+
cur = self
|
541
|
+
path_elements << __to_xpath_helper( self )
|
542
|
+
while cur.parent
|
543
|
+
cur = cur.parent
|
544
|
+
path_elements << __to_xpath_helper( cur )
|
545
|
+
end
|
546
|
+
return path_elements.reverse.join( "/" )
|
547
|
+
end
|
548
|
+
|
549
|
+
#################################################
|
550
|
+
# Attributes #
|
551
|
+
#################################################
|
552
|
+
|
553
|
+
def attribute( name, namespace=nil )
|
554
|
+
prefix = nil
|
555
|
+
if namespaces.respond_to? :key
|
556
|
+
prefix = namespaces.key(namespace) if namespace
|
557
|
+
else
|
558
|
+
prefix = namespaces.index(namespace) if namespace
|
559
|
+
end
|
560
|
+
prefix = nil if prefix == 'xmlns'
|
561
|
+
|
562
|
+
ret_val =
|
563
|
+
attributes.get_attribute( "#{prefix ? prefix + ':' : ''}#{name}" )
|
564
|
+
|
565
|
+
return ret_val unless ret_val.nil?
|
566
|
+
return nil if prefix.nil?
|
567
|
+
|
568
|
+
# now check that prefix'es namespace is not the same as the
|
569
|
+
# default namespace
|
570
|
+
return nil unless ( namespaces[ prefix ] == namespaces[ 'xmlns' ] )
|
571
|
+
|
572
|
+
attributes.get_attribute( name )
|
573
|
+
|
574
|
+
end
|
575
|
+
|
576
|
+
# Evaluates to +true+ if this element has any attributes set, false
|
577
|
+
# otherwise.
|
578
|
+
def has_attributes?
|
579
|
+
return !@attributes.empty?
|
580
|
+
end
|
581
|
+
|
582
|
+
# Adds an attribute to this element, overwriting any existing attribute
|
583
|
+
# by the same name.
|
584
|
+
# key::
|
585
|
+
# can be either an Attribute or a String. If an Attribute,
|
586
|
+
# the attribute is added to the list of Element attributes. If String,
|
587
|
+
# the argument is used as the name of the new attribute, and the value
|
588
|
+
# parameter must be supplied.
|
589
|
+
# value::
|
590
|
+
# Required if +key+ is a String, and ignored if the first argument is
|
591
|
+
# an Attribute. This is a String, and is used as the value
|
592
|
+
# of the new Attribute. This should be the unnormalized value of the
|
593
|
+
# attribute (without entities).
|
594
|
+
# Returns:: the Attribute added
|
595
|
+
# e = Element.new 'e'
|
596
|
+
# e.add_attribute( 'a', 'b' ) #-> <e a='b'/>
|
597
|
+
# e.add_attribute( 'x:a', 'c' ) #-> <e a='b' x:a='c'/>
|
598
|
+
# e.add_attribute Attribute.new('b', 'd') #-> <e a='b' x:a='c' b='d'/>
|
599
|
+
def add_attribute( key, value=nil )
|
600
|
+
#if key.kind_of? Attribute
|
601
|
+
# @attributes << key
|
602
|
+
#else
|
603
|
+
@attributes[key] = value
|
604
|
+
#end
|
605
|
+
end
|
606
|
+
|
607
|
+
# Add multiple attributes to this element.
|
608
|
+
# hash:: is either a hash, or array of arrays
|
609
|
+
# el.add_attributes( {"name1"=>"value1", "name2"=>"value2"} )
|
610
|
+
# el.add_attributes( [ ["name1","value1"], ["name2"=>"value2"] ] )
|
611
|
+
def add_attributes hash
|
612
|
+
if hash.kind_of? Hash
|
613
|
+
hash.each_pair {|key, value| @attributes[key] = value }
|
614
|
+
elsif hash.kind_of? Array
|
615
|
+
hash.each { |value| @attributes[ value[0] ] = value[1] }
|
616
|
+
end
|
617
|
+
end
|
618
|
+
|
619
|
+
# Removes an attribute
|
620
|
+
# key::
|
621
|
+
# either an Attribute or a String. In either case, the
|
622
|
+
# attribute is found by matching the attribute name to the argument,
|
623
|
+
# and then removed. If no attribute is found, no action is taken.
|
624
|
+
# Returns::
|
625
|
+
# the attribute removed, or nil if this Element did not contain
|
626
|
+
# a matching attribute
|
627
|
+
# e = Element.new('E')
|
628
|
+
# e.add_attribute( 'name', 'Sean' ) #-> <E name='Sean'/>
|
629
|
+
# r = e.add_attribute( 'sur:name', 'Russell' ) #-> <E name='Sean' sur:name='Russell'/>
|
630
|
+
# e.delete_attribute( 'name' ) #-> <E sur:name='Russell'/>
|
631
|
+
# e.delete_attribute( r ) #-> <E/>
|
632
|
+
def delete_attribute(key)
|
633
|
+
attr = @attributes.get_attribute(key)
|
634
|
+
attr.remove unless attr.nil?
|
635
|
+
end
|
636
|
+
=begin
|
637
|
+
#################################################
|
638
|
+
# Other Utilities #
|
639
|
+
#################################################
|
640
|
+
|
641
|
+
# Get an array of all CData children.
|
642
|
+
# IMMUTABLE
|
643
|
+
def cdatas
|
644
|
+
find_all { |child| child.kind_of? CData }.freeze
|
645
|
+
end
|
646
|
+
|
647
|
+
# Get an array of all Comment children.
|
648
|
+
# IMMUTABLE
|
649
|
+
def comments
|
650
|
+
find_all { |child| child.kind_of? Comment }.freeze
|
651
|
+
end
|
652
|
+
|
653
|
+
# Get an array of all Instruction children.
|
654
|
+
# IMMUTABLE
|
655
|
+
def instructions
|
656
|
+
find_all { |child| child.kind_of? Instruction }.freeze
|
657
|
+
end
|
658
|
+
|
659
|
+
# Get an array of all Text children.
|
660
|
+
# IMMUTABLE
|
661
|
+
def texts
|
662
|
+
find_all { |child| child.kind_of? Text }.freeze
|
663
|
+
end
|
664
|
+
|
665
|
+
# == DEPRECATED
|
666
|
+
# See REXML::Formatters
|
667
|
+
#
|
668
|
+
# Writes out this element, and recursively, all children.
|
669
|
+
# output::
|
670
|
+
# output an object which supports '<< string'; this is where the
|
671
|
+
# document will be written.
|
672
|
+
# indent::
|
673
|
+
# An integer. If -1, no indenting will be used; otherwise, the
|
674
|
+
# indentation will be this number of spaces, and children will be
|
675
|
+
# indented an additional amount. Defaults to -1
|
676
|
+
# transitive::
|
677
|
+
# If transitive is true and indent is >= 0, then the output will be
|
678
|
+
# pretty-printed in such a way that the added whitespace does not affect
|
679
|
+
# the parse tree of the document
|
680
|
+
# ie_hack::
|
681
|
+
# Internet Explorer is the worst piece of crap to have ever been
|
682
|
+
# written, with the possible exception of Windows itself. Since IE is
|
683
|
+
# unable to parse proper XML, we have to provide a hack to generate XML
|
684
|
+
# that IE's limited abilities can handle. This hack inserts a space
|
685
|
+
# before the /> on empty tags. Defaults to false
|
686
|
+
#
|
687
|
+
# out = ''
|
688
|
+
# doc.write( out ) #-> doc is written to the string 'out'
|
689
|
+
# doc.write( $stdout ) #-> doc written to the console
|
690
|
+
def write(output=$stdout, indent=-1, transitive=false, ie_hack=false)
|
691
|
+
Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters")
|
692
|
+
formatter = if indent > -1
|
693
|
+
if transitive
|
694
|
+
require "rexml/formatters/transitive"
|
695
|
+
REXML::Formatters::Transitive.new( indent, ie_hack )
|
696
|
+
else
|
697
|
+
REXML::Formatters::Pretty.new( indent, ie_hack )
|
698
|
+
end
|
699
|
+
else
|
700
|
+
REXML::Formatters::Default.new( ie_hack )
|
701
|
+
end
|
702
|
+
formatter.write( self, output )
|
703
|
+
end
|
704
|
+
|
705
|
+
|
706
|
+
private
|
707
|
+
def __to_xpath_helper node
|
708
|
+
rv = node.expanded_name.clone
|
709
|
+
if node.parent
|
710
|
+
results = node.parent.find_all {|n|
|
711
|
+
n.kind_of?(REXML::Element) and n.expanded_name == node.expanded_name
|
712
|
+
}
|
713
|
+
if results.length > 1
|
714
|
+
idx = results.index( node )
|
715
|
+
rv << "[#{idx+1}]"
|
716
|
+
end
|
717
|
+
end
|
718
|
+
rv
|
719
|
+
end
|
720
|
+
|
721
|
+
# A private helper method
|
722
|
+
def each_with_something( test, max=0, name=nil )
|
723
|
+
num = 0
|
724
|
+
@elements.each( name ){ |child|
|
725
|
+
yield child if test.call(child) and num += 1
|
726
|
+
return if max>0 and num == max
|
727
|
+
}
|
728
|
+
end
|
729
|
+
=end
|
730
|
+
end
|
731
|
+
|
732
|
+
########################################################################
|
733
|
+
# ELEMENTS #
|
734
|
+
########################################################################
|
735
|
+
|
736
|
+
# A class which provides filtering of children for Elements, and
|
737
|
+
# XPath search support. You are expected to only encounter this class as
|
738
|
+
# the <tt>element.elements</tt> object. Therefore, you are
|
739
|
+
# _not_ expected to instantiate this yourself.
|
740
|
+
class Elements
|
741
|
+
include Enumerable
|
742
|
+
# Constructor
|
743
|
+
# parent:: the parent Element
|
744
|
+
def initialize parent
|
745
|
+
@element = parent
|
746
|
+
end
|
747
|
+
|
748
|
+
# Fetches a child element. Filters only Element children, regardless of
|
749
|
+
# the XPath match.
|
750
|
+
# index::
|
751
|
+
# the search parameter. This is either an Integer, which
|
752
|
+
# will be used to find the index'th child Element, or an XPath,
|
753
|
+
# which will be used to search for the Element. <em>Because
|
754
|
+
# of the nature of XPath searches, any element in the connected XML
|
755
|
+
# document can be fetched through any other element.</em> <b>The
|
756
|
+
# Integer index is 1-based, not 0-based.</b> This means that the first
|
757
|
+
# child element is at index 1, not 0, and the +n+th element is at index
|
758
|
+
# +n+, not <tt>n-1</tt>. This is because XPath indexes element children
|
759
|
+
# starting from 1, not 0, and the indexes should be the same.
|
760
|
+
# name::
|
761
|
+
# optional, and only used in the first argument is an
|
762
|
+
# Integer. In that case, the index'th child Element that has the
|
763
|
+
# supplied name will be returned. Note again that the indexes start at 1.
|
764
|
+
# Returns:: the first matching Element, or nil if no child matched
|
765
|
+
# doc = Document.new '<a><b/><c id="1"/><c id="2"/><d/></a>'
|
766
|
+
# doc.root.elements[1] #-> <b/>
|
767
|
+
# doc.root.elements['c'] #-> <c id="1"/>
|
768
|
+
# doc.root.elements[2,'c'] #-> <c id="2"/>
|
769
|
+
def []( index, name=nil)
|
770
|
+
if index.kind_of? Integer
|
771
|
+
raise "index (#{index}) must be >= 1" if index < 1
|
772
|
+
name = literalize(name) if name
|
773
|
+
num = 0
|
774
|
+
@element.find { |child|
|
775
|
+
child.kind_of?( Element ) and
|
776
|
+
(name.nil? ? true : child.has_name?( name )) and
|
777
|
+
(num += 1) == index
|
778
|
+
}
|
779
|
+
else
|
780
|
+
if index.kind_of?( String ) && index.length() > 0 && !index.index('/')
|
781
|
+
name = literalize(index)
|
782
|
+
res = @element.find { |child|
|
783
|
+
child.kind_of?( Element ) and child.has_name?( name )
|
784
|
+
}
|
785
|
+
|
786
|
+
return res
|
787
|
+
end
|
788
|
+
|
789
|
+
return XPath::first( @element, index )
|
790
|
+
#{ |element|
|
791
|
+
# return element if element.kind_of? Element
|
792
|
+
#}
|
793
|
+
#return nil
|
794
|
+
end
|
795
|
+
end
|
796
|
+
|
797
|
+
# Sets an element, replacing any previous matching element. If no
|
798
|
+
# existing element is found ,the element is added.
|
799
|
+
# index:: Used to find a matching element to replace. See []().
|
800
|
+
# element::
|
801
|
+
# The element to replace the existing element with
|
802
|
+
# the previous element
|
803
|
+
# Returns:: nil if no previous element was found.
|
804
|
+
#
|
805
|
+
# doc = Document.new '<a/>'
|
806
|
+
# doc.root.elements[10] = Element.new('b') #-> <a><b/></a>
|
807
|
+
# doc.root.elements[1] #-> <b/>
|
808
|
+
# doc.root.elements[1] = Element.new('c') #-> <a><c/></a>
|
809
|
+
# doc.root.elements['c'] = Element.new('d') #-> <a><d/></a>
|
810
|
+
def []=( index, element )
|
811
|
+
previous = self[index]
|
812
|
+
if previous.nil?
|
813
|
+
@element.add element
|
814
|
+
else
|
815
|
+
previous.replace_with element
|
816
|
+
end
|
817
|
+
return previous
|
818
|
+
end
|
819
|
+
|
820
|
+
# Returns +true+ if there are no +Element+ children, +false+ otherwise
|
821
|
+
def empty?
|
822
|
+
@element.find{ |child| child.kind_of? Element}.nil?
|
823
|
+
end
|
824
|
+
|
825
|
+
# Returns the index of the supplied child (starting at 1), or -1 if
|
826
|
+
# the element is not a child
|
827
|
+
# element:: an +Element+ child
|
828
|
+
def index element
|
829
|
+
rv = 0
|
830
|
+
found = @element.find do |child|
|
831
|
+
child.kind_of?( Element ) and
|
832
|
+
(rv += 1) and
|
833
|
+
child == element
|
834
|
+
end
|
835
|
+
return rv if found == element
|
836
|
+
return -1
|
837
|
+
end
|
838
|
+
|
839
|
+
# Deletes a child Element
|
840
|
+
# element::
|
841
|
+
# Either an Element, which is removed directly; an
|
842
|
+
# xpath, where the first matching child is removed; or an Integer,
|
843
|
+
# where the n'th Element is removed.
|
844
|
+
# Returns:: the removed child
|
845
|
+
# doc = Document.new '<a><b/><c/><c id="1"/></a>'
|
846
|
+
# b = doc.root.elements[1]
|
847
|
+
# doc.root.elements.delete b #-> <a><c/><c id="1"/></a>
|
848
|
+
# doc.elements.delete("a/c[@id='1']") #-> <a><c/></a>
|
849
|
+
# doc.root.elements.delete 1 #-> <a/>
|
850
|
+
def delete element
|
851
|
+
if element.kind_of? Element
|
852
|
+
@element.delete element
|
853
|
+
else
|
854
|
+
el = self[element]
|
855
|
+
el.remove if el
|
856
|
+
end
|
857
|
+
end
|
858
|
+
|
859
|
+
# Removes multiple elements. Filters for Element children, regardless of
|
860
|
+
# XPath matching.
|
861
|
+
# xpath:: all elements matching this String path are removed.
|
862
|
+
# Returns:: an Array of Elements that have been removed
|
863
|
+
# doc = Document.new '<a><c/><c/><c/><c/></a>'
|
864
|
+
# deleted = doc.elements.delete_all 'a/c' #-> [<c/>, <c/>, <c/>, <c/>]
|
865
|
+
def delete_all( xpath )
|
866
|
+
rv = []
|
867
|
+
XPath::each( @element, xpath) {|element|
|
868
|
+
rv << element if element.kind_of? Element
|
869
|
+
}
|
870
|
+
rv.each do |element|
|
871
|
+
@element.delete element
|
872
|
+
element.remove
|
873
|
+
end
|
874
|
+
return rv
|
875
|
+
end
|
876
|
+
|
877
|
+
# Adds an element
|
878
|
+
# element::
|
879
|
+
# if supplied, is either an Element, String, or
|
880
|
+
# Source (see Element.initialize). If not supplied or nil, a
|
881
|
+
# new, default Element will be constructed
|
882
|
+
# Returns:: the added Element
|
883
|
+
# a = Element.new('a')
|
884
|
+
# a.elements.add(Element.new('b')) #-> <a><b/></a>
|
885
|
+
# a.elements.add('c') #-> <a><b/><c/></a>
|
886
|
+
def add element=nil
|
887
|
+
rv = nil
|
888
|
+
if element.nil?
|
889
|
+
Element.new("", self, @element.context)
|
890
|
+
elsif not element.kind_of?(Element)
|
891
|
+
Element.new(element, self, @element.context)
|
892
|
+
else
|
893
|
+
@element << element
|
894
|
+
element.context = @element.context
|
895
|
+
element
|
896
|
+
end
|
897
|
+
end
|
898
|
+
|
899
|
+
alias :<< :add
|
900
|
+
|
901
|
+
# Iterates through all of the child Elements, optionally filtering
|
902
|
+
# them by a given XPath
|
903
|
+
# xpath::
|
904
|
+
# optional. If supplied, this is a String XPath, and is used to
|
905
|
+
# filter the children, so that only matching children are yielded. Note
|
906
|
+
# that XPaths are automatically filtered for Elements, so that
|
907
|
+
# non-Element children will not be yielded
|
908
|
+
# doc = Document.new '<a><b/><c/><d/>sean<b/><c/><d/></a>'
|
909
|
+
# doc.root.each {|e|p e} #-> Yields b, c, d, b, c, d elements
|
910
|
+
# doc.root.each('b') {|e|p e} #-> Yields b, b elements
|
911
|
+
# doc.root.each('child::node()') {|e|p e}
|
912
|
+
# #-> Yields <b/>, <c/>, <d/>, <b/>, <c/>, <d/>
|
913
|
+
# XPath.each(doc.root, 'child::node()', &block)
|
914
|
+
# #-> Yields <b/>, <c/>, <d/>, sean, <b/>, <c/>, <d/>
|
915
|
+
def each( xpath=nil, &block)
|
916
|
+
puts "each : #{xpath}"
|
917
|
+
XPath::each( @element, xpath ) {|e| yield e if e.kind_of? Element }
|
918
|
+
end
|
919
|
+
|
920
|
+
def collect( xpath=nil, &block )
|
921
|
+
collection = []
|
922
|
+
XPath::each( @element, xpath ) {|e|
|
923
|
+
collection << yield(e) if e.kind_of?(Element)
|
924
|
+
}
|
925
|
+
collection
|
926
|
+
end
|
927
|
+
|
928
|
+
def inject( xpath=nil, initial=nil, &block )
|
929
|
+
first = true
|
930
|
+
XPath::each( @element, xpath ) {|e|
|
931
|
+
if (e.kind_of? Element)
|
932
|
+
if (first and initial == nil)
|
933
|
+
initial = e
|
934
|
+
first = false
|
935
|
+
else
|
936
|
+
initial = yield( initial, e ) if e.kind_of? Element
|
937
|
+
end
|
938
|
+
end
|
939
|
+
}
|
940
|
+
initial
|
941
|
+
end
|
942
|
+
|
943
|
+
# Returns the number of +Element+ children of the parent object.
|
944
|
+
# doc = Document.new '<a>sean<b/>elliott<b/>russell<b/></a>'
|
945
|
+
# doc.root.size #-> 6, 3 element and 3 text nodes
|
946
|
+
# doc.root.elements.size #-> 3
|
947
|
+
def size
|
948
|
+
count = 0
|
949
|
+
@element.each {|child| count+=1 if child.kind_of? Element }
|
950
|
+
count
|
951
|
+
end
|
952
|
+
|
953
|
+
# Returns an Array of Element children. An XPath may be supplied to
|
954
|
+
# filter the children. Only Element children are returned, even if the
|
955
|
+
# supplied XPath matches non-Element children.
|
956
|
+
# doc = Document.new '<a>sean<b/>elliott<c/></a>'
|
957
|
+
# doc.root.elements.to_a #-> [ <b/>, <c/> ]
|
958
|
+
# doc.root.elements.to_a("child::node()") #-> [ <b/>, <c/> ]
|
959
|
+
# XPath.match(doc.root, "child::node()") #-> [ sean, <b/>, elliott, <c/> ]
|
960
|
+
def to_a( xpath=nil )
|
961
|
+
rv = XPath.match( @element, xpath )
|
962
|
+
return rv.find_all{|e| e.kind_of? Element} if xpath
|
963
|
+
rv
|
964
|
+
end
|
965
|
+
|
966
|
+
private
|
967
|
+
# Private helper class. Removes quotes from quoted strings
|
968
|
+
def literalize name
|
969
|
+
name = name[1..-2] if name[0] == ?' or name[0] == ?" #'
|
970
|
+
name
|
971
|
+
end
|
972
|
+
end
|
973
|
+
|
974
|
+
########################################################################
|
975
|
+
# ATTRIBUTES #
|
976
|
+
########################################################################
|
977
|
+
|
978
|
+
# A class that defines the set of Attributes of an Element and provides
|
979
|
+
# operations for accessing elements in that set.
|
980
|
+
class Attributes < Hash
|
981
|
+
# Constructor
|
982
|
+
# element:: the Element of which this is an Attribute
|
983
|
+
def initialize element
|
984
|
+
@element = element
|
985
|
+
end
|
986
|
+
end
|
987
|
+
end
|