rho-tau-extensions 6.2.0 → 7.1.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/libs/crosswalk/ext/platform/android/Rakefile +19 -1
  3. data/libs/enterprise-barcode/ext/platform/android/adds/lib/aarch64/liballbarcode.a +0 -0
  4. data/libs/enterprise-barcode/ext/platform/android/adds/lib/armeabi/liballbarcode.a +0 -0
  5. data/libs/enterprise-barcode/ext/platform/android/adds/lib/x86/liballbarcode.a +0 -0
  6. data/libs/enterprise-barcode/ext/platform/android/allbarcode.jar +0 -0
  7. data/libs/fcm-push-18/ext.yml +20 -0
  8. data/libs/fcm-push-18/ext/android/AndroidManifest.rb +10 -0
  9. data/libs/fcm-push-18/ext/android/ApplicationManifestAdds.erb +39 -0
  10. data/libs/fcm-push-18/ext/android/Rakefile +187 -0
  11. data/libs/fcm-push-18/ext/android/ext_java.files +3 -0
  12. data/libs/fcm-push-18/ext/android/ext_native.files +1 -0
  13. data/libs/fcm-push-18/ext/android/jni/src/fcmpushclient.cpp +215 -0
  14. data/libs/fcm-push-18/ext/android/jni/src/fcmpushclient.h +74 -0
  15. data/libs/fcm-push-18/ext/android/res/res/values/fireBaseValues.xml +5 -0
  16. data/libs/fcm-push-18/ext/android/src/com/rhomobile/rhodes/fcm/FCMFacade.java +164 -0
  17. data/libs/fcm-push-18/ext/android/src/com/rhomobile/rhodes/fcm/FCMIntentService.java +219 -0
  18. data/libs/fcm-push-18/ext/android/src/com/rhomobile/rhodes/fcm/FCMListener.java +88 -0
  19. data/libs/fcm-push-18/ext/build +16 -0
  20. data/libs/fcm-push-18/ext/build.bat +8 -0
  21. data/libs/fcm-push-18/ext/fcm.xml +9 -0
  22. data/libs/fcm-push-18/ext/iphone/ApplePush_Prefix.pch +7 -0
  23. data/libs/fcm-push-18/ext/iphone/Firebase.h +68 -0
  24. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/FirebaseAnalytics +0 -0
  25. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FIRAnalytics+AppDelegate.h +62 -0
  26. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FIRAnalytics.h +115 -0
  27. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FIRAnalyticsConfiguration.h +1 -0
  28. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FIRAnalyticsSwiftNameSupport.h +13 -0
  29. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FIRApp.h +1 -0
  30. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FIRConfiguration.h +1 -0
  31. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FIREventNames.h +389 -0
  32. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FIROptions.h +1 -0
  33. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FIRParameterNames.h +485 -0
  34. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FIRUserPropertyNames.h +15 -0
  35. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Headers/FirebaseAnalytics.h +10 -0
  36. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseAnalytics.framework/Modules/module.modulemap +10 -0
  37. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseCore.framework/FirebaseCore +0 -0
  38. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseCore.framework/Headers/FIRAnalyticsConfiguration.h +52 -0
  39. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseCore.framework/Headers/FIRApp.h +130 -0
  40. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseCore.framework/Headers/FIRConfiguration.h +78 -0
  41. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseCore.framework/Headers/FIRLoggerLevel.h +35 -0
  42. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseCore.framework/Headers/FIROptions.h +133 -0
  43. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseCore.framework/Headers/FirebaseCore.h +5 -0
  44. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseCore.framework/Modules/module.modulemap +7 -0
  45. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseCoreDiagnostics.framework/FirebaseCoreDiagnostics +0 -0
  46. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseCoreDiagnostics.framework/Modules/module.modulemap +6 -0
  47. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseInstanceID.framework/FirebaseInstanceID +0 -0
  48. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseInstanceID.framework/Headers/FIRInstanceID.h +276 -0
  49. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseInstanceID.framework/Headers/FirebaseInstanceID.h +1 -0
  50. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseInstanceID.framework/Modules/module.modulemap +7 -0
  51. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseMessaging.framework/FirebaseMessaging +0 -0
  52. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseMessaging.framework/Headers/FIRMessaging.h +508 -0
  53. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseMessaging.framework/Headers/FirebaseMessaging.h +1 -0
  54. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseMessaging.framework/Modules/module.modulemap +8 -0
  55. data/libs/fcm-push-18/ext/iphone/Frameworks/FirebaseNanoPB.framework/FirebaseNanoPB +0 -0
  56. data/libs/fcm-push-18/ext/iphone/Frameworks/GoogleToolboxForMac.framework/GoogleToolboxForMac +0 -0
  57. data/libs/fcm-push-18/ext/iphone/Frameworks/GoogleToolboxForMac.framework/Modules/module.modulemap +5 -0
  58. data/libs/fcm-push-18/ext/iphone/Frameworks/Protobuf.framework/Modules/module.modulemap +5 -0
  59. data/libs/fcm-push-18/ext/iphone/Frameworks/Protobuf.framework/Protobuf +0 -0
  60. data/libs/fcm-push-18/ext/iphone/Frameworks/nanopb.framework/Modules/module.modulemap +5 -0
  61. data/libs/fcm-push-18/ext/iphone/Frameworks/nanopb.framework/nanopb +0 -0
  62. data/libs/fcm-push-18/ext/iphone/Rakefile +81 -0
  63. data/libs/fcm-push-18/ext/iphone/fcm-push.xcodeproj/project.pbxproj +370 -0
  64. data/libs/fcm-push-18/ext/iphone/impl/PushNotificationsReceiver.h +28 -0
  65. data/libs/fcm-push-18/ext/iphone/impl/PushNotificationsReceiver.m +27 -0
  66. data/libs/fcm-push-18/ext/iphone/impl/PushNotificationsReceiver.mm +234 -0
  67. data/libs/fcm-push-18/ext/iphone/impl/applePushSetup.c +10 -0
  68. data/libs/fcm-push-18/ext/iphone/impl/applepushclient.h +69 -0
  69. data/libs/fcm-push-18/ext/iphone/impl/applepushclient.mm +164 -0
  70. data/libs/fcm-push-18/ext/iphone/impl/readme.txt +7 -0
  71. data/libs/fcm-push-18/ext/platform/iphone/impl/readme.txt +7 -0
  72. data/libs/nodejs/ext/platform/android/jxCore/jx.h +17 -1
  73. data/libs/nodejs/ext/platform/android/jxCore/libcares_arm.a +0 -0
  74. data/libs/nodejs/ext/platform/android/jxCore/libcares_ia32.a +0 -0
  75. data/libs/nodejs/ext/platform/android/jxCore/libcares_x64.a +0 -0
  76. data/libs/nodejs/ext/platform/android/jxCore/libchrome_zlib_arm.a +0 -0
  77. data/libs/nodejs/ext/platform/android/jxCore/libchrome_zlib_ia32.a +0 -0
  78. data/libs/nodejs/ext/platform/android/jxCore/libchrome_zlib_x64.a +0 -0
  79. data/libs/nodejs/ext/platform/android/jxCore/libhttp_parser_arm.a +0 -0
  80. data/libs/nodejs/ext/platform/android/jxCore/libhttp_parser_ia32.a +0 -0
  81. data/libs/nodejs/ext/platform/android/jxCore/libhttp_parser_x64.a +0 -0
  82. data/libs/nodejs/ext/platform/android/jxCore/libjx_arm.a +0 -0
  83. data/libs/nodejs/ext/platform/android/jxCore/libjx_ia32.a +0 -0
  84. data/libs/nodejs/ext/platform/android/jxCore/libjx_x64.a +0 -0
  85. data/libs/nodejs/ext/platform/android/jxCore/libopenssl_arm.a +0 -0
  86. data/libs/nodejs/ext/platform/android/jxCore/libopenssl_ia32.a +0 -0
  87. data/libs/nodejs/ext/platform/android/jxCore/libopenssl_x64.a +0 -0
  88. data/libs/nodejs/ext/platform/android/jxCore/libsqlite3_arm.a +0 -0
  89. data/libs/nodejs/ext/platform/android/jxCore/libsqlite3_ia32.a +0 -0
  90. data/libs/nodejs/ext/platform/android/jxCore/libsqlite3_x64.a +0 -0
  91. data/libs/nodejs/ext/platform/android/jxCore/libuv_arm.a +0 -0
  92. data/libs/nodejs/ext/platform/android/jxCore/libuv_ia32.a +0 -0
  93. data/libs/nodejs/ext/platform/android/jxCore/libuv_x64.a +0 -0
  94. data/libs/nodejs/ext/platform/android/jxCore/libv8_base_arm.a +0 -0
  95. data/libs/nodejs/ext/platform/android/jxCore/libv8_base_ia32.a +0 -0
  96. data/libs/nodejs/ext/platform/android/jxCore/libv8_base_x64.a +0 -0
  97. data/libs/nodejs/ext/platform/android/jxCore/libv8_nosnapshot_arm.a +0 -0
  98. data/libs/nodejs/ext/platform/android/jxCore/libv8_nosnapshot_ia32.a +0 -0
  99. data/libs/nodejs/ext/platform/android/jxCore/libv8_nosnapshot_x64.a +0 -0
  100. data/libs/nodejs/ext/platform/iphone/jxCore/jx.h +17 -1
  101. data/libs/nodejs/ext/platform/iphone/jxCore/libcares.a +0 -0
  102. data/libs/nodejs/ext/platform/iphone/jxCore/libchrome_zlib.a +0 -0
  103. data/libs/nodejs/ext/platform/iphone/jxCore/libhttp_parser.a +0 -0
  104. data/libs/nodejs/ext/platform/iphone/jxCore/libjx.a +0 -0
  105. data/libs/nodejs/ext/platform/iphone/jxCore/libmozjs.a +0 -0
  106. data/libs/nodejs/ext/platform/iphone/jxCore/libopenssl.a +0 -0
  107. data/libs/nodejs/ext/platform/iphone/jxCore/libsqlite3.a +0 -0
  108. data/libs/nodejs/ext/platform/iphone/jxCore/libuv.a +0 -0
  109. data/libs/rexml-edge/rexml/attlistdecl.rb +63 -0
  110. data/libs/rexml-edge/rexml/attribute.rb +192 -0
  111. data/libs/rexml-edge/rexml/cdata.rb +68 -0
  112. data/libs/rexml-edge/rexml/child.rb +97 -0
  113. data/libs/rexml-edge/rexml/comment.rb +80 -0
  114. data/libs/rexml-edge/rexml/doctype.rb +270 -0
  115. data/libs/rexml-edge/rexml/document.rb +291 -0
  116. data/libs/rexml-edge/rexml/dtd/attlistdecl.rb +11 -0
  117. data/libs/rexml-edge/rexml/dtd/dtd.rb +47 -0
  118. data/libs/rexml-edge/rexml/dtd/elementdecl.rb +18 -0
  119. data/libs/rexml-edge/rexml/dtd/entitydecl.rb +57 -0
  120. data/libs/rexml-edge/rexml/dtd/notationdecl.rb +40 -0
  121. data/libs/rexml-edge/rexml/element.rb +1241 -0
  122. data/libs/rexml-edge/rexml/encoding.rb +51 -0
  123. data/libs/rexml-edge/rexml/entity.rb +174 -0
  124. data/libs/rexml-edge/rexml/formatters/default.rb +112 -0
  125. data/libs/rexml-edge/rexml/formatters/pretty.rb +142 -0
  126. data/libs/rexml-edge/rexml/formatters/transitive.rb +58 -0
  127. data/libs/rexml-edge/rexml/functions.rb +418 -0
  128. data/libs/rexml-edge/rexml/instruction.rb +71 -0
  129. data/libs/rexml-edge/rexml/light/node.rb +196 -0
  130. data/libs/rexml-edge/rexml/namespace.rb +48 -0
  131. data/libs/rexml-edge/rexml/node.rb +76 -0
  132. data/libs/rexml-edge/rexml/output.rb +30 -0
  133. data/libs/rexml-edge/rexml/parent.rb +166 -0
  134. data/libs/rexml-edge/rexml/parseexception.rb +52 -0
  135. data/libs/rexml-edge/rexml/parsers/baseparser.rb +533 -0
  136. data/libs/rexml-edge/rexml/parsers/lightparser.rb +59 -0
  137. data/libs/rexml-edge/rexml/parsers/pullparser.rb +197 -0
  138. data/libs/rexml-edge/rexml/parsers/sax2parser.rb +273 -0
  139. data/libs/rexml-edge/rexml/parsers/streamparser.rb +61 -0
  140. data/libs/rexml-edge/rexml/parsers/treeparser.rb +101 -0
  141. data/libs/rexml-edge/rexml/parsers/ultralightparser.rb +57 -0
  142. data/libs/rexml-edge/rexml/parsers/xpathparser.rb +657 -0
  143. data/libs/rexml-edge/rexml/quickpath.rb +266 -0
  144. data/libs/rexml-edge/rexml/rexml.rb +32 -0
  145. data/libs/rexml-edge/rexml/sax2listener.rb +98 -0
  146. data/libs/rexml-edge/rexml/security.rb +28 -0
  147. data/libs/rexml-edge/rexml/source.rb +297 -0
  148. data/libs/rexml-edge/rexml/streamlistener.rb +93 -0
  149. data/libs/rexml-edge/rexml/syncenumerator.rb +33 -0
  150. data/libs/rexml-edge/rexml/text.rb +426 -0
  151. data/libs/rexml-edge/rexml/undefinednamespaceexception.rb +9 -0
  152. data/libs/rexml-edge/rexml/validation/relaxng.rb +539 -0
  153. data/libs/rexml-edge/rexml/validation/validation.rb +144 -0
  154. data/libs/rexml-edge/rexml/validation/validationexception.rb +10 -0
  155. data/libs/rexml-edge/rexml/xmldecl.rb +116 -0
  156. data/libs/rexml-edge/rexml/xmltokens.rb +85 -0
  157. data/libs/rexml-edge/rexml/xpath.rb +81 -0
  158. data/libs/rexml-edge/rexml/xpath_parser.rb +704 -0
  159. data/version +1 -1
  160. metadata +118 -2
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: false
2
+ require "rexml/child"
3
+ module REXML
4
+ module DTD
5
+ class AttlistDecl < Child
6
+ START = "<!ATTLIST"
7
+ START_RE = /^\s*#{START}/um
8
+ PATTERN_RE = /\s*(#{START}.*?>)/um
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: false
2
+ require "rexml/dtd/elementdecl"
3
+ require "rexml/dtd/entitydecl"
4
+ require "rexml/comment"
5
+ require "rexml/dtd/notationdecl"
6
+ require "rexml/dtd/attlistdecl"
7
+ require "rexml/parent"
8
+
9
+ module REXML
10
+ module DTD
11
+ class Parser
12
+ def Parser.parse( input )
13
+ case input
14
+ when String
15
+ parse_helper input
16
+ when File
17
+ parse_helper input.read
18
+ end
19
+ end
20
+
21
+ # Takes a String and parses it out
22
+ def Parser.parse_helper( input )
23
+ contents = Parent.new
24
+ while input.size > 0
25
+ case input
26
+ when ElementDecl.PATTERN_RE
27
+ match = $&
28
+ contents << ElementDecl.new( match )
29
+ when AttlistDecl.PATTERN_RE
30
+ matchdata = $~
31
+ contents << AttlistDecl.new( matchdata )
32
+ when EntityDecl.PATTERN_RE
33
+ matchdata = $~
34
+ contents << EntityDecl.new( matchdata )
35
+ when Comment.PATTERN_RE
36
+ matchdata = $~
37
+ contents << Comment.new( matchdata )
38
+ when NotationDecl.PATTERN_RE
39
+ matchdata = $~
40
+ contents << NotationDecl.new( matchdata )
41
+ end
42
+ end
43
+ contents
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: false
2
+ require "rexml/child"
3
+ module REXML
4
+ module DTD
5
+ class ElementDecl < Child
6
+ START = "<!ELEMENT"
7
+ START_RE = /^\s*#{START}/um
8
+ # PATTERN_RE = /^\s*(#{START}.*?)>/um
9
+ PATTERN_RE = /^\s*#{START}\s+((?:[:\w][-\.\w]*:)?[-!\*\.\w]*)(.*?)>/
10
+ #\s*((((["']).*?\5)|[^\/'">]*)*?)(\/)?>/um, true)
11
+
12
+ def initialize match
13
+ @name = match[1]
14
+ @rest = match[2]
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: false
2
+ require "rexml/child"
3
+ module REXML
4
+ module DTD
5
+ class EntityDecl < Child
6
+ START = "<!ENTITY"
7
+ START_RE = /^\s*#{START}/um
8
+ PUBLIC = /^\s*#{START}\s+(?:%\s+)?(\w+)\s+PUBLIC\s+((["']).*?\3)\s+((["']).*?\5)\s*>/um
9
+ SYSTEM = /^\s*#{START}\s+(?:%\s+)?(\w+)\s+SYSTEM\s+((["']).*?\3)(?:\s+NDATA\s+\w+)?\s*>/um
10
+ PLAIN = /^\s*#{START}\s+(\w+)\s+((["']).*?\3)\s*>/um
11
+ PERCENT = /^\s*#{START}\s+%\s+(\w+)\s+((["']).*?\3)\s*>/um
12
+ # <!ENTITY name SYSTEM "...">
13
+ # <!ENTITY name "...">
14
+ def initialize src
15
+ super()
16
+ md = nil
17
+ if src.match( PUBLIC )
18
+ md = src.match( PUBLIC, true )
19
+ @middle = "PUBLIC"
20
+ @content = "#{md[2]} #{md[4]}"
21
+ elsif src.match( SYSTEM )
22
+ md = src.match( SYSTEM, true )
23
+ @middle = "SYSTEM"
24
+ @content = md[2]
25
+ elsif src.match( PLAIN )
26
+ md = src.match( PLAIN, true )
27
+ @middle = ""
28
+ @content = md[2]
29
+ elsif src.match( PERCENT )
30
+ md = src.match( PERCENT, true )
31
+ @middle = ""
32
+ @content = md[2]
33
+ end
34
+ raise ParseException.new("failed Entity match", src) if md.nil?
35
+ @name = md[1]
36
+ end
37
+
38
+ def to_s
39
+ rv = "<!ENTITY #@name "
40
+ rv << "#@middle " if @middle.size > 0
41
+ rv << @content
42
+ rv
43
+ end
44
+
45
+ def write( output, indent )
46
+ indent( output, indent )
47
+ output << to_s
48
+ end
49
+
50
+ def EntityDecl.parse_source source, listener
51
+ md = source.match( PATTERN_RE, true )
52
+ thing = md[0].squeeze(" \t\n\r")
53
+ listener.send inspect.downcase, thing
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: false
2
+ require "rexml/child"
3
+ module REXML
4
+ module DTD
5
+ class NotationDecl < Child
6
+ START = "<!NOTATION"
7
+ START_RE = /^\s*#{START}/um
8
+ PUBLIC = /^\s*#{START}\s+(\w[\w-]*)\s+(PUBLIC)\s+((["']).*?\4)\s*>/um
9
+ SYSTEM = /^\s*#{START}\s+(\w[\w-]*)\s+(SYSTEM)\s+((["']).*?\4)\s*>/um
10
+ def initialize src
11
+ super()
12
+ if src.match( PUBLIC )
13
+ md = src.match( PUBLIC, true )
14
+ elsif src.match( SYSTEM )
15
+ md = src.match( SYSTEM, true )
16
+ else
17
+ raise ParseException.new( "error parsing notation: no matching pattern", src )
18
+ end
19
+ @name = md[1]
20
+ @middle = md[2]
21
+ @rest = md[3]
22
+ end
23
+
24
+ def to_s
25
+ "<!NOTATION #@name #@middle #@rest>"
26
+ end
27
+
28
+ def write( output, indent )
29
+ indent( output, indent )
30
+ output << to_s
31
+ end
32
+
33
+ def NotationDecl.parse_source source, listener
34
+ md = source.match( PATTERN_RE, true )
35
+ thing = md[0].squeeze(" \t\n\r")
36
+ listener.send inspect.downcase, thing
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,1241 @@
1
+ # frozen_string_literal: false
2
+ require "rexml/parent"
3
+ require "rexml/namespace"
4
+ require "rexml/attribute"
5
+ require "rexml/cdata"
6
+ require "rexml/xpath"
7
+ require "rexml/parseexception"
8
+
9
+ module REXML
10
+ # An implementation note about namespaces:
11
+ # As we parse, when we find namespaces we put them in a hash and assign
12
+ # them a unique ID. We then convert the namespace prefix for the node
13
+ # to the unique ID. This makes namespace lookup much faster for the
14
+ # cost of extra memory use. We save the namespace prefix for the
15
+ # context node and convert it back when we write it.
16
+ @@namespaces = {}
17
+
18
+ # Represents a tagged XML element. Elements are characterized by
19
+ # having children, attributes, and names, and can themselves be
20
+ # children.
21
+ class Element < Parent
22
+ include Namespace
23
+
24
+ UNDEFINED = "UNDEFINED"; # The default name
25
+
26
+ # Mechanisms for accessing attributes and child elements of this
27
+ # element.
28
+ attr_reader :attributes, :elements
29
+ # The context holds information about the processing environment, such as
30
+ # whitespace handling.
31
+ attr_accessor :context
32
+
33
+ # Constructor
34
+ # arg::
35
+ # if not supplied, will be set to the default value.
36
+ # If a String, the name of this object will be set to the argument.
37
+ # If an Element, the object will be shallowly cloned; name,
38
+ # attributes, and namespaces will be copied. Children will +not+ be
39
+ # copied.
40
+ # parent::
41
+ # if supplied, must be a Parent, and will be used as
42
+ # the parent of this object.
43
+ # context::
44
+ # If supplied, must be a hash containing context items. Context items
45
+ # include:
46
+ # * <tt>:respect_whitespace</tt> the value of this is :+all+ or an array of
47
+ # strings being the names of the elements to respect
48
+ # whitespace for. Defaults to :+all+.
49
+ # * <tt>:compress_whitespace</tt> the value can be :+all+ or an array of
50
+ # strings being the names of the elements to ignore whitespace on.
51
+ # Overrides :+respect_whitespace+.
52
+ # * <tt>:ignore_whitespace_nodes</tt> the value can be :+all+ or an array
53
+ # of strings being the names of the elements in which to ignore
54
+ # whitespace-only nodes. If this is set, Text nodes which contain only
55
+ # whitespace will not be added to the document tree.
56
+ # * <tt>:raw</tt> can be :+all+, or an array of strings being the names of
57
+ # the elements to process in raw mode. In raw mode, special
58
+ # characters in text is not converted to or from entities.
59
+ def initialize( arg = UNDEFINED, parent=nil, context=nil )
60
+ super(parent)
61
+
62
+ @elements = Elements.new(self)
63
+ @attributes = Attributes.new(self)
64
+ @context = context
65
+
66
+ if arg.kind_of? String
67
+ self.name = arg
68
+ elsif arg.kind_of? Element
69
+ self.name = arg.expanded_name
70
+ arg.attributes.each_attribute{ |attribute|
71
+ @attributes << Attribute.new( attribute )
72
+ }
73
+ @context = arg.context
74
+ end
75
+ end
76
+
77
+ def inspect
78
+ rv = "<#@expanded_name"
79
+
80
+ @attributes.each_attribute do |attr|
81
+ rv << " "
82
+ attr.write( rv, 0 )
83
+ end
84
+
85
+ if children.size > 0
86
+ rv << "> ... </>"
87
+ else
88
+ rv << "/>"
89
+ end
90
+ end
91
+
92
+
93
+ # Creates a shallow copy of self.
94
+ # d = Document.new "<a><b/><b/><c><d/></c></a>"
95
+ # new_a = d.root.clone
96
+ # puts new_a # => "<a/>"
97
+ def clone
98
+ self.class.new self
99
+ end
100
+
101
+ # Evaluates to the root node of the document that this element
102
+ # belongs to. If this element doesn't belong to a document, but does
103
+ # belong to another Element, the parent's root will be returned, until the
104
+ # earliest ancestor is found.
105
+ #
106
+ # Note that this is not the same as the document element.
107
+ # In the following example, <a> is the document element, and the root
108
+ # node is the parent node of the document element. You may ask yourself
109
+ # why the root node is useful: consider the doctype and XML declaration,
110
+ # and any processing instructions before the document element... they
111
+ # are children of the root node, or siblings of the document element.
112
+ # The only time this isn't true is when an Element is created that is
113
+ # not part of any Document. In this case, the ancestor that has no
114
+ # parent acts as the root node.
115
+ # d = Document.new '<a><b><c/></b></a>'
116
+ # a = d[1] ; c = a[1][1]
117
+ # d.root_node == d # TRUE
118
+ # a.root_node # namely, d
119
+ # c.root_node # again, d
120
+ def root_node
121
+ parent.nil? ? self : parent.root_node
122
+ end
123
+
124
+ def root
125
+ return elements[1] if self.kind_of? Document
126
+ return self if parent.kind_of? Document or parent.nil?
127
+ return parent.root
128
+ end
129
+
130
+ # Evaluates to the document to which this element belongs, or nil if this
131
+ # element doesn't belong to a document.
132
+ def document
133
+ rt = root
134
+ rt.parent if rt
135
+ end
136
+
137
+ # Evaluates to +true+ if whitespace is respected for this element. This
138
+ # is the case if:
139
+ # 1. Neither :+respect_whitespace+ nor :+compress_whitespace+ has any value
140
+ # 2. The context has :+respect_whitespace+ set to :+all+ or
141
+ # an array containing the name of this element, and
142
+ # :+compress_whitespace+ isn't set to :+all+ or an array containing the
143
+ # name of this element.
144
+ # The evaluation is tested against +expanded_name+, and so is namespace
145
+ # sensitive.
146
+ def whitespace
147
+ @whitespace = nil
148
+ if @context
149
+ if @context[:respect_whitespace]
150
+ @whitespace = (@context[:respect_whitespace] == :all or
151
+ @context[:respect_whitespace].include? expanded_name)
152
+ end
153
+ @whitespace = false if (@context[:compress_whitespace] and
154
+ (@context[:compress_whitespace] == :all or
155
+ @context[:compress_whitespace].include? expanded_name)
156
+ )
157
+ end
158
+ @whitespace = true unless @whitespace == false
159
+ @whitespace
160
+ end
161
+
162
+ def ignore_whitespace_nodes
163
+ @ignore_whitespace_nodes = false
164
+ if @context
165
+ if @context[:ignore_whitespace_nodes]
166
+ @ignore_whitespace_nodes =
167
+ (@context[:ignore_whitespace_nodes] == :all or
168
+ @context[:ignore_whitespace_nodes].include? expanded_name)
169
+ end
170
+ end
171
+ end
172
+
173
+ # Evaluates to +true+ if raw mode is set for this element. This
174
+ # is the case if the context has :+raw+ set to :+all+ or
175
+ # an array containing the name of this element.
176
+ #
177
+ # The evaluation is tested against +expanded_name+, and so is namespace
178
+ # sensitive.
179
+ def raw
180
+ @raw = (@context and @context[:raw] and
181
+ (@context[:raw] == :all or
182
+ @context[:raw].include? expanded_name))
183
+ @raw
184
+ end
185
+
186
+ #once :whitespace, :raw, :ignore_whitespace_nodes
187
+
188
+ #################################################
189
+ # Namespaces #
190
+ #################################################
191
+
192
+ # Evaluates to an +Array+ containing the prefixes (names) of all defined
193
+ # namespaces at this context node.
194
+ # doc = Document.new("<a xmlns:x='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
195
+ # doc.elements['//b'].prefixes # -> ['x', 'y']
196
+ def prefixes
197
+ prefixes = []
198
+ prefixes = parent.prefixes if parent
199
+ prefixes |= attributes.prefixes
200
+ return prefixes
201
+ end
202
+
203
+ def namespaces
204
+ namespaces = {}
205
+ namespaces = parent.namespaces if parent
206
+ namespaces = namespaces.merge( attributes.namespaces )
207
+ return namespaces
208
+ end
209
+
210
+ # Evaluates to the URI for a prefix, or the empty string if no such
211
+ # namespace is declared for this element. Evaluates recursively for
212
+ # ancestors. Returns the default namespace, if there is one.
213
+ # prefix::
214
+ # the prefix to search for. If not supplied, returns the default
215
+ # namespace if one exists
216
+ # Returns::
217
+ # the namespace URI as a String, or nil if no such namespace
218
+ # exists. If the namespace is undefined, returns an empty string
219
+ # doc = Document.new("<a xmlns='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
220
+ # b = doc.elements['//b']
221
+ # b.namespace # -> '1'
222
+ # b.namespace("y") # -> '2'
223
+ def namespace(prefix=nil)
224
+ if prefix.nil?
225
+ prefix = prefix()
226
+ end
227
+ if prefix == ''
228
+ prefix = "xmlns"
229
+ else
230
+ prefix = "xmlns:#{prefix}" unless prefix[0,5] == 'xmlns'
231
+ end
232
+ ns = attributes[ prefix ]
233
+ ns = parent.namespace(prefix) if ns.nil? and parent
234
+ ns = '' if ns.nil? and prefix == 'xmlns'
235
+ return ns
236
+ end
237
+
238
+ # Adds a namespace to this element.
239
+ # prefix::
240
+ # the prefix string, or the namespace URI if +uri+ is not
241
+ # supplied
242
+ # uri::
243
+ # the namespace URI. May be nil, in which +prefix+ is used as
244
+ # the URI
245
+ # Evaluates to: this Element
246
+ # a = Element.new("a")
247
+ # a.add_namespace("xmlns:foo", "bar" )
248
+ # a.add_namespace("foo", "bar") # shorthand for previous line
249
+ # a.add_namespace("twiddle")
250
+ # puts a #-> <a xmlns:foo='bar' xmlns='twiddle'/>
251
+ def add_namespace( prefix, uri=nil )
252
+ unless uri
253
+ @attributes["xmlns"] = prefix
254
+ else
255
+ prefix = "xmlns:#{prefix}" unless prefix =~ /^xmlns:/
256
+ @attributes[ prefix ] = uri
257
+ end
258
+ self
259
+ end
260
+
261
+ # Removes a namespace from this node. This only works if the namespace is
262
+ # actually declared in this node. If no argument is passed, deletes the
263
+ # default namespace.
264
+ #
265
+ # Evaluates to: this element
266
+ # doc = Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
267
+ # doc.root.delete_namespace
268
+ # puts doc # -> <a xmlns:foo='bar'/>
269
+ # doc.root.delete_namespace 'foo'
270
+ # puts doc # -> <a/>
271
+ def delete_namespace namespace="xmlns"
272
+ namespace = "xmlns:#{namespace}" unless namespace == 'xmlns'
273
+ attribute = attributes.get_attribute(namespace)
274
+ attribute.remove unless attribute.nil?
275
+ self
276
+ end
277
+
278
+ #################################################
279
+ # Elements #
280
+ #################################################
281
+
282
+ # Adds a child to this element, optionally setting attributes in
283
+ # the element.
284
+ # element::
285
+ # optional. If Element, the element is added.
286
+ # Otherwise, a new Element is constructed with the argument (see
287
+ # Element.initialize).
288
+ # attrs::
289
+ # If supplied, must be a Hash containing String name,value
290
+ # pairs, which will be used to set the attributes of the new Element.
291
+ # Returns:: the Element that was added
292
+ # el = doc.add_element 'my-tag'
293
+ # el = doc.add_element 'my-tag', {'attr1'=>'val1', 'attr2'=>'val2'}
294
+ # el = Element.new 'my-tag'
295
+ # doc.add_element el
296
+ def add_element element, attrs=nil
297
+ raise "First argument must be either an element name, or an Element object" if element.nil?
298
+ el = @elements.add(element)
299
+ attrs.each do |key, value|
300
+ el.attributes[key]=value
301
+ end if attrs.kind_of? Hash
302
+ el
303
+ end
304
+
305
+ # Deletes a child element.
306
+ # element::
307
+ # Must be an +Element+, +String+, or +Integer+. If Element,
308
+ # the element is removed. If String, the element is found (via XPath)
309
+ # and removed. <em>This means that any parent can remove any
310
+ # descendant.<em> If Integer, the Element indexed by that number will be
311
+ # removed.
312
+ # Returns:: the element that was removed.
313
+ # doc.delete_element "/a/b/c[@id='4']"
314
+ # doc.delete_element doc.elements["//k"]
315
+ # doc.delete_element 1
316
+ def delete_element element
317
+ @elements.delete element
318
+ end
319
+
320
+ # Evaluates to +true+ if this element has at least one child Element
321
+ # doc = Document.new "<a><b/><c>Text</c></a>"
322
+ # doc.root.has_elements # -> true
323
+ # doc.elements["/a/b"].has_elements # -> false
324
+ # doc.elements["/a/c"].has_elements # -> false
325
+ def has_elements?
326
+ !@elements.empty?
327
+ end
328
+
329
+ # Iterates through the child elements, yielding for each Element that
330
+ # has a particular attribute set.
331
+ # key::
332
+ # the name of the attribute to search for
333
+ # value::
334
+ # the value of the attribute
335
+ # max::
336
+ # (optional) causes this method to return after yielding
337
+ # for this number of matching children
338
+ # name::
339
+ # (optional) if supplied, this is an XPath that filters
340
+ # the children to check.
341
+ #
342
+ # doc = Document.new "<a><b @id='1'/><c @id='2'/><d @id='1'/><e/></a>"
343
+ # # Yields b, c, d
344
+ # doc.root.each_element_with_attribute( 'id' ) {|e| p e}
345
+ # # Yields b, d
346
+ # doc.root.each_element_with_attribute( 'id', '1' ) {|e| p e}
347
+ # # Yields b
348
+ # doc.root.each_element_with_attribute( 'id', '1', 1 ) {|e| p e}
349
+ # # Yields d
350
+ # doc.root.each_element_with_attribute( 'id', '1', 0, 'd' ) {|e| p e}
351
+ def each_element_with_attribute( key, value=nil, max=0, name=nil, &block ) # :yields: Element
352
+ each_with_something( proc {|child|
353
+ if value.nil?
354
+ child.attributes[key] != nil
355
+ else
356
+ child.attributes[key]==value
357
+ end
358
+ }, max, name, &block )
359
+ end
360
+
361
+ # Iterates through the children, yielding for each Element that
362
+ # has a particular text set.
363
+ # text::
364
+ # the text to search for. If nil, or not supplied, will iterate
365
+ # over all +Element+ children that contain at least one +Text+ node.
366
+ # max::
367
+ # (optional) causes this method to return after yielding
368
+ # for this number of matching children
369
+ # name::
370
+ # (optional) if supplied, this is an XPath that filters
371
+ # the children to check.
372
+ #
373
+ # doc = Document.new '<a><b>b</b><c>b</c><d>d</d><e/></a>'
374
+ # # Yields b, c, d
375
+ # doc.each_element_with_text {|e|p e}
376
+ # # Yields b, c
377
+ # doc.each_element_with_text('b'){|e|p e}
378
+ # # Yields b
379
+ # doc.each_element_with_text('b', 1){|e|p e}
380
+ # # Yields d
381
+ # doc.each_element_with_text(nil, 0, 'd'){|e|p e}
382
+ def each_element_with_text( text=nil, max=0, name=nil, &block ) # :yields: Element
383
+ each_with_something( proc {|child|
384
+ if text.nil?
385
+ child.has_text?
386
+ else
387
+ child.text == text
388
+ end
389
+ }, max, name, &block )
390
+ end
391
+
392
+ # Synonym for Element.elements.each
393
+ def each_element( xpath=nil, &block ) # :yields: Element
394
+ @elements.each( xpath, &block )
395
+ end
396
+
397
+ # Synonym for Element.to_a
398
+ # This is a little slower than calling elements.each directly.
399
+ # xpath:: any XPath by which to search for elements in the tree
400
+ # Returns:: an array of Elements that match the supplied path
401
+ def get_elements( xpath )
402
+ @elements.to_a( xpath )
403
+ end
404
+
405
+ # Returns the next sibling that is an element, or nil if there is
406
+ # no Element sibling after this one
407
+ # doc = Document.new '<a><b/>text<c/></a>'
408
+ # doc.root.elements['b'].next_element #-> <c/>
409
+ # doc.root.elements['c'].next_element #-> nil
410
+ def next_element
411
+ element = next_sibling
412
+ element = element.next_sibling until element.nil? or element.kind_of? Element
413
+ return element
414
+ end
415
+
416
+ # Returns the previous sibling that is an element, or nil if there is
417
+ # no Element sibling prior to this one
418
+ # doc = Document.new '<a><b/>text<c/></a>'
419
+ # doc.root.elements['c'].previous_element #-> <b/>
420
+ # doc.root.elements['b'].previous_element #-> nil
421
+ def previous_element
422
+ element = previous_sibling
423
+ element = element.previous_sibling until element.nil? or element.kind_of? Element
424
+ return element
425
+ end
426
+
427
+
428
+ #################################################
429
+ # Text #
430
+ #################################################
431
+
432
+ # Evaluates to +true+ if this element has at least one Text child
433
+ def has_text?
434
+ not text().nil?
435
+ end
436
+
437
+ # A convenience method which returns the String value of the _first_
438
+ # child text element, if one exists, and +nil+ otherwise.
439
+ #
440
+ # <em>Note that an element may have multiple Text elements, perhaps
441
+ # separated by other children</em>. Be aware that this method only returns
442
+ # the first Text node.
443
+ #
444
+ # This method returns the +value+ of the first text child node, which
445
+ # ignores the +raw+ setting, so always returns normalized text. See
446
+ # the Text::value documentation.
447
+ #
448
+ # doc = Document.new "<p>some text <b>this is bold!</b> more text</p>"
449
+ # # The element 'p' has two text elements, "some text " and " more text".
450
+ # doc.root.text #-> "some text "
451
+ def text( path = nil )
452
+ rv = get_text(path)
453
+ return rv.value unless rv.nil?
454
+ nil
455
+ end
456
+
457
+ # Returns the first child Text node, if any, or +nil+ otherwise.
458
+ # This method returns the actual +Text+ node, rather than the String content.
459
+ # doc = Document.new "<p>some text <b>this is bold!</b> more text</p>"
460
+ # # The element 'p' has two text elements, "some text " and " more text".
461
+ # doc.root.get_text.value #-> "some text "
462
+ def get_text path = nil
463
+ rv = nil
464
+ if path
465
+ element = @elements[ path ]
466
+ rv = element.get_text unless element.nil?
467
+ else
468
+ rv = @children.find { |node| node.kind_of? Text }
469
+ end
470
+ return rv
471
+ end
472
+
473
+ # Sets the first Text child of this object. See text() for a
474
+ # discussion about Text children.
475
+ #
476
+ # If a Text child already exists, the child is replaced by this
477
+ # content. This means that Text content can be deleted by calling
478
+ # this method with a nil argument. In this case, the next Text
479
+ # child becomes the first Text child. In no case is the order of
480
+ # any siblings disturbed.
481
+ # text::
482
+ # If a String, a new Text child is created and added to
483
+ # this Element as the first Text child. If Text, the text is set
484
+ # as the first Child element. If nil, then any existing first Text
485
+ # child is removed.
486
+ # Returns:: this Element.
487
+ # doc = Document.new '<a><b/></a>'
488
+ # doc.root.text = 'Sean' #-> '<a><b/>Sean</a>'
489
+ # doc.root.text = 'Elliott' #-> '<a><b/>Elliott</a>'
490
+ # doc.root.add_element 'c' #-> '<a><b/>Elliott<c/></a>'
491
+ # doc.root.text = 'Russell' #-> '<a><b/>Russell<c/></a>'
492
+ # doc.root.text = nil #-> '<a><b/><c/></a>'
493
+ def text=( text )
494
+ if text.kind_of? String
495
+ text = Text.new( text, whitespace(), nil, raw() )
496
+ elsif !text.nil? and !text.kind_of? Text
497
+ text = Text.new( text.to_s, whitespace(), nil, raw() )
498
+ end
499
+ old_text = get_text
500
+ if text.nil?
501
+ old_text.remove unless old_text.nil?
502
+ else
503
+ if old_text.nil?
504
+ self << text
505
+ else
506
+ old_text.replace_with( text )
507
+ end
508
+ end
509
+ return self
510
+ end
511
+
512
+ # A helper method to add a Text child. Actual Text instances can
513
+ # be added with regular Parent methods, such as add() and <<()
514
+ # text::
515
+ # if a String, a new Text instance is created and added
516
+ # to the parent. If Text, the object is added directly.
517
+ # Returns:: this Element
518
+ # e = Element.new('a') #-> <e/>
519
+ # e.add_text 'foo' #-> <e>foo</e>
520
+ # e.add_text Text.new(' bar') #-> <e>foo bar</e>
521
+ # Note that at the end of this example, the branch has <b>3</b> nodes; the 'e'
522
+ # element and <b>2</b> Text node children.
523
+ def add_text( text )
524
+ if text.kind_of? String
525
+ if @children[-1].kind_of? Text
526
+ @children[-1] << text
527
+ return
528
+ end
529
+ text = Text.new( text, whitespace(), nil, raw() )
530
+ end
531
+ self << text unless text.nil?
532
+ return self
533
+ end
534
+
535
+ def node_type
536
+ :element
537
+ end
538
+
539
+ def xpath
540
+ path_elements = []
541
+ cur = self
542
+ path_elements << __to_xpath_helper( self )
543
+ while cur.parent
544
+ cur = cur.parent
545
+ path_elements << __to_xpath_helper( cur )
546
+ end
547
+ return path_elements.reverse.join( "/" )
548
+ end
549
+
550
+ #################################################
551
+ # Attributes #
552
+ #################################################
553
+
554
+ def attribute( name, namespace=nil )
555
+ prefix = nil
556
+ if namespaces.respond_to? :key
557
+ prefix = namespaces.key(namespace) if namespace
558
+ else
559
+ prefix = namespaces.index(namespace) if namespace
560
+ end
561
+ prefix = nil if prefix == 'xmlns'
562
+
563
+ ret_val =
564
+ attributes.get_attribute( "#{prefix ? prefix + ':' : ''}#{name}" )
565
+
566
+ return ret_val unless ret_val.nil?
567
+ return nil if prefix.nil?
568
+
569
+ # now check that prefix'es namespace is not the same as the
570
+ # default namespace
571
+ return nil unless ( namespaces[ prefix ] == namespaces[ 'xmlns' ] )
572
+
573
+ attributes.get_attribute( name )
574
+
575
+ end
576
+
577
+ # Evaluates to +true+ if this element has any attributes set, false
578
+ # otherwise.
579
+ def has_attributes?
580
+ return !@attributes.empty?
581
+ end
582
+
583
+ # Adds an attribute to this element, overwriting any existing attribute
584
+ # by the same name.
585
+ # key::
586
+ # can be either an Attribute or a String. If an Attribute,
587
+ # the attribute is added to the list of Element attributes. If String,
588
+ # the argument is used as the name of the new attribute, and the value
589
+ # parameter must be supplied.
590
+ # value::
591
+ # Required if +key+ is a String, and ignored if the first argument is
592
+ # an Attribute. This is a String, and is used as the value
593
+ # of the new Attribute. This should be the unnormalized value of the
594
+ # attribute (without entities).
595
+ # Returns:: the Attribute added
596
+ # e = Element.new 'e'
597
+ # e.add_attribute( 'a', 'b' ) #-> <e a='b'/>
598
+ # e.add_attribute( 'x:a', 'c' ) #-> <e a='b' x:a='c'/>
599
+ # e.add_attribute Attribute.new('b', 'd') #-> <e a='b' x:a='c' b='d'/>
600
+ def add_attribute( key, value=nil )
601
+ if key.kind_of? Attribute
602
+ @attributes << key
603
+ else
604
+ @attributes[key] = value
605
+ end
606
+ end
607
+
608
+ # Add multiple attributes to this element.
609
+ # hash:: is either a hash, or array of arrays
610
+ # el.add_attributes( {"name1"=>"value1", "name2"=>"value2"} )
611
+ # el.add_attributes( [ ["name1","value1"], ["name2"=>"value2"] ] )
612
+ def add_attributes hash
613
+ if hash.kind_of? Hash
614
+ hash.each_pair {|key, value| @attributes[key] = value }
615
+ elsif hash.kind_of? Array
616
+ hash.each { |value| @attributes[ value[0] ] = value[1] }
617
+ end
618
+ end
619
+
620
+ # Removes an attribute
621
+ # key::
622
+ # either an Attribute or a String. In either case, the
623
+ # attribute is found by matching the attribute name to the argument,
624
+ # and then removed. If no attribute is found, no action is taken.
625
+ # Returns::
626
+ # the attribute removed, or nil if this Element did not contain
627
+ # a matching attribute
628
+ # e = Element.new('E')
629
+ # e.add_attribute( 'name', 'Sean' ) #-> <E name='Sean'/>
630
+ # r = e.add_attribute( 'sur:name', 'Russell' ) #-> <E name='Sean' sur:name='Russell'/>
631
+ # e.delete_attribute( 'name' ) #-> <E sur:name='Russell'/>
632
+ # e.delete_attribute( r ) #-> <E/>
633
+ def delete_attribute(key)
634
+ attr = @attributes.get_attribute(key)
635
+ attr.remove unless attr.nil?
636
+ end
637
+
638
+ #################################################
639
+ # Other Utilities #
640
+ #################################################
641
+
642
+ # Get an array of all CData children.
643
+ # IMMUTABLE
644
+ def cdatas
645
+ find_all { |child| child.kind_of? CData }.freeze
646
+ end
647
+
648
+ # Get an array of all Comment children.
649
+ # IMMUTABLE
650
+ def comments
651
+ find_all { |child| child.kind_of? Comment }.freeze
652
+ end
653
+
654
+ # Get an array of all Instruction children.
655
+ # IMMUTABLE
656
+ def instructions
657
+ find_all { |child| child.kind_of? Instruction }.freeze
658
+ end
659
+
660
+ # Get an array of all Text children.
661
+ # IMMUTABLE
662
+ def texts
663
+ find_all { |child| child.kind_of? Text }.freeze
664
+ end
665
+
666
+ # == DEPRECATED
667
+ # See REXML::Formatters
668
+ #
669
+ # Writes out this element, and recursively, all children.
670
+ # output::
671
+ # output an object which supports '<< string'; this is where the
672
+ # document will be written.
673
+ # indent::
674
+ # An integer. If -1, no indenting will be used; otherwise, the
675
+ # indentation will be this number of spaces, and children will be
676
+ # indented an additional amount. Defaults to -1
677
+ # transitive::
678
+ # If transitive is true and indent is >= 0, then the output will be
679
+ # pretty-printed in such a way that the added whitespace does not affect
680
+ # the parse tree of the document
681
+ # ie_hack::
682
+ # This hack inserts a space before the /> on empty tags to address
683
+ # a limitation of Internet Explorer. Defaults to false
684
+ #
685
+ # out = ''
686
+ # doc.write( out ) #-> doc is written to the string 'out'
687
+ # doc.write( $stdout ) #-> doc written to the console
688
+ def write(output=$stdout, indent=-1, transitive=false, ie_hack=false)
689
+ Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters")
690
+ formatter = if indent > -1
691
+ if transitive
692
+ require "rexml/formatters/transitive"
693
+ REXML::Formatters::Transitive.new( indent, ie_hack )
694
+ else
695
+ REXML::Formatters::Pretty.new( indent, ie_hack )
696
+ end
697
+ else
698
+ REXML::Formatters::Default.new( ie_hack )
699
+ end
700
+ formatter.write( self, output )
701
+ end
702
+
703
+
704
+ private
705
+ def __to_xpath_helper node
706
+ rv = node.expanded_name.clone
707
+ if node.parent
708
+ results = node.parent.find_all {|n|
709
+ n.kind_of?(REXML::Element) and n.expanded_name == node.expanded_name
710
+ }
711
+ if results.length > 1
712
+ idx = results.index( node )
713
+ rv << "[#{idx+1}]"
714
+ end
715
+ end
716
+ rv
717
+ end
718
+
719
+ # A private helper method
720
+ def each_with_something( test, max=0, name=nil )
721
+ num = 0
722
+ @elements.each( name ){ |child|
723
+ yield child if test.call(child) and num += 1
724
+ return if max>0 and num == max
725
+ }
726
+ end
727
+ end
728
+
729
+ ########################################################################
730
+ # ELEMENTS #
731
+ ########################################################################
732
+
733
+ # A class which provides filtering of children for Elements, and
734
+ # XPath search support. You are expected to only encounter this class as
735
+ # the <tt>element.elements</tt> object. Therefore, you are
736
+ # _not_ expected to instantiate this yourself.
737
+ class Elements
738
+ include Enumerable
739
+ # Constructor
740
+ # parent:: the parent Element
741
+ def initialize parent
742
+ @element = parent
743
+ end
744
+
745
+ # Fetches a child element. Filters only Element children, regardless of
746
+ # the XPath match.
747
+ # index::
748
+ # the search parameter. This is either an Integer, which
749
+ # will be used to find the index'th child Element, or an XPath,
750
+ # which will be used to search for the Element. <em>Because
751
+ # of the nature of XPath searches, any element in the connected XML
752
+ # document can be fetched through any other element.</em> <b>The
753
+ # Integer index is 1-based, not 0-based.</b> This means that the first
754
+ # child element is at index 1, not 0, and the +n+th element is at index
755
+ # +n+, not <tt>n-1</tt>. This is because XPath indexes element children
756
+ # starting from 1, not 0, and the indexes should be the same.
757
+ # name::
758
+ # optional, and only used in the first argument is an
759
+ # Integer. In that case, the index'th child Element that has the
760
+ # supplied name will be returned. Note again that the indexes start at 1.
761
+ # Returns:: the first matching Element, or nil if no child matched
762
+ # doc = Document.new '<a><b/><c id="1"/><c id="2"/><d/></a>'
763
+ # doc.root.elements[1] #-> <b/>
764
+ # doc.root.elements['c'] #-> <c id="1"/>
765
+ # doc.root.elements[2,'c'] #-> <c id="2"/>
766
+ def []( index, name=nil)
767
+ if index.kind_of? Integer
768
+ raise "index (#{index}) must be >= 1" if index < 1
769
+ name = literalize(name) if name
770
+ num = 0
771
+ @element.find { |child|
772
+ child.kind_of? Element and
773
+ (name.nil? ? true : child.has_name?( name )) and
774
+ (num += 1) == index
775
+ }
776
+ else
777
+ return XPath::first( @element, index )
778
+ #{ |element|
779
+ # return element if element.kind_of? Element
780
+ #}
781
+ #return nil
782
+ end
783
+ end
784
+
785
+ # Sets an element, replacing any previous matching element. If no
786
+ # existing element is found ,the element is added.
787
+ # index:: Used to find a matching element to replace. See []().
788
+ # element::
789
+ # The element to replace the existing element with
790
+ # the previous element
791
+ # Returns:: nil if no previous element was found.
792
+ #
793
+ # doc = Document.new '<a/>'
794
+ # doc.root.elements[10] = Element.new('b') #-> <a><b/></a>
795
+ # doc.root.elements[1] #-> <b/>
796
+ # doc.root.elements[1] = Element.new('c') #-> <a><c/></a>
797
+ # doc.root.elements['c'] = Element.new('d') #-> <a><d/></a>
798
+ def []=( index, element )
799
+ previous = self[index]
800
+ if previous.nil?
801
+ @element.add element
802
+ else
803
+ previous.replace_with element
804
+ end
805
+ return previous
806
+ end
807
+
808
+ # Returns +true+ if there are no +Element+ children, +false+ otherwise
809
+ def empty?
810
+ @element.find{ |child| child.kind_of? Element}.nil?
811
+ end
812
+
813
+ # Returns the index of the supplied child (starting at 1), or -1 if
814
+ # the element is not a child
815
+ # element:: an +Element+ child
816
+ def index element
817
+ rv = 0
818
+ found = @element.find do |child|
819
+ child.kind_of? Element and
820
+ (rv += 1) and
821
+ child == element
822
+ end
823
+ return rv if found == element
824
+ return -1
825
+ end
826
+
827
+ # Deletes a child Element
828
+ # element::
829
+ # Either an Element, which is removed directly; an
830
+ # xpath, where the first matching child is removed; or an Integer,
831
+ # where the n'th Element is removed.
832
+ # Returns:: the removed child
833
+ # doc = Document.new '<a><b/><c/><c id="1"/></a>'
834
+ # b = doc.root.elements[1]
835
+ # doc.root.elements.delete b #-> <a><c/><c id="1"/></a>
836
+ # doc.elements.delete("a/c[@id='1']") #-> <a><c/></a>
837
+ # doc.root.elements.delete 1 #-> <a/>
838
+ def delete element
839
+ if element.kind_of? Element
840
+ @element.delete element
841
+ else
842
+ el = self[element]
843
+ el.remove if el
844
+ end
845
+ end
846
+
847
+ # Removes multiple elements. Filters for Element children, regardless of
848
+ # XPath matching.
849
+ # xpath:: all elements matching this String path are removed.
850
+ # Returns:: an Array of Elements that have been removed
851
+ # doc = Document.new '<a><c/><c/><c/><c/></a>'
852
+ # deleted = doc.elements.delete_all 'a/c' #-> [<c/>, <c/>, <c/>, <c/>]
853
+ def delete_all( xpath )
854
+ rv = []
855
+ XPath::each( @element, xpath) {|element|
856
+ rv << element if element.kind_of? Element
857
+ }
858
+ rv.each do |element|
859
+ @element.delete element
860
+ element.remove
861
+ end
862
+ return rv
863
+ end
864
+
865
+ # Adds an element
866
+ # element::
867
+ # if supplied, is either an Element, String, or
868
+ # Source (see Element.initialize). If not supplied or nil, a
869
+ # new, default Element will be constructed
870
+ # Returns:: the added Element
871
+ # a = Element.new('a')
872
+ # a.elements.add(Element.new('b')) #-> <a><b/></a>
873
+ # a.elements.add('c') #-> <a><b/><c/></a>
874
+ def add element=nil
875
+ if element.nil?
876
+ Element.new("", self, @element.context)
877
+ elsif not element.kind_of?(Element)
878
+ Element.new(element, self, @element.context)
879
+ else
880
+ @element << element
881
+ element.context = @element.context
882
+ element
883
+ end
884
+ end
885
+
886
+ alias :<< :add
887
+
888
+ # Iterates through all of the child Elements, optionally filtering
889
+ # them by a given XPath
890
+ # xpath::
891
+ # optional. If supplied, this is a String XPath, and is used to
892
+ # filter the children, so that only matching children are yielded. Note
893
+ # that XPaths are automatically filtered for Elements, so that
894
+ # non-Element children will not be yielded
895
+ # doc = Document.new '<a><b/><c/><d/>sean<b/><c/><d/></a>'
896
+ # doc.root.elements.each {|e|p e} #-> Yields b, c, d, b, c, d elements
897
+ # doc.root.elements.each('b') {|e|p e} #-> Yields b, b elements
898
+ # doc.root.elements.each('child::node()') {|e|p e}
899
+ # #-> Yields <b/>, <c/>, <d/>, <b/>, <c/>, <d/>
900
+ # XPath.each(doc.root, 'child::node()', &block)
901
+ # #-> Yields <b/>, <c/>, <d/>, sean, <b/>, <c/>, <d/>
902
+ def each( xpath=nil )
903
+ XPath::each( @element, xpath ) {|e| yield e if e.kind_of? Element }
904
+ end
905
+
906
+ def collect( xpath=nil )
907
+ collection = []
908
+ XPath::each( @element, xpath ) {|e|
909
+ collection << yield(e) if e.kind_of?(Element)
910
+ }
911
+ collection
912
+ end
913
+
914
+ def inject( xpath=nil, initial=nil )
915
+ first = true
916
+ XPath::each( @element, xpath ) {|e|
917
+ if (e.kind_of? Element)
918
+ if (first and initial == nil)
919
+ initial = e
920
+ first = false
921
+ else
922
+ initial = yield( initial, e ) if e.kind_of? Element
923
+ end
924
+ end
925
+ }
926
+ initial
927
+ end
928
+
929
+ # Returns the number of +Element+ children of the parent object.
930
+ # doc = Document.new '<a>sean<b/>elliott<b/>russell<b/></a>'
931
+ # doc.root.size #-> 6, 3 element and 3 text nodes
932
+ # doc.root.elements.size #-> 3
933
+ def size
934
+ count = 0
935
+ @element.each {|child| count+=1 if child.kind_of? Element }
936
+ count
937
+ end
938
+
939
+ # Returns an Array of Element children. An XPath may be supplied to
940
+ # filter the children. Only Element children are returned, even if the
941
+ # supplied XPath matches non-Element children.
942
+ # doc = Document.new '<a>sean<b/>elliott<c/></a>'
943
+ # doc.root.elements.to_a #-> [ <b/>, <c/> ]
944
+ # doc.root.elements.to_a("child::node()") #-> [ <b/>, <c/> ]
945
+ # XPath.match(doc.root, "child::node()") #-> [ sean, <b/>, elliott, <c/> ]
946
+ def to_a( xpath=nil )
947
+ rv = XPath.match( @element, xpath )
948
+ return rv.find_all{|e| e.kind_of? Element} if xpath
949
+ rv
950
+ end
951
+
952
+ private
953
+ # Private helper class. Removes quotes from quoted strings
954
+ def literalize name
955
+ name = name[1..-2] if name[0] == ?' or name[0] == ?" #'
956
+ name
957
+ end
958
+ end
959
+
960
+ ########################################################################
961
+ # ATTRIBUTES #
962
+ ########################################################################
963
+
964
+ # A class that defines the set of Attributes of an Element and provides
965
+ # operations for accessing elements in that set.
966
+ class Attributes < Hash
967
+ # Constructor
968
+ # element:: the Element of which this is an Attribute
969
+ def initialize element
970
+ @element = element
971
+ end
972
+
973
+ # Fetches an attribute value. If you want to get the Attribute itself,
974
+ # use get_attribute()
975
+ # name:: an XPath attribute name. Namespaces are relevant here.
976
+ # Returns::
977
+ # the String value of the matching attribute, or +nil+ if no
978
+ # matching attribute was found. This is the unnormalized value
979
+ # (with entities expanded).
980
+ #
981
+ # doc = Document.new "<a foo:att='1' bar:att='2' att='&lt;'/>"
982
+ # doc.root.attributes['att'] #-> '<'
983
+ # doc.root.attributes['bar:att'] #-> '2'
984
+ def [](name)
985
+ attr = get_attribute(name)
986
+ return attr.value unless attr.nil?
987
+ return nil
988
+ end
989
+
990
+ def to_a
991
+ enum_for(:each_attribute).to_a
992
+ end
993
+
994
+ # Returns the number of attributes the owning Element contains.
995
+ # doc = Document "<a x='1' y='2' foo:x='3'/>"
996
+ # doc.root.attributes.length #-> 3
997
+ def length
998
+ c = 0
999
+ each_attribute { c+=1 }
1000
+ c
1001
+ end
1002
+ alias :size :length
1003
+
1004
+ # Iterates over the attributes of an Element. Yields actual Attribute
1005
+ # nodes, not String values.
1006
+ #
1007
+ # doc = Document.new '<a x="1" y="2"/>'
1008
+ # doc.root.attributes.each_attribute {|attr|
1009
+ # p attr.expanded_name+" => "+attr.value
1010
+ # }
1011
+ def each_attribute # :yields: attribute
1012
+ each_value do |val|
1013
+ if val.kind_of? Attribute
1014
+ yield val
1015
+ else
1016
+ val.each_value { |atr| yield atr }
1017
+ end
1018
+ end
1019
+ end
1020
+
1021
+ # Iterates over each attribute of an Element, yielding the expanded name
1022
+ # and value as a pair of Strings.
1023
+ #
1024
+ # doc = Document.new '<a x="1" y="2"/>'
1025
+ # doc.root.attributes.each {|name, value| p name+" => "+value }
1026
+ def each
1027
+ each_attribute do |attr|
1028
+ yield [attr.expanded_name, attr.value]
1029
+ end
1030
+ end
1031
+
1032
+ # Fetches an attribute
1033
+ # name::
1034
+ # the name by which to search for the attribute. Can be a
1035
+ # <tt>prefix:name</tt> namespace name.
1036
+ # Returns:: The first matching attribute, or nil if there was none. This
1037
+ # value is an Attribute node, not the String value of the attribute.
1038
+ # doc = Document.new '<a x:foo="1" foo="2" bar="3"/>'
1039
+ # doc.root.attributes.get_attribute("foo").value #-> "2"
1040
+ # doc.root.attributes.get_attribute("x:foo").value #-> "1"
1041
+ def get_attribute( name )
1042
+ attr = fetch( name, nil )
1043
+ if attr.nil?
1044
+ return nil if name.nil?
1045
+ # Look for prefix
1046
+ name =~ Namespace::NAMESPLIT
1047
+ prefix, n = $1, $2
1048
+ if prefix
1049
+ attr = fetch( n, nil )
1050
+ # check prefix
1051
+ if attr == nil
1052
+ elsif attr.kind_of? Attribute
1053
+ return attr if prefix == attr.prefix
1054
+ else
1055
+ attr = attr[ prefix ]
1056
+ return attr
1057
+ end
1058
+ end
1059
+ element_document = @element.document
1060
+ if element_document and element_document.doctype
1061
+ expn = @element.expanded_name
1062
+ expn = element_document.doctype.name if expn.size == 0
1063
+ attr_val = element_document.doctype.attribute_of(expn, name)
1064
+ return Attribute.new( name, attr_val ) if attr_val
1065
+ end
1066
+ return nil
1067
+ end
1068
+ if attr.kind_of? Hash
1069
+ attr = attr[ @element.prefix ]
1070
+ end
1071
+ return attr
1072
+ end
1073
+
1074
+ # Sets an attribute, overwriting any existing attribute value by the
1075
+ # same name. Namespace is significant.
1076
+ # name:: the name of the attribute
1077
+ # value::
1078
+ # (optional) If supplied, the value of the attribute. If
1079
+ # nil, any existing matching attribute is deleted.
1080
+ # Returns::
1081
+ # Owning element
1082
+ # doc = Document.new "<a x:foo='1' foo='3'/>"
1083
+ # doc.root.attributes['y:foo'] = '2'
1084
+ # doc.root.attributes['foo'] = '4'
1085
+ # doc.root.attributes['x:foo'] = nil
1086
+ def []=( name, value )
1087
+ if value.nil? # Delete the named attribute
1088
+ attr = get_attribute(name)
1089
+ delete attr
1090
+ return
1091
+ end
1092
+
1093
+ unless value.kind_of? Attribute
1094
+ if @element.document and @element.document.doctype
1095
+ value = Text::normalize( value, @element.document.doctype )
1096
+ else
1097
+ value = Text::normalize( value, nil )
1098
+ end
1099
+ value = Attribute.new(name, value)
1100
+ end
1101
+ value.element = @element
1102
+ old_attr = fetch(value.name, nil)
1103
+ if old_attr.nil?
1104
+ store(value.name, value)
1105
+ elsif old_attr.kind_of? Hash
1106
+ old_attr[value.prefix] = value
1107
+ elsif old_attr.prefix != value.prefix
1108
+ # Check for conflicting namespaces
1109
+ raise ParseException.new(
1110
+ "Namespace conflict in adding attribute \"#{value.name}\": "+
1111
+ "Prefix \"#{old_attr.prefix}\" = "+
1112
+ "\"#{@element.namespace(old_attr.prefix)}\" and prefix "+
1113
+ "\"#{value.prefix}\" = \"#{@element.namespace(value.prefix)}\"") if
1114
+ value.prefix != "xmlns" and old_attr.prefix != "xmlns" and
1115
+ @element.namespace( old_attr.prefix ) ==
1116
+ @element.namespace( value.prefix )
1117
+ store value.name, { old_attr.prefix => old_attr,
1118
+ value.prefix => value }
1119
+ else
1120
+ store value.name, value
1121
+ end
1122
+ return @element
1123
+ end
1124
+
1125
+ # Returns an array of Strings containing all of the prefixes declared
1126
+ # by this set of # attributes. The array does not include the default
1127
+ # namespace declaration, if one exists.
1128
+ # doc = Document.new("<a xmlns='foo' xmlns:x='bar' xmlns:y='twee' "+
1129
+ # "z='glorp' p:k='gru'/>")
1130
+ # prefixes = doc.root.attributes.prefixes #-> ['x', 'y']
1131
+ def prefixes
1132
+ ns = []
1133
+ each_attribute do |attribute|
1134
+ ns << attribute.name if attribute.prefix == 'xmlns'
1135
+ end
1136
+ if @element.document and @element.document.doctype
1137
+ expn = @element.expanded_name
1138
+ expn = @element.document.doctype.name if expn.size == 0
1139
+ @element.document.doctype.attributes_of(expn).each {
1140
+ |attribute|
1141
+ ns << attribute.name if attribute.prefix == 'xmlns'
1142
+ }
1143
+ end
1144
+ ns
1145
+ end
1146
+
1147
+ def namespaces
1148
+ namespaces = {}
1149
+ each_attribute do |attribute|
1150
+ namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns'
1151
+ end
1152
+ if @element.document and @element.document.doctype
1153
+ expn = @element.expanded_name
1154
+ expn = @element.document.doctype.name if expn.size == 0
1155
+ @element.document.doctype.attributes_of(expn).each {
1156
+ |attribute|
1157
+ namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns'
1158
+ }
1159
+ end
1160
+ namespaces
1161
+ end
1162
+
1163
+ # Removes an attribute
1164
+ # attribute::
1165
+ # either a String, which is the name of the attribute to remove --
1166
+ # namespaces are significant here -- or the attribute to remove.
1167
+ # Returns:: the owning element
1168
+ # doc = Document.new "<a y:foo='0' x:foo='1' foo='3' z:foo='4'/>"
1169
+ # doc.root.attributes.delete 'foo' #-> <a y:foo='0' x:foo='1' z:foo='4'/>"
1170
+ # doc.root.attributes.delete 'x:foo' #-> <a y:foo='0' z:foo='4'/>"
1171
+ # attr = doc.root.attributes.get_attribute('y:foo')
1172
+ # doc.root.attributes.delete attr #-> <a z:foo='4'/>"
1173
+ def delete( attribute )
1174
+ name = nil
1175
+ prefix = nil
1176
+ if attribute.kind_of? Attribute
1177
+ name = attribute.name
1178
+ prefix = attribute.prefix
1179
+ else
1180
+ attribute =~ Namespace::NAMESPLIT
1181
+ prefix, name = $1, $2
1182
+ prefix = '' unless prefix
1183
+ end
1184
+ old = fetch(name, nil)
1185
+ if old.kind_of? Hash # the supplied attribute is one of many
1186
+ old.delete(prefix)
1187
+ if old.size == 1
1188
+ repl = nil
1189
+ old.each_value{|v| repl = v}
1190
+ store name, repl
1191
+ end
1192
+ elsif old.nil?
1193
+ return @element
1194
+ else # the supplied attribute is a top-level one
1195
+ super(name)
1196
+ end
1197
+ @element
1198
+ end
1199
+
1200
+ # Adds an attribute, overriding any existing attribute by the
1201
+ # same name. Namespaces are significant.
1202
+ # attribute:: An Attribute
1203
+ def add( attribute )
1204
+ self[attribute.name] = attribute
1205
+ end
1206
+
1207
+ alias :<< :add
1208
+
1209
+ # Deletes all attributes matching a name. Namespaces are significant.
1210
+ # name::
1211
+ # A String; all attributes that match this path will be removed
1212
+ # Returns:: an Array of the Attributes that were removed
1213
+ def delete_all( name )
1214
+ rv = []
1215
+ each_attribute { |attribute|
1216
+ rv << attribute if attribute.expanded_name == name
1217
+ }
1218
+ rv.each{ |attr| attr.remove }
1219
+ return rv
1220
+ end
1221
+
1222
+ # The +get_attribute_ns+ method retrieves a method by its namespace
1223
+ # and name. Thus it is possible to reliably identify an attribute
1224
+ # even if an XML processor has changed the prefix.
1225
+ #
1226
+ # Method contributed by Henrik Martensson
1227
+ def get_attribute_ns(namespace, name)
1228
+ result = nil
1229
+ each_attribute() { |attribute|
1230
+ if name == attribute.name &&
1231
+ namespace == attribute.namespace() &&
1232
+ ( !namespace.empty? || !attribute.fully_expanded_name.index(':') )
1233
+ # foo will match xmlns:foo, but only if foo isn't also an attribute
1234
+ result = attribute if !result or !namespace.empty? or
1235
+ !attribute.fully_expanded_name.index(':')
1236
+ end
1237
+ }
1238
+ result
1239
+ end
1240
+ end
1241
+ end