nokogiri 1.5.0.beta.1 → 1.5.0.beta.2
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.
- data/CHANGELOG.ja.rdoc +28 -8
- data/CHANGELOG.rdoc +23 -0
- data/Manifest.txt +63 -1
- data/README.ja.rdoc +1 -1
- data/README.rdoc +22 -4
- data/Rakefile +6 -2
- data/ext/java/nokogiri/EncodingHandler.java +92 -0
- data/ext/java/nokogiri/HtmlDocument.java +116 -0
- data/ext/java/nokogiri/HtmlElementDescription.java +111 -0
- data/ext/java/nokogiri/HtmlEntityLookup.java +45 -0
- data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -0
- data/ext/java/nokogiri/NokogiriService.java +370 -0
- data/ext/java/nokogiri/XmlAttr.java +147 -0
- data/ext/java/nokogiri/XmlAttributeDecl.java +98 -0
- data/ext/java/nokogiri/XmlCdata.java +50 -0
- data/ext/java/nokogiri/XmlComment.java +47 -0
- data/ext/java/nokogiri/XmlDocument.java +463 -0
- data/ext/java/nokogiri/XmlDocumentFragment.java +207 -0
- data/ext/java/nokogiri/XmlDtd.java +427 -0
- data/ext/java/nokogiri/XmlElement.java +172 -0
- data/ext/java/nokogiri/XmlElementContent.java +350 -0
- data/ext/java/nokogiri/XmlElementDecl.java +115 -0
- data/ext/java/nokogiri/XmlEntityDecl.java +129 -0
- data/ext/java/nokogiri/XmlEntityReference.java +42 -0
- data/ext/java/nokogiri/XmlNamespace.java +77 -0
- data/ext/java/nokogiri/XmlNode.java +1399 -0
- data/ext/java/nokogiri/XmlNodeSet.java +248 -0
- data/ext/java/nokogiri/XmlProcessingInstruction.java +70 -0
- data/ext/java/nokogiri/XmlReader.java +373 -0
- data/ext/java/nokogiri/XmlRelaxng.java +166 -0
- data/ext/java/nokogiri/XmlSaxParserContext.java +308 -0
- data/ext/java/nokogiri/XmlSaxPushParser.java +146 -0
- data/ext/java/nokogiri/XmlSchema.java +142 -0
- data/ext/java/nokogiri/XmlSyntaxError.java +84 -0
- data/ext/java/nokogiri/XmlText.java +96 -0
- data/ext/java/nokogiri/XmlXpathContext.java +130 -0
- data/ext/java/nokogiri/XsltStylesheet.java +126 -0
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +181 -0
- data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +39 -0
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +42 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +251 -0
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +526 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +136 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +80 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +37 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +54 -0
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +49 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +88 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +23 -0
- data/ext/java/nokogiri/internals/ParserContext.java +235 -0
- data/ext/java/nokogiri/internals/PushInputStream.java +381 -0
- data/ext/java/nokogiri/internals/ReaderNode.java +431 -0
- data/ext/java/nokogiri/internals/SaveContext.java +249 -0
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +35 -0
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +10 -0
- data/ext/java/nokogiri/internals/XmlDomParser.java +45 -0
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +201 -0
- data/ext/java/nokogiri/internals/XmlSaxParser.java +33 -0
- data/ext/nokogiri/depend +32 -0
- data/ext/nokogiri/extconf.rb +61 -32
- data/ext/nokogiri/nokogiri.c +0 -5
- data/ext/nokogiri/nokogiri.h +2 -2
- data/ext/nokogiri/xml_document.c +5 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_node.c +56 -16
- data/ext/nokogiri/xml_node_set.c +7 -7
- data/ext/nokogiri/xml_reader.c +20 -1
- data/ext/nokogiri/xml_relax_ng.c +0 -7
- data/ext/nokogiri/xml_xpath_context.c +2 -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 +1 -2
- data/lib/nokogiri/css/generated_parser.rb +155 -148
- data/lib/nokogiri/css/generated_tokenizer.rb +2 -1
- data/lib/nokogiri/css/parser.y +3 -0
- data/lib/nokogiri/css/xpath_visitor.rb +1 -7
- data/lib/nokogiri/html.rb +2 -2
- data/lib/nokogiri/html/document_fragment.rb +7 -4
- data/lib/nokogiri/nokogiri.jar +0 -0
- data/lib/nokogiri/version.rb +3 -6
- data/lib/nokogiri/xml/builder.rb +1 -1
- data/lib/nokogiri/xml/document.rb +1 -2
- data/lib/nokogiri/xml/document_fragment.rb +7 -0
- data/lib/nokogiri/xml/node.rb +5 -3
- data/lib/nokogiri/xml/node_set.rb +25 -0
- data/lib/nokogiri/xml/reader.rb +2 -0
- data/lib/nokogiri/xml/sax/document.rb +3 -1
- data/lib/xercesImpl.jar +0 -0
- data/spec/helper.rb +3 -0
- data/spec/xml/reader_spec.rb +307 -0
- data/tasks/test.rb +1 -1
- data/test/css/test_parser.rb +11 -1
- data/test/html/sax/test_parser_context.rb +2 -2
- data/test/html/test_document.rb +2 -2
- data/test/html/test_document_fragment.rb +34 -6
- data/test/test_memory_leak.rb +2 -2
- data/test/test_reader.rb +28 -6
- data/test/test_xslt_transforms.rb +2 -3
- data/test/xml/test_attr.rb +31 -4
- data/test/xml/test_builder.rb +5 -5
- data/test/xml/test_cdata.rb +3 -3
- data/test/xml/test_document.rb +8 -8
- data/test/xml/test_document_fragment.rb +4 -12
- data/test/xml/test_node.rb +1 -1
- data/test/xml/test_node_reparenting.rb +26 -11
- data/test/xml/test_node_set.rb +38 -2
- data/test/xml/test_text.rb +11 -2
- data/test/xml/test_unparented_node.rb +1 -1
- data/test/xml/test_xpath.rb +11 -7
- metadata +68 -5
- data/lib/nokogiri/version_warning.rb +0 -14
@@ -0,0 +1,172 @@
|
|
1
|
+
package nokogiri;
|
2
|
+
|
3
|
+
import static nokogiri.internals.NokogiriHelpers.namedNodeMapToRubyArray;
|
4
|
+
import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
|
5
|
+
import nokogiri.internals.SaveContext;
|
6
|
+
|
7
|
+
import org.jruby.Ruby;
|
8
|
+
import org.jruby.RubyArray;
|
9
|
+
import org.jruby.RubyClass;
|
10
|
+
import org.jruby.anno.JRubyClass;
|
11
|
+
import org.jruby.anno.JRubyMethod;
|
12
|
+
import org.jruby.runtime.ThreadContext;
|
13
|
+
import org.jruby.runtime.Visibility;
|
14
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
15
|
+
import org.w3c.dom.Attr;
|
16
|
+
import org.w3c.dom.Element;
|
17
|
+
import org.w3c.dom.NamedNodeMap;
|
18
|
+
import org.w3c.dom.Node;
|
19
|
+
|
20
|
+
/**
|
21
|
+
*
|
22
|
+
* @author sergio
|
23
|
+
*/
|
24
|
+
@JRubyClass(name="Nokogiri::XML::Element", parent="Nokogiri::XML::Node")
|
25
|
+
public class XmlElement extends XmlNode {
|
26
|
+
|
27
|
+
public XmlElement(Ruby runtime, RubyClass klazz) {
|
28
|
+
super(runtime, klazz);
|
29
|
+
}
|
30
|
+
|
31
|
+
public XmlElement(Ruby runtime, RubyClass klazz, Node element) {
|
32
|
+
super(runtime, klazz, element);
|
33
|
+
}
|
34
|
+
|
35
|
+
@Override
|
36
|
+
@JRubyMethod(name = {"add_namespace_definition", "add_namespace"})
|
37
|
+
public IRubyObject add_namespace_definition(ThreadContext context,
|
38
|
+
IRubyObject prefix,
|
39
|
+
IRubyObject href) {
|
40
|
+
Element element = (Element) node;
|
41
|
+
|
42
|
+
final String uri = "http://www.w3.org/2000/xmlns/";
|
43
|
+
String qName =
|
44
|
+
prefix.isNil() ? "xmlns" : "xmlns:" + rubyStringToString(prefix);
|
45
|
+
element.setAttributeNS(uri, qName, rubyStringToString(href));
|
46
|
+
|
47
|
+
XmlNamespace ns = (XmlNamespace)
|
48
|
+
super.add_namespace_definition(context, prefix, href);
|
49
|
+
updateNodeNamespaceIfNecessary(context, ns);
|
50
|
+
|
51
|
+
return ns;
|
52
|
+
}
|
53
|
+
|
54
|
+
@Override
|
55
|
+
public boolean isElement() { return true; }
|
56
|
+
|
57
|
+
@Override
|
58
|
+
@JRubyMethod(visibility = Visibility.PRIVATE)
|
59
|
+
public IRubyObject get(ThreadContext context, IRubyObject rbkey) {
|
60
|
+
if (rbkey == null || rbkey.isNil()) context.getRuntime().getNil();
|
61
|
+
String key = (String)rbkey.toJava(String.class);
|
62
|
+
Element element = (Element) node;
|
63
|
+
String value = element.getAttribute(key);
|
64
|
+
if(!value.equals("")){
|
65
|
+
return context.getRuntime().newString(value);
|
66
|
+
}
|
67
|
+
return context.getRuntime().getNil();
|
68
|
+
}
|
69
|
+
|
70
|
+
@Override
|
71
|
+
public IRubyObject key_p(ThreadContext context, IRubyObject rbkey) {
|
72
|
+
String key = rubyStringToString(rbkey);
|
73
|
+
Element element = (Element) node;
|
74
|
+
return context.getRuntime().newBoolean(element.hasAttribute(key));
|
75
|
+
}
|
76
|
+
|
77
|
+
@Override
|
78
|
+
public IRubyObject op_aset(ThreadContext context,
|
79
|
+
IRubyObject rbkey,
|
80
|
+
IRubyObject rbval) {
|
81
|
+
String key = rubyStringToString(rbkey);
|
82
|
+
String val = rubyStringToString(rbval);
|
83
|
+
Element element = (Element) node;
|
84
|
+
element.setAttribute(key, val);
|
85
|
+
return this;
|
86
|
+
}
|
87
|
+
|
88
|
+
@Override
|
89
|
+
public IRubyObject remove_attribute(ThreadContext context, IRubyObject name) {
|
90
|
+
String key = name.convertToString().asJavaString();
|
91
|
+
Element element = (Element) node;
|
92
|
+
element.removeAttribute(key);
|
93
|
+
return this;
|
94
|
+
}
|
95
|
+
|
96
|
+
@Override
|
97
|
+
public void relink_namespace(ThreadContext context) {
|
98
|
+
Element e = (Element) node;
|
99
|
+
|
100
|
+
e.getOwnerDocument().renameNode(e, e.lookupNamespaceURI(e.getPrefix()), e.getNodeName());
|
101
|
+
|
102
|
+
if(e.hasAttributes()) {
|
103
|
+
NamedNodeMap attrs = e.getAttributes();
|
104
|
+
|
105
|
+
for(int i = 0; i < attrs.getLength(); i++) {
|
106
|
+
Attr attr = (Attr) attrs.item(i);
|
107
|
+
String nsUri = "";
|
108
|
+
String prefix = attr.getPrefix();
|
109
|
+
String nodeName = attr.getNodeName();
|
110
|
+
if("xml".equals(prefix)) {
|
111
|
+
nsUri = "http://www.w3.org/XML/1998/namespace";
|
112
|
+
} else if("xmlns".equals(prefix) || nodeName.equals("xmlns")) {
|
113
|
+
nsUri = "http://www.w3.org/2000/xmlns/";
|
114
|
+
} else {
|
115
|
+
nsUri = attr.lookupNamespaceURI(nodeName);
|
116
|
+
}
|
117
|
+
|
118
|
+
e.getOwnerDocument().renameNode(attr, nsUri, nodeName);
|
119
|
+
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
if(e.hasChildNodes()) {
|
124
|
+
((XmlNodeSet) children(context)).relink_namespace(context);
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
/**
|
129
|
+
* TODO: previous code handled elements with parent 'p' differently?.
|
130
|
+
*/
|
131
|
+
@Override
|
132
|
+
public void saveContent(ThreadContext context, SaveContext ctx) {
|
133
|
+
Node firstChild = node.getFirstChild();
|
134
|
+
boolean empty = (firstChild == null);
|
135
|
+
short type = -1;
|
136
|
+
if (!empty) type = firstChild.getNodeType();
|
137
|
+
boolean inline = (!empty &&
|
138
|
+
(type == Node.TEXT_NODE ||
|
139
|
+
type == Node.CDATA_SECTION_NODE ||
|
140
|
+
type == Node.ENTITY_REFERENCE_NODE));
|
141
|
+
|
142
|
+
if (empty) {
|
143
|
+
ctx.emptyTagStart(node.getNodeName());
|
144
|
+
} else if (inline) {
|
145
|
+
ctx.openTagInlineStart(node.getNodeName());
|
146
|
+
} else {
|
147
|
+
ctx.openTagStart(node.getNodeName());
|
148
|
+
}
|
149
|
+
|
150
|
+
RubyArray attr_list = namedNodeMapToRubyArray(context.getRuntime(), node.getAttributes());
|
151
|
+
saveNodeListContent(context, attr_list, ctx);
|
152
|
+
|
153
|
+
if (empty) {
|
154
|
+
ctx.emptyTagEnd(node.getNodeName());
|
155
|
+
return;
|
156
|
+
} else if (inline) {
|
157
|
+
ctx.openTagInlineEnd();
|
158
|
+
} else {
|
159
|
+
ctx.openTagEnd();
|
160
|
+
}
|
161
|
+
|
162
|
+
saveNodeListContent(context, (XmlNodeSet) children(context), ctx);
|
163
|
+
|
164
|
+
if (inline) {
|
165
|
+
ctx.closeTagInline(node.getNodeName());
|
166
|
+
} else {
|
167
|
+
ctx.closeTag(node.getNodeName());
|
168
|
+
}
|
169
|
+
|
170
|
+
}
|
171
|
+
|
172
|
+
}
|
@@ -0,0 +1,350 @@
|
|
1
|
+
package nokogiri;
|
2
|
+
|
3
|
+
import static nokogiri.internals.NokogiriHelpers.getLocalPart;
|
4
|
+
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
|
5
|
+
import static nokogiri.internals.NokogiriHelpers.getPrefix;
|
6
|
+
import static nokogiri.internals.NokogiriHelpers.nonEmptyStringOrNil;
|
7
|
+
|
8
|
+
import org.cyberneko.dtd.DTDConfiguration;
|
9
|
+
import org.jruby.Ruby;
|
10
|
+
import org.jruby.RubyClass;
|
11
|
+
import org.jruby.RubyObject;
|
12
|
+
import org.jruby.anno.JRubyClass;
|
13
|
+
import org.jruby.anno.JRubyMethod;
|
14
|
+
import org.jruby.runtime.ThreadContext;
|
15
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
16
|
+
import org.w3c.dom.Element;
|
17
|
+
import org.w3c.dom.Node;
|
18
|
+
|
19
|
+
/**
|
20
|
+
* DTD element content model. This converts the nice tree of content
|
21
|
+
* model declarations returned by NekoDTD into the convoluted binary
|
22
|
+
* tree used by libxml.
|
23
|
+
*
|
24
|
+
* @author Patrick Mahoney <pat@polycrystal.org>
|
25
|
+
*/
|
26
|
+
@JRubyClass(name="Nokogiri::XML::ElementContent")
|
27
|
+
public class XmlElementContent extends RubyObject {
|
28
|
+
protected String element_name = null;
|
29
|
+
|
30
|
+
protected String name;
|
31
|
+
protected Type type;
|
32
|
+
protected Occur occur;
|
33
|
+
protected IRubyObject left;
|
34
|
+
protected IRubyObject right;
|
35
|
+
|
36
|
+
/** values hardcoded from nokogiri/xml/element_content.rb; this
|
37
|
+
* makes me uneasy, but it works */
|
38
|
+
public enum Type {
|
39
|
+
PCDATA (1),
|
40
|
+
ELEMENT (2),
|
41
|
+
SEQ (3),
|
42
|
+
OR (4);
|
43
|
+
|
44
|
+
private final int value;
|
45
|
+
Type(int value) {
|
46
|
+
this.value = value;
|
47
|
+
}
|
48
|
+
public IRubyObject value(Ruby runtime) {
|
49
|
+
return runtime.newFixnum(value);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
public enum Occur {
|
54
|
+
ONCE (1),
|
55
|
+
OPT (2),
|
56
|
+
MULT (3),
|
57
|
+
PLUS (4);
|
58
|
+
|
59
|
+
private final int value;
|
60
|
+
Occur(int value) {
|
61
|
+
this.value = value;
|
62
|
+
}
|
63
|
+
public IRubyObject value(Ruby runtime) {
|
64
|
+
return runtime.newFixnum(value);
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
public XmlElementContent(Ruby runtime, RubyClass klass,
|
69
|
+
XmlDocument document, Node node) {
|
70
|
+
this(runtime, klass, document, new NodeIter(node));
|
71
|
+
element_name = ((Element)node).getAttribute("ename");
|
72
|
+
|
73
|
+
/*
|
74
|
+
* This is a bit of a hack to match libxml behavior.
|
75
|
+
*
|
76
|
+
* If the tree contains but a single group with a single
|
77
|
+
* element, we can simply return the bare element without the
|
78
|
+
* surrounding group.
|
79
|
+
*
|
80
|
+
* TODO: is SEQ/ONCE with a single child the only case for
|
81
|
+
* reduction?
|
82
|
+
*
|
83
|
+
* - pmahoney
|
84
|
+
*/
|
85
|
+
if (!this.left.isNil()) {
|
86
|
+
XmlElementContent left = (XmlElementContent) this.left;
|
87
|
+
if (type == Type.SEQ &&
|
88
|
+
occur == Occur.ONCE &&
|
89
|
+
left.type == Type.ELEMENT &&
|
90
|
+
right.isNil()) {
|
91
|
+
this.name = left.name;
|
92
|
+
this.type = left.type;
|
93
|
+
this.occur = left.occur;
|
94
|
+
this.left = this.right; // both nil
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
public XmlElementContent(Ruby runtime, XmlDocument document, Node node) {
|
100
|
+
this(runtime, getNokogiriClass(runtime, "Nokogiri::XML::ElementContent"), document, node);
|
101
|
+
}
|
102
|
+
|
103
|
+
public XmlElementContent(Ruby runtime, RubyClass klass,
|
104
|
+
XmlDocument doc, NodeIter iter) {
|
105
|
+
super(runtime, klass);
|
106
|
+
|
107
|
+
setInstanceVariable("@document", doc);
|
108
|
+
|
109
|
+
name = null;
|
110
|
+
type = Type.SEQ;
|
111
|
+
occur = Occur.ONCE;
|
112
|
+
left = runtime.getNil();
|
113
|
+
right = runtime.getNil();
|
114
|
+
|
115
|
+
apply(runtime, klass, doc, iter);
|
116
|
+
}
|
117
|
+
|
118
|
+
protected XmlElementContent(Ruby runtime, RubyClass klass,
|
119
|
+
Type type, XmlDocument doc, NodeIter iter,
|
120
|
+
XmlElementContent left) {
|
121
|
+
super(runtime, klass);
|
122
|
+
|
123
|
+
setInstanceVariable("@document", doc);
|
124
|
+
|
125
|
+
name = null;
|
126
|
+
this.type = type;
|
127
|
+
occur = Occur.ONCE;
|
128
|
+
this.left = left;
|
129
|
+
right = runtime.getNil();
|
130
|
+
|
131
|
+
switch (type) {
|
132
|
+
case SEQ:
|
133
|
+
case OR:
|
134
|
+
applyGroup(runtime, klass, doc, iter);
|
135
|
+
default:
|
136
|
+
// noop
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
/**
|
141
|
+
* Applies the current node in <code>iter</code> to this content
|
142
|
+
* model. When finished, <code>iter</code> will point to the last
|
143
|
+
* processed node.
|
144
|
+
*/
|
145
|
+
protected void apply(Ruby runtime, RubyClass klass,
|
146
|
+
XmlDocument doc,
|
147
|
+
NodeIter iter) {
|
148
|
+
if (iter.isNull()) return;
|
149
|
+
|
150
|
+
Element elem = (Element) iter.current();
|
151
|
+
|
152
|
+
if (isGroup(elem) && iter.hasChildren()) {
|
153
|
+
iter.firstChild();
|
154
|
+
applyGroup(runtime, klass, doc, iter);
|
155
|
+
iter.parent();
|
156
|
+
} else if (isElement(elem)) {
|
157
|
+
name = elem.getAttribute("name");
|
158
|
+
type = Type.ELEMENT;
|
159
|
+
}
|
160
|
+
|
161
|
+
iter.nextSibling();
|
162
|
+
if (iter.isNull()) return;
|
163
|
+
if (isOccurrence(iter.current())) {
|
164
|
+
setOccur(((Element)iter.current()).getAttribute("type"));
|
165
|
+
iter.nextSibling();
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
protected void applyGroup(Ruby runtime, RubyClass klass,
|
170
|
+
XmlDocument doc, NodeIter iter) {
|
171
|
+
// LEFT branch
|
172
|
+
|
173
|
+
if (iter.isNull()) return;
|
174
|
+
|
175
|
+
if (left.isNil()) {
|
176
|
+
left = new XmlElementContent(runtime, klass, doc, iter);
|
177
|
+
|
178
|
+
if (iter.isNull()) return;
|
179
|
+
|
180
|
+
if (isSeparator(iter.current())) {
|
181
|
+
setType(((Element)iter.current()).getAttribute("type"));
|
182
|
+
iter.nextSibling(); // skip separator
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
// RIGHT branch
|
187
|
+
|
188
|
+
if (iter.isNull()) return;
|
189
|
+
|
190
|
+
right = new XmlElementContent(runtime, klass, doc, iter);
|
191
|
+
|
192
|
+
if (iter.isNull()) return;
|
193
|
+
if (isSeparator(iter.current()))
|
194
|
+
iter.nextSibling(); // skip separator
|
195
|
+
if (iter.isNull()) return;
|
196
|
+
|
197
|
+
// binary tree can only hold two children. If we have more,
|
198
|
+
// the right child is another tree with the same sequence
|
199
|
+
// "type". The "left" of the new tree is what we've
|
200
|
+
// currently consumed as our "right" branch of this tree.
|
201
|
+
right = new XmlElementContent(runtime, klass, type, doc, iter,
|
202
|
+
(XmlElementContent) right);
|
203
|
+
}
|
204
|
+
|
205
|
+
/**
|
206
|
+
* Set the type based on the separator node type string.
|
207
|
+
*/
|
208
|
+
protected void setType(String type) {
|
209
|
+
if ("|".equals(type)) this.type = Type.OR;
|
210
|
+
else if (",".equals(type)) this.type = Type.SEQ;
|
211
|
+
}
|
212
|
+
|
213
|
+
protected void setOccur(String type) {
|
214
|
+
if ("*".equals(type)) this.occur = Occur.MULT;
|
215
|
+
else if ("+".equals(type)) this.occur = Occur.PLUS;
|
216
|
+
}
|
217
|
+
|
218
|
+
public static boolean isGroup(Node node) {
|
219
|
+
return XmlDtd.nameEquals(node, DTDConfiguration.E_GROUP);
|
220
|
+
}
|
221
|
+
|
222
|
+
// content model element, not Element node type
|
223
|
+
public static boolean isElement(Node node) {
|
224
|
+
return XmlDtd.nameEquals(node, DTDConfiguration.E_ELEMENT);
|
225
|
+
}
|
226
|
+
|
227
|
+
public static boolean isSeparator(Node node) {
|
228
|
+
return XmlDtd.nameEquals(node, DTDConfiguration.E_SEPARATOR);
|
229
|
+
}
|
230
|
+
|
231
|
+
public static boolean isOccurrence(Node node) {
|
232
|
+
return XmlDtd.nameEquals(node, DTDConfiguration.E_OCCURRENCE);
|
233
|
+
}
|
234
|
+
|
235
|
+
/**
|
236
|
+
* Return the name of the element to which this content model
|
237
|
+
* applies. Only works for the root of the tree.
|
238
|
+
*/
|
239
|
+
public IRubyObject element_name(ThreadContext context) {
|
240
|
+
return nonEmptyStringOrNil(context.getRuntime(), element_name);
|
241
|
+
}
|
242
|
+
|
243
|
+
@JRubyMethod
|
244
|
+
public IRubyObject prefix(ThreadContext context) {
|
245
|
+
return nonEmptyStringOrNil(context.getRuntime(), getPrefix(name));
|
246
|
+
}
|
247
|
+
|
248
|
+
@JRubyMethod
|
249
|
+
public IRubyObject name(ThreadContext context) {
|
250
|
+
return nonEmptyStringOrNil(context.getRuntime(), getLocalPart(name));
|
251
|
+
}
|
252
|
+
|
253
|
+
@JRubyMethod
|
254
|
+
public IRubyObject type(ThreadContext context) {
|
255
|
+
return type.value(context.getRuntime());
|
256
|
+
}
|
257
|
+
|
258
|
+
@JRubyMethod
|
259
|
+
public IRubyObject occur(ThreadContext context) {
|
260
|
+
return occur.value(context.getRuntime());
|
261
|
+
}
|
262
|
+
|
263
|
+
@JRubyMethod
|
264
|
+
public IRubyObject c1(ThreadContext context) {
|
265
|
+
return left;
|
266
|
+
}
|
267
|
+
|
268
|
+
@JRubyMethod
|
269
|
+
public IRubyObject c2(ThreadContext context) {
|
270
|
+
return right;
|
271
|
+
}
|
272
|
+
|
273
|
+
/**
|
274
|
+
* Iterator for a tree of Nodes. Has a current position that
|
275
|
+
* points to a given node. Calling nextSibling() on the last
|
276
|
+
* sibling results in a current position of null. This position
|
277
|
+
* is not fatal and can be escaped by calling parent() (which
|
278
|
+
* moves to the parent of previous sibling). The null position is
|
279
|
+
* used to indicate the end of a list.
|
280
|
+
*/
|
281
|
+
protected static class NodeIter {
|
282
|
+
protected Node pre;
|
283
|
+
protected Node cur;
|
284
|
+
|
285
|
+
/**
|
286
|
+
* The first time, we fake a previous sibling element. Thus,
|
287
|
+
* initially, current() is null, and the first call should be
|
288
|
+
* nextSibling().
|
289
|
+
*/
|
290
|
+
public NodeIter(Node node) {
|
291
|
+
pre = null;
|
292
|
+
cur = node.getFirstChild(); // skip root contentModel node
|
293
|
+
}
|
294
|
+
|
295
|
+
public Node current() {
|
296
|
+
return cur;
|
297
|
+
}
|
298
|
+
|
299
|
+
public boolean isNull() {
|
300
|
+
return (cur == null);
|
301
|
+
}
|
302
|
+
|
303
|
+
public boolean hasChildren() {
|
304
|
+
return (cur != null && cur.hasChildNodes());
|
305
|
+
}
|
306
|
+
|
307
|
+
/**
|
308
|
+
* Descend to the first child.
|
309
|
+
*/
|
310
|
+
public Node firstChild() {
|
311
|
+
if (cur == null) throw new RuntimeException("no children");
|
312
|
+
Node ch = cur.getFirstChild();
|
313
|
+
if (ch == null) throw new RuntimeException("no children");
|
314
|
+
|
315
|
+
cur = ch;
|
316
|
+
return cur;
|
317
|
+
}
|
318
|
+
|
319
|
+
/**
|
320
|
+
* Move to the next sibling
|
321
|
+
*/
|
322
|
+
public Node nextSibling() {
|
323
|
+
if (cur == null) {
|
324
|
+
throw new RuntimeException("no next sibling");
|
325
|
+
} else {
|
326
|
+
Node ns = cur.getNextSibling();
|
327
|
+
if (ns == null) {
|
328
|
+
pre = cur;
|
329
|
+
cur = null;
|
330
|
+
} else {
|
331
|
+
cur = ns;
|
332
|
+
}
|
333
|
+
return cur;
|
334
|
+
}
|
335
|
+
}
|
336
|
+
|
337
|
+
/**
|
338
|
+
* Move to the parent.
|
339
|
+
*/
|
340
|
+
public Node parent() {
|
341
|
+
if (cur == null) cur = pre;
|
342
|
+
|
343
|
+
Node p = cur.getParentNode();
|
344
|
+
if (p == null) throw new RuntimeException("no parent");
|
345
|
+
|
346
|
+
cur = p;
|
347
|
+
return cur;
|
348
|
+
}
|
349
|
+
}
|
350
|
+
}
|