rubysl-rexml 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +25 -0
  6. data/README.md +29 -0
  7. data/Rakefile +1 -0
  8. data/lib/rexml/attlistdecl.rb +62 -0
  9. data/lib/rexml/attribute.rb +185 -0
  10. data/lib/rexml/cdata.rb +67 -0
  11. data/lib/rexml/child.rb +96 -0
  12. data/lib/rexml/comment.rb +80 -0
  13. data/lib/rexml/doctype.rb +271 -0
  14. data/lib/rexml/document.rb +230 -0
  15. data/lib/rexml/dtd/attlistdecl.rb +10 -0
  16. data/lib/rexml/dtd/dtd.rb +51 -0
  17. data/lib/rexml/dtd/elementdecl.rb +17 -0
  18. data/lib/rexml/dtd/entitydecl.rb +56 -0
  19. data/lib/rexml/dtd/notationdecl.rb +39 -0
  20. data/lib/rexml/element.rb +1227 -0
  21. data/lib/rexml/encoding.rb +71 -0
  22. data/lib/rexml/encodings/CP-1252.rb +103 -0
  23. data/lib/rexml/encodings/EUC-JP.rb +35 -0
  24. data/lib/rexml/encodings/ICONV.rb +22 -0
  25. data/lib/rexml/encodings/ISO-8859-1.rb +7 -0
  26. data/lib/rexml/encodings/ISO-8859-15.rb +72 -0
  27. data/lib/rexml/encodings/SHIFT-JIS.rb +37 -0
  28. data/lib/rexml/encodings/SHIFT_JIS.rb +1 -0
  29. data/lib/rexml/encodings/UNILE.rb +34 -0
  30. data/lib/rexml/encodings/US-ASCII.rb +30 -0
  31. data/lib/rexml/encodings/UTF-16.rb +35 -0
  32. data/lib/rexml/encodings/UTF-8.rb +18 -0
  33. data/lib/rexml/entity.rb +166 -0
  34. data/lib/rexml/formatters/default.rb +109 -0
  35. data/lib/rexml/formatters/pretty.rb +138 -0
  36. data/lib/rexml/formatters/transitive.rb +56 -0
  37. data/lib/rexml/functions.rb +382 -0
  38. data/lib/rexml/instruction.rb +70 -0
  39. data/lib/rexml/light/node.rb +196 -0
  40. data/lib/rexml/namespace.rb +47 -0
  41. data/lib/rexml/node.rb +75 -0
  42. data/lib/rexml/output.rb +24 -0
  43. data/lib/rexml/parent.rb +166 -0
  44. data/lib/rexml/parseexception.rb +51 -0
  45. data/lib/rexml/parsers/baseparser.rb +503 -0
  46. data/lib/rexml/parsers/lightparser.rb +60 -0
  47. data/lib/rexml/parsers/pullparser.rb +196 -0
  48. data/lib/rexml/parsers/sax2parser.rb +238 -0
  49. data/lib/rexml/parsers/streamparser.rb +46 -0
  50. data/lib/rexml/parsers/treeparser.rb +97 -0
  51. data/lib/rexml/parsers/ultralightparser.rb +56 -0
  52. data/lib/rexml/parsers/xpathparser.rb +698 -0
  53. data/lib/rexml/quickpath.rb +266 -0
  54. data/lib/rexml/rexml.rb +32 -0
  55. data/lib/rexml/sax2listener.rb +97 -0
  56. data/lib/rexml/source.rb +251 -0
  57. data/lib/rexml/streamlistener.rb +92 -0
  58. data/lib/rexml/syncenumerator.rb +33 -0
  59. data/lib/rexml/text.rb +344 -0
  60. data/lib/rexml/undefinednamespaceexception.rb +8 -0
  61. data/lib/rexml/validation/relaxng.rb +559 -0
  62. data/lib/rexml/validation/validation.rb +155 -0
  63. data/lib/rexml/validation/validationexception.rb +9 -0
  64. data/lib/rexml/xmldecl.rb +119 -0
  65. data/lib/rexml/xmltokens.rb +18 -0
  66. data/lib/rexml/xpath.rb +66 -0
  67. data/lib/rexml/xpath_parser.rb +792 -0
  68. data/lib/rubysl/rexml.rb +1 -0
  69. data/lib/rubysl/rexml/version.rb +5 -0
  70. data/rubysl-rexml.gemspec +23 -0
  71. data/spec/attribute/clone_spec.rb +10 -0
  72. data/spec/attribute/element_spec.rb +22 -0
  73. data/spec/attribute/equal_value_spec.rb +17 -0
  74. data/spec/attribute/hash_spec.rb +12 -0
  75. data/spec/attribute/initialize_spec.rb +28 -0
  76. data/spec/attribute/inspect_spec.rb +19 -0
  77. data/spec/attribute/namespace_spec.rb +23 -0
  78. data/spec/attribute/node_type_spec.rb +9 -0
  79. data/spec/attribute/prefix_spec.rb +17 -0
  80. data/spec/attribute/remove_spec.rb +19 -0
  81. data/spec/attribute/to_s_spec.rb +13 -0
  82. data/spec/attribute/to_string_spec.rb +14 -0
  83. data/spec/attribute/value_spec.rb +14 -0
  84. data/spec/attribute/write_spec.rb +22 -0
  85. data/spec/attribute/xpath_spec.rb +19 -0
  86. data/spec/attributes/add_spec.rb +6 -0
  87. data/spec/attributes/append_spec.rb +6 -0
  88. data/spec/attributes/delete_all_spec.rb +30 -0
  89. data/spec/attributes/delete_spec.rb +26 -0
  90. data/spec/attributes/each_attribute_spec.rb +24 -0
  91. data/spec/attributes/each_spec.rb +24 -0
  92. data/spec/attributes/element_reference_spec.rb +18 -0
  93. data/spec/attributes/element_set_spec.rb +25 -0
  94. data/spec/attributes/get_attribute_ns_spec.rb +13 -0
  95. data/spec/attributes/get_attribute_spec.rb +28 -0
  96. data/spec/attributes/initialize_spec.rb +18 -0
  97. data/spec/attributes/length_spec.rb +6 -0
  98. data/spec/attributes/namespaces_spec.rb +5 -0
  99. data/spec/attributes/prefixes_spec.rb +23 -0
  100. data/spec/attributes/shared/add.rb +17 -0
  101. data/spec/attributes/shared/length.rb +12 -0
  102. data/spec/attributes/size_spec.rb +6 -0
  103. data/spec/attributes/to_a_spec.rb +20 -0
  104. data/spec/cdata/clone_spec.rb +9 -0
  105. data/spec/cdata/initialize_spec.rb +24 -0
  106. data/spec/cdata/shared/to_s.rb +11 -0
  107. data/spec/cdata/to_s_spec.rb +6 -0
  108. data/spec/cdata/value_spec.rb +6 -0
  109. data/spec/document/add_element_spec.rb +30 -0
  110. data/spec/document/add_spec.rb +60 -0
  111. data/spec/document/clone_spec.rb +19 -0
  112. data/spec/document/doctype_spec.rb +14 -0
  113. data/spec/document/encoding_spec.rb +21 -0
  114. data/spec/document/expanded_name_spec.rb +15 -0
  115. data/spec/document/new_spec.rb +37 -0
  116. data/spec/document/node_type_spec.rb +7 -0
  117. data/spec/document/root_spec.rb +11 -0
  118. data/spec/document/stand_alone_spec.rb +18 -0
  119. data/spec/document/version_spec.rb +13 -0
  120. data/spec/document/write_spec.rb +38 -0
  121. data/spec/document/xml_decl_spec.rb +14 -0
  122. data/spec/element/add_attribute_spec.rb +40 -0
  123. data/spec/element/add_attributes_spec.rb +21 -0
  124. data/spec/element/add_element_spec.rb +38 -0
  125. data/spec/element/add_namespace_spec.rb +23 -0
  126. data/spec/element/add_text_spec.rb +23 -0
  127. data/spec/element/attribute_spec.rb +16 -0
  128. data/spec/element/attributes_spec.rb +18 -0
  129. data/spec/element/cdatas_spec.rb +23 -0
  130. data/spec/element/clone_spec.rb +28 -0
  131. data/spec/element/comments_spec.rb +20 -0
  132. data/spec/element/delete_attribute_spec.rb +38 -0
  133. data/spec/element/delete_element_spec.rb +50 -0
  134. data/spec/element/delete_namespace_spec.rb +24 -0
  135. data/spec/element/document_spec.rb +17 -0
  136. data/spec/element/each_element_with_attribute_spec.rb +34 -0
  137. data/spec/element/each_element_with_text_spec.rb +30 -0
  138. data/spec/element/get_text_spec.rb +17 -0
  139. data/spec/element/has_attributes_spec.rb +16 -0
  140. data/spec/element/has_elements_spec.rb +17 -0
  141. data/spec/element/has_text_spec.rb +15 -0
  142. data/spec/element/inspect_spec.rb +26 -0
  143. data/spec/element/instructions_spec.rb +20 -0
  144. data/spec/element/namespace_spec.rb +26 -0
  145. data/spec/element/namespaces_spec.rb +31 -0
  146. data/spec/element/new_spec.rb +34 -0
  147. data/spec/element/next_element_spec.rb +18 -0
  148. data/spec/element/node_type_spec.rb +7 -0
  149. data/spec/element/prefixes_spec.rb +22 -0
  150. data/spec/element/previous_element_spec.rb +19 -0
  151. data/spec/element/raw_spec.rb +23 -0
  152. data/spec/element/root_spec.rb +27 -0
  153. data/spec/element/text_spec.rb +45 -0
  154. data/spec/element/texts_spec.rb +15 -0
  155. data/spec/element/whitespace_spec.rb +22 -0
  156. data/spec/node/each_recursive_spec.rb +20 -0
  157. data/spec/node/find_first_recursive_spec.rb +24 -0
  158. data/spec/node/index_in_parent_spec.rb +14 -0
  159. data/spec/node/next_sibling_node_spec.rb +20 -0
  160. data/spec/node/parent_spec.rb +20 -0
  161. data/spec/node/previous_sibling_node_spec.rb +20 -0
  162. data/spec/shared/each_element.rb +35 -0
  163. data/spec/shared/elements_to_a.rb +35 -0
  164. data/spec/text/append_spec.rb +9 -0
  165. data/spec/text/clone_spec.rb +9 -0
  166. data/spec/text/comparison_spec.rb +24 -0
  167. data/spec/text/empty_spec.rb +11 -0
  168. data/spec/text/indent_text_spec.rb +23 -0
  169. data/spec/text/inspect_spec.rb +7 -0
  170. data/spec/text/new_spec.rb +48 -0
  171. data/spec/text/node_type_spec.rb +7 -0
  172. data/spec/text/normalize_spec.rb +7 -0
  173. data/spec/text/read_with_substitution_spec.rb +12 -0
  174. data/spec/text/to_s_spec.rb +17 -0
  175. data/spec/text/unnormalize_spec.rb +7 -0
  176. data/spec/text/value_spec.rb +36 -0
  177. data/spec/text/wrap_spec.rb +20 -0
  178. data/spec/text/write_with_substitution_spec.rb +32 -0
  179. metadata +385 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e77bc3d878fd21ec47c298c95206f0a123b0ecdd
4
+ data.tar.gz: a737cd6cf7249364bb1d9e0b21f509440dbeefeb
5
+ SHA512:
6
+ metadata.gz: 8223bf80e756128360ce2d16e933e2a0d26c072e60f79a0eef1806711dbe375483a9680212390c7bb3dff1fe66a3aa4085ce5fc92729b6913937c79c8a89ebea
7
+ data.tar.gz: 14b5508fa353ddb7ae50ba4d72c482f30f75d2e79595f5eea54ea0f5aecc9663811b323a9599d79e6af23772045e1c2bf24f320d9a8c028b5c73a8f4df3c1e64
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem update --system
4
+ - gem --version
5
+ - gem install rubysl-bundler
6
+ script: bundle exec mspec spec
7
+ rvm:
8
+ - rbx-nightly-18mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubysl-rexml.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2013, Brian Shirai
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ 3. Neither the name of the library nor the names of its contributors may be
13
+ used to endorse or promote products derived from this software without
14
+ specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
20
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,29 @@
1
+ # Rubysl::Rexml
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rubysl-rexml'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubysl-rexml
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,62 @@
1
+ #vim:ts=2 sw=2 noexpandtab:
2
+ require 'rexml/child'
3
+ require 'rexml/source'
4
+
5
+ module REXML
6
+ # This class needs:
7
+ # * Documentation
8
+ # * Work! Not all types of attlists are intelligently parsed, so we just
9
+ # spew back out what we get in. This works, but it would be better if
10
+ # we formatted the output ourselves.
11
+ #
12
+ # AttlistDecls provide *just* enough support to allow namespace
13
+ # declarations. If you need some sort of generalized support, or have an
14
+ # interesting idea about how to map the hideous, terrible design of DTD
15
+ # AttlistDecls onto an intuitive Ruby interface, let me know. I'm desperate
16
+ # for anything to make DTDs more palateable.
17
+ class AttlistDecl < Child
18
+ include Enumerable
19
+
20
+ # What is this? Got me.
21
+ attr_reader :element_name
22
+
23
+ # Create an AttlistDecl, pulling the information from a Source. Notice
24
+ # that this isn't very convenient; to create an AttlistDecl, you basically
25
+ # have to format it yourself, and then have the initializer parse it.
26
+ # Sorry, but for the forseeable future, DTD support in REXML is pretty
27
+ # weak on convenience. Have I mentioned how much I hate DTDs?
28
+ def initialize(source)
29
+ super()
30
+ if (source.kind_of? Array)
31
+ @element_name, @pairs, @contents = *source
32
+ end
33
+ end
34
+
35
+ # Access the attlist attribute/value pairs.
36
+ # value = attlist_decl[ attribute_name ]
37
+ def [](key)
38
+ @pairs[key]
39
+ end
40
+
41
+ # Whether an attlist declaration includes the given attribute definition
42
+ # if attlist_decl.include? "xmlns:foobar"
43
+ def include?(key)
44
+ @pairs.keys.include? key
45
+ end
46
+
47
+ # Iterate over the key/value pairs:
48
+ # attlist_decl.each { |attribute_name, attribute_value| ... }
49
+ def each(&block)
50
+ @pairs.each(&block)
51
+ end
52
+
53
+ # Write out exactly what we got in.
54
+ def write out, indent=-1
55
+ out << @contents
56
+ end
57
+
58
+ def node_type
59
+ :attlistdecl
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,185 @@
1
+ require "rexml/namespace"
2
+ require 'rexml/text'
3
+
4
+ module REXML
5
+ # Defines an Element Attribute; IE, a attribute=value pair, as in:
6
+ # <element attribute="value"/>. Attributes can be in their own
7
+ # namespaces. General users of REXML will not interact with the
8
+ # Attribute class much.
9
+ class Attribute
10
+ include Node
11
+ include Namespace
12
+
13
+ # The element to which this attribute belongs
14
+ attr_reader :element
15
+ # The normalized value of this attribute. That is, the attribute with
16
+ # entities intact.
17
+ attr_writer :normalized
18
+ PATTERN = /\s*(#{NAME_STR})\s*=\s*(["'])(.*?)\2/um
19
+
20
+ # Constructor.
21
+ # FIXME: The parser doesn't catch illegal characters in attributes
22
+ #
23
+ # first::
24
+ # Either: an Attribute, which this new attribute will become a
25
+ # clone of; or a String, which is the name of this attribute
26
+ # second::
27
+ # If +first+ is an Attribute, then this may be an Element, or nil.
28
+ # If nil, then the Element parent of this attribute is the parent
29
+ # of the +first+ Attribute. If the first argument is a String,
30
+ # then this must also be a String, and is the content of the attribute.
31
+ # If this is the content, it must be fully normalized (contain no
32
+ # illegal characters).
33
+ # parent::
34
+ # Ignored unless +first+ is a String; otherwise, may be the Element
35
+ # parent of this attribute, or nil.
36
+ #
37
+ #
38
+ # Attribute.new( attribute_to_clone )
39
+ # Attribute.new( attribute_to_clone, parent_element )
40
+ # Attribute.new( "attr", "attr_value" )
41
+ # Attribute.new( "attr", "attr_value", parent_element )
42
+ def initialize( first, second=nil, parent=nil )
43
+ @normalized = @unnormalized = @element = nil
44
+ if first.kind_of? Attribute
45
+ self.name = first.expanded_name
46
+ @unnormalized = first.value
47
+ if second.kind_of? Element
48
+ @element = second
49
+ else
50
+ @element = first.element
51
+ end
52
+ elsif first.kind_of? String
53
+ @element = parent
54
+ self.name = first
55
+ @normalized = second.to_s
56
+ else
57
+ raise "illegal argument #{first.class.name} to Attribute constructor"
58
+ end
59
+ end
60
+
61
+ # Returns the namespace of the attribute.
62
+ #
63
+ # e = Element.new( "elns:myelement" )
64
+ # e.add_attribute( "nsa:a", "aval" )
65
+ # e.add_attribute( "b", "bval" )
66
+ # e.attributes.get_attribute( "a" ).prefix # -> "nsa"
67
+ # e.attributes.get_attribute( "b" ).prefix # -> "elns"
68
+ # a = Attribute.new( "x", "y" )
69
+ # a.prefix # -> ""
70
+ def prefix
71
+ pf = super
72
+ if pf == ""
73
+ pf = @element.prefix if @element
74
+ end
75
+ pf
76
+ end
77
+
78
+ # Returns the namespace URL, if defined, or nil otherwise
79
+ #
80
+ # e = Element.new("el")
81
+ # e.add_attributes({"xmlns:ns", "http://url"})
82
+ # e.namespace( "ns" ) # -> "http://url"
83
+ def namespace arg=nil
84
+ arg = prefix if arg.nil?
85
+ @element.namespace arg
86
+ end
87
+
88
+ # Returns true if other is an Attribute and has the same name and value,
89
+ # false otherwise.
90
+ def ==( other )
91
+ other.kind_of?(Attribute) and other.name==name and other.value==value
92
+ end
93
+
94
+ # Creates (and returns) a hash from both the name and value
95
+ def hash
96
+ name.hash + value.hash
97
+ end
98
+
99
+ # Returns this attribute out as XML source, expanding the name
100
+ #
101
+ # a = Attribute.new( "x", "y" )
102
+ # a.to_string # -> "x='y'"
103
+ # b = Attribute.new( "ns:x", "y" )
104
+ # b.to_string # -> "ns:x='y'"
105
+ def to_string
106
+ if @element and @element.context and @element.context[:attribute_quote] == :quote
107
+ %Q^#@expanded_name="#{to_s().gsub(/"/, '&quote;')}"^
108
+ else
109
+ "#@expanded_name='#{to_s().gsub(/'/, '&apos;')}'"
110
+ end
111
+ end
112
+
113
+ # Returns the attribute value, with entities replaced
114
+ def to_s
115
+ return @normalized if @normalized
116
+
117
+ doctype = nil
118
+ if @element
119
+ doc = @element.document
120
+ doctype = doc.doctype if doc
121
+ end
122
+
123
+ @normalized = Text::normalize( @unnormalized, doctype )
124
+ @unnormalized = nil
125
+ @normalized
126
+ end
127
+
128
+ # Returns the UNNORMALIZED value of this attribute. That is, entities
129
+ # have been expanded to their values
130
+ def value
131
+ return @unnormalized if @unnormalized
132
+ doctype = nil
133
+ if @element
134
+ doc = @element.document
135
+ doctype = doc.doctype if doc
136
+ end
137
+ @unnormalized = Text::unnormalize( @normalized, doctype )
138
+ @normalized = nil
139
+ @unnormalized
140
+ end
141
+
142
+ # Returns a copy of this attribute
143
+ def clone
144
+ Attribute.new self
145
+ end
146
+
147
+ # Sets the element of which this object is an attribute. Normally, this
148
+ # is not directly called.
149
+ #
150
+ # Returns this attribute
151
+ def element=( element )
152
+ @element = element
153
+ self
154
+ end
155
+
156
+ # Removes this Attribute from the tree, and returns true if successfull
157
+ #
158
+ # This method is usually not called directly.
159
+ def remove
160
+ @element.attributes.delete self.name unless @element.nil?
161
+ end
162
+
163
+ # Writes this attribute (EG, puts 'key="value"' to the output)
164
+ def write( output, indent=-1 )
165
+ output << to_string
166
+ end
167
+
168
+ def node_type
169
+ :attribute
170
+ end
171
+
172
+ def inspect
173
+ rv = ""
174
+ write( rv )
175
+ rv
176
+ end
177
+
178
+ def xpath
179
+ path = @element.xpath
180
+ path += "/@#{self.expanded_name}"
181
+ return path
182
+ end
183
+ end
184
+ end
185
+ #vim:ts=2 sw=2 noexpandtab:
@@ -0,0 +1,67 @@
1
+ require "rexml/text"
2
+
3
+ module REXML
4
+ class CData < Text
5
+ START = '<![CDATA['
6
+ STOP = ']]>'
7
+ ILLEGAL = /(\]\]>)/
8
+
9
+ # Constructor. CData is data between <![CDATA[ ... ]]>
10
+ #
11
+ # _Examples_
12
+ # CData.new( source )
13
+ # CData.new( "Here is some CDATA" )
14
+ # CData.new( "Some unprocessed data", respect_whitespace_TF, parent_element )
15
+ def initialize( first, whitespace=true, parent=nil )
16
+ super( first, whitespace, parent, true, true, ILLEGAL )
17
+ end
18
+
19
+ # Make a copy of this object
20
+ #
21
+ # _Examples_
22
+ # c = CData.new( "Some text" )
23
+ # d = c.clone
24
+ # d.to_s # -> "Some text"
25
+ def clone
26
+ CData.new self
27
+ end
28
+
29
+ # Returns the content of this CData object
30
+ #
31
+ # _Examples_
32
+ # c = CData.new( "Some text" )
33
+ # c.to_s # -> "Some text"
34
+ def to_s
35
+ @string
36
+ end
37
+
38
+ def value
39
+ @string
40
+ end
41
+
42
+ # == DEPRECATED
43
+ # See the rexml/formatters package
44
+ #
45
+ # Generates XML output of this object
46
+ #
47
+ # output::
48
+ # Where to write the string. Defaults to $stdout
49
+ # indent::
50
+ # The amount to indent this node by
51
+ # transitive::
52
+ # Ignored
53
+ # ie_hack::
54
+ # Ignored
55
+ #
56
+ # _Examples_
57
+ # c = CData.new( " Some text " )
58
+ # c.write( $stdout ) #-> <![CDATA[ Some text ]]>
59
+ def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
60
+ Kernel.warn( "#{self.class.name}.write is deprecated" )
61
+ indent( output, indent )
62
+ output << START
63
+ output << @string
64
+ output << STOP
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,96 @@
1
+ require "rexml/node"
2
+
3
+ module REXML
4
+ ##
5
+ # A Child object is something contained by a parent, and this class
6
+ # contains methods to support that. Most user code will not use this
7
+ # class directly.
8
+ class Child
9
+ include Node
10
+ attr_reader :parent # The Parent of this object
11
+
12
+ # Constructor. Any inheritors of this class should call super to make
13
+ # sure this method is called.
14
+ # parent::
15
+ # if supplied, the parent of this child will be set to the
16
+ # supplied value, and self will be added to the parent
17
+ def initialize( parent = nil )
18
+ @parent = nil
19
+ # Declare @parent, but don't define it. The next line sets the
20
+ # parent.
21
+ parent.add( self ) if parent
22
+ end
23
+
24
+ # Replaces this object with another object. Basically, calls
25
+ # Parent.replace_child
26
+ #
27
+ # Returns:: self
28
+ def replace_with( child )
29
+ @parent.replace_child( self, child )
30
+ self
31
+ end
32
+
33
+ # Removes this child from the parent.
34
+ #
35
+ # Returns:: self
36
+ def remove
37
+ unless @parent.nil?
38
+ @parent.delete self
39
+ end
40
+ self
41
+ end
42
+
43
+ # Sets the parent of this child to the supplied argument.
44
+ #
45
+ # other::
46
+ # Must be a Parent object. If this object is the same object as the
47
+ # existing parent of this child, no action is taken. Otherwise, this
48
+ # child is removed from the current parent (if one exists), and is added
49
+ # to the new parent.
50
+ # Returns:: The parent added
51
+ def parent=( other )
52
+ return @parent if @parent == other
53
+ @parent.delete self if defined? @parent and @parent
54
+ @parent = other
55
+ end
56
+
57
+ alias :next_sibling :next_sibling_node
58
+ alias :previous_sibling :previous_sibling_node
59
+
60
+ # Sets the next sibling of this child. This can be used to insert a child
61
+ # after some other child.
62
+ # a = Element.new("a")
63
+ # b = a.add_element("b")
64
+ # c = Element.new("c")
65
+ # b.next_sibling = c
66
+ # # => <a><b/><c/></a>
67
+ def next_sibling=( other )
68
+ parent.insert_after self, other
69
+ end
70
+
71
+ # Sets the previous sibling of this child. This can be used to insert a
72
+ # child before some other child.
73
+ # a = Element.new("a")
74
+ # b = a.add_element("b")
75
+ # c = Element.new("c")
76
+ # b.previous_sibling = c
77
+ # # => <a><b/><c/></a>
78
+ def previous_sibling=(other)
79
+ parent.insert_before self, other
80
+ end
81
+
82
+ # Returns:: the document this child belongs to, or nil if this child
83
+ # belongs to no document
84
+ def document
85
+ return parent.document unless parent.nil?
86
+ nil
87
+ end
88
+
89
+ # This doesn't yet handle encodings
90
+ def bytes
91
+ encoding = document.encoding
92
+
93
+ to_s
94
+ end
95
+ end
96
+ end