rubysl-rexml 1.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +3 -2
- data/lib/rexml/attlistdecl.rb +56 -56
- data/lib/rexml/attribute.rb +155 -149
- data/lib/rexml/cdata.rb +48 -48
- data/lib/rexml/child.rb +82 -82
- data/lib/rexml/comment.rb +59 -59
- data/lib/rexml/doctype.rb +22 -24
- data/lib/rexml/document.rb +185 -129
- data/lib/rexml/dtd/attlistdecl.rb +7 -7
- data/lib/rexml/dtd/dtd.rb +41 -41
- data/lib/rexml/dtd/elementdecl.rb +13 -13
- data/lib/rexml/dtd/entitydecl.rb +49 -49
- data/lib/rexml/dtd/notationdecl.rb +32 -32
- data/lib/rexml/element.rb +122 -107
- data/lib/rexml/encoding.rb +37 -58
- data/lib/rexml/entity.rb +144 -144
- data/lib/rexml/formatters/default.rb +6 -4
- data/lib/rexml/formatters/pretty.rb +11 -8
- data/lib/rexml/formatters/transitive.rb +4 -3
- data/lib/rexml/functions.rb +33 -21
- data/lib/rexml/instruction.rb +49 -49
- data/lib/rexml/light/node.rb +190 -191
- data/lib/rexml/namespace.rb +39 -39
- data/lib/rexml/node.rb +38 -38
- data/lib/rexml/output.rb +17 -12
- data/lib/rexml/parent.rb +26 -25
- data/lib/rexml/parseexception.rb +4 -4
- data/lib/rexml/parsers/baseparser.rb +90 -61
- data/lib/rexml/parsers/lightparser.rb +41 -43
- data/lib/rexml/parsers/pullparser.rb +1 -1
- data/lib/rexml/parsers/sax2parser.rb +233 -198
- data/lib/rexml/parsers/streamparser.rb +6 -2
- data/lib/rexml/parsers/treeparser.rb +9 -6
- data/lib/rexml/parsers/ultralightparser.rb +40 -40
- data/lib/rexml/parsers/xpathparser.rb +51 -52
- data/lib/rexml/quickpath.rb +247 -248
- data/lib/rexml/rexml.rb +9 -10
- data/lib/rexml/sax2listener.rb +92 -92
- data/lib/rexml/security.rb +27 -0
- data/lib/rexml/source.rb +95 -50
- data/lib/rexml/streamlistener.rb +90 -90
- data/lib/rexml/syncenumerator.rb +3 -4
- data/lib/rexml/text.rb +157 -76
- data/lib/rexml/validation/relaxng.rb +18 -18
- data/lib/rexml/validation/validation.rb +5 -5
- data/lib/rexml/xmldecl.rb +59 -63
- data/lib/rexml/xmltokens.rb +14 -14
- data/lib/rexml/xpath.rb +67 -53
- data/lib/rexml/xpath_parser.rb +49 -38
- data/lib/rubysl/rexml.rb +1 -0
- data/lib/rubysl/rexml/version.rb +1 -1
- data/rubysl-rexml.gemspec +3 -1
- metadata +19 -28
- data/lib/rexml/encodings/CP-1252.rb +0 -103
- data/lib/rexml/encodings/EUC-JP.rb +0 -35
- data/lib/rexml/encodings/ICONV.rb +0 -22
- data/lib/rexml/encodings/ISO-8859-1.rb +0 -7
- data/lib/rexml/encodings/ISO-8859-15.rb +0 -72
- data/lib/rexml/encodings/SHIFT-JIS.rb +0 -37
- data/lib/rexml/encodings/SHIFT_JIS.rb +0 -1
- data/lib/rexml/encodings/UNILE.rb +0 -34
- data/lib/rexml/encodings/US-ASCII.rb +0 -30
- data/lib/rexml/encodings/UTF-16.rb +0 -35
- data/lib/rexml/encodings/UTF-8.rb +0 -18
data/lib/rexml/document.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require "rexml/security"
|
1
2
|
require "rexml/element"
|
2
3
|
require "rexml/xmldecl"
|
3
4
|
require "rexml/source"
|
@@ -16,131 +17,138 @@ module REXML
|
|
16
17
|
# Document has a single child that can be accessed by root().
|
17
18
|
# Note that if you want to have an XML declaration written for a document
|
18
19
|
# you create, you must add one; REXML documents do not write a default
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
# declaration for you. See |DECLARATION| and |write|.
|
21
|
+
class Document < Element
|
22
|
+
# A convenient default XML declaration. If you want an XML declaration,
|
23
|
+
# the easiest way to add one is mydoc << Document::DECLARATION
|
23
24
|
# +DEPRECATED+
|
24
25
|
# Use: mydoc << XMLDecl.default
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
26
|
+
DECLARATION = XMLDecl.default
|
27
|
+
|
28
|
+
# Constructor
|
29
|
+
# @param source if supplied, must be a Document, String, or IO.
|
30
|
+
# Documents have their context and Element attributes cloned.
|
31
|
+
# Strings are expected to be valid XML documents. IOs are expected
|
32
|
+
# to be sources of valid XML documents.
|
33
|
+
# @param context if supplied, contains the context of the document;
|
34
|
+
# this should be a Hash.
|
35
|
+
def initialize( source = nil, context = {} )
|
35
36
|
@entity_expansion_count = 0
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
37
|
+
super()
|
38
|
+
@context = context
|
39
|
+
return if source.nil?
|
40
|
+
if source.kind_of? Document
|
41
|
+
@context = source.context
|
42
|
+
super source
|
43
|
+
else
|
44
|
+
build( source )
|
45
|
+
end
|
46
|
+
end
|
46
47
|
|
47
48
|
def node_type
|
48
49
|
:document
|
49
50
|
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
# Should be obvious
|
53
|
+
def clone
|
54
|
+
Document.new self
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
57
|
+
# According to the XML spec, a root node has no expanded name
|
58
|
+
def expanded_name
|
59
|
+
''
|
60
|
+
#d = doc_type
|
61
|
+
#d ? d.name : "UNDEFINED"
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
+
alias :name :expanded_name
|
64
65
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
66
|
+
# We override this, because XMLDecls and DocTypes must go at the start
|
67
|
+
# of the document
|
68
|
+
def add( child )
|
69
|
+
if child.kind_of? XMLDecl
|
70
|
+
if @children[0].kind_of? XMLDecl
|
71
|
+
@children[0] = child
|
72
|
+
else
|
73
|
+
@children.unshift child
|
74
|
+
end
|
70
75
|
child.parent = self
|
71
|
-
|
72
|
-
# Find first Element or DocType node and insert the decl right
|
76
|
+
elsif child.kind_of? DocType
|
77
|
+
# Find first Element or DocType node and insert the decl right
|
73
78
|
# before it. If there is no such node, just insert the child at the
|
74
79
|
# end. If there is a child and it is an DocType, then replace it.
|
75
|
-
insert_before_index =
|
76
|
-
@children.find { |x|
|
77
|
-
insert_before_index += 1
|
80
|
+
insert_before_index = @children.find_index { |x|
|
78
81
|
x.kind_of?(Element) || x.kind_of?(DocType)
|
79
82
|
}
|
80
|
-
if
|
81
|
-
if @children[ insert_before_index ].kind_of DocType
|
83
|
+
if insert_before_index # Not null = not end of list
|
84
|
+
if @children[ insert_before_index ].kind_of? DocType
|
82
85
|
@children[ insert_before_index ] = child
|
83
86
|
else
|
84
|
-
@children[
|
87
|
+
@children[ insert_before_index-1, 0 ] = child
|
85
88
|
end
|
86
89
|
else # Insert at end of list
|
87
|
-
@children
|
90
|
+
@children << child
|
88
91
|
end
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
92
|
+
child.parent = self
|
93
|
+
else
|
94
|
+
rv = super
|
95
|
+
raise "attempted adding second root element to document" if @elements.size > 1
|
96
|
+
rv
|
97
|
+
end
|
98
|
+
end
|
99
|
+
alias :<< :add
|
100
|
+
|
101
|
+
def add_element(arg=nil, arg2=nil)
|
102
|
+
rv = super
|
103
|
+
raise "attempted adding second root element to document" if @elements.size > 1
|
104
|
+
rv
|
105
|
+
end
|
106
|
+
|
107
|
+
# @return the root Element of the document, or nil if this document
|
108
|
+
# has no children.
|
109
|
+
def root
|
107
110
|
elements[1]
|
108
111
|
#self
|
109
112
|
#@children.find { |item| item.kind_of? Element }
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
113
|
+
end
|
114
|
+
|
115
|
+
# @return the DocType child of the document, if one exists,
|
116
|
+
# and nil otherwise.
|
117
|
+
def doctype
|
118
|
+
@children.find { |item| item.kind_of? DocType }
|
119
|
+
end
|
120
|
+
|
121
|
+
# @return the XMLDecl of this document; if no XMLDecl has been
|
122
|
+
# set, the default declaration is returned.
|
123
|
+
def xml_decl
|
124
|
+
rv = @children[0]
|
122
125
|
return rv if rv.kind_of? XMLDecl
|
123
126
|
rv = @children.unshift(XMLDecl.default)[0]
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
127
|
+
end
|
128
|
+
|
129
|
+
# @return the XMLDecl version of this document as a String.
|
130
|
+
# If no XMLDecl has been set, returns the default version.
|
131
|
+
def version
|
132
|
+
xml_decl().version
|
133
|
+
end
|
134
|
+
|
135
|
+
# @return the XMLDecl encoding of this document as an
|
136
|
+
# Encoding object.
|
137
|
+
# If no XMLDecl has been set, returns the default encoding.
|
138
|
+
def encoding
|
139
|
+
xml_decl().encoding
|
140
|
+
end
|
141
|
+
|
142
|
+
# @return the XMLDecl standalone value of this document as a String.
|
143
|
+
# If no XMLDecl has been set, returns the default setting.
|
144
|
+
def stand_alone?
|
145
|
+
xml_decl().stand_alone?
|
146
|
+
end
|
143
147
|
|
148
|
+
# :call-seq:
|
149
|
+
# doc.write(output=$stdout, indent=-1, transtive=false, ie_hack=false, encoding=nil)
|
150
|
+
# doc.write(options={:output => $stdout, :indent => -1, :transtive => false, :ie_hack => false, :encoding => nil})
|
151
|
+
#
|
144
152
|
# Write the XML tree out, optionally with indent. This writes out the
|
145
153
|
# entire XML document, including XML declarations, doctype declarations,
|
146
154
|
# and processing instructions (if any are given).
|
@@ -151,41 +159,73 @@ module REXML
|
|
151
159
|
# specified, because it adds unnecessary bandwidth to applications such
|
152
160
|
# as XML-RPC.
|
153
161
|
#
|
162
|
+
# Accept Nth argument style and options Hash style as argument.
|
163
|
+
# The recommended style is options Hash style for one or more
|
164
|
+
# arguments case.
|
165
|
+
#
|
166
|
+
# _Examples_
|
167
|
+
# Document.new("<a><b/></a>").write
|
168
|
+
#
|
169
|
+
# output = ""
|
170
|
+
# Document.new("<a><b/></a>").write(output)
|
171
|
+
#
|
172
|
+
# output = ""
|
173
|
+
# Document.new("<a><b/></a>").write(:output => output, :indent => 2)
|
174
|
+
#
|
154
175
|
# See also the classes in the rexml/formatters package for the proper way
|
155
|
-
# to change the default formatting of XML output
|
176
|
+
# to change the default formatting of XML output.
|
156
177
|
#
|
157
178
|
# _Examples_
|
158
|
-
# Document.new("<a><b/></a>").serialize
|
159
179
|
#
|
160
|
-
#
|
161
|
-
# tr = Transitive.new
|
162
|
-
# Document.new("<a><b/></a>")
|
180
|
+
# output = ""
|
181
|
+
# tr = Transitive.new
|
182
|
+
# tr.write(Document.new("<a><b/></a>"), output)
|
163
183
|
#
|
164
184
|
# output::
|
165
|
-
#
|
185
|
+
# output an object which supports '<< string'; this is where the
|
166
186
|
# document will be written.
|
167
187
|
# indent::
|
168
188
|
# An integer. If -1, no indenting will be used; otherwise, the
|
169
189
|
# indentation will be twice this number of spaces, and children will be
|
170
|
-
# indented an additional amount. For a value of 3, every item will be
|
190
|
+
# indented an additional amount. For a value of 3, every item will be
|
171
191
|
# indented 3 more levels, or 6 more spaces (2 * 3). Defaults to -1
|
172
|
-
#
|
192
|
+
# transitive::
|
173
193
|
# If transitive is true and indent is >= 0, then the output will be
|
174
194
|
# pretty-printed in such a way that the added whitespace does not affect
|
175
195
|
# the absolute *value* of the document -- that is, it leaves the value
|
176
196
|
# and number of Text nodes in the document unchanged.
|
177
197
|
# ie_hack::
|
178
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
|
184
|
-
|
185
|
-
|
198
|
+
# This hack inserts a space before the /> on empty tags to address
|
199
|
+
# a limitation of Internet Explorer. Defaults to false
|
200
|
+
# encoding::
|
201
|
+
# Encoding name as String. Change output encoding to specified encoding
|
202
|
+
# instead of encoding in XML declaration.
|
203
|
+
# Defaults to nil. It means encoding in XML declaration is used.
|
204
|
+
def write(*arguments)
|
205
|
+
if arguments.size == 1 and arguments[0].class == Hash
|
206
|
+
options = arguments[0]
|
207
|
+
|
208
|
+
output = options[:output]
|
209
|
+
indent = options[:indent]
|
210
|
+
transitive = options[:transitive]
|
211
|
+
ie_hack = options[:ie_hack]
|
212
|
+
encoding = options[:encoding]
|
213
|
+
else
|
214
|
+
output, indent, transitive, ie_hack, encoding, = *arguments
|
215
|
+
end
|
216
|
+
|
217
|
+
output ||= $stdout
|
218
|
+
indent ||= -1
|
219
|
+
transitive = false if transitive.nil?
|
220
|
+
ie_hack = false if ie_hack.nil?
|
221
|
+
encoding ||= xml_decl.encoding
|
222
|
+
|
223
|
+
if encoding != 'UTF-8' && !output.kind_of?(Output)
|
224
|
+
output = Output.new( output, encoding )
|
186
225
|
end
|
187
226
|
formatter = if indent > -1
|
188
|
-
if
|
227
|
+
if transitive
|
228
|
+
require "rexml/formatters/transitive"
|
189
229
|
REXML::Formatters::Transitive.new( indent, ie_hack )
|
190
230
|
else
|
191
231
|
REXML::Formatters::Pretty.new( indent, ie_hack )
|
@@ -194,37 +234,53 @@ module REXML
|
|
194
234
|
REXML::Formatters::Default.new( ie_hack )
|
195
235
|
end
|
196
236
|
formatter.write( self, output )
|
197
|
-
|
237
|
+
end
|
198
238
|
|
199
|
-
|
200
|
-
def Document::parse_stream( source, listener )
|
201
|
-
Parsers::StreamParser.new( source, listener ).parse
|
202
|
-
end
|
203
239
|
|
204
|
-
|
240
|
+
def Document::parse_stream( source, listener )
|
241
|
+
Parsers::StreamParser.new( source, listener ).parse
|
242
|
+
end
|
205
243
|
|
206
244
|
# Set the entity expansion limit. By default the limit is set to 10000.
|
245
|
+
#
|
246
|
+
# Deprecated. Use REXML::Security.entity_expansion_limit= instead.
|
207
247
|
def Document::entity_expansion_limit=( val )
|
208
|
-
|
248
|
+
Security.entity_expansion_limit = val
|
209
249
|
end
|
210
250
|
|
211
251
|
# Get the entity expansion limit. By default the limit is set to 10000.
|
252
|
+
#
|
253
|
+
# Deprecated. Use REXML::Security.entity_expansion_limit= instead.
|
212
254
|
def Document::entity_expansion_limit
|
213
|
-
return
|
255
|
+
return Security.entity_expansion_limit
|
256
|
+
end
|
257
|
+
|
258
|
+
# Set the entity expansion limit. By default the limit is set to 10240.
|
259
|
+
#
|
260
|
+
# Deprecated. Use REXML::Security.entity_expansion_text_limit= instead.
|
261
|
+
def Document::entity_expansion_text_limit=( val )
|
262
|
+
Security.entity_expansion_text_limit = val
|
263
|
+
end
|
264
|
+
|
265
|
+
# Get the entity expansion limit. By default the limit is set to 10240.
|
266
|
+
#
|
267
|
+
# Deprecated. Use REXML::Security.entity_expansion_text_limit instead.
|
268
|
+
def Document::entity_expansion_text_limit
|
269
|
+
return Security.entity_expansion_text_limit
|
214
270
|
end
|
215
271
|
|
216
272
|
attr_reader :entity_expansion_count
|
217
|
-
|
273
|
+
|
218
274
|
def record_entity_expansion
|
219
275
|
@entity_expansion_count += 1
|
220
|
-
if @entity_expansion_count >
|
276
|
+
if @entity_expansion_count > Security.entity_expansion_limit
|
221
277
|
raise "number of entity expansions exceeded, processing aborted."
|
222
278
|
end
|
223
279
|
end
|
224
280
|
|
225
|
-
|
226
|
-
|
281
|
+
private
|
282
|
+
def build( source )
|
227
283
|
Parsers::TreeParser.new( source, self ).parse
|
228
|
-
|
229
|
-
|
284
|
+
end
|
285
|
+
end
|
230
286
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require "rexml/child"
|
2
2
|
module REXML
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
module DTD
|
4
|
+
class AttlistDecl < Child
|
5
|
+
START = "<!ATTLIST"
|
6
|
+
START_RE = /^\s*#{START}/um
|
7
|
+
PATTERN_RE = /\s*(#{START}.*?>)/um
|
8
|
+
end
|
9
|
+
end
|
10
10
|
end
|
data/lib/rexml/dtd/dtd.rb
CHANGED
@@ -6,46 +6,46 @@ require "rexml/dtd/attlistdecl"
|
|
6
6
|
require "rexml/parent"
|
7
7
|
|
8
8
|
module REXML
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
9
|
+
module DTD
|
10
|
+
class Parser
|
11
|
+
def Parser.parse( input )
|
12
|
+
case input
|
13
|
+
when String
|
14
|
+
parse_helper input
|
15
|
+
when File
|
16
|
+
parse_helper input.read
|
17
|
+
end
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
20
|
+
# Takes a String and parses it out
|
21
|
+
def Parser.parse_helper( input )
|
22
|
+
contents = Parent.new
|
23
|
+
while input.size > 0
|
24
|
+
case input
|
25
|
+
when ElementDecl.PATTERN_RE
|
26
|
+
match = $&
|
27
|
+
source = $'
|
28
|
+
contents << ElementDecl.new( match )
|
29
|
+
when AttlistDecl.PATTERN_RE
|
30
|
+
matchdata = $~
|
31
|
+
source = $'
|
32
|
+
contents << AttlistDecl.new( matchdata )
|
33
|
+
when EntityDecl.PATTERN_RE
|
34
|
+
matchdata = $~
|
35
|
+
source = $'
|
36
|
+
contents << EntityDecl.new( matchdata )
|
37
|
+
when Comment.PATTERN_RE
|
38
|
+
matchdata = $~
|
39
|
+
source = $'
|
40
|
+
contents << Comment.new( matchdata )
|
41
|
+
when NotationDecl.PATTERN_RE
|
42
|
+
matchdata = $~
|
43
|
+
source = $'
|
44
|
+
contents << NotationDecl.new( matchdata )
|
45
|
+
end
|
46
|
+
end
|
47
|
+
contents
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
51
|
end
|