1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
package nokogiri;
import static nokogiri.internals.NokogiriHelpers.getLocalPart;
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
import static nokogiri.internals.NokogiriHelpers.getPrefix;
import static nokogiri.internals.NokogiriHelpers.nonEmptyStringOrNil;
import org.cyberneko.dtd.DTDConfiguration;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyObject;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
* DTD element content model. This converts the nice tree of content
* model declarations returned by NekoDTD into the convoluted binary
* tree used by libxml.
*
* @author Patrick Mahoney <pat@polycrystal.org>
*/
@JRubyClass(name = "Nokogiri::XML::ElementContent")
public class XmlElementContent extends RubyObject
{
protected String element_name = null;
protected String name;
protected Type type;
protected Occur occur;
protected IRubyObject left;
protected IRubyObject right;
/** values hardcoded from nokogiri/xml/element_content.rb; this
* makes me uneasy, but it works */
public enum Type {
PCDATA(1),
ELEMENT(2),
SEQ(3),
OR(4);
private final int value;
Type(int value)
{
this.value = value;
}
public IRubyObject value(Ruby runtime)
{
return runtime.newFixnum(value);
}
}
public enum Occur {
ONCE(1),
OPT(2),
MULT(3),
PLUS(4);
private final int value;
Occur(int value)
{
this.value = value;
}
public IRubyObject value(Ruby runtime)
{
return runtime.newFixnum(value);
}
}
public
XmlElementContent(Ruby runtime, RubyClass klass,
XmlDocument document, Node node)
{
this(runtime, klass, document, new NodeIter(node));
element_name = ((Element)node).getAttribute("ename");
/*
* This is a bit of a hack to match libxml behavior.
*
* If the tree contains but a single group with a single
* element, we can simply return the bare element without the
* surrounding group.
*
* TODO: is SEQ/ONCE with a single child the only case for
* reduction?
*
* - pmahoney
*/
if (!this.left.isNil()) {
XmlElementContent left = (XmlElementContent) this.left;
if (type == Type.SEQ &&
occur == Occur.ONCE &&
left.type == Type.ELEMENT &&
right.isNil()) {
this.name = left.name;
this.type = left.type;
this.occur = left.occur;
this.left = this.right; // both nil
}
}
}
public
XmlElementContent(Ruby runtime, XmlDocument document, Node node)
{
this(runtime, getNokogiriClass(runtime, "Nokogiri::XML::ElementContent"), document, node);
}
public
XmlElementContent(Ruby runtime, RubyClass klass,
XmlDocument doc, NodeIter iter)
{
super(runtime, klass);
setInstanceVariable("@document", doc);
name = null;
type = Type.SEQ;
occur = Occur.ONCE;
left = runtime.getNil();
right = runtime.getNil();
apply(runtime, klass, doc, iter);
}
protected
XmlElementContent(Ruby runtime, RubyClass klass,
Type type, XmlDocument doc, NodeIter iter,
XmlElementContent left)
{
super(runtime, klass);
setInstanceVariable("@document", doc);
name = null;
this.type = type;
occur = Occur.ONCE;
this.left = left;
right = runtime.getNil();
switch (type) {
case SEQ:
case OR:
applyGroup(runtime, klass, doc, iter);
default:
// noop
}
}
/**
* Applies the current node in <code>iter</code> to this content
* model. When finished, <code>iter</code> will point to the last
* processed node.
*/
protected void
apply(Ruby runtime, RubyClass klass,
XmlDocument doc,
NodeIter iter)
{
if (iter.isNull()) { return; }
Element elem = (Element) iter.current();
if (isGroup(elem) && iter.hasChildren()) {
iter.firstChild();
applyGroup(runtime, klass, doc, iter);
iter.parent();
} else if (isElement(elem)) {
name = elem.getAttribute("name");
type = Type.ELEMENT;
}
iter.nextSibling();
if (iter.isNull()) { return; }
if (isOccurrence(iter.current())) {
setOccur(((Element)iter.current()).getAttribute("type"));
iter.nextSibling();
}
}
protected void
applyGroup(Ruby runtime, RubyClass klass,
XmlDocument doc, NodeIter iter)
{
// LEFT branch
if (iter.isNull()) { return; }
if (left.isNil()) {
left = new XmlElementContent(runtime, klass, doc, iter);
if (iter.isNull()) { return; }
if (isSeparator(iter.current())) {
setType(((Element)iter.current()).getAttribute("type"));
iter.nextSibling(); // skip separator
}
}
// RIGHT branch
if (iter.isNull()) { return; }
right = new XmlElementContent(runtime, klass, doc, iter);
if (iter.isNull()) { return; }
if (isSeparator(iter.current())) {
iter.nextSibling(); // skip separator
}
if (iter.isNull()) { return; }
// binary tree can only hold two children. If we have more,
// the right child is another tree with the same sequence
// "type". The "left" of the new tree is what we've
// currently consumed as our "right" branch of this tree.
right = new XmlElementContent(runtime, klass, type, doc, iter,
(XmlElementContent) right);
}
/**
* Set the type based on the separator node type string.
*/
protected void
setType(String type)
{
if ("|".equals(type)) { this.type = Type.OR; }
else if (",".equals(type)) { this.type = Type.SEQ; }
}
protected void
setOccur(String type)
{
if ("*".equals(type)) { this.occur = Occur.MULT; }
else if ("+".equals(type)) { this.occur = Occur.PLUS; }
}
public static boolean
isGroup(Node node)
{
return XmlDtd.nameEquals(node, DTDConfiguration.E_GROUP);
}
// content model element, not Element node type
public static boolean
isElement(Node node)
{
return XmlDtd.nameEquals(node, DTDConfiguration.E_ELEMENT);
}
public static boolean
isSeparator(Node node)
{
return XmlDtd.nameEquals(node, DTDConfiguration.E_SEPARATOR);
}
public static boolean
isOccurrence(Node node)
{
return XmlDtd.nameEquals(node, DTDConfiguration.E_OCCURRENCE);
}
/**
* Return the name of the element to which this content model
* applies. Only works for the root of the tree.
*/
public IRubyObject
element_name(ThreadContext context)
{
return nonEmptyStringOrNil(context.getRuntime(), element_name);
}
@JRubyMethod
public IRubyObject
prefix(ThreadContext context)
{
return nonEmptyStringOrNil(context.getRuntime(), getPrefix(name));
}
@JRubyMethod
public IRubyObject
name(ThreadContext context)
{
return nonEmptyStringOrNil(context.getRuntime(), getLocalPart(name));
}
@JRubyMethod
public IRubyObject
type(ThreadContext context)
{
return type.value(context.getRuntime());
}
@JRubyMethod
public IRubyObject
occur(ThreadContext context)
{
return occur.value(context.getRuntime());
}
@JRubyMethod
public IRubyObject
c1(ThreadContext context)
{
return left;
}
@JRubyMethod
public IRubyObject
c2(ThreadContext context)
{
return right;
}
/**
* Iterator for a tree of Nodes. Has a current position that
* points to a given node. Calling nextSibling() on the last
* sibling results in a current position of null. This position
* is not fatal and can be escaped by calling parent() (which
* moves to the parent of previous sibling). The null position is
* used to indicate the end of a list.
*/
protected static class NodeIter
{
protected Node pre;
protected Node cur;
/**
* The first time, we fake a previous sibling element. Thus,
* initially, current() is null, and the first call should be
* nextSibling().
*/
public
NodeIter(Node node)
{
pre = null;
cur = node.getFirstChild(); // skip root contentModel node
}
public Node
current()
{
return cur;
}
public boolean
isNull()
{
return (cur == null);
}
public boolean
hasChildren()
{
return (cur != null && cur.hasChildNodes());
}
/**
* Descend to the first child.
*/
public Node
firstChild()
{
if (cur == null) { throw new RuntimeException("no children"); }
Node ch = cur.getFirstChild();
if (ch == null) { throw new RuntimeException("no children"); }
cur = ch;
return cur;
}
/**
* Move to the next sibling
*/
public Node
nextSibling()
{
if (cur == null) {
throw new RuntimeException("no next sibling");
} else {
Node ns = cur.getNextSibling();
if (ns == null) {
pre = cur;
cur = null;
} else {
cur = ns;
}
return cur;
}
}
/**
* Move to the parent.
*/
public Node
parent()
{
if (cur == null) { cur = pre; }
Node p = cur.getParentNode();
if (p == null) { throw new RuntimeException("no parent"); }
cur = p;
return cur;
}
}
}
|