nokogiri-backupify 1.5.0.beta.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +26 -0
- data/CHANGELOG.ja.rdoc +509 -0
- data/CHANGELOG.rdoc +490 -0
- data/Manifest.txt +274 -0
- data/README.ja.rdoc +106 -0
- data/README.rdoc +150 -0
- data/Rakefile +217 -0
- data/bin/nokogiri +54 -0
- data/deps.rip +5 -0
- data/ext/java/nokogiri/EncodingHandler.java +124 -0
- data/ext/java/nokogiri/HtmlDocument.java +146 -0
- data/ext/java/nokogiri/HtmlElementDescription.java +145 -0
- data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
- data/ext/java/nokogiri/HtmlSaxParserContext.java +256 -0
- data/ext/java/nokogiri/NokogiriService.java +466 -0
- data/ext/java/nokogiri/XmlAttr.java +183 -0
- data/ext/java/nokogiri/XmlAttributeDecl.java +130 -0
- data/ext/java/nokogiri/XmlCdata.java +89 -0
- data/ext/java/nokogiri/XmlComment.java +84 -0
- data/ext/java/nokogiri/XmlDocument.java +514 -0
- data/ext/java/nokogiri/XmlDocumentFragment.java +216 -0
- data/ext/java/nokogiri/XmlDtd.java +464 -0
- data/ext/java/nokogiri/XmlElement.java +221 -0
- data/ext/java/nokogiri/XmlElementContent.java +382 -0
- data/ext/java/nokogiri/XmlElementDecl.java +147 -0
- data/ext/java/nokogiri/XmlEntityDecl.java +161 -0
- data/ext/java/nokogiri/XmlEntityReference.java +75 -0
- data/ext/java/nokogiri/XmlNamespace.java +127 -0
- data/ext/java/nokogiri/XmlNode.java +1392 -0
- data/ext/java/nokogiri/XmlNodeSet.java +284 -0
- data/ext/java/nokogiri/XmlProcessingInstruction.java +103 -0
- data/ext/java/nokogiri/XmlReader.java +409 -0
- data/ext/java/nokogiri/XmlRelaxng.java +199 -0
- data/ext/java/nokogiri/XmlSaxParserContext.java +353 -0
- data/ext/java/nokogiri/XmlSaxPushParser.java +182 -0
- data/ext/java/nokogiri/XmlSchema.java +175 -0
- data/ext/java/nokogiri/XmlSyntaxError.java +114 -0
- data/ext/java/nokogiri/XmlText.java +135 -0
- data/ext/java/nokogiri/XmlXpathContext.java +175 -0
- data/ext/java/nokogiri/XsltStylesheet.java +181 -0
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +205 -0
- data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +73 -0
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +80 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +326 -0
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +583 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +170 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +118 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +73 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +78 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +120 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +56 -0
- data/ext/java/nokogiri/internals/ParserContext.java +278 -0
- data/ext/java/nokogiri/internals/PushInputStream.java +411 -0
- data/ext/java/nokogiri/internals/ReaderNode.java +473 -0
- data/ext/java/nokogiri/internals/SaveContext.java +282 -0
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +68 -0
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
- data/ext/java/nokogiri/internals/XmlDomParser.java +77 -0
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +233 -0
- data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
- data/ext/java/nokogiri/internals/XsltExtensionFunction.java +72 -0
- data/ext/nokogiri/depend +358 -0
- data/ext/nokogiri/extconf.rb +124 -0
- data/ext/nokogiri/html_document.c +154 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +276 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +94 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/nokogiri.c +92 -0
- data/ext/nokogiri/nokogiri.h +160 -0
- data/ext/nokogiri/xml_attr.c +94 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +56 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +54 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +478 -0
- data/ext/nokogiri/xml_document.h +23 -0
- data/ext/nokogiri/xml_document_fragment.c +48 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +202 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_encoding_handler.c +79 -0
- data/ext/nokogiri/xml_encoding_handler.h +8 -0
- data/ext/nokogiri/xml_entity_decl.c +110 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +52 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +31 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_namespace.c +84 -0
- data/ext/nokogiri/xml_namespace.h +13 -0
- data/ext/nokogiri/xml_node.c +1384 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +418 -0
- data/ext/nokogiri/xml_node_set.h +9 -0
- data/ext/nokogiri/xml_processing_instruction.c +56 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +684 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +161 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +288 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +199 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +115 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +205 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +58 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +50 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +309 -0
- data/ext/nokogiri/xml_xpath_context.h +9 -0
- data/ext/nokogiri/xslt_stylesheet.c +258 -0
- data/ext/nokogiri/xslt_stylesheet.h +9 -0
- data/lib/isorelax.jar +0 -0
- data/lib/jing.jar +0 -0
- data/lib/nekodtd.jar +0 -0
- data/lib/nekohtml.jar +0 -0
- data/lib/nokogiri.rb +143 -0
- data/lib/nokogiri/css.rb +23 -0
- data/lib/nokogiri/css/node.rb +99 -0
- data/lib/nokogiri/css/parser.rb +677 -0
- data/lib/nokogiri/css/parser.y +237 -0
- data/lib/nokogiri/css/parser_extras.rb +91 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +152 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +171 -0
- data/lib/nokogiri/decorators/slop.rb +35 -0
- data/lib/nokogiri/html.rb +36 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +221 -0
- data/lib/nokogiri/html/document_fragment.rb +41 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/element_description_defaults.rb +671 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +52 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +35 -0
- data/lib/nokogiri/xml.rb +67 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +418 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +218 -0
- data/lib/nokogiri/xml/document_fragment.rb +84 -0
- data/lib/nokogiri/xml/dtd.rb +22 -0
- data/lib/nokogiri/xml/element_content.rb +36 -0
- data/lib/nokogiri/xml/element_decl.rb +13 -0
- data/lib/nokogiri/xml/entity_decl.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +907 -0
- data/lib/nokogiri/xml/node/save_options.rb +45 -0
- data/lib/nokogiri/xml/node_set.rb +350 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +85 -0
- data/lib/nokogiri/xml/pp.rb +2 -0
- data/lib/nokogiri/xml/pp/character_data.rb +18 -0
- data/lib/nokogiri/xml/pp/node.rb +56 -0
- data/lib/nokogiri/xml/processing_instruction.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +112 -0
- data/lib/nokogiri/xml/relax_ng.rb +32 -0
- data/lib/nokogiri/xml/sax.rb +4 -0
- data/lib/nokogiri/xml/sax/document.rb +164 -0
- data/lib/nokogiri/xml/sax/parser.rb +115 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
- data/lib/nokogiri/xml/schema.rb +57 -0
- data/lib/nokogiri/xml/syntax_error.rb +47 -0
- data/lib/nokogiri/xml/text.rb +9 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xslt.rb +52 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xercesImpl.jar +0 -0
- data/lib/xsd/xmlparser/nokogiri.rb +90 -0
- data/tasks/cross_compile.rb +177 -0
- data/tasks/test.rb +94 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +303 -0
- data/test/css/test_tokenizer.rb +198 -0
- data/test/css/test_xpath_visitor.rb +85 -0
- data/test/decorators/test_slop.rb +16 -0
- data/test/files/2ch.html +108 -0
- data/test/files/address_book.rlx +12 -0
- data/test/files/address_book.xml +10 -0
- data/test/files/bar/bar.xsd +4 -0
- data/test/files/dont_hurt_em_why.xml +422 -0
- data/test/files/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/foo/foo.xsd +4 -0
- data/test/files/po.xml +32 -0
- data/test/files/po.xsd +66 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -0
- data/test/files/snuggles.xml +3 -0
- data/test/files/staff.dtd +10 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/tlm.html +850 -0
- data/test/files/valid_bar.xml +2 -0
- data/test/helper.rb +171 -0
- data/test/html/sax/test_parser.rb +136 -0
- data/test/html/sax/test_parser_context.rb +48 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +457 -0
- data/test/html/test_document_encoding.rb +123 -0
- data/test/html/test_document_fragment.rb +255 -0
- data/test/html/test_element_description.rb +100 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +190 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/test_convert_xpath.rb +135 -0
- data/test/test_css_cache.rb +45 -0
- data/test/test_encoding_handler.rb +46 -0
- data/test/test_memory_leak.rb +52 -0
- data/test/test_nokogiri.rb +132 -0
- data/test/test_reader.rb +403 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +189 -0
- data/test/xml/node/test_save_options.rb +20 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +338 -0
- data/test/xml/sax/test_parser_context.rb +113 -0
- data/test/xml/sax/test_push_parser.rb +156 -0
- data/test/xml/test_attr.rb +65 -0
- data/test/xml/test_attribute_decl.rb +86 -0
- data/test/xml/test_builder.rb +210 -0
- data/test/xml/test_cdata.rb +50 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +675 -0
- data/test/xml/test_document_encoding.rb +26 -0
- data/test/xml/test_document_fragment.rb +192 -0
- data/test/xml/test_dtd.rb +107 -0
- data/test/xml/test_dtd_encoding.rb +33 -0
- data/test/xml/test_element_content.rb +56 -0
- data/test/xml/test_element_decl.rb +73 -0
- data/test/xml/test_entity_decl.rb +122 -0
- data/test/xml/test_entity_reference.rb +21 -0
- data/test/xml/test_namespace.rb +70 -0
- data/test/xml/test_node.rb +899 -0
- data/test/xml/test_node_attributes.rb +34 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_reparenting.rb +321 -0
- data/test/xml/test_node_set.rb +708 -0
- data/test/xml/test_parse_options.rb +52 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +126 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +89 -0
- data/test/xml/test_syntax_error.rb +12 -0
- data/test/xml/test_text.rb +47 -0
- data/test/xml/test_unparented_node.rb +381 -0
- data/test/xml/test_xpath.rb +237 -0
- data/test/xslt/test_custom_functions.rb +94 -0
- metadata +525 -0
@@ -0,0 +1,411 @@
|
|
1
|
+
/**
|
2
|
+
* (The MIT License)
|
3
|
+
*
|
4
|
+
* Copyright (c) 2008 - 2010:
|
5
|
+
*
|
6
|
+
* * {Aaron Patterson}[http://tenderlovemaking.com]
|
7
|
+
* * {Mike Dalessio}[http://mike.daless.io]
|
8
|
+
* * {Charles Nutter}[http://blog.headius.com/]
|
9
|
+
* * {Sergio Arbeo}[http://www.serabe.com/]
|
10
|
+
* * {Patrick Mahoney}[http://polycrystal.org/]
|
11
|
+
* * {Yoko Harada}[http://yokolet.blogspot.com/]
|
12
|
+
*
|
13
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
14
|
+
* a copy of this software and associated documentation files (the
|
15
|
+
* 'Software'), to deal in the Software without restriction, including
|
16
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
17
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
18
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
19
|
+
* the following conditions:
|
20
|
+
*
|
21
|
+
* The above copyright notice and this permission notice shall be
|
22
|
+
* included in all copies or substantial portions of the Software.
|
23
|
+
*
|
24
|
+
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
25
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
26
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
27
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
28
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
29
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
30
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
31
|
+
*/
|
32
|
+
|
33
|
+
package nokogiri.internals;
|
34
|
+
|
35
|
+
import java.io.IOException;
|
36
|
+
import java.io.InputStream;
|
37
|
+
import java.nio.channels.ClosedChannelException;
|
38
|
+
import java.util.ArrayList;
|
39
|
+
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Implements a "push" InputStream. An owner thread create an
|
43
|
+
* InputStream and passes it to a second thread. The owner thread
|
44
|
+
* calls PushInputStream.write() to write data to the stream. The
|
45
|
+
* second thread calls PushInputStream.read() and other InputStream
|
46
|
+
* methods.
|
47
|
+
*
|
48
|
+
* You should ensure that only one thread write to, and only one
|
49
|
+
* thread reads to, this stream, though nothing enforces this
|
50
|
+
* strictly.
|
51
|
+
*/
|
52
|
+
public class PushInputStream extends InputStream {
|
53
|
+
/**
|
54
|
+
* Current position in the stream relative to the start of the
|
55
|
+
* buffer.
|
56
|
+
*/
|
57
|
+
protected int pos;
|
58
|
+
|
59
|
+
/**
|
60
|
+
* Current mark position, or -1 if there is no mark.
|
61
|
+
*/
|
62
|
+
protected int mark;
|
63
|
+
|
64
|
+
protected int readlimit;
|
65
|
+
|
66
|
+
/**
|
67
|
+
* State is open or closed.
|
68
|
+
*/
|
69
|
+
protected boolean isOpen;
|
70
|
+
|
71
|
+
protected Buffer buffer;
|
72
|
+
|
73
|
+
public PushInputStream() {
|
74
|
+
pos = 0;
|
75
|
+
mark = -1;
|
76
|
+
readlimit = -1;
|
77
|
+
isOpen = true;
|
78
|
+
|
79
|
+
buffer = new Buffer(512);
|
80
|
+
}
|
81
|
+
|
82
|
+
protected synchronized void ensureOpen() throws IOException {
|
83
|
+
if (!isOpen) {
|
84
|
+
throw new ClosedChannelException();
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
/**
|
89
|
+
* Write data that can be read from the stream.
|
90
|
+
*/
|
91
|
+
public synchronized void write(byte[] b) {
|
92
|
+
if (buffer == null) System.out.println("BUFFER IS NULL");
|
93
|
+
if (b == null) System.out.println("BYTE ARRAY IS NILL");
|
94
|
+
buffer.put(b);
|
95
|
+
notifyAll(); // notify readers waiting
|
96
|
+
}
|
97
|
+
|
98
|
+
/**
|
99
|
+
* Write data and then wait until all the data has been read
|
100
|
+
* (waits until the thread reading from this stream is blocked in
|
101
|
+
* a read()).
|
102
|
+
*/
|
103
|
+
public synchronized void writeAndWaitForRead(byte[] b) throws IOException {
|
104
|
+
ensureOpen();
|
105
|
+
write(b);
|
106
|
+
for (;;) {
|
107
|
+
try {
|
108
|
+
wait();
|
109
|
+
break;
|
110
|
+
} catch (InterruptedException e) {
|
111
|
+
// continue waiting
|
112
|
+
}
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
/*
|
117
|
+
*------------------------------------------------------------
|
118
|
+
* InputStream methods
|
119
|
+
*------------------------------------------------------------
|
120
|
+
*/
|
121
|
+
|
122
|
+
/**
|
123
|
+
* @see InputStream.available()
|
124
|
+
*/
|
125
|
+
@Override
|
126
|
+
public synchronized int available() throws IOException {
|
127
|
+
ensureOpen();
|
128
|
+
return buffer.size() - pos;
|
129
|
+
}
|
130
|
+
|
131
|
+
int nClose = 0;
|
132
|
+
/**
|
133
|
+
* @see InputStream.close()
|
134
|
+
*/
|
135
|
+
@Override
|
136
|
+
public synchronized void close() throws IOException {
|
137
|
+
if (!isOpen) return;
|
138
|
+
isOpen = false;
|
139
|
+
buffer = null;
|
140
|
+
notifyAll();
|
141
|
+
}
|
142
|
+
|
143
|
+
/**
|
144
|
+
* @see InputStream.mark()
|
145
|
+
*/
|
146
|
+
@Override
|
147
|
+
public synchronized void mark(int readlimit) {
|
148
|
+
this.mark = pos;
|
149
|
+
this.readlimit = readlimit;
|
150
|
+
}
|
151
|
+
|
152
|
+
/**
|
153
|
+
* Mark the current position in this stream. Supported by
|
154
|
+
* PushInputStream.
|
155
|
+
*
|
156
|
+
* @see InputStream.markSupported()
|
157
|
+
*/
|
158
|
+
@Override
|
159
|
+
public synchronized boolean markSupported() {
|
160
|
+
return true;
|
161
|
+
}
|
162
|
+
|
163
|
+
/**
|
164
|
+
* @see InputStream.read()
|
165
|
+
*/
|
166
|
+
@Override
|
167
|
+
public synchronized int read() throws IOException {
|
168
|
+
ensureOpen();
|
169
|
+
byte[] b = new byte[1];
|
170
|
+
read(b, 0, 1);
|
171
|
+
return (int) b[0];
|
172
|
+
}
|
173
|
+
|
174
|
+
/**
|
175
|
+
* @see InputStream.read(byte[])
|
176
|
+
*/
|
177
|
+
@Override
|
178
|
+
public synchronized int read(byte[] b) throws IOException {
|
179
|
+
ensureOpen();
|
180
|
+
return read(b, 0, b.length);
|
181
|
+
}
|
182
|
+
|
183
|
+
protected synchronized boolean markIsValid() {
|
184
|
+
return (mark >= 0 && pos < mark+readlimit);
|
185
|
+
}
|
186
|
+
|
187
|
+
/**
|
188
|
+
* @see InputStream.read(byte[], int, int)
|
189
|
+
*/
|
190
|
+
@Override
|
191
|
+
public synchronized int read(byte[] b, int off, int len) throws IOException {
|
192
|
+
while (isOpen && available() == 0) {
|
193
|
+
/* block until data available */
|
194
|
+
try {
|
195
|
+
notifyAll(); // notify writers waiting
|
196
|
+
wait();
|
197
|
+
} catch (InterruptedException e) {
|
198
|
+
// continue waiting
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
if (!isOpen) {
|
203
|
+
return -1;
|
204
|
+
}
|
205
|
+
|
206
|
+
int readLen = Math.min(available(), len);
|
207
|
+
|
208
|
+
buffer.get(pos, readLen, b, off);
|
209
|
+
pos += readLen;
|
210
|
+
|
211
|
+
int reduce;
|
212
|
+
|
213
|
+
if (markIsValid()) {
|
214
|
+
reduce = mark;
|
215
|
+
} else {
|
216
|
+
reduce = pos;
|
217
|
+
}
|
218
|
+
|
219
|
+
buffer.truncateFromStart(buffer.size - reduce);
|
220
|
+
pos -= reduce;
|
221
|
+
mark -= reduce;
|
222
|
+
if (mark < 0) mark = -1; // don't wrap mark around?
|
223
|
+
|
224
|
+
return readLen;
|
225
|
+
}
|
226
|
+
|
227
|
+
/**
|
228
|
+
* @see InputStream.reset()
|
229
|
+
*/
|
230
|
+
@Override
|
231
|
+
public synchronized void reset() throws IOException {
|
232
|
+
ensureOpen();
|
233
|
+
if (markIsValid())
|
234
|
+
pos = mark;
|
235
|
+
}
|
236
|
+
|
237
|
+
/**
|
238
|
+
* @see InputStream.skip()
|
239
|
+
*/
|
240
|
+
@Override
|
241
|
+
public synchronized long skip(long n) throws IOException {
|
242
|
+
ensureOpen();
|
243
|
+
pos += n;
|
244
|
+
return n;
|
245
|
+
}
|
246
|
+
|
247
|
+
/*
|
248
|
+
*------------------------------------------------------------
|
249
|
+
* Data Buffer
|
250
|
+
*------------------------------------------------------------
|
251
|
+
*/
|
252
|
+
|
253
|
+
public static class Block {
|
254
|
+
protected byte[] data;
|
255
|
+
|
256
|
+
public Block(int size) {
|
257
|
+
data = new byte[size];
|
258
|
+
}
|
259
|
+
|
260
|
+
public void copyIn(byte[] src, int srcPos, int destPos, int length) {
|
261
|
+
System.arraycopy(src, srcPos, data, destPos, length);
|
262
|
+
}
|
263
|
+
|
264
|
+
public void copyOut(int srcPos, byte[] dest, int destPos, int length) {
|
265
|
+
System.arraycopy(data, srcPos, dest, destPos, length);
|
266
|
+
}
|
267
|
+
}
|
268
|
+
|
269
|
+
public static class BlockList extends ArrayList<Block> {
|
270
|
+
public BlockList() {
|
271
|
+
super();
|
272
|
+
}
|
273
|
+
|
274
|
+
@Override
|
275
|
+
public void removeRange(int fromIndex, int toIndex) {
|
276
|
+
super.removeRange(fromIndex, toIndex);
|
277
|
+
}
|
278
|
+
}
|
279
|
+
|
280
|
+
public static class Buffer {
|
281
|
+
protected int blockSize;
|
282
|
+
protected BlockList blocks;
|
283
|
+
|
284
|
+
/**
|
285
|
+
* Offset (position) to the first logical byte in the buffer.
|
286
|
+
*/
|
287
|
+
protected int offset;
|
288
|
+
|
289
|
+
/**
|
290
|
+
* Logical size of the buffer.
|
291
|
+
*/
|
292
|
+
protected int size;
|
293
|
+
|
294
|
+
public Buffer(int blockSize) {
|
295
|
+
this.blockSize = blockSize;
|
296
|
+
this.blocks = new BlockList();
|
297
|
+
this.offset = 0;
|
298
|
+
this.size = 0;
|
299
|
+
}
|
300
|
+
|
301
|
+
public int size() {
|
302
|
+
return size;
|
303
|
+
}
|
304
|
+
|
305
|
+
protected class Segment {
|
306
|
+
/**
|
307
|
+
* Block index.
|
308
|
+
*/
|
309
|
+
protected int block;
|
310
|
+
|
311
|
+
/**
|
312
|
+
* Offset into the block.
|
313
|
+
*/
|
314
|
+
protected int off;
|
315
|
+
|
316
|
+
/**
|
317
|
+
* Length of segment.
|
318
|
+
*/
|
319
|
+
protected int len;
|
320
|
+
|
321
|
+
/**
|
322
|
+
* Calculate the block number and block offset given a position.
|
323
|
+
*/
|
324
|
+
protected Segment(int pos) {
|
325
|
+
int absPos = offset + pos;
|
326
|
+
block = (int) (absPos / blockSize);
|
327
|
+
off = (int) (absPos % blockSize);
|
328
|
+
len = -1;
|
329
|
+
}
|
330
|
+
}
|
331
|
+
|
332
|
+
protected Segment[] accessList(int pos, int size) {
|
333
|
+
Segment start = new Segment(pos);
|
334
|
+
Segment end = new Segment(pos + size);
|
335
|
+
int nBlocks = end.block - start.block + 1;
|
336
|
+
Segment[] segs = new Segment[nBlocks];
|
337
|
+
|
338
|
+
start.len = Math.min(size, blockSize - start.off);
|
339
|
+
segs[0] = start;
|
340
|
+
int currPos = pos + start.len;
|
341
|
+
int currSize = start.len;
|
342
|
+
for (int i = 1; i < nBlocks; i++) {
|
343
|
+
Segment seg = new Segment(currPos);
|
344
|
+
seg.len = Math.min(blockSize, size - currSize);
|
345
|
+
segs[i] = seg;
|
346
|
+
currPos += seg.len;
|
347
|
+
currSize += seg.len;
|
348
|
+
}
|
349
|
+
|
350
|
+
return segs;
|
351
|
+
}
|
352
|
+
|
353
|
+
protected void ensureCapacity(int pos) {
|
354
|
+
Segment seg = new Segment(pos-1);
|
355
|
+
|
356
|
+
while (blocks.size() < (seg.block + 1))
|
357
|
+
blocks.add(new Block(blockSize));
|
358
|
+
}
|
359
|
+
|
360
|
+
public void put(byte b) {
|
361
|
+
byte[] buf = new byte[1];
|
362
|
+
buf[0] = b;
|
363
|
+
put(buf);
|
364
|
+
}
|
365
|
+
|
366
|
+
public void put(byte[] b) {
|
367
|
+
ensureCapacity(size + b.length);
|
368
|
+
Segment[] segs = accessList(size, b.length);
|
369
|
+
|
370
|
+
int off = 0;
|
371
|
+
for (int i = 0; i < segs.length; i++) {
|
372
|
+
Block block = blocks.get(segs[i].block);
|
373
|
+
block.copyIn(b, off, segs[i].off, segs[i].len);
|
374
|
+
}
|
375
|
+
|
376
|
+
size += b.length;
|
377
|
+
}
|
378
|
+
|
379
|
+
public byte[] get(int pos, int len) {
|
380
|
+
byte[] b = new byte[len];
|
381
|
+
get(pos, len, b, 0);
|
382
|
+
return b;
|
383
|
+
}
|
384
|
+
|
385
|
+
/**
|
386
|
+
* Throws IndexOutOfBoundsException.
|
387
|
+
*/
|
388
|
+
public void get(int pos, int len, byte[] b, int off) {
|
389
|
+
Segment[] segs = accessList(pos, len);
|
390
|
+
for (int i = 0; i < segs.length; i++) {
|
391
|
+
Block block = blocks.get(segs[i].block);
|
392
|
+
block.copyOut(segs[i].off, b, off, segs[i].len);
|
393
|
+
}
|
394
|
+
}
|
395
|
+
|
396
|
+
/**
|
397
|
+
* Truncate the buffer to <code>newSize</code> by removing
|
398
|
+
* data from the start of the buffer.
|
399
|
+
*/
|
400
|
+
public void truncateFromStart(int newSize) {
|
401
|
+
if (newSize > size || newSize < 0)
|
402
|
+
throw new RuntimeException("invalid size");
|
403
|
+
|
404
|
+
Segment newStart = new Segment(size - newSize);
|
405
|
+
blocks.removeRange(0, newStart.block);
|
406
|
+
|
407
|
+
size = newSize;
|
408
|
+
offset = newStart.off;
|
409
|
+
}
|
410
|
+
}
|
411
|
+
}
|
@@ -0,0 +1,473 @@
|
|
1
|
+
/**
|
2
|
+
* (The MIT License)
|
3
|
+
*
|
4
|
+
* Copyright (c) 2008 - 2010:
|
5
|
+
*
|
6
|
+
* * {Aaron Patterson}[http://tenderlovemaking.com]
|
7
|
+
* * {Mike Dalessio}[http://mike.daless.io]
|
8
|
+
* * {Charles Nutter}[http://blog.headius.com]
|
9
|
+
* * {Sergio Arbeo}[http://www.serabe.com]
|
10
|
+
* * {Patrick Mahoney}[http://polycrystal.org]
|
11
|
+
* * {Yoko Harada}[http://yokolet.blogspot.com]
|
12
|
+
*
|
13
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
14
|
+
* a copy of this software and associated documentation files (the
|
15
|
+
* 'Software'), to deal in the Software without restriction, including
|
16
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
17
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
18
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
19
|
+
* the following conditions:
|
20
|
+
*
|
21
|
+
* The above copyright notice and this permission notice shall be
|
22
|
+
* included in all copies or substantial portions of the Software.
|
23
|
+
*
|
24
|
+
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
25
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
26
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
27
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
28
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
29
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
30
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
31
|
+
*/
|
32
|
+
|
33
|
+
package nokogiri.internals;
|
34
|
+
|
35
|
+
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
|
36
|
+
import static nokogiri.internals.NokogiriHelpers.isNamespace;
|
37
|
+
import static nokogiri.internals.NokogiriHelpers.isXmlBase;
|
38
|
+
import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
|
39
|
+
import static nokogiri.internals.NokogiriHelpers.stringOrBlank;
|
40
|
+
import static nokogiri.internals.NokogiriHelpers.stringOrNil;
|
41
|
+
|
42
|
+
import java.util.ArrayList;
|
43
|
+
import java.util.HashMap;
|
44
|
+
import java.util.List;
|
45
|
+
import java.util.Map;
|
46
|
+
import java.util.Set;
|
47
|
+
import java.util.Stack;
|
48
|
+
|
49
|
+
import nokogiri.XmlAttr;
|
50
|
+
import nokogiri.XmlDocument;
|
51
|
+
import nokogiri.XmlSyntaxError;
|
52
|
+
|
53
|
+
import org.jruby.Ruby;
|
54
|
+
import org.jruby.RubyArray;
|
55
|
+
import org.jruby.RubyBoolean;
|
56
|
+
import org.jruby.RubyHash;
|
57
|
+
import org.jruby.runtime.ThreadContext;
|
58
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
59
|
+
import org.w3c.dom.Attr;
|
60
|
+
import org.w3c.dom.Document;
|
61
|
+
import org.xml.sax.Attributes;
|
62
|
+
import org.xml.sax.SAXParseException;
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Abstract class of Node for XmlReader.
|
66
|
+
*
|
67
|
+
* @author Yoko Harada <yokolet@gmail.com>
|
68
|
+
*
|
69
|
+
*/
|
70
|
+
public abstract class ReaderNode {
|
71
|
+
|
72
|
+
Ruby ruby;
|
73
|
+
public ReaderAttributeList attributeList;
|
74
|
+
public Map<String, String> namespaces;
|
75
|
+
public int depth, nodeType;
|
76
|
+
public String lang, localName, xmlBase, prefix, name, uri, value, xmlVersion = "1.0";
|
77
|
+
public boolean hasChildren = false;
|
78
|
+
public abstract String getString();
|
79
|
+
private Document document = null;
|
80
|
+
|
81
|
+
public IRubyObject getAttributeByIndex(IRubyObject index){
|
82
|
+
if(index.isNil()) return index;
|
83
|
+
|
84
|
+
long i = index.convertToInteger().getLongValue();
|
85
|
+
if(i > Integer.MAX_VALUE) {
|
86
|
+
throw ruby.newArgumentError("value too long to be an array index");
|
87
|
+
}
|
88
|
+
|
89
|
+
if (attributeList == null) return ruby.getNil();
|
90
|
+
if (i<0 || attributeList.length <= i) return ruby.getNil();
|
91
|
+
return stringOrBlank(ruby, attributeList.values.get(((Long)i).intValue()));
|
92
|
+
}
|
93
|
+
|
94
|
+
public IRubyObject getAttributeByName(IRubyObject name){
|
95
|
+
if(attributeList == null) return ruby.getNil();
|
96
|
+
String value = attributeList.getByName(rubyStringToString(name));
|
97
|
+
return stringOrNil(ruby, value);
|
98
|
+
}
|
99
|
+
|
100
|
+
public IRubyObject getAttributeByName(String name){
|
101
|
+
if(attributeList == null) return ruby.getNil();
|
102
|
+
String value = attributeList.getByName(name);
|
103
|
+
return stringOrNil(ruby, value);
|
104
|
+
}
|
105
|
+
|
106
|
+
public IRubyObject getAttributeCount(){
|
107
|
+
if(attributeList == null) return ruby.newFixnum(0);
|
108
|
+
return ruby.newFixnum(attributeList.length);
|
109
|
+
}
|
110
|
+
|
111
|
+
public IRubyObject getAttributesNodes() {
|
112
|
+
RubyArray array = RubyArray.newArray(ruby);
|
113
|
+
if (attributeList != null && attributeList.length > 0) {
|
114
|
+
if (document == null) {
|
115
|
+
document = ((XmlDocument) getNokogiriClass(ruby, "Nokogiri::XML::Document").allocate()).getDocument();
|
116
|
+
}
|
117
|
+
for (int i=0; i<attributeList.length; i++) {
|
118
|
+
if (!isNamespace(attributeList.names.get(i))) {
|
119
|
+
Attr attr = document.createAttributeNS(attributeList.namespaces.get(i), attributeList.names.get(i));
|
120
|
+
attr.setValue(attributeList.values.get(i));
|
121
|
+
XmlAttr xmlAttr = (XmlAttr) getNokogiriClass(ruby, "Nokogiri::XML::Attr").allocate();
|
122
|
+
xmlAttr.setNode(ruby.getCurrentContext(), attr);
|
123
|
+
array.append(xmlAttr);
|
124
|
+
}
|
125
|
+
}
|
126
|
+
}
|
127
|
+
return array;
|
128
|
+
}
|
129
|
+
|
130
|
+
public IRubyObject getAttributes(ThreadContext context) {
|
131
|
+
if(attributeList == null) return context.getRuntime().getNil();
|
132
|
+
RubyHash hash = RubyHash.newHash(context.getRuntime());
|
133
|
+
for (int i=0; i<attributeList.length; i++) {
|
134
|
+
IRubyObject k = stringOrBlank(context.getRuntime(), attributeList.names.get(i));
|
135
|
+
IRubyObject v = stringOrBlank(context.getRuntime(), attributeList.values.get(i));
|
136
|
+
if (context.getRuntime().is1_9()) hash.op_aset19(context, k, v);
|
137
|
+
else hash.op_aset(context, k, v);
|
138
|
+
}
|
139
|
+
return hash;
|
140
|
+
}
|
141
|
+
|
142
|
+
public IRubyObject getDepth() {
|
143
|
+
return ruby.newFixnum(depth);
|
144
|
+
}
|
145
|
+
|
146
|
+
public IRubyObject getLang() {
|
147
|
+
return stringOrNil(ruby, lang);
|
148
|
+
}
|
149
|
+
|
150
|
+
public IRubyObject getLocalName() {
|
151
|
+
return stringOrNil(ruby, localName);
|
152
|
+
}
|
153
|
+
|
154
|
+
public IRubyObject getName() {
|
155
|
+
return stringOrNil(ruby, name);
|
156
|
+
}
|
157
|
+
|
158
|
+
public IRubyObject getNamespaces(ThreadContext context) {
|
159
|
+
if(namespaces == null) return ruby.getNil();
|
160
|
+
RubyHash hash = RubyHash.newHash(ruby);
|
161
|
+
Set<String> keys = namespaces.keySet();
|
162
|
+
for (String key : keys) {
|
163
|
+
String stringValue = namespaces.get(key);
|
164
|
+
IRubyObject k = stringOrBlank(context.getRuntime(), key);
|
165
|
+
IRubyObject v = stringOrBlank(context.getRuntime(), stringValue);
|
166
|
+
if (context.getRuntime().is1_9()) hash.op_aset19(context, k, v);
|
167
|
+
else hash.op_aset(context, k, v);
|
168
|
+
}
|
169
|
+
return hash;
|
170
|
+
}
|
171
|
+
|
172
|
+
public IRubyObject getXmlBase() {
|
173
|
+
return stringOrNil(ruby, xmlBase);
|
174
|
+
}
|
175
|
+
|
176
|
+
public IRubyObject getPrefix() {
|
177
|
+
return stringOrNil(ruby, prefix);
|
178
|
+
}
|
179
|
+
|
180
|
+
public IRubyObject getUri() {
|
181
|
+
return stringOrNil(ruby, uri);
|
182
|
+
}
|
183
|
+
|
184
|
+
public IRubyObject getValue() {
|
185
|
+
return stringOrNil(ruby, value);
|
186
|
+
}
|
187
|
+
|
188
|
+
public IRubyObject getXmlVersion() {
|
189
|
+
return ruby.newString(xmlVersion);
|
190
|
+
}
|
191
|
+
|
192
|
+
public RubyBoolean hasAttributes() {
|
193
|
+
if (attributeList == null || attributeList.length == 0) return ruby.getFalse();
|
194
|
+
return ruby.getTrue();
|
195
|
+
}
|
196
|
+
|
197
|
+
public abstract RubyBoolean hasValue();
|
198
|
+
|
199
|
+
public RubyBoolean isDefault(){
|
200
|
+
// TODO Implement.
|
201
|
+
return ruby.getFalse();
|
202
|
+
}
|
203
|
+
|
204
|
+
public boolean isError() { return false; }
|
205
|
+
|
206
|
+
protected void parsePrefix(String qName) {
|
207
|
+
int index = qName.indexOf(':');
|
208
|
+
if(index != -1) prefix = qName.substring(0, index);
|
209
|
+
}
|
210
|
+
|
211
|
+
public void setLang(String lang) {
|
212
|
+
lang = (lang != null) ? lang : null;
|
213
|
+
}
|
214
|
+
|
215
|
+
public IRubyObject toSyntaxError() { return ruby.getNil(); }
|
216
|
+
|
217
|
+
public IRubyObject getNodeType() { return ruby.newFixnum(nodeType); }
|
218
|
+
|
219
|
+
public static enum ReaderNodeType {
|
220
|
+
NODE(0),
|
221
|
+
ELEMENT(1),
|
222
|
+
ATTRIBUTE(2),
|
223
|
+
TEXT(3),
|
224
|
+
CDATA(4),
|
225
|
+
ENTITY_REFERENCE(5),
|
226
|
+
ENTITY(6),
|
227
|
+
PROCESSING_INSTRUCTION(7),
|
228
|
+
COMMENT(8),
|
229
|
+
DOCUMENT(9),
|
230
|
+
DOCUMENT_TYPE(10),
|
231
|
+
DOCUMENTFRAGMENT(11),
|
232
|
+
NOTATION(12),
|
233
|
+
WHITESPACE(13),
|
234
|
+
SIGNIFICANT_WHITESPACE(14),
|
235
|
+
END_ELEMENT(15),
|
236
|
+
END_ENTITY(16),
|
237
|
+
XML_DECLARATION(17);
|
238
|
+
|
239
|
+
private final int value;
|
240
|
+
ReaderNodeType(int value) {
|
241
|
+
this.value = value;
|
242
|
+
}
|
243
|
+
|
244
|
+
public int getValue() {
|
245
|
+
return value;
|
246
|
+
}
|
247
|
+
}
|
248
|
+
|
249
|
+
public static class ClosingNode extends ReaderNode {
|
250
|
+
|
251
|
+
public ClosingNode(Ruby ruby, String uri, String localName, String qName, int depth, Stack<String> langStack, Stack<String> xmlBaseStack) {
|
252
|
+
this.ruby = ruby;
|
253
|
+
nodeType = ReaderNodeType.END_ELEMENT.getValue();
|
254
|
+
this.uri = "".equals(uri) ? null : uri;
|
255
|
+
this.localName = localName.trim().length() > 0 ? localName : qName;
|
256
|
+
this.name = qName;
|
257
|
+
parsePrefix(qName);
|
258
|
+
this.depth = depth;
|
259
|
+
if (!langStack.isEmpty()) this.lang = langStack.peek();
|
260
|
+
if (!xmlBaseStack.isEmpty()) this.xmlBase = xmlBaseStack.peek();
|
261
|
+
}
|
262
|
+
|
263
|
+
@Override
|
264
|
+
public IRubyObject getAttributeCount() {
|
265
|
+
return ruby.newFixnum(0);
|
266
|
+
}
|
267
|
+
|
268
|
+
@Override
|
269
|
+
public RubyBoolean hasValue() {
|
270
|
+
return ruby.getFalse();
|
271
|
+
}
|
272
|
+
|
273
|
+
@Override
|
274
|
+
public String getString() {
|
275
|
+
StringBuffer sb = new StringBuffer();
|
276
|
+
sb.append("</").append(name).append(">");
|
277
|
+
return new String(sb);
|
278
|
+
}
|
279
|
+
}
|
280
|
+
|
281
|
+
public static class ElementNode extends ReaderNode {
|
282
|
+
private List<String> attributeStrings = new ArrayList<String>();
|
283
|
+
|
284
|
+
public ElementNode(Ruby ruby, String uri, String localName, String qName, Attributes attrs, int depth, Stack<String> langStack, Stack<String> xmlBaseStack) {
|
285
|
+
this.ruby = ruby;
|
286
|
+
this.nodeType = ReaderNodeType.ELEMENT.getValue();
|
287
|
+
this.uri = "".equals(uri) ? null : uri;
|
288
|
+
this.localName = localName.trim().length() > 0 ? localName : qName;
|
289
|
+
this.name = qName;
|
290
|
+
parsePrefix(qName);
|
291
|
+
this.depth = depth;
|
292
|
+
hasChildren = true;
|
293
|
+
parseAttributes(attrs, langStack, xmlBaseStack);
|
294
|
+
}
|
295
|
+
|
296
|
+
@Override
|
297
|
+
public RubyBoolean hasValue() {
|
298
|
+
return ruby.getFalse();
|
299
|
+
}
|
300
|
+
|
301
|
+
private void parseAttributes(Attributes attrs, Stack<String> langStack, Stack<String> xmlBaseStack) {
|
302
|
+
if (attrs.getLength() > 0) attributeList = new ReaderAttributeList();
|
303
|
+
String u, n, v;
|
304
|
+
for (int i = 0; i < attrs.getLength(); i++) {
|
305
|
+
u = attrs.getURI(i);
|
306
|
+
n = attrs.getQName(i);
|
307
|
+
v = attrs.getValue(i);
|
308
|
+
if (isNamespace(n)) {
|
309
|
+
if (namespaces == null) namespaces = new HashMap<String, String>();
|
310
|
+
namespaces.put(n, v);
|
311
|
+
} else {
|
312
|
+
if (lang == null) lang = resolveLang(n, v, langStack);
|
313
|
+
if (xmlBase == null) xmlBase = resolveXmlBase(n, v, xmlBaseStack);
|
314
|
+
}
|
315
|
+
attributeList.add(u, n, v);
|
316
|
+
attributeStrings.add(n + "=\"" + v + "\"");
|
317
|
+
}
|
318
|
+
}
|
319
|
+
|
320
|
+
private String resolveLang(String n, String v, Stack<String> langStack) {
|
321
|
+
if ("xml:lang".equals(n)) {
|
322
|
+
return v;
|
323
|
+
} else if (!langStack.isEmpty()) {
|
324
|
+
return langStack.peek();
|
325
|
+
} else {
|
326
|
+
return null;
|
327
|
+
}
|
328
|
+
}
|
329
|
+
|
330
|
+
private String resolveXmlBase(String n, String v, Stack<String> xmlBaseStack) {
|
331
|
+
if (isXmlBase(n)) {
|
332
|
+
return getXmlBaseUri(n, v, xmlBaseStack);
|
333
|
+
} else if (!xmlBaseStack.isEmpty()) {
|
334
|
+
return xmlBaseStack.peek();
|
335
|
+
} else {
|
336
|
+
return null;
|
337
|
+
}
|
338
|
+
}
|
339
|
+
|
340
|
+
private String getXmlBaseUri(String n, String v, Stack<String> xmlBaseStack) {
|
341
|
+
if ("xml:base".equals(n)) {
|
342
|
+
if (v.startsWith("http://")) {
|
343
|
+
return v;
|
344
|
+
} else if (v.startsWith("/") && v.endsWith("/")) {
|
345
|
+
String sub = v.substring(1, v.length() - 2);
|
346
|
+
String base = xmlBaseStack.peek();
|
347
|
+
if (base.endsWith("/")) {
|
348
|
+
base = base.substring(0, base.length() - 1);
|
349
|
+
}
|
350
|
+
int pos = base.lastIndexOf("/");
|
351
|
+
return base.substring(0, pos).concat(sub);
|
352
|
+
} else {
|
353
|
+
String base = xmlBaseStack.peek();
|
354
|
+
if (base.endsWith("/")) return base.concat(v);
|
355
|
+
else return base.concat("/").concat(v);
|
356
|
+
}
|
357
|
+
} else if ("xlink:href".equals(n)) {
|
358
|
+
String base = xmlBaseStack.peek();
|
359
|
+
if (base.endsWith("/")) return base.concat(v);
|
360
|
+
else return base.concat("/").concat(v);
|
361
|
+
}
|
362
|
+
return null;
|
363
|
+
}
|
364
|
+
|
365
|
+
@Override
|
366
|
+
public String getString() {
|
367
|
+
StringBuffer sb = new StringBuffer();
|
368
|
+
sb.append("<").append(name);
|
369
|
+
if (attributeList != null) {
|
370
|
+
for (int i=0; i<attributeList.length; i++) {
|
371
|
+
sb.append(" ").append(attributeStrings.get(i));
|
372
|
+
}
|
373
|
+
}
|
374
|
+
if (hasChildren) sb.append(">");
|
375
|
+
else sb.append("/>");
|
376
|
+
return new String(sb);
|
377
|
+
}
|
378
|
+
}
|
379
|
+
|
380
|
+
public static class ReaderAttributeList {
|
381
|
+
List<String> namespaces = new ArrayList<String>();
|
382
|
+
List<String> names = new ArrayList<String>();
|
383
|
+
List<String> values = new ArrayList<String>();
|
384
|
+
int length = 0;
|
385
|
+
|
386
|
+
void add(String namespace, String name, String value) {
|
387
|
+
namespace = namespace != null ? namespace : "";
|
388
|
+
namespaces.add(namespace);
|
389
|
+
name = name != null ? name : "";
|
390
|
+
names.add(name);
|
391
|
+
value = value != null ? value : "";
|
392
|
+
values.add(value);
|
393
|
+
length++;
|
394
|
+
}
|
395
|
+
|
396
|
+
String getByName(String name) {
|
397
|
+
for (int i=0; i<names.size(); i++) {
|
398
|
+
if (name.equals(names.get(i))) {
|
399
|
+
return values.get(i);
|
400
|
+
}
|
401
|
+
}
|
402
|
+
return null;
|
403
|
+
}
|
404
|
+
}
|
405
|
+
|
406
|
+
public static class EmptyNode extends ReaderNode {
|
407
|
+
|
408
|
+
public EmptyNode(Ruby ruby) {
|
409
|
+
this.ruby = ruby;
|
410
|
+
this.nodeType = ReaderNodeType.NODE.getValue();
|
411
|
+
}
|
412
|
+
|
413
|
+
@Override
|
414
|
+
public IRubyObject getXmlVersion() {
|
415
|
+
return this.ruby.getNil();
|
416
|
+
}
|
417
|
+
|
418
|
+
@Override
|
419
|
+
public RubyBoolean hasValue() {
|
420
|
+
return ruby.getFalse();
|
421
|
+
}
|
422
|
+
|
423
|
+
@Override
|
424
|
+
public String getString() {
|
425
|
+
return null;
|
426
|
+
}
|
427
|
+
}
|
428
|
+
|
429
|
+
public static class ExceptionNode extends EmptyNode {
|
430
|
+
private final XmlSyntaxError exception;
|
431
|
+
|
432
|
+
// Still don't know what to do with ex.
|
433
|
+
public ExceptionNode(Ruby ruby, SAXParseException ex) {
|
434
|
+
super(ruby);
|
435
|
+
this.exception = new XmlSyntaxError(ruby);
|
436
|
+
}
|
437
|
+
|
438
|
+
@Override
|
439
|
+
public boolean isError() {
|
440
|
+
return true;
|
441
|
+
}
|
442
|
+
|
443
|
+
@Override
|
444
|
+
public IRubyObject toSyntaxError() {
|
445
|
+
return this.exception;
|
446
|
+
}
|
447
|
+
}
|
448
|
+
|
449
|
+
public static class TextNode extends ReaderNode {
|
450
|
+
|
451
|
+
public TextNode(Ruby ruby, String content, int depth, Stack<String> langStack, Stack<String> xmlBaseStack) {
|
452
|
+
this.ruby = ruby;
|
453
|
+
this.value = content;
|
454
|
+
this.localName = "#text";
|
455
|
+
this.name = "#text";
|
456
|
+
this.depth = depth;
|
457
|
+
if (content.trim().length() > 0) nodeType = ReaderNodeType.TEXT.getValue();
|
458
|
+
else nodeType = ReaderNodeType.SIGNIFICANT_WHITESPACE.getValue();
|
459
|
+
if (!langStack.isEmpty()) this.lang = langStack.peek();
|
460
|
+
if (!xmlBaseStack.isEmpty()) this.xmlBase = xmlBaseStack.peek();
|
461
|
+
}
|
462
|
+
|
463
|
+
@Override
|
464
|
+
public RubyBoolean hasValue() {
|
465
|
+
return ruby.getTrue();
|
466
|
+
}
|
467
|
+
|
468
|
+
@Override
|
469
|
+
public String getString() {
|
470
|
+
return value;
|
471
|
+
}
|
472
|
+
}
|
473
|
+
}
|