nokogiri 1.11.1-java → 1.11.2-java

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

Potentially problematic release.


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

Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE-DEPENDENCIES.md +12 -12
  3. data/LICENSE.md +1 -1
  4. data/README.md +20 -15
  5. data/ext/java/nokogiri/EncodingHandler.java +78 -59
  6. data/ext/java/nokogiri/HtmlDocument.java +137 -114
  7. data/ext/java/nokogiri/HtmlElementDescription.java +104 -87
  8. data/ext/java/nokogiri/HtmlEntityLookup.java +31 -26
  9. data/ext/java/nokogiri/HtmlSaxParserContext.java +220 -192
  10. data/ext/java/nokogiri/HtmlSaxPushParser.java +164 -139
  11. data/ext/java/nokogiri/NokogiriService.java +597 -526
  12. data/ext/java/nokogiri/XmlAttr.java +120 -96
  13. data/ext/java/nokogiri/XmlAttributeDecl.java +97 -76
  14. data/ext/java/nokogiri/XmlCdata.java +35 -26
  15. data/ext/java/nokogiri/XmlComment.java +48 -37
  16. data/ext/java/nokogiri/XmlDocument.java +642 -540
  17. data/ext/java/nokogiri/XmlDocumentFragment.java +127 -107
  18. data/ext/java/nokogiri/XmlDtd.java +450 -384
  19. data/ext/java/nokogiri/XmlElement.java +25 -18
  20. data/ext/java/nokogiri/XmlElementContent.java +345 -286
  21. data/ext/java/nokogiri/XmlElementDecl.java +126 -95
  22. data/ext/java/nokogiri/XmlEntityDecl.java +121 -97
  23. data/ext/java/nokogiri/XmlEntityReference.java +51 -42
  24. data/ext/java/nokogiri/XmlNamespace.java +177 -145
  25. data/ext/java/nokogiri/XmlNode.java +1843 -1588
  26. data/ext/java/nokogiri/XmlNodeSet.java +361 -299
  27. data/ext/java/nokogiri/XmlProcessingInstruction.java +49 -39
  28. data/ext/java/nokogiri/XmlReader.java +513 -418
  29. data/ext/java/nokogiri/XmlRelaxng.java +91 -78
  30. data/ext/java/nokogiri/XmlSaxParserContext.java +330 -285
  31. data/ext/java/nokogiri/XmlSaxPushParser.java +229 -190
  32. data/ext/java/nokogiri/XmlSchema.java +328 -263
  33. data/ext/java/nokogiri/XmlSyntaxError.java +113 -83
  34. data/ext/java/nokogiri/XmlText.java +57 -46
  35. data/ext/java/nokogiri/XmlXpathContext.java +240 -206
  36. data/ext/java/nokogiri/XsltStylesheet.java +282 -239
  37. data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
  38. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +199 -168
  39. data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
  40. data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
  41. data/ext/java/nokogiri/internals/NokogiriDomParser.java +65 -50
  42. data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
  43. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +25 -18
  44. data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -254
  45. data/ext/java/nokogiri/internals/NokogiriHelpers.java +738 -622
  46. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +186 -143
  47. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +83 -68
  48. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +66 -49
  49. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +86 -69
  50. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +44 -29
  51. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +118 -101
  52. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -24
  53. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +25 -17
  54. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +57 -42
  55. data/ext/java/nokogiri/internals/ParserContext.java +206 -179
  56. data/ext/java/nokogiri/internals/ReaderNode.java +478 -371
  57. data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -707
  58. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +28 -19
  59. data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
  60. data/ext/java/nokogiri/internals/XmlDeclHandler.java +5 -4
  61. data/ext/java/nokogiri/internals/XmlDomParserContext.java +208 -177
  62. data/ext/java/nokogiri/internals/XmlSaxParser.java +24 -17
  63. data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
  64. data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
  65. data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
  66. data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
  67. data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
  68. data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
  69. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
  70. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
  71. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
  72. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
  73. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
  74. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
  75. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
  76. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
  77. data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
  78. data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
  79. data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
  80. data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
  81. data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
  82. data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
  83. data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
  84. data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
  85. data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
  86. data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
  87. data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
  88. data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
  89. data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
  90. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
  91. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -570
  92. data/ext/nokogiri/depend +34 -474
  93. data/ext/nokogiri/extconf.rb +253 -183
  94. data/ext/nokogiri/html_document.c +10 -15
  95. data/ext/nokogiri/html_element_description.c +84 -71
  96. data/ext/nokogiri/html_entity_lookup.c +21 -16
  97. data/ext/nokogiri/html_sax_parser_context.c +66 -65
  98. data/ext/nokogiri/html_sax_push_parser.c +29 -27
  99. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  100. data/ext/nokogiri/nokogiri.c +171 -63
  101. data/ext/nokogiri/test_global_handlers.c +3 -4
  102. data/ext/nokogiri/xml_attr.c +15 -15
  103. data/ext/nokogiri/xml_attribute_decl.c +18 -18
  104. data/ext/nokogiri/xml_cdata.c +13 -18
  105. data/ext/nokogiri/xml_comment.c +19 -26
  106. data/ext/nokogiri/xml_document.c +221 -164
  107. data/ext/nokogiri/xml_document_fragment.c +13 -15
  108. data/ext/nokogiri/xml_dtd.c +54 -48
  109. data/ext/nokogiri/xml_element_content.c +30 -27
  110. data/ext/nokogiri/xml_element_decl.c +22 -22
  111. data/ext/nokogiri/xml_encoding_handler.c +17 -11
  112. data/ext/nokogiri/xml_entity_decl.c +32 -30
  113. data/ext/nokogiri/xml_entity_reference.c +16 -18
  114. data/ext/nokogiri/xml_namespace.c +56 -49
  115. data/ext/nokogiri/xml_node.c +338 -286
  116. data/ext/nokogiri/xml_node_set.c +168 -156
  117. data/ext/nokogiri/xml_processing_instruction.c +17 -19
  118. data/ext/nokogiri/xml_reader.c +191 -157
  119. data/ext/nokogiri/xml_relax_ng.c +29 -23
  120. data/ext/nokogiri/xml_sax_parser.c +117 -112
  121. data/ext/nokogiri/xml_sax_parser_context.c +100 -85
  122. data/ext/nokogiri/xml_sax_push_parser.c +34 -27
  123. data/ext/nokogiri/xml_schema.c +48 -42
  124. data/ext/nokogiri/xml_syntax_error.c +21 -23
  125. data/ext/nokogiri/xml_text.c +13 -17
  126. data/ext/nokogiri/xml_xpath_context.c +134 -127
  127. data/ext/nokogiri/xslt_stylesheet.c +157 -157
  128. data/lib/nokogiri.rb +1 -22
  129. data/lib/nokogiri/css/parser.rb +1 -1
  130. data/lib/nokogiri/extension.rb +26 -0
  131. data/lib/nokogiri/html/document_fragment.rb +15 -15
  132. data/lib/nokogiri/nokogiri.jar +0 -0
  133. data/lib/nokogiri/version/constant.rb +1 -1
  134. data/lib/nokogiri/version/info.rb +31 -8
  135. data/lib/nokogiri/xml/document.rb +31 -11
  136. data/lib/nokogiri/xml/node.rb +38 -42
  137. data/lib/nokogiri/xml/reader.rb +2 -9
  138. data/lib/nokogiri/xml/xpath.rb +1 -3
  139. data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
  140. metadata +7 -8
  141. data/ext/nokogiri/xml_io.c +0 -63
  142. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
@@ -62,427 +62,534 @@ import static nokogiri.internals.NokogiriHelpers.*;
62
62
  * @author Yoko Harada <yokolet@gmail.com>
63
63
  *
64
64
  */
65
- public abstract class ReaderNode {
66
-
67
- final Ruby ruby;
68
- public ReaderAttributeList attributeList;
69
- public Map<String, String> namespaces;
70
- public int depth, nodeType;
71
- public String lang, localName, xmlBase, prefix, name, uri, value, xmlVersion = "1.0";
72
- public int startOffset, endOffset;
73
- public boolean hasChildren = false;
74
- private Document document = null;
75
-
76
- protected ReaderNode(final Ruby runtime) {
77
- this.ruby = runtime;
65
+ public abstract class ReaderNode
66
+ {
67
+
68
+ final Ruby ruby;
69
+ public ReaderAttributeList attributeList;
70
+ public Map<String, String> namespaces;
71
+ public int depth, nodeType;
72
+ public String lang, localName, xmlBase, prefix, name, uri, value, xmlVersion = "1.0";
73
+ public int startOffset, endOffset;
74
+ public boolean hasChildren = false;
75
+ private Document document = null;
76
+
77
+ protected
78
+ ReaderNode(final Ruby runtime)
79
+ {
80
+ this.ruby = runtime;
81
+ }
82
+
83
+ public abstract String getString();
84
+
85
+ public IRubyObject
86
+ getAttributeByIndex(IRubyObject index)
87
+ {
88
+ if (index.isNil()) { return index; }
89
+
90
+ long i = index.convertToInteger().getLongValue();
91
+ if (i > Integer.MAX_VALUE) {
92
+ throw ruby.newArgumentError("value too long to be an array index");
78
93
  }
79
94
 
80
- public abstract String getString();
81
-
82
- public IRubyObject getAttributeByIndex(IRubyObject index){
83
- if(index.isNil()) return index;
84
-
85
- long i = index.convertToInteger().getLongValue();
86
- if(i > Integer.MAX_VALUE) {
87
- throw ruby.newArgumentError("value too long to be an array index");
95
+ if (attributeList == null) { return ruby.getNil(); }
96
+ if (i < 0 || attributeList.length <= i) { return ruby.getNil(); }
97
+ return stringOrBlank(ruby, attributeList.values.get(((Long)i).intValue()));
98
+ }
99
+
100
+ public IRubyObject
101
+ getAttributeByName(IRubyObject name)
102
+ {
103
+ if (attributeList == null) { return ruby.getNil(); }
104
+ String value = attributeList.getByName(rubyStringToString(name));
105
+ return stringOrNil(ruby, value);
106
+ }
107
+
108
+ public IRubyObject
109
+ getAttributeByName(String name)
110
+ {
111
+ if (attributeList == null) { return ruby.getNil(); }
112
+ String value = attributeList.getByName(name);
113
+ return stringOrNil(ruby, value);
114
+ }
115
+
116
+ public IRubyObject
117
+ getAttributeCount()
118
+ {
119
+ if (attributeList == null) { return ruby.newFixnum(0); }
120
+ return ruby.newFixnum(attributeList.length);
121
+ }
122
+
123
+ public IRubyObject
124
+ getAttributesNodes()
125
+ {
126
+ RubyArray array = RubyArray.newArray(ruby);
127
+ if (attributeList != null && attributeList.length > 0) {
128
+ if (document == null) {
129
+ document = XmlDocument.createNewDocument(ruby);
130
+ }
131
+ for (int i = 0; i < attributeList.length; i++) {
132
+ if (!isNamespace(attributeList.names.get(i))) {
133
+ Attr attr = document.createAttributeNS(attributeList.namespaces.get(i), attributeList.names.get(i));
134
+ attr.setValue(attributeList.values.get(i));
135
+ XmlAttr xmlAttr = new XmlAttr(ruby, attr);
136
+ array.append(xmlAttr);
88
137
  }
89
-
90
- if (attributeList == null) return ruby.getNil();
91
- if (i<0 || attributeList.length <= i) return ruby.getNil();
92
- return stringOrBlank(ruby, attributeList.values.get(((Long)i).intValue()));
138
+ }
93
139
  }
94
-
95
- public IRubyObject getAttributeByName(IRubyObject name){
96
- if(attributeList == null) return ruby.getNil();
97
- String value = attributeList.getByName(rubyStringToString(name));
98
- return stringOrNil(ruby, value);
140
+ return array;
141
+ }
142
+
143
+ public IRubyObject
144
+ getAttributes(ThreadContext context)
145
+ {
146
+ final Ruby runtime = context.runtime;
147
+ if (attributeList == null) { return runtime.getNil(); }
148
+ RubyHash hash = RubyHash.newHash(runtime);
149
+ for (int i = 0; i < attributeList.length; i++) {
150
+ IRubyObject k = stringOrBlank(runtime, attributeList.names.get(i));
151
+ IRubyObject v = stringOrBlank(runtime, attributeList.values.get(i));
152
+ hash.fastASetCheckString(runtime, k, v); // hash.op_aset(context, k, v)
99
153
  }
100
-
101
- public IRubyObject getAttributeByName(String name){
102
- if(attributeList == null) return ruby.getNil();
103
- String value = attributeList.getByName(name);
104
- return stringOrNil(ruby, value);
154
+ return hash;
155
+ }
156
+
157
+ public IRubyObject
158
+ getDepth()
159
+ {
160
+ return ruby.newFixnum(depth);
161
+ }
162
+
163
+ public IRubyObject
164
+ getLang()
165
+ {
166
+ return stringOrNil(ruby, lang);
167
+ }
168
+
169
+ public IRubyObject
170
+ getLocalName()
171
+ {
172
+ return stringOrNil(ruby, localName);
173
+ }
174
+
175
+ public IRubyObject
176
+ getName()
177
+ {
178
+ return stringOrNil(ruby, name);
179
+ }
180
+
181
+ public IRubyObject
182
+ getNamespaces(ThreadContext context)
183
+ {
184
+ final Ruby runtime = context.runtime;
185
+ if (namespaces == null) { return runtime.getNil(); }
186
+ RubyHash hash = RubyHash.newHash(runtime);
187
+ for (Map.Entry<String, String> entry : namespaces.entrySet()) {
188
+ IRubyObject k = stringOrBlank(runtime, entry.getKey());
189
+ IRubyObject v = stringOrBlank(runtime, entry.getValue());
190
+ hash.fastASetCheckString(runtime, k, v); // hash.op_aset(context, k, v)
105
191
  }
106
-
107
- public IRubyObject getAttributeCount(){
108
- if(attributeList == null) return ruby.newFixnum(0);
109
- return ruby.newFixnum(attributeList.length);
192
+ return hash;
193
+ }
194
+
195
+ public IRubyObject
196
+ getXmlBase()
197
+ {
198
+ return stringOrNil(ruby, xmlBase);
199
+ }
200
+
201
+ public IRubyObject
202
+ getPrefix()
203
+ {
204
+ return stringOrNil(ruby, prefix);
205
+ }
206
+
207
+ public IRubyObject
208
+ getUri()
209
+ {
210
+ return stringOrNil(ruby, uri);
211
+ }
212
+
213
+ public IRubyObject
214
+ getValue()
215
+ {
216
+ return stringOrNil(ruby, value);
217
+ }
218
+
219
+ public IRubyObject
220
+ getXmlVersion()
221
+ {
222
+ return ruby.newString(xmlVersion);
223
+ }
224
+
225
+ public RubyBoolean
226
+ hasAttributes()
227
+ {
228
+ if (attributeList == null || attributeList.length == 0) { return ruby.getFalse(); }
229
+ return ruby.getTrue();
230
+ }
231
+
232
+ public abstract RubyBoolean hasValue();
233
+
234
+ public RubyBoolean
235
+ isDefault()
236
+ {
237
+ // TODO Implement.
238
+ return ruby.getFalse();
239
+ }
240
+
241
+ public boolean
242
+ isError() { return false; }
243
+
244
+ protected void
245
+ parsePrefix(String qName)
246
+ {
247
+ int index = qName.indexOf(':');
248
+ if (index != -1) { prefix = qName.substring(0, index); }
249
+ }
250
+
251
+ public void
252
+ setLang(String lang)
253
+ {
254
+ this.lang = lang;
255
+ }
256
+
257
+ public IRubyObject
258
+ toSyntaxError() { return ruby.getNil(); }
259
+
260
+ public IRubyObject
261
+ getNodeType() { return ruby.newFixnum(nodeType); }
262
+
263
+ public static enum ReaderNodeType {
264
+ NODE(0),
265
+ ELEMENT(1),
266
+ ATTRIBUTE(2),
267
+ TEXT(3),
268
+ CDATA(4),
269
+ ENTITY_REFERENCE(5),
270
+ ENTITY(6),
271
+ PROCESSING_INSTRUCTION(7),
272
+ COMMENT(8),
273
+ DOCUMENT(9),
274
+ DOCUMENT_TYPE(10),
275
+ DOCUMENTFRAGMENT(11),
276
+ NOTATION(12),
277
+ WHITESPACE(13),
278
+ SIGNIFICANT_WHITESPACE(14),
279
+ END_ELEMENT(15),
280
+ END_ENTITY(16),
281
+ XML_DECLARATION(17);
282
+
283
+ private final int value;
284
+ ReaderNodeType(int value)
285
+ {
286
+ this.value = value;
110
287
  }
111
288
 
112
- public IRubyObject getAttributesNodes() {
113
- RubyArray array = RubyArray.newArray(ruby);
114
- if (attributeList != null && attributeList.length > 0) {
115
- if (document == null) {
116
- document = XmlDocument.createNewDocument(ruby);
117
- }
118
- for (int i=0; i<attributeList.length; i++) {
119
- if (!isNamespace(attributeList.names.get(i))) {
120
- Attr attr = document.createAttributeNS(attributeList.namespaces.get(i), attributeList.names.get(i));
121
- attr.setValue(attributeList.values.get(i));
122
- XmlAttr xmlAttr = new XmlAttr(ruby, attr);
123
- array.append(xmlAttr);
124
- }
125
- }
126
- }
127
- return array;
289
+ public int getValue()
290
+ {
291
+ return value;
128
292
  }
129
-
130
- public IRubyObject getAttributes(ThreadContext context) {
131
- final Ruby runtime = context.runtime;
132
- if (attributeList == null) return runtime.getNil();
133
- RubyHash hash = RubyHash.newHash(runtime);
134
- for (int i=0; i<attributeList.length; i++) {
135
- IRubyObject k = stringOrBlank(runtime, attributeList.names.get(i));
136
- IRubyObject v = stringOrBlank(runtime, attributeList.values.get(i));
137
- hash.fastASetCheckString(runtime, k, v); // hash.op_aset(context, k, v)
138
- }
139
- return hash;
293
+ }
294
+
295
+ public static ClosingNode
296
+ createClosingNode(Ruby ruby, String uri, String localName, String qName, int depth, Stack<String> langStack,
297
+ Stack<String> xmlBaseStack)
298
+ {
299
+ return new ClosingNode(ruby, uri, localName, qName, depth, langStack, xmlBaseStack);
300
+ }
301
+
302
+ public static class ClosingNode extends ReaderNode
303
+ {
304
+
305
+ // public ClosingNode() {}
306
+
307
+ ClosingNode(Ruby runtime, String uri, String localName, String qName, int depth, Stack<String> langStack,
308
+ Stack<String> xmlBaseStack)
309
+ {
310
+ super(runtime);
311
+ nodeType = ReaderNodeType.END_ELEMENT.getValue();
312
+ this.uri = "".equals(uri) ? null : uri;
313
+ this.localName = ! isBlank(localName) ? localName : qName;
314
+ this.name = qName;
315
+ parsePrefix(qName);
316
+ this.depth = depth;
317
+ if (!langStack.isEmpty()) { this.lang = langStack.peek(); }
318
+ if (!xmlBaseStack.isEmpty()) { this.xmlBase = xmlBaseStack.peek(); }
140
319
  }
141
320
 
142
- public IRubyObject getDepth() {
143
- return ruby.newFixnum(depth);
321
+ @Override
322
+ public IRubyObject
323
+ getAttributeCount()
324
+ {
325
+ return ruby.newFixnum(0);
144
326
  }
145
327
 
146
- public IRubyObject getLang() {
147
- return stringOrNil(ruby, lang);
328
+ @Override
329
+ public RubyBoolean
330
+ hasValue()
331
+ {
332
+ return ruby.getFalse();
148
333
  }
149
334
 
150
- public IRubyObject getLocalName() {
151
- return stringOrNil(ruby, localName);
335
+ @Override
336
+ public String
337
+ getString()
338
+ {
339
+ return "</" + name + '>';
340
+ }
341
+ }
342
+
343
+ public static ElementNode
344
+ createElementNode(Ruby ruby, String uri, String localName, String qName, XMLAttributes attrs, int depth,
345
+ Stack<String> langStack, Stack<String> xmlBaseStack)
346
+ {
347
+ return new ElementNode(ruby, uri, localName, qName, attrs, depth, langStack, xmlBaseStack);
348
+ }
349
+
350
+ public static class ElementNode extends ReaderNode
351
+ {
352
+
353
+ // public ElementNode() {}
354
+
355
+ ElementNode(Ruby runtime, String uri, String localName, String qName, XMLAttributes attrs, int depth,
356
+ Stack<String> langStack, Stack<String> xmlBaseStack)
357
+ {
358
+ super(runtime);
359
+ this.nodeType = ReaderNodeType.ELEMENT.getValue();
360
+ this.uri = "".equals(uri) ? null : uri;
361
+ this.localName = ! isBlank(localName) ? localName : qName;
362
+ this.name = qName;
363
+ parsePrefix(qName);
364
+ this.depth = depth;
365
+ parseAttributes(attrs, langStack, xmlBaseStack);
152
366
  }
153
367
 
154
- public IRubyObject getName() {
155
- return stringOrNil(ruby, name);
368
+ @Override
369
+ public RubyBoolean
370
+ hasValue()
371
+ {
372
+ return ruby.getFalse();
156
373
  }
157
374
 
158
- public IRubyObject getNamespaces(ThreadContext context) {
159
- final Ruby runtime = context.runtime;
160
- if (namespaces == null) return runtime.getNil();
161
- RubyHash hash = RubyHash.newHash(runtime);
162
- for (Map.Entry<String, String> entry : namespaces.entrySet()) {
163
- IRubyObject k = stringOrBlank(runtime, entry.getKey());
164
- IRubyObject v = stringOrBlank(runtime, entry.getValue());
165
- hash.fastASetCheckString(runtime, k, v); // hash.op_aset(context, k, v)
375
+ private void
376
+ parseAttributes(XMLAttributes attrs, Stack<String> langStack, Stack<String> xmlBaseStack)
377
+ {
378
+ if (attrs.getLength() > 0) { attributeList = new ReaderAttributeList(); }
379
+ String u, n, v;
380
+ for (int i = 0; i < attrs.getLength(); i++) {
381
+ u = attrs.getURI(i);
382
+ n = attrs.getQName(i);
383
+ v = attrs.getValue(i);
384
+ if (isNamespace(n)) {
385
+ if (namespaces == null) { namespaces = new HashMap<String, String>(); }
386
+ namespaces.put(n, v);
387
+ } else {
388
+ if (lang == null) { lang = resolveLang(n, v, langStack); }
389
+ if (xmlBase == null) { xmlBase = resolveXmlBase(n, v, xmlBaseStack); }
166
390
  }
167
- return hash;
391
+ attributeList.add(u, n, v);
392
+ }
168
393
  }
169
394
 
170
- public IRubyObject getXmlBase() {
171
- return stringOrNil(ruby, xmlBase);
395
+ private String
396
+ resolveLang(String n, String v, Stack<String> langStack)
397
+ {
398
+ if ("xml:lang".equals(n)) {
399
+ return v;
400
+ } else if (!langStack.isEmpty()) {
401
+ return langStack.peek();
402
+ } else {
403
+ return null;
404
+ }
172
405
  }
173
406
 
174
- public IRubyObject getPrefix() {
175
- return stringOrNil(ruby, prefix);
407
+ private String
408
+ resolveXmlBase(String n, String v, Stack<String> xmlBaseStack)
409
+ {
410
+ if (isXmlBase(n)) {
411
+ return getXmlBaseUri(n, v, xmlBaseStack);
412
+ } else if (!xmlBaseStack.isEmpty()) {
413
+ return xmlBaseStack.peek();
414
+ } else {
415
+ return null;
416
+ }
176
417
  }
177
418
 
178
- public IRubyObject getUri() {
179
- return stringOrNil(ruby, uri);
419
+ private String
420
+ getXmlBaseUri(String n, String v, Stack<String> xmlBaseStack)
421
+ {
422
+ if ("xml:base".equals(n)) {
423
+ if (v.startsWith("http://")) {
424
+ return v;
425
+ } else if (v.startsWith("/") && v.endsWith("/")) {
426
+ String sub = v.substring(1, v.length() - 2);
427
+ String base = xmlBaseStack.peek();
428
+ if (base.endsWith("/")) {
429
+ base = base.substring(0, base.length() - 1);
430
+ }
431
+ int pos = base.lastIndexOf("/");
432
+ return base.substring(0, pos).concat(sub);
433
+ } else {
434
+ String base = xmlBaseStack.peek();
435
+ if (base.endsWith("/")) { return base.concat(v); }
436
+ else { return base.concat("/").concat(v); }
437
+ }
438
+ } else if ("xlink:href".equals(n)) {
439
+ if (v.startsWith("http://")) {
440
+ return v;
441
+ } else if (!xmlBaseStack.isEmpty()) {
442
+ String base = xmlBaseStack.peek();
443
+ return base;
444
+ }
445
+ }
446
+ return null;
180
447
  }
181
448
 
182
- public IRubyObject getValue() {
183
- return stringOrNil(ruby, value);
449
+ @Override
450
+ public String
451
+ getString()
452
+ {
453
+ StringBuffer sb = new StringBuffer(24);
454
+ sb.append('<').append(name);
455
+ if (attributeList != null) {
456
+ for (int i = 0; i < attributeList.length; i++) {
457
+ String n = attributeList.names.get(i);
458
+ String v = attributeList.values.get(i);
459
+ sb.append(' ').append(n).append('=')
460
+ .append('"').append(v).append('"');
461
+ }
462
+ }
463
+ if (hasChildren) { sb.append('>'); }
464
+ else { sb.append("/>"); }
465
+ return sb.toString();
184
466
  }
185
-
186
- public IRubyObject getXmlVersion() {
187
- return ruby.newString(xmlVersion);
467
+ }
468
+
469
+ private static class ReaderAttributeList
470
+ {
471
+ final List<String> namespaces = new ArrayList<String>();
472
+ final List<String> names = new ArrayList<String>();
473
+ final List<String> values = new ArrayList<String>();
474
+ int length = 0;
475
+
476
+ void
477
+ add(String namespace, String name, String value)
478
+ {
479
+ namespaces.add(namespace != null ? namespace : "");
480
+ names.add(name != null ? name : "");
481
+ values.add(value != null ? value : "");
482
+ length++;
188
483
  }
189
484
 
190
- public RubyBoolean hasAttributes() {
191
- if (attributeList == null || attributeList.length == 0) return ruby.getFalse();
192
- return ruby.getTrue();
485
+ String
486
+ getByName(String name)
487
+ {
488
+ for (int i = 0; i < names.size(); i++) {
489
+ if (name.equals(names.get(i))) {
490
+ return values.get(i);
491
+ }
492
+ }
493
+ return null;
193
494
  }
495
+ }
194
496
 
195
- public abstract RubyBoolean hasValue();
497
+ public static class EmptyNode extends ReaderNode
498
+ {
196
499
 
197
- public RubyBoolean isDefault(){
198
- // TODO Implement.
199
- return ruby.getFalse();
500
+ public
501
+ EmptyNode(Ruby runtime)
502
+ {
503
+ super(runtime);
504
+ this.nodeType = ReaderNodeType.NODE.getValue();
200
505
  }
201
506
 
202
- public boolean isError() { return false; }
203
-
204
- protected void parsePrefix(String qName) {
205
- int index = qName.indexOf(':');
206
- if(index != -1) prefix = qName.substring(0, index);
507
+ @Override
508
+ public IRubyObject
509
+ getXmlVersion()
510
+ {
511
+ return this.ruby.getNil();
207
512
  }
208
513
 
209
- public void setLang(String lang) {
210
- this.lang = lang;
514
+ @Override
515
+ public RubyBoolean
516
+ hasValue()
517
+ {
518
+ return ruby.getFalse();
211
519
  }
212
520
 
213
- public IRubyObject toSyntaxError() { return ruby.getNil(); }
214
-
215
- public IRubyObject getNodeType() { return ruby.newFixnum(nodeType); }
216
-
217
- public static enum ReaderNodeType {
218
- NODE(0),
219
- ELEMENT(1),
220
- ATTRIBUTE(2),
221
- TEXT(3),
222
- CDATA(4),
223
- ENTITY_REFERENCE(5),
224
- ENTITY(6),
225
- PROCESSING_INSTRUCTION(7),
226
- COMMENT(8),
227
- DOCUMENT(9),
228
- DOCUMENT_TYPE(10),
229
- DOCUMENTFRAGMENT(11),
230
- NOTATION(12),
231
- WHITESPACE(13),
232
- SIGNIFICANT_WHITESPACE(14),
233
- END_ELEMENT(15),
234
- END_ENTITY(16),
235
- XML_DECLARATION(17);
236
-
237
- private final int value;
238
- ReaderNodeType(int value) {
239
- this.value = value;
240
- }
241
-
242
- public int getValue() {
243
- return value;
244
- }
521
+ @Override
522
+ public String
523
+ getString()
524
+ {
525
+ return null;
245
526
  }
246
-
247
- public static ClosingNode createClosingNode(Ruby ruby, String uri, String localName, String qName, int depth, Stack<String> langStack, Stack<String> xmlBaseStack) {
248
- return new ClosingNode(ruby, uri, localName, qName, depth, langStack, xmlBaseStack);
527
+ }
528
+
529
+ public static class ExceptionNode extends EmptyNode
530
+ {
531
+ private final XmlSyntaxError exception;
532
+
533
+ public
534
+ ExceptionNode(Ruby runtime, Exception ex)
535
+ {
536
+ super(runtime);
537
+ exception = XmlSyntaxError.createXMLSyntaxError(runtime); // Nokogiri::XML::SyntaxError
538
+ exception.setException(ex);
249
539
  }
250
540
 
251
- public static class ClosingNode extends ReaderNode {
252
-
253
- // public ClosingNode() {}
254
-
255
- ClosingNode(Ruby runtime, String uri, String localName, String qName, int depth, Stack<String> langStack, Stack<String> xmlBaseStack) {
256
- super(runtime);
257
- nodeType = ReaderNodeType.END_ELEMENT.getValue();
258
- this.uri = "".equals(uri) ? null : uri;
259
- this.localName = ! isBlank(localName) ? localName : qName;
260
- this.name = qName;
261
- parsePrefix(qName);
262
- this.depth = depth;
263
- if (!langStack.isEmpty()) this.lang = langStack.peek();
264
- if (!xmlBaseStack.isEmpty()) this.xmlBase = xmlBaseStack.peek();
265
- }
266
-
267
- @Override
268
- public IRubyObject getAttributeCount() {
269
- return ruby.newFixnum(0);
270
- }
271
-
272
- @Override
273
- public RubyBoolean hasValue() {
274
- return ruby.getFalse();
275
- }
276
-
277
- @Override
278
- public String getString() {
279
- return "</" + name + '>';
280
- }
541
+ @Override
542
+ public boolean
543
+ isError()
544
+ {
545
+ return true;
281
546
  }
282
547
 
283
- public static ElementNode createElementNode(Ruby ruby, String uri, String localName, String qName, XMLAttributes attrs, int depth, Stack<String> langStack, Stack<String> xmlBaseStack) {
284
- return new ElementNode(ruby, uri, localName, qName, attrs, depth, langStack, xmlBaseStack);
548
+ @Override
549
+ public IRubyObject
550
+ toSyntaxError()
551
+ {
552
+ return this.exception;
285
553
  }
286
-
287
- public static class ElementNode extends ReaderNode {
288
-
289
- // public ElementNode() {}
290
-
291
- ElementNode(Ruby runtime, String uri, String localName, String qName, XMLAttributes attrs, int depth, Stack<String> langStack, Stack<String> xmlBaseStack) {
292
- super(runtime);
293
- this.nodeType = ReaderNodeType.ELEMENT.getValue();
294
- this.uri = "".equals(uri) ? null : uri;
295
- this.localName = ! isBlank(localName) ? localName : qName;
296
- this.name = qName;
297
- parsePrefix(qName);
298
- this.depth = depth;
299
- parseAttributes(attrs, langStack, xmlBaseStack);
300
- }
301
-
302
- @Override
303
- public RubyBoolean hasValue() {
304
- return ruby.getFalse();
305
- }
306
-
307
- private void parseAttributes(XMLAttributes attrs, Stack<String> langStack, Stack<String> xmlBaseStack) {
308
- if (attrs.getLength() > 0) attributeList = new ReaderAttributeList();
309
- String u, n, v;
310
- for (int i = 0; i < attrs.getLength(); i++) {
311
- u = attrs.getURI(i);
312
- n = attrs.getQName(i);
313
- v = attrs.getValue(i);
314
- if (isNamespace(n)) {
315
- if (namespaces == null) namespaces = new HashMap<String, String>();
316
- namespaces.put(n, v);
317
- } else {
318
- if (lang == null) lang = resolveLang(n, v, langStack);
319
- if (xmlBase == null) xmlBase = resolveXmlBase(n, v, xmlBaseStack);
320
- }
321
- attributeList.add(u, n, v);
322
- }
323
- }
324
-
325
- private String resolveLang(String n, String v, Stack<String> langStack) {
326
- if ("xml:lang".equals(n)) {
327
- return v;
328
- } else if (!langStack.isEmpty()) {
329
- return langStack.peek();
330
- } else {
331
- return null;
332
- }
333
- }
334
-
335
- private String resolveXmlBase(String n, String v, Stack<String> xmlBaseStack) {
336
- if (isXmlBase(n)) {
337
- return getXmlBaseUri(n, v, xmlBaseStack);
338
- } else if (!xmlBaseStack.isEmpty()) {
339
- return xmlBaseStack.peek();
340
- } else {
341
- return null;
342
- }
343
- }
344
-
345
- private String getXmlBaseUri(String n, String v, Stack<String> xmlBaseStack) {
346
- if ("xml:base".equals(n)) {
347
- if (v.startsWith("http://")) {
348
- return v;
349
- } else if (v.startsWith("/") && v.endsWith("/")) {
350
- String sub = v.substring(1, v.length() - 2);
351
- String base = xmlBaseStack.peek();
352
- if (base.endsWith("/")) {
353
- base = base.substring(0, base.length() - 1);
354
- }
355
- int pos = base.lastIndexOf("/");
356
- return base.substring(0, pos).concat(sub);
357
- } else {
358
- String base = xmlBaseStack.peek();
359
- if (base.endsWith("/")) return base.concat(v);
360
- else return base.concat("/").concat(v);
361
- }
362
- } else if ("xlink:href".equals(n)) {
363
- if (v.startsWith("http://")) {
364
- return v;
365
- } else if (!xmlBaseStack.isEmpty()) {
366
- String base = xmlBaseStack.peek();
367
- return base;
368
- }
369
- }
370
- return null;
371
- }
372
-
373
- @Override
374
- public String getString() {
375
- StringBuffer sb = new StringBuffer(24);
376
- sb.append('<').append(name);
377
- if (attributeList != null) {
378
- for (int i=0; i<attributeList.length; i++) {
379
- String n = attributeList.names.get(i);
380
- String v = attributeList.values.get(i);
381
- sb.append(' ').append(n).append('=')
382
- .append('"').append(v).append('"');
383
- }
384
- }
385
- if (hasChildren) sb.append('>');
386
- else sb.append("/>");
387
- return sb.toString();
388
- }
554
+ }
555
+
556
+ public static TextNode
557
+ createTextNode(Ruby ruby, String content, int depth, Stack<String> langStack, Stack<String> xmlBaseStack)
558
+ {
559
+ return new TextNode(ruby, content, depth, langStack, xmlBaseStack);
560
+ }
561
+
562
+ public static class TextNode extends ReaderNode
563
+ {
564
+
565
+ // public TextNode() {}
566
+
567
+ TextNode(Ruby runtime, String content, int depth, Stack<String> langStack, Stack<String> xmlBaseStack)
568
+ {
569
+ super(runtime);
570
+ this.value = content;
571
+ this.localName = "#text";
572
+ this.name = "#text";
573
+ this.depth = depth;
574
+ if (!isBlank(content)) { nodeType = ReaderNodeType.TEXT.getValue(); }
575
+ else { nodeType = ReaderNodeType.SIGNIFICANT_WHITESPACE.getValue(); }
576
+ if (!langStack.isEmpty()) { this.lang = langStack.peek(); }
577
+ if (!xmlBaseStack.isEmpty()) { this.xmlBase = xmlBaseStack.peek(); }
389
578
  }
390
579
 
391
- private static class ReaderAttributeList {
392
- final List<String> namespaces = new ArrayList<String>();
393
- final List<String> names = new ArrayList<String>();
394
- final List<String> values = new ArrayList<String>();
395
- int length = 0;
396
-
397
- void add(String namespace, String name, String value) {
398
- namespaces.add(namespace != null ? namespace : "");
399
- names.add(name != null ? name : "");
400
- values.add(value != null ? value : "");
401
- length++;
402
- }
403
-
404
- String getByName(String name) {
405
- for (int i=0; i<names.size(); i++) {
406
- if (name.equals(names.get(i))) {
407
- return values.get(i);
408
- }
409
- }
410
- return null;
411
- }
580
+ @Override
581
+ public RubyBoolean
582
+ hasValue()
583
+ {
584
+ return ruby.getTrue();
412
585
  }
413
586
 
414
- public static class EmptyNode extends ReaderNode {
415
-
416
- public EmptyNode(Ruby runtime) {
417
- super(runtime);
418
- this.nodeType = ReaderNodeType.NODE.getValue();
419
- }
420
-
421
- @Override
422
- public IRubyObject getXmlVersion() {
423
- return this.ruby.getNil();
424
- }
425
-
426
- @Override
427
- public RubyBoolean hasValue() {
428
- return ruby.getFalse();
429
- }
430
-
431
- @Override
432
- public String getString() {
433
- return null;
434
- }
435
- }
436
-
437
- public static class ExceptionNode extends EmptyNode {
438
- private final XmlSyntaxError exception;
439
-
440
- public ExceptionNode(Ruby runtime, Exception ex) {
441
- super(runtime);
442
- exception = XmlSyntaxError.createXMLSyntaxError(runtime); // Nokogiri::XML::SyntaxError
443
- exception.setException(ex);
444
- }
445
-
446
- @Override
447
- public boolean isError() {
448
- return true;
449
- }
450
-
451
- @Override
452
- public IRubyObject toSyntaxError() {
453
- return this.exception;
454
- }
455
- }
456
-
457
- public static TextNode createTextNode(Ruby ruby, String content, int depth, Stack<String> langStack, Stack<String> xmlBaseStack) {
458
- return new TextNode(ruby, content, depth, langStack, xmlBaseStack);
459
- }
460
-
461
- public static class TextNode extends ReaderNode {
462
-
463
- // public TextNode() {}
464
-
465
- TextNode(Ruby runtime, String content, int depth, Stack<String> langStack, Stack<String> xmlBaseStack) {
466
- super(runtime);
467
- this.value = content;
468
- this.localName = "#text";
469
- this.name = "#text";
470
- this.depth = depth;
471
- if (!isBlank(content)) nodeType = ReaderNodeType.TEXT.getValue();
472
- else nodeType = ReaderNodeType.SIGNIFICANT_WHITESPACE.getValue();
473
- if (!langStack.isEmpty()) this.lang = langStack.peek();
474
- if (!xmlBaseStack.isEmpty()) this.xmlBase = xmlBaseStack.peek();
475
- }
476
-
477
- @Override
478
- public RubyBoolean hasValue() {
479
- return ruby.getTrue();
480
- }
481
-
482
- @Override
483
- public String getString() {
484
- return value;
485
- }
587
+ @Override
588
+ public String
589
+ getString()
590
+ {
591
+ return value;
486
592
  }
593
+ }
487
594
 
488
595
  }