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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +10 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rexml/attlistdecl.rb +63 -0
- data/lib/rexml/attribute.rb +192 -0
- data/lib/rexml/cdata.rb +68 -0
- data/lib/rexml/child.rb +97 -0
- data/lib/rexml/comment.rb +80 -0
- data/lib/rexml/doctype.rb +270 -0
- data/lib/rexml/document.rb +291 -0
- data/lib/rexml/dtd/attlistdecl.rb +11 -0
- data/lib/rexml/dtd/dtd.rb +47 -0
- data/lib/rexml/dtd/elementdecl.rb +18 -0
- data/lib/rexml/dtd/entitydecl.rb +57 -0
- data/lib/rexml/dtd/notationdecl.rb +40 -0
- data/lib/rexml/element.rb +1267 -0
- data/lib/rexml/encoding.rb +51 -0
- data/lib/rexml/entity.rb +171 -0
- data/lib/rexml/formatters/default.rb +112 -0
- data/lib/rexml/formatters/pretty.rb +142 -0
- data/lib/rexml/formatters/transitive.rb +58 -0
- data/lib/rexml/functions.rb +447 -0
- data/lib/rexml/instruction.rb +71 -0
- data/lib/rexml/light/node.rb +196 -0
- data/lib/rexml/namespace.rb +48 -0
- data/lib/rexml/node.rb +76 -0
- data/lib/rexml/output.rb +30 -0
- data/lib/rexml/parent.rb +166 -0
- data/lib/rexml/parseexception.rb +52 -0
- data/lib/rexml/parsers/baseparser.rb +586 -0
- data/lib/rexml/parsers/lightparser.rb +59 -0
- data/lib/rexml/parsers/pullparser.rb +197 -0
- data/lib/rexml/parsers/sax2parser.rb +273 -0
- data/lib/rexml/parsers/streamparser.rb +61 -0
- data/lib/rexml/parsers/treeparser.rb +101 -0
- data/lib/rexml/parsers/ultralightparser.rb +57 -0
- data/lib/rexml/parsers/xpathparser.rb +675 -0
- data/lib/rexml/quickpath.rb +266 -0
- data/lib/rexml/rexml.rb +32 -0
- data/lib/rexml/sax2listener.rb +98 -0
- data/lib/rexml/security.rb +28 -0
- data/lib/rexml/source.rb +298 -0
- data/lib/rexml/streamlistener.rb +93 -0
- data/lib/rexml/syncenumerator.rb +33 -0
- data/lib/rexml/text.rb +424 -0
- data/lib/rexml/undefinednamespaceexception.rb +9 -0
- data/lib/rexml/validation/relaxng.rb +539 -0
- data/lib/rexml/validation/validation.rb +144 -0
- data/lib/rexml/validation/validationexception.rb +10 -0
- data/lib/rexml/xmldecl.rb +116 -0
- data/lib/rexml/xmltokens.rb +85 -0
- data/lib/rexml/xpath.rb +81 -0
- data/lib/rexml/xpath_parser.rb +934 -0
- data/rexml.gemspec +42 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 76db133f3cf02da57b3a0745ca0901720481eb747221f6f66e5752ab422ba9bc
|
4
|
+
data.tar.gz: c000ee99c1d08c9f5be7e93df159af8b7c4682a8d5c339fb0633bae9ee9c10a5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aca2b95bb726f5457f602ca006251d2c8d52cfdb6e8514814660802bb3a1ac646e59a98c0beb3b479c64dd61e54f6671f614f8ce8803279d8b17a5ab3540d6d0
|
7
|
+
data.tar.gz: 6e6bd7310393d5c6c27194d7c456382bfe75ee2f9b31c85ec4de38da5767e1cafa44878a89f51e87632fb4f6c2fdd5d2c49ba3def51656fe33f4b95b2551598e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# REXML
|
2
|
+
|
3
|
+
REXML was inspired by the Electric XML library for Java, which features an easy-to-use API, small size, and speed. Hopefully, REXML, designed with the same philosophy, has these same features. I've tried to keep the API as intuitive as possible, and have followed the Ruby methodology for method naming and code flow, rather than mirroring the Java API.
|
4
|
+
|
5
|
+
REXML supports both tree and stream document parsing. Stream parsing is faster (about 1.5 times as fast). However, with stream parsing, you don't get access to features such as XPath.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'rexml'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install rexml
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
We'll start with parsing an XML document
|
26
|
+
|
27
|
+
```
|
28
|
+
require "rexml/document"
|
29
|
+
file = File.new( "mydoc.xml" )
|
30
|
+
doc = REXML::Document.new file
|
31
|
+
```
|
32
|
+
|
33
|
+
Line 3 creates a new document and parses the supplied file. You can also do the following
|
34
|
+
|
35
|
+
```
|
36
|
+
require "rexml/document"
|
37
|
+
include REXML # so that we don't have to prefix everything with REXML::...
|
38
|
+
string = <<EOF
|
39
|
+
<mydoc>
|
40
|
+
<someelement attribute="nanoo">Text, text, text</someelement>
|
41
|
+
</mydoc>
|
42
|
+
EOF
|
43
|
+
doc = Document.new string
|
44
|
+
```
|
45
|
+
|
46
|
+
So parsing a string is just as easy as parsing a file.
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
51
|
+
|
52
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/rexml.
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
The gem is available as open source under the terms of the [BSD-2-Clause](LICENSE.txt).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rexml"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
#vim:ts=2 sw=2 noexpandtab:
|
3
|
+
require_relative 'child'
|
4
|
+
require_relative 'source'
|
5
|
+
|
6
|
+
module REXML
|
7
|
+
# This class needs:
|
8
|
+
# * Documentation
|
9
|
+
# * Work! Not all types of attlists are intelligently parsed, so we just
|
10
|
+
# spew back out what we get in. This works, but it would be better if
|
11
|
+
# we formatted the output ourselves.
|
12
|
+
#
|
13
|
+
# AttlistDecls provide *just* enough support to allow namespace
|
14
|
+
# declarations. If you need some sort of generalized support, or have an
|
15
|
+
# interesting idea about how to map the hideous, terrible design of DTD
|
16
|
+
# AttlistDecls onto an intuitive Ruby interface, let me know. I'm desperate
|
17
|
+
# for anything to make DTDs more palateable.
|
18
|
+
class AttlistDecl < Child
|
19
|
+
include Enumerable
|
20
|
+
|
21
|
+
# What is this? Got me.
|
22
|
+
attr_reader :element_name
|
23
|
+
|
24
|
+
# Create an AttlistDecl, pulling the information from a Source. Notice
|
25
|
+
# that this isn't very convenient; to create an AttlistDecl, you basically
|
26
|
+
# have to format it yourself, and then have the initializer parse it.
|
27
|
+
# Sorry, but for the foreseeable future, DTD support in REXML is pretty
|
28
|
+
# weak on convenience. Have I mentioned how much I hate DTDs?
|
29
|
+
def initialize(source)
|
30
|
+
super()
|
31
|
+
if (source.kind_of? Array)
|
32
|
+
@element_name, @pairs, @contents = *source
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Access the attlist attribute/value pairs.
|
37
|
+
# value = attlist_decl[ attribute_name ]
|
38
|
+
def [](key)
|
39
|
+
@pairs[key]
|
40
|
+
end
|
41
|
+
|
42
|
+
# Whether an attlist declaration includes the given attribute definition
|
43
|
+
# if attlist_decl.include? "xmlns:foobar"
|
44
|
+
def include?(key)
|
45
|
+
@pairs.keys.include? key
|
46
|
+
end
|
47
|
+
|
48
|
+
# Iterate over the key/value pairs:
|
49
|
+
# attlist_decl.each { |attribute_name, attribute_value| ... }
|
50
|
+
def each(&block)
|
51
|
+
@pairs.each(&block)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Write out exactly what we got in.
|
55
|
+
def write out, indent=-1
|
56
|
+
out << @contents
|
57
|
+
end
|
58
|
+
|
59
|
+
def node_type
|
60
|
+
:attlistdecl
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,192 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative "namespace"
|
3
|
+
require_relative 'text'
|
4
|
+
|
5
|
+
module REXML
|
6
|
+
# Defines an Element Attribute; IE, a attribute=value pair, as in:
|
7
|
+
# <element attribute="value"/>. Attributes can be in their own
|
8
|
+
# namespaces. General users of REXML will not interact with the
|
9
|
+
# Attribute class much.
|
10
|
+
class Attribute
|
11
|
+
include Node
|
12
|
+
include Namespace
|
13
|
+
|
14
|
+
# The element to which this attribute belongs
|
15
|
+
attr_reader :element
|
16
|
+
# The normalized value of this attribute. That is, the attribute with
|
17
|
+
# entities intact.
|
18
|
+
attr_writer :normalized
|
19
|
+
PATTERN = /\s*(#{NAME_STR})\s*=\s*(["'])(.*?)\2/um
|
20
|
+
|
21
|
+
NEEDS_A_SECOND_CHECK = /(<|&((#{Entity::NAME});|(#0*((?:\d+)|(?:x[a-fA-F0-9]+)));)?)/um
|
22
|
+
|
23
|
+
# Constructor.
|
24
|
+
# FIXME: The parser doesn't catch illegal characters in attributes
|
25
|
+
#
|
26
|
+
# first::
|
27
|
+
# Either: an Attribute, which this new attribute will become a
|
28
|
+
# clone of; or a String, which is the name of this attribute
|
29
|
+
# second::
|
30
|
+
# If +first+ is an Attribute, then this may be an Element, or nil.
|
31
|
+
# If nil, then the Element parent of this attribute is the parent
|
32
|
+
# of the +first+ Attribute. If the first argument is a String,
|
33
|
+
# then this must also be a String, and is the content of the attribute.
|
34
|
+
# If this is the content, it must be fully normalized (contain no
|
35
|
+
# illegal characters).
|
36
|
+
# parent::
|
37
|
+
# Ignored unless +first+ is a String; otherwise, may be the Element
|
38
|
+
# parent of this attribute, or nil.
|
39
|
+
#
|
40
|
+
#
|
41
|
+
# Attribute.new( attribute_to_clone )
|
42
|
+
# Attribute.new( attribute_to_clone, parent_element )
|
43
|
+
# Attribute.new( "attr", "attr_value" )
|
44
|
+
# Attribute.new( "attr", "attr_value", parent_element )
|
45
|
+
def initialize( first, second=nil, parent=nil )
|
46
|
+
@normalized = @unnormalized = @element = nil
|
47
|
+
if first.kind_of? Attribute
|
48
|
+
self.name = first.expanded_name
|
49
|
+
@unnormalized = first.value
|
50
|
+
if second.kind_of? Element
|
51
|
+
@element = second
|
52
|
+
else
|
53
|
+
@element = first.element
|
54
|
+
end
|
55
|
+
elsif first.kind_of? String
|
56
|
+
@element = parent
|
57
|
+
self.name = first
|
58
|
+
@normalized = second.to_s
|
59
|
+
else
|
60
|
+
raise "illegal argument #{first.class.name} to Attribute constructor"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns the namespace of the attribute.
|
65
|
+
#
|
66
|
+
# e = Element.new( "elns:myelement" )
|
67
|
+
# e.add_attribute( "nsa:a", "aval" )
|
68
|
+
# e.add_attribute( "b", "bval" )
|
69
|
+
# e.attributes.get_attribute( "a" ).prefix # -> "nsa"
|
70
|
+
# e.attributes.get_attribute( "b" ).prefix # -> "elns"
|
71
|
+
# a = Attribute.new( "x", "y" )
|
72
|
+
# a.prefix # -> ""
|
73
|
+
def prefix
|
74
|
+
pf = super
|
75
|
+
if pf == ""
|
76
|
+
pf = @element.prefix if @element
|
77
|
+
end
|
78
|
+
pf
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns the namespace URL, if defined, or nil otherwise
|
82
|
+
#
|
83
|
+
# e = Element.new("el")
|
84
|
+
# e.add_namespace("ns", "http://url")
|
85
|
+
# e.add_attribute("ns:a", "b")
|
86
|
+
# e.add_attribute("nsx:a", "c")
|
87
|
+
# e.attribute("ns:a").namespace # => "http://url"
|
88
|
+
# e.attribute("nsx:a").namespace # => nil
|
89
|
+
def namespace arg=nil
|
90
|
+
arg = prefix if arg.nil?
|
91
|
+
@element.namespace arg
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns true if other is an Attribute and has the same name and value,
|
95
|
+
# false otherwise.
|
96
|
+
def ==( other )
|
97
|
+
other.kind_of?(Attribute) and other.name==name and other.value==value
|
98
|
+
end
|
99
|
+
|
100
|
+
# Creates (and returns) a hash from both the name and value
|
101
|
+
def hash
|
102
|
+
name.hash + value.hash
|
103
|
+
end
|
104
|
+
|
105
|
+
# Returns this attribute out as XML source, expanding the name
|
106
|
+
#
|
107
|
+
# a = Attribute.new( "x", "y" )
|
108
|
+
# a.to_string # -> "x='y'"
|
109
|
+
# b = Attribute.new( "ns:x", "y" )
|
110
|
+
# b.to_string # -> "ns:x='y'"
|
111
|
+
def to_string
|
112
|
+
if @element and @element.context and @element.context[:attribute_quote] == :quote
|
113
|
+
%Q^#@expanded_name="#{to_s().gsub(/"/, '"')}"^
|
114
|
+
else
|
115
|
+
"#@expanded_name='#{to_s().gsub(/'/, ''')}'"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def doctype
|
120
|
+
if @element
|
121
|
+
doc = @element.document
|
122
|
+
doc.doctype if doc
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Returns the attribute value, with entities replaced
|
127
|
+
def to_s
|
128
|
+
return @normalized if @normalized
|
129
|
+
|
130
|
+
@normalized = Text::normalize( @unnormalized, doctype )
|
131
|
+
@unnormalized = nil
|
132
|
+
@normalized
|
133
|
+
end
|
134
|
+
|
135
|
+
# Returns the UNNORMALIZED value of this attribute. That is, entities
|
136
|
+
# have been expanded to their values
|
137
|
+
def value
|
138
|
+
return @unnormalized if @unnormalized
|
139
|
+
@unnormalized = Text::unnormalize( @normalized, doctype )
|
140
|
+
@normalized = nil
|
141
|
+
@unnormalized
|
142
|
+
end
|
143
|
+
|
144
|
+
# Returns a copy of this attribute
|
145
|
+
def clone
|
146
|
+
Attribute.new self
|
147
|
+
end
|
148
|
+
|
149
|
+
# Sets the element of which this object is an attribute. Normally, this
|
150
|
+
# is not directly called.
|
151
|
+
#
|
152
|
+
# Returns this attribute
|
153
|
+
def element=( element )
|
154
|
+
@element = element
|
155
|
+
|
156
|
+
if @normalized
|
157
|
+
Text.check( @normalized, NEEDS_A_SECOND_CHECK, doctype )
|
158
|
+
end
|
159
|
+
|
160
|
+
self
|
161
|
+
end
|
162
|
+
|
163
|
+
# Removes this Attribute from the tree, and returns true if successful
|
164
|
+
#
|
165
|
+
# This method is usually not called directly.
|
166
|
+
def remove
|
167
|
+
@element.attributes.delete self.name unless @element.nil?
|
168
|
+
end
|
169
|
+
|
170
|
+
# Writes this attribute (EG, puts 'key="value"' to the output)
|
171
|
+
def write( output, indent=-1 )
|
172
|
+
output << to_string
|
173
|
+
end
|
174
|
+
|
175
|
+
def node_type
|
176
|
+
:attribute
|
177
|
+
end
|
178
|
+
|
179
|
+
def inspect
|
180
|
+
rv = ""
|
181
|
+
write( rv )
|
182
|
+
rv
|
183
|
+
end
|
184
|
+
|
185
|
+
def xpath
|
186
|
+
path = @element.xpath
|
187
|
+
path += "/@#{self.expanded_name}"
|
188
|
+
return path
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
#vim:ts=2 sw=2 noexpandtab:
|
data/lib/rexml/cdata.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative "text"
|
3
|
+
|
4
|
+
module REXML
|
5
|
+
class CData < Text
|
6
|
+
START = '<![CDATA['
|
7
|
+
STOP = ']]>'
|
8
|
+
ILLEGAL = /(\]\]>)/
|
9
|
+
|
10
|
+
# Constructor. CData is data between <![CDATA[ ... ]]>
|
11
|
+
#
|
12
|
+
# _Examples_
|
13
|
+
# CData.new( source )
|
14
|
+
# CData.new( "Here is some CDATA" )
|
15
|
+
# CData.new( "Some unprocessed data", respect_whitespace_TF, parent_element )
|
16
|
+
def initialize( first, whitespace=true, parent=nil )
|
17
|
+
super( first, whitespace, parent, false, true, ILLEGAL )
|
18
|
+
end
|
19
|
+
|
20
|
+
# Make a copy of this object
|
21
|
+
#
|
22
|
+
# _Examples_
|
23
|
+
# c = CData.new( "Some text" )
|
24
|
+
# d = c.clone
|
25
|
+
# d.to_s # -> "Some text"
|
26
|
+
def clone
|
27
|
+
CData.new self
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns the content of this CData object
|
31
|
+
#
|
32
|
+
# _Examples_
|
33
|
+
# c = CData.new( "Some text" )
|
34
|
+
# c.to_s # -> "Some text"
|
35
|
+
def to_s
|
36
|
+
@string
|
37
|
+
end
|
38
|
+
|
39
|
+
def value
|
40
|
+
@string
|
41
|
+
end
|
42
|
+
|
43
|
+
# == DEPRECATED
|
44
|
+
# See the rexml/formatters package
|
45
|
+
#
|
46
|
+
# Generates XML output of this object
|
47
|
+
#
|
48
|
+
# output::
|
49
|
+
# Where to write the string. Defaults to $stdout
|
50
|
+
# indent::
|
51
|
+
# The amount to indent this node by
|
52
|
+
# transitive::
|
53
|
+
# Ignored
|
54
|
+
# ie_hack::
|
55
|
+
# Ignored
|
56
|
+
#
|
57
|
+
# _Examples_
|
58
|
+
# c = CData.new( " Some text " )
|
59
|
+
# c.write( $stdout ) #-> <![CDATA[ Some text ]]>
|
60
|
+
def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
|
61
|
+
Kernel.warn( "#{self.class.name}.write is deprecated", uplevel: 1)
|
62
|
+
indent( output, indent )
|
63
|
+
output << START
|
64
|
+
output << @string
|
65
|
+
output << STOP
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|