rexml 3.1.7.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rexml might be problematic. Click here for more details.

Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +10 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +60 -0
  7. data/Rakefile +10 -0
  8. data/bin/console +14 -0
  9. data/bin/setup +8 -0
  10. data/lib/rexml/attlistdecl.rb +63 -0
  11. data/lib/rexml/attribute.rb +192 -0
  12. data/lib/rexml/cdata.rb +68 -0
  13. data/lib/rexml/child.rb +97 -0
  14. data/lib/rexml/comment.rb +80 -0
  15. data/lib/rexml/doctype.rb +270 -0
  16. data/lib/rexml/document.rb +291 -0
  17. data/lib/rexml/dtd/attlistdecl.rb +11 -0
  18. data/lib/rexml/dtd/dtd.rb +47 -0
  19. data/lib/rexml/dtd/elementdecl.rb +18 -0
  20. data/lib/rexml/dtd/entitydecl.rb +57 -0
  21. data/lib/rexml/dtd/notationdecl.rb +40 -0
  22. data/lib/rexml/element.rb +1267 -0
  23. data/lib/rexml/encoding.rb +51 -0
  24. data/lib/rexml/entity.rb +171 -0
  25. data/lib/rexml/formatters/default.rb +112 -0
  26. data/lib/rexml/formatters/pretty.rb +142 -0
  27. data/lib/rexml/formatters/transitive.rb +58 -0
  28. data/lib/rexml/functions.rb +447 -0
  29. data/lib/rexml/instruction.rb +71 -0
  30. data/lib/rexml/light/node.rb +196 -0
  31. data/lib/rexml/namespace.rb +48 -0
  32. data/lib/rexml/node.rb +76 -0
  33. data/lib/rexml/output.rb +30 -0
  34. data/lib/rexml/parent.rb +166 -0
  35. data/lib/rexml/parseexception.rb +52 -0
  36. data/lib/rexml/parsers/baseparser.rb +586 -0
  37. data/lib/rexml/parsers/lightparser.rb +59 -0
  38. data/lib/rexml/parsers/pullparser.rb +197 -0
  39. data/lib/rexml/parsers/sax2parser.rb +273 -0
  40. data/lib/rexml/parsers/streamparser.rb +61 -0
  41. data/lib/rexml/parsers/treeparser.rb +101 -0
  42. data/lib/rexml/parsers/ultralightparser.rb +57 -0
  43. data/lib/rexml/parsers/xpathparser.rb +675 -0
  44. data/lib/rexml/quickpath.rb +266 -0
  45. data/lib/rexml/rexml.rb +32 -0
  46. data/lib/rexml/sax2listener.rb +98 -0
  47. data/lib/rexml/security.rb +28 -0
  48. data/lib/rexml/source.rb +298 -0
  49. data/lib/rexml/streamlistener.rb +93 -0
  50. data/lib/rexml/syncenumerator.rb +33 -0
  51. data/lib/rexml/text.rb +424 -0
  52. data/lib/rexml/undefinednamespaceexception.rb +9 -0
  53. data/lib/rexml/validation/relaxng.rb +539 -0
  54. data/lib/rexml/validation/validation.rb +144 -0
  55. data/lib/rexml/validation/validationexception.rb +10 -0
  56. data/lib/rexml/xmldecl.rb +116 -0
  57. data/lib/rexml/xmltokens.rb +85 -0
  58. data/lib/rexml/xpath.rb +81 -0
  59. data/lib/rexml/xpath_parser.rb +934 -0
  60. data/rexml.gemspec +42 -0
  61. metadata +131 -0
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: false
2
+ require_relative "child"
3
+ require_relative "source"
4
+
5
+ module REXML
6
+ # Represents an XML Instruction; IE, <? ... ?>
7
+ # TODO: Add parent arg (3rd arg) to constructor
8
+ class Instruction < Child
9
+ START = '<\?'
10
+ STOP = '\?>'
11
+
12
+ # target is the "name" of the Instruction; IE, the "tag" in <?tag ...?>
13
+ # content is everything else.
14
+ attr_accessor :target, :content
15
+
16
+ # Constructs a new Instruction
17
+ # @param target can be one of a number of things. If String, then
18
+ # the target of this instruction is set to this. If an Instruction,
19
+ # then the Instruction is shallowly cloned (target and content are
20
+ # copied). If a Source, then the source is scanned and parsed for
21
+ # an Instruction declaration.
22
+ # @param content Must be either a String, or a Parent. Can only
23
+ # be a Parent if the target argument is a Source. Otherwise, this
24
+ # String is set as the content of this instruction.
25
+ def initialize(target, content=nil)
26
+ if target.kind_of? String
27
+ super()
28
+ @target = target
29
+ @content = content
30
+ elsif target.kind_of? Instruction
31
+ super(content)
32
+ @target = target.target
33
+ @content = target.content
34
+ end
35
+ @content.strip! if @content
36
+ end
37
+
38
+ def clone
39
+ Instruction.new self
40
+ end
41
+
42
+ # == DEPRECATED
43
+ # See the rexml/formatters package
44
+ #
45
+ def write writer, indent=-1, transitive=false, ie_hack=false
46
+ Kernel.warn( "#{self.class.name}.write is deprecated", uplevel: 1)
47
+ indent(writer, indent)
48
+ writer << START.sub(/\\/u, '')
49
+ writer << @target
50
+ writer << ' '
51
+ writer << @content
52
+ writer << STOP.sub(/\\/u, '')
53
+ end
54
+
55
+ # @return true if other is an Instruction, and the content and target
56
+ # of the other matches the target and content of this object.
57
+ def ==( other )
58
+ other.kind_of? Instruction and
59
+ other.target == @target and
60
+ other.content == @content
61
+ end
62
+
63
+ def node_type
64
+ :processing_instruction
65
+ end
66
+
67
+ def inspect
68
+ "<?p-i #{target} ...?>"
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,196 @@
1
+ # frozen_string_literal: false
2
+ require_relative '../xmltokens'
3
+
4
+ # [ :element, parent, name, attributes, children* ]
5
+ # a = Node.new
6
+ # a << "B" # => <a>B</a>
7
+ # a.b # => <a>B<b/></a>
8
+ # a.b[1] # => <a>B<b/><b/><a>
9
+ # a.b[1]["x"] = "y" # => <a>B<b/><b x="y"/></a>
10
+ # a.b[0].c # => <a>B<b><c/></b><b x="y"/></a>
11
+ # a.b.c << "D" # => <a>B<b><c>D</c></b><b x="y"/></a>
12
+ module REXML
13
+ module Light
14
+ # Represents a tagged XML element. Elements are characterized by
15
+ # having children, attributes, and names, and can themselves be
16
+ # children.
17
+ class Node
18
+ NAMESPLIT = /^(?:(#{XMLTokens::NCNAME_STR}):)?(#{XMLTokens::NCNAME_STR})/u
19
+ PARENTS = [ :element, :document, :doctype ]
20
+ # Create a new element.
21
+ def initialize node=nil
22
+ @node = node
23
+ if node.kind_of? String
24
+ node = [ :text, node ]
25
+ elsif node.nil?
26
+ node = [ :document, nil, nil ]
27
+ elsif node[0] == :start_element
28
+ node[0] = :element
29
+ elsif node[0] == :start_doctype
30
+ node[0] = :doctype
31
+ elsif node[0] == :start_document
32
+ node[0] = :document
33
+ end
34
+ end
35
+
36
+ def size
37
+ if PARENTS.include? @node[0]
38
+ @node[-1].size
39
+ else
40
+ 0
41
+ end
42
+ end
43
+
44
+ def each
45
+ size.times { |x| yield( at(x+4) ) }
46
+ end
47
+
48
+ def name
49
+ at(2)
50
+ end
51
+
52
+ def name=( name_str, ns=nil )
53
+ pfx = ''
54
+ pfx = "#{prefix(ns)}:" if ns
55
+ _old_put(2, "#{pfx}#{name_str}")
56
+ end
57
+
58
+ def parent=( node )
59
+ _old_put(1,node)
60
+ end
61
+
62
+ def local_name
63
+ namesplit
64
+ @name
65
+ end
66
+
67
+ def local_name=( name_str )
68
+ _old_put( 1, "#@prefix:#{name_str}" )
69
+ end
70
+
71
+ def prefix( namespace=nil )
72
+ prefix_of( self, namespace )
73
+ end
74
+
75
+ def namespace( prefix=prefix() )
76
+ namespace_of( self, prefix )
77
+ end
78
+
79
+ def namespace=( namespace )
80
+ @prefix = prefix( namespace )
81
+ pfx = ''
82
+ pfx = "#@prefix:" if @prefix.size > 0
83
+ _old_put(1, "#{pfx}#@name")
84
+ end
85
+
86
+ def []( reference, ns=nil )
87
+ if reference.kind_of? String
88
+ pfx = ''
89
+ pfx = "#{prefix(ns)}:" if ns
90
+ at(3)["#{pfx}#{reference}"]
91
+ elsif reference.kind_of? Range
92
+ _old_get( Range.new(4+reference.begin, reference.end, reference.exclude_end?) )
93
+ else
94
+ _old_get( 4+reference )
95
+ end
96
+ end
97
+
98
+ def =~( path )
99
+ XPath.match( self, path )
100
+ end
101
+
102
+ # Doesn't handle namespaces yet
103
+ def []=( reference, ns, value=nil )
104
+ if reference.kind_of? String
105
+ value = ns unless value
106
+ at( 3 )[reference] = value
107
+ elsif reference.kind_of? Range
108
+ _old_put( Range.new(3+reference.begin, reference.end, reference.exclude_end?), ns )
109
+ else
110
+ if value
111
+ _old_put( 4+reference, ns, value )
112
+ else
113
+ _old_put( 4+reference, ns )
114
+ end
115
+ end
116
+ end
117
+
118
+ # Append a child to this element, optionally under a provided namespace.
119
+ # The namespace argument is ignored if the element argument is an Element
120
+ # object. Otherwise, the element argument is a string, the namespace (if
121
+ # provided) is the namespace the element is created in.
122
+ def << element
123
+ if node_type() == :text
124
+ at(-1) << element
125
+ else
126
+ newnode = Node.new( element )
127
+ newnode.parent = self
128
+ self.push( newnode )
129
+ end
130
+ at(-1)
131
+ end
132
+
133
+ def node_type
134
+ _old_get(0)
135
+ end
136
+
137
+ def text=( foo )
138
+ replace = at(4).kind_of?(String)? 1 : 0
139
+ self._old_put(4,replace, normalizefoo)
140
+ end
141
+
142
+ def root
143
+ context = self
144
+ context = context.at(1) while context.at(1)
145
+ end
146
+
147
+ def has_name?( name, namespace = '' )
148
+ at(3) == name and namespace() == namespace
149
+ end
150
+
151
+ def children
152
+ self
153
+ end
154
+
155
+ def parent
156
+ at(1)
157
+ end
158
+
159
+ def to_s
160
+
161
+ end
162
+
163
+ private
164
+
165
+ def namesplit
166
+ return if @name.defined?
167
+ at(2) =~ NAMESPLIT
168
+ @prefix = '' || $1
169
+ @name = $2
170
+ end
171
+
172
+ def namespace_of( node, prefix=nil )
173
+ if not prefix
174
+ name = at(2)
175
+ name =~ NAMESPLIT
176
+ prefix = $1
177
+ end
178
+ to_find = 'xmlns'
179
+ to_find = "xmlns:#{prefix}" if not prefix.nil?
180
+ ns = at(3)[ to_find ]
181
+ ns ? ns : namespace_of( @node[0], prefix )
182
+ end
183
+
184
+ def prefix_of( node, namespace=nil )
185
+ if not namespace
186
+ name = node.name
187
+ name =~ NAMESPLIT
188
+ $1
189
+ else
190
+ ns = at(3).find { |k,v| v == namespace }
191
+ ns ? ns : prefix_of( node.parent, namespace )
192
+ end
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: false
2
+ require_relative 'xmltokens'
3
+
4
+ module REXML
5
+ # Adds named attributes to an object.
6
+ module Namespace
7
+ # The name of the object, valid if set
8
+ attr_reader :name, :expanded_name
9
+ # The expanded name of the object, valid if name is set
10
+ attr_accessor :prefix
11
+ include XMLTokens
12
+ NAMESPLIT = /^(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u
13
+
14
+ # Sets the name and the expanded name
15
+ def name=( name )
16
+ @expanded_name = name
17
+ name =~ NAMESPLIT
18
+ if $1
19
+ @prefix = $1
20
+ else
21
+ @prefix = ""
22
+ @namespace = ""
23
+ end
24
+ @name = $2
25
+ end
26
+
27
+ # Compares names optionally WITH namespaces
28
+ def has_name?( other, ns=nil )
29
+ if ns
30
+ return (namespace() == ns and name() == other)
31
+ elsif other.include? ":"
32
+ return fully_expanded_name == other
33
+ else
34
+ return name == other
35
+ end
36
+ end
37
+
38
+ alias :local_name :name
39
+
40
+ # Fully expand the name, even if the prefix wasn't specified in the
41
+ # source file.
42
+ def fully_expanded_name
43
+ ns = prefix
44
+ return "#{ns}:#@name" if ns.size > 0
45
+ return @name
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: false
2
+ require_relative "parseexception"
3
+ require_relative "formatters/pretty"
4
+ require_relative "formatters/default"
5
+
6
+ module REXML
7
+ # Represents a node in the tree. Nodes are never encountered except as
8
+ # superclasses of other objects. Nodes have siblings.
9
+ module Node
10
+ # @return the next sibling (nil if unset)
11
+ def next_sibling_node
12
+ return nil if @parent.nil?
13
+ @parent[ @parent.index(self) + 1 ]
14
+ end
15
+
16
+ # @return the previous sibling (nil if unset)
17
+ def previous_sibling_node
18
+ return nil if @parent.nil?
19
+ ind = @parent.index(self)
20
+ return nil if ind == 0
21
+ @parent[ ind - 1 ]
22
+ end
23
+
24
+ # indent::
25
+ # *DEPRECATED* This parameter is now ignored. See the formatters in the
26
+ # REXML::Formatters package for changing the output style.
27
+ def to_s indent=nil
28
+ unless indent.nil?
29
+ Kernel.warn( "#{self.class.name}.to_s(indent) parameter is deprecated", uplevel: 1)
30
+ f = REXML::Formatters::Pretty.new( indent )
31
+ f.write( self, rv = "" )
32
+ else
33
+ f = REXML::Formatters::Default.new
34
+ f.write( self, rv = "" )
35
+ end
36
+ return rv
37
+ end
38
+
39
+ def indent to, ind
40
+ if @parent and @parent.context and not @parent.context[:indentstyle].nil? then
41
+ indentstyle = @parent.context[:indentstyle]
42
+ else
43
+ indentstyle = ' '
44
+ end
45
+ to << indentstyle*ind unless ind<1
46
+ end
47
+
48
+ def parent?
49
+ false;
50
+ end
51
+
52
+
53
+ # Visit all subnodes of +self+ recursively
54
+ def each_recursive(&block) # :yields: node
55
+ self.elements.each {|node|
56
+ block.call(node)
57
+ node.each_recursive(&block)
58
+ }
59
+ end
60
+
61
+ # Find (and return) first subnode (recursively) for which the block
62
+ # evaluates to true. Returns +nil+ if none was found.
63
+ def find_first_recursive(&block) # :yields: node
64
+ each_recursive {|node|
65
+ return node if block.call(node)
66
+ }
67
+ return nil
68
+ end
69
+
70
+ # Returns the position that +self+ holds in its parent's array, indexed
71
+ # from 1.
72
+ def index_in_parent
73
+ parent.index(self)+1
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: false
2
+ require_relative 'encoding'
3
+
4
+ module REXML
5
+ class Output
6
+ include Encoding
7
+
8
+ attr_reader :encoding
9
+
10
+ def initialize real_IO, encd="iso-8859-1"
11
+ @output = real_IO
12
+ self.encoding = encd
13
+
14
+ @to_utf = encoding != 'UTF-8'
15
+
16
+ if encoding == "UTF-16"
17
+ @output << "\ufeff".encode("UTF-16BE")
18
+ self.encoding = "UTF-16BE"
19
+ end
20
+ end
21
+
22
+ def <<( content )
23
+ @output << (@to_utf ? self.encode(content) : content)
24
+ end
25
+
26
+ def to_s
27
+ "Output[#{encoding}]"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,166 @@
1
+ # frozen_string_literal: false
2
+ require_relative "child"
3
+
4
+ module REXML
5
+ # A parent has children, and has methods for accessing them. The Parent
6
+ # class is never encountered except as the superclass for some other
7
+ # object.
8
+ class Parent < Child
9
+ include Enumerable
10
+
11
+ # Constructor
12
+ # @param parent if supplied, will be set as the parent of this object
13
+ def initialize parent=nil
14
+ super(parent)
15
+ @children = []
16
+ end
17
+
18
+ def add( object )
19
+ object.parent = self
20
+ @children << object
21
+ object
22
+ end
23
+
24
+ alias :push :add
25
+ alias :<< :push
26
+
27
+ def unshift( object )
28
+ object.parent = self
29
+ @children.unshift object
30
+ end
31
+
32
+ def delete( object )
33
+ found = false
34
+ @children.delete_if {|c| c.equal?(object) and found = true }
35
+ object.parent = nil if found
36
+ found ? object : nil
37
+ end
38
+
39
+ def each(&block)
40
+ @children.each(&block)
41
+ end
42
+
43
+ def delete_if( &block )
44
+ @children.delete_if(&block)
45
+ end
46
+
47
+ def delete_at( index )
48
+ @children.delete_at index
49
+ end
50
+
51
+ def each_index( &block )
52
+ @children.each_index(&block)
53
+ end
54
+
55
+ # Fetches a child at a given index
56
+ # @param index the Integer index of the child to fetch
57
+ def []( index )
58
+ @children[index]
59
+ end
60
+
61
+ alias :each_child :each
62
+
63
+
64
+
65
+ # Set an index entry. See Array.[]=
66
+ # @param index the index of the element to set
67
+ # @param opt either the object to set, or an Integer length
68
+ # @param child if opt is an Integer, this is the child to set
69
+ # @return the parent (self)
70
+ def []=( *args )
71
+ args[-1].parent = self
72
+ @children[*args[0..-2]] = args[-1]
73
+ end
74
+
75
+ # Inserts an child before another child
76
+ # @param child1 this is either an xpath or an Element. If an Element,
77
+ # child2 will be inserted before child1 in the child list of the parent.
78
+ # If an xpath, child2 will be inserted before the first child to match
79
+ # the xpath.
80
+ # @param child2 the child to insert
81
+ # @return the parent (self)
82
+ def insert_before( child1, child2 )
83
+ if child1.kind_of? String
84
+ child1 = XPath.first( self, child1 )
85
+ child1.parent.insert_before child1, child2
86
+ else
87
+ ind = index(child1)
88
+ child2.parent.delete(child2) if child2.parent
89
+ @children[ind,0] = child2
90
+ child2.parent = self
91
+ end
92
+ self
93
+ end
94
+
95
+ # Inserts an child after another child
96
+ # @param child1 this is either an xpath or an Element. If an Element,
97
+ # child2 will be inserted after child1 in the child list of the parent.
98
+ # If an xpath, child2 will be inserted after the first child to match
99
+ # the xpath.
100
+ # @param child2 the child to insert
101
+ # @return the parent (self)
102
+ def insert_after( child1, child2 )
103
+ if child1.kind_of? String
104
+ child1 = XPath.first( self, child1 )
105
+ child1.parent.insert_after child1, child2
106
+ else
107
+ ind = index(child1)+1
108
+ child2.parent.delete(child2) if child2.parent
109
+ @children[ind,0] = child2
110
+ child2.parent = self
111
+ end
112
+ self
113
+ end
114
+
115
+ def to_a
116
+ @children.dup
117
+ end
118
+
119
+ # Fetches the index of a given child
120
+ # @param child the child to get the index of
121
+ # @return the index of the child, or nil if the object is not a child
122
+ # of this parent.
123
+ def index( child )
124
+ count = -1
125
+ @children.find { |i| count += 1 ; i.hash == child.hash }
126
+ count
127
+ end
128
+
129
+ # @return the number of children of this parent
130
+ def size
131
+ @children.size
132
+ end
133
+
134
+ alias :length :size
135
+
136
+ # Replaces one child with another, making sure the nodelist is correct
137
+ # @param to_replace the child to replace (must be a Child)
138
+ # @param replacement the child to insert into the nodelist (must be a
139
+ # Child)
140
+ def replace_child( to_replace, replacement )
141
+ @children.map! {|c| c.equal?( to_replace ) ? replacement : c }
142
+ to_replace.parent = nil
143
+ replacement.parent = self
144
+ end
145
+
146
+ # Deeply clones this object. This creates a complete duplicate of this
147
+ # Parent, including all descendants.
148
+ def deep_clone
149
+ cl = clone()
150
+ each do |child|
151
+ if child.kind_of? Parent
152
+ cl << child.deep_clone
153
+ else
154
+ cl << child.clone
155
+ end
156
+ end
157
+ cl
158
+ end
159
+
160
+ alias :children :to_a
161
+
162
+ def parent?
163
+ true
164
+ end
165
+ end
166
+ end