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.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -2
  3. data/lib/rexml/attlistdecl.rb +56 -56
  4. data/lib/rexml/attribute.rb +155 -149
  5. data/lib/rexml/cdata.rb +48 -48
  6. data/lib/rexml/child.rb +82 -82
  7. data/lib/rexml/comment.rb +59 -59
  8. data/lib/rexml/doctype.rb +22 -24
  9. data/lib/rexml/document.rb +185 -129
  10. data/lib/rexml/dtd/attlistdecl.rb +7 -7
  11. data/lib/rexml/dtd/dtd.rb +41 -41
  12. data/lib/rexml/dtd/elementdecl.rb +13 -13
  13. data/lib/rexml/dtd/entitydecl.rb +49 -49
  14. data/lib/rexml/dtd/notationdecl.rb +32 -32
  15. data/lib/rexml/element.rb +122 -107
  16. data/lib/rexml/encoding.rb +37 -58
  17. data/lib/rexml/entity.rb +144 -144
  18. data/lib/rexml/formatters/default.rb +6 -4
  19. data/lib/rexml/formatters/pretty.rb +11 -8
  20. data/lib/rexml/formatters/transitive.rb +4 -3
  21. data/lib/rexml/functions.rb +33 -21
  22. data/lib/rexml/instruction.rb +49 -49
  23. data/lib/rexml/light/node.rb +190 -191
  24. data/lib/rexml/namespace.rb +39 -39
  25. data/lib/rexml/node.rb +38 -38
  26. data/lib/rexml/output.rb +17 -12
  27. data/lib/rexml/parent.rb +26 -25
  28. data/lib/rexml/parseexception.rb +4 -4
  29. data/lib/rexml/parsers/baseparser.rb +90 -61
  30. data/lib/rexml/parsers/lightparser.rb +41 -43
  31. data/lib/rexml/parsers/pullparser.rb +1 -1
  32. data/lib/rexml/parsers/sax2parser.rb +233 -198
  33. data/lib/rexml/parsers/streamparser.rb +6 -2
  34. data/lib/rexml/parsers/treeparser.rb +9 -6
  35. data/lib/rexml/parsers/ultralightparser.rb +40 -40
  36. data/lib/rexml/parsers/xpathparser.rb +51 -52
  37. data/lib/rexml/quickpath.rb +247 -248
  38. data/lib/rexml/rexml.rb +9 -10
  39. data/lib/rexml/sax2listener.rb +92 -92
  40. data/lib/rexml/security.rb +27 -0
  41. data/lib/rexml/source.rb +95 -50
  42. data/lib/rexml/streamlistener.rb +90 -90
  43. data/lib/rexml/syncenumerator.rb +3 -4
  44. data/lib/rexml/text.rb +157 -76
  45. data/lib/rexml/validation/relaxng.rb +18 -18
  46. data/lib/rexml/validation/validation.rb +5 -5
  47. data/lib/rexml/xmldecl.rb +59 -63
  48. data/lib/rexml/xmltokens.rb +14 -14
  49. data/lib/rexml/xpath.rb +67 -53
  50. data/lib/rexml/xpath_parser.rb +49 -38
  51. data/lib/rubysl/rexml.rb +1 -0
  52. data/lib/rubysl/rexml/version.rb +1 -1
  53. data/rubysl-rexml.gemspec +3 -1
  54. metadata +19 -28
  55. data/lib/rexml/encodings/CP-1252.rb +0 -103
  56. data/lib/rexml/encodings/EUC-JP.rb +0 -35
  57. data/lib/rexml/encodings/ICONV.rb +0 -22
  58. data/lib/rexml/encodings/ISO-8859-1.rb +0 -7
  59. data/lib/rexml/encodings/ISO-8859-15.rb +0 -72
  60. data/lib/rexml/encodings/SHIFT-JIS.rb +0 -37
  61. data/lib/rexml/encodings/SHIFT_JIS.rb +0 -1
  62. data/lib/rexml/encodings/UNILE.rb +0 -34
  63. data/lib/rexml/encodings/US-ASCII.rb +0 -30
  64. data/lib/rexml/encodings/UTF-16.rb +0 -35
  65. data/lib/rexml/encodings/UTF-8.rb +0 -18
@@ -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
- # declaration for you. See |DECLARATION| and |write|.
20
- class Document < Element
21
- # A convenient default XML declaration. If you want an XML declaration,
22
- # the easiest way to add one is mydoc << Document::DECLARATION
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
- DECLARATION = XMLDecl.default
26
-
27
- # Constructor
28
- # @param source if supplied, must be a Document, String, or IO.
29
- # Documents have their context and Element attributes cloned.
30
- # Strings are expected to be valid XML documents. IOs are expected
31
- # to be sources of valid XML documents.
32
- # @param context if supplied, contains the context of the document;
33
- # this should be a Hash.
34
- def initialize( source = nil, context = {} )
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
- super()
37
- @context = context
38
- return if source.nil?
39
- if source.kind_of? Document
40
- @context = source.context
41
- super source
42
- else
43
- build( source )
44
- end
45
- end
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
- # Should be obvious
52
- def clone
53
- Document.new self
54
- end
52
+ # Should be obvious
53
+ def clone
54
+ Document.new self
55
+ end
55
56
 
56
- # According to the XML spec, a root node has no expanded name
57
- def expanded_name
58
- ''
59
- #d = doc_type
60
- #d ? d.name : "UNDEFINED"
61
- end
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
- alias :name :expanded_name
64
+ alias :name :expanded_name
64
65
 
65
- # We override this, because XMLDecls and DocTypes must go at the start
66
- # of the document
67
- def add( child )
68
- if child.kind_of? XMLDecl
69
- @children.unshift child
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
- elsif child.kind_of? DocType
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 = 0
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 @children[ insert_before_index ] # Not null = not end of list
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[ index_before_index-1, 0 ] = child
87
+ @children[ insert_before_index-1, 0 ] = child
85
88
  end
86
89
  else # Insert at end of list
87
- @children[insert_before_index] = child
90
+ @children << child
88
91
  end
89
- child.parent = self
90
- else
91
- rv = super
92
- raise "attempted adding second root element to document" if @elements.size > 1
93
- rv
94
- end
95
- end
96
- alias :<< :add
97
-
98
- def add_element(arg=nil, arg2=nil)
99
- rv = super
100
- raise "attempted adding second root element to document" if @elements.size > 1
101
- rv
102
- end
103
-
104
- # @return the root Element of the document, or nil if this document
105
- # has no children.
106
- def root
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
- end
111
-
112
- # @return the DocType child of the document, if one exists,
113
- # and nil otherwise.
114
- def doctype
115
- @children.find { |item| item.kind_of? DocType }
116
- end
117
-
118
- # @return the XMLDecl of this document; if no XMLDecl has been
119
- # set, the default declaration is returned.
120
- def xml_decl
121
- rv = @children[0]
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
- end
125
-
126
- # @return the XMLDecl version of this document as a String.
127
- # If no XMLDecl has been set, returns the default version.
128
- def version
129
- xml_decl().version
130
- end
131
-
132
- # @return the XMLDecl encoding of this document as a String.
133
- # If no XMLDecl has been set, returns the default encoding.
134
- def encoding
135
- xml_decl().encoding
136
- end
137
-
138
- # @return the XMLDecl standalone value of this document as a String.
139
- # If no XMLDecl has been set, returns the default setting.
140
- def stand_alone?
141
- xml_decl().stand_alone?
142
- end
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
- # output_string = ""
161
- # tr = Transitive.new( output_string )
162
- # Document.new("<a><b/></a>").serialize( tr )
180
+ # output = ""
181
+ # tr = Transitive.new
182
+ # tr.write(Document.new("<a><b/></a>"), output)
163
183
  #
164
184
  # output::
165
- # output an object which supports '<< string'; this is where the
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
- # trans::
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
- # Internet Explorer is the worst piece of crap to have ever been
179
- # written, with the possible exception of Windows itself. Since IE is
180
- # unable to parse proper XML, we have to provide a hack to generate XML
181
- # that IE's limited abilities can handle. This hack inserts a space
182
- # before the /> on empty tags. Defaults to false
183
- def write( output=$stdout, indent=-1, trans=false, ie_hack=false )
184
- if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
185
- output = Output.new( output, xml_decl.encoding )
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 trans
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
- end
237
+ end
198
238
 
199
-
200
- def Document::parse_stream( source, listener )
201
- Parsers::StreamParser.new( source, listener ).parse
202
- end
203
239
 
204
- @@entity_expansion_limit = 10_000
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
- @@entity_expansion_limit = val
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 @@entity_expansion_limit
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 > @@entity_expansion_limit
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
- private
226
- def build( source )
281
+ private
282
+ def build( source )
227
283
  Parsers::TreeParser.new( source, self ).parse
228
- end
229
- end
284
+ end
285
+ end
230
286
  end
@@ -1,10 +1,10 @@
1
1
  require "rexml/child"
2
2
  module REXML
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
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
- 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
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
- # 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
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