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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
|
package nokogiri;
import static java.lang.Math.max;
import static nokogiri.internals.NokogiriHelpers.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.*;
import org.apache.xerces.dom.CoreDocumentImpl;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyInteger;
import org.jruby.RubyModule;
import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.Block;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.w3c.dom.Comment;
import nokogiri.internals.HtmlDomParserContext;
import nokogiri.internals.NokogiriHelpers;
import nokogiri.internals.NokogiriNamespaceCache;
import nokogiri.internals.SaveContextVisitor;
import nokogiri.internals.XmlDomParserContext;
/**
* Class for Nokogiri::XML::Node
*
* @author sergio
* @author Patrick Mahoney <pat@polycrystal.org>
* @author Yoko Harada <yokolet@gmail.com>
* @author John Shahid <jvshahid@gmail.com>
*/
@JRubyClass(name = "Nokogiri::XML::Node")
public class XmlNode extends RubyObject
{
protected static final String TEXT_WRAPPER_NAME = "nokogiri_text_wrapper";
/** The underlying Node object. */
protected Node node;
/* Cached objects */
protected IRubyObject content = null;
private transient XmlDocument doc;
protected transient RubyString name;
/*
* Taken from http://ejohn.org/blog/comparing-document-position/
* Used for compareDocumentPosition.
* <ironic>Thanks to both java api and w3 doc for its helpful documentation</ironic>
*/
protected static final int IDENTICAL_ELEMENTS = 0;
protected static final int IN_DIFFERENT_DOCUMENTS = 1;
protected static final int SECOND_PRECEDES_FIRST = 2;
protected static final int FIRST_PRECEDES_SECOND = 4;
protected static final int SECOND_CONTAINS_FIRST = 8;
protected static final int FIRST_CONTAINS_SECOND = 16;
/**
* Cast <code>node</code> to an XmlNode or raise a type error
* in <code>context</code>.
*/
protected static XmlNode
asXmlNode(ThreadContext context, IRubyObject node)
{
if (!(node instanceof XmlNode)) {
final Ruby runtime = context.runtime;
throw runtime.newTypeError(node == null ? runtime.getNil() : node, getNokogiriClass(runtime, "Nokogiri::XML::Node"));
}
return (XmlNode) node;
}
/**
* Cast <code>node</code> to an XmlNode, or null if RubyNil, or
* raise a type error in <code>context</code>.
*/
protected static XmlNode
asXmlNodeOrNull(ThreadContext context, IRubyObject node)
{
if (node == null || node.isNil()) { return null; }
return asXmlNode(context, node);
}
/**
* Coalesce to adjacent TextNodes.
* @param context
* @param prev Previous node to cur.
* @param cur Next node to prev.
*/
public static void
coalesceTextNodes(ThreadContext context, IRubyObject prev, IRubyObject cur)
{
XmlNode p = asXmlNode(context, prev);
XmlNode c = asXmlNode(context, cur);
Node pNode = p.node;
Node cNode = c.node;
pNode.setNodeValue(pNode.getNodeValue() + cNode.getNodeValue());
p.content = null; // clear cached content
c.assimilateXmlNode(context, p);
}
/**
* Coalesce text nodes around <code>anchorNode</code>. If
* <code>anchorNode</code> has siblings (previous or next) that
* are text nodes, the content will be merged into
* <code>anchorNode</code> and the redundant nodes will be removed
* from the DOM.
*
* To match libxml behavior (?) the final content of
* <code>anchorNode</code> and any removed nodes will be
* identical.
*
* @param context
* @param anchorNode
*/
protected static void
coalesceTextNodes(ThreadContext context,
IRubyObject anchorNode,
AdoptScheme scheme)
{
XmlNode xa = asXmlNode(context, anchorNode);
XmlNode xp = asXmlNodeOrNull(context, xa.previous_sibling(context));
XmlNode xn = asXmlNodeOrNull(context, xa.next_sibling(context));
Node p = xp == null ? null : xp.node;
Node a = xa.node;
Node n = xn == null ? null : xn.node;
Node parent = a.getParentNode();
boolean shouldMergeP = scheme == AdoptScheme.NEXT_SIBLING || scheme == AdoptScheme.CHILD
|| scheme == AdoptScheme.REPLACEMENT;
boolean shouldMergeN = scheme == AdoptScheme.PREV_SIBLING || scheme == AdoptScheme.REPLACEMENT;
// apply the merge right to left
if (shouldMergeN && n != null && n.getNodeType() == Node.TEXT_NODE) {
xa.setContent(a.getNodeValue() + n.getNodeValue());
parent.removeChild(n);
xn.assimilateXmlNode(context, xa);
}
if (shouldMergeP && p != null && p.getNodeType() == Node.TEXT_NODE) {
xp.setContent(p.getNodeValue() + a.getNodeValue());
parent.removeChild(a);
xa.assimilateXmlNode(context, xp);
}
}
/**
* This is the allocator for XmlNode class. It should only be
* called from Ruby code.
*/
public
XmlNode(Ruby runtime, RubyClass klass)
{
super(runtime, klass);
}
/**
* This is a constructor to create an XmlNode from an already
* existing node. It may be called by Java code.
*/
public
XmlNode(Ruby runtime, RubyClass klass, Node node)
{
super(runtime, klass);
setNode(runtime, node);
}
protected void
decorate(final Ruby runtime)
{
if (node != null) {
resetCache();
if (node.getNodeType() != Node.DOCUMENT_NODE) {
setDocumentAndDecorate(runtime.getCurrentContext(), this, document(runtime));
}
}
}
/**
* Create and return a copy of this object.
*
* @return a clone of this object
*/
@Override
public Object
clone() throws CloneNotSupportedException
{
return super.clone();
}
protected void
resetCache()
{
node.setUserData(NokogiriHelpers.CACHED_NODE, this, null);
}
/**
* Allocate a new object, perform initialization, call that
* object's initialize method, and call any block passing the
* object as the only argument. If <code>cls</code> is
* Nokogiri::XML::Node, creates a new Nokogiri::XML::Element
* instead.
*
* This static method seems to be inherited, strangely enough.
* E.g. creating a new XmlAttr from Ruby code calls this method if
* XmlAttr does not define its own 'new' method.
*
* Since there is some Java bookkeeping that always needs to
* happen, we don't define the 'initialize' method in Java because
* we'd have to count on subclasses calling 'super'.
*
* The main consequence of this is that every subclass needs to
* define its own 'new' method.
*
* As a convenience, this method does the following:
*
* <ul>
*
* <li>allocates a new object using the allocator assigned to
* <code>cls</code></li>
*
* <li>calls the Java method init(); subclasses can override this,
* otherwise they should implement a specific 'new' method</li>
*
* <li>invokes the Ruby initializer</li>
*
* <li>if a block is given, calls the block with the new node as
* the argument</li>
*
* </ul>
*
* -pmahoney
*/
@JRubyMethod(name = "new", meta = true, rest = true)
public static IRubyObject
rbNew(ThreadContext context, IRubyObject cls,
IRubyObject[] args, Block block)
{
Ruby ruby = context.runtime;
RubyClass klazz = (RubyClass) cls;
if ("Nokogiri::XML::Node".equals(klazz.getName())) {
klazz = getNokogiriClass(ruby, "Nokogiri::XML::Element");
}
XmlNode xmlNode = (XmlNode) klazz.allocate();
xmlNode.init(context, args);
xmlNode.callInit(args, block);
assert xmlNode.node != null;
if (block.isGiven()) { block.call(context, xmlNode); }
return xmlNode;
}
/**
* Initialize the object from Ruby arguments. Should be
* overridden by subclasses. Should check for a minimum number of
* args but not for an exact number. Any extra args will then be
* passed to 'initialize'. The way 'new' and this 'init' function
* interact means that subclasses cannot arbitrarily change the
* require aruments by defining an 'initialize' method. This is
* how the C libxml wrapper works also.
*
* As written it performs initialization for a new Element with
* the given <code>name</code> within the document
* <code>doc</code>. So XmlElement need not override this. This
* implementation cannot be moved to XmlElement however, because
* subclassing XmlNode must result in something that behaves much
* like XmlElement.
*/
protected void
init(ThreadContext context, IRubyObject[] args)
{
if (args.length < 2) {
throw context.runtime.newArgumentError(args.length, 2);
}
IRubyObject name = args[0];
IRubyObject doc = args[1];
if (!(doc instanceof XmlNode)) {
throw context.runtime.newArgumentError("document must be a Nokogiri::XML::Node");
}
if (!(doc instanceof XmlDocument)) {
// TODO: deprecate allowing Node
context.runtime.getWarnings().warn("Passing a Node as the second parameter to Node.new is deprecated. Please pass a Document instead, or prefer an alternative constructor like Node#add_child. This will become an error in a future release of Nokogiri.");
}
Document document = asXmlNode(context, doc).getOwnerDocument();
if (document == null) {
throw context.runtime.newArgumentError("node must have owner document");
}
Element element;
String node_name = rubyStringToString(name);
String prefix = NokogiriHelpers.getPrefix(node_name);
String namespace_uri = null;
if (document.getDocumentElement() != null) {
namespace_uri = document.getDocumentElement().lookupNamespaceURI(prefix);
}
element = document.createElementNS(namespace_uri, node_name);
setNode(context.runtime, element);
}
/**
* Set the underlying node of this node to the underlying node of
* <code>otherNode</code>.
*
* FIXME: also update the cached node?
*/
protected void
assimilateXmlNode(ThreadContext context, IRubyObject otherNode)
{
XmlNode toAssimilate = asXmlNode(context, otherNode);
this.node = toAssimilate.node;
content = null; // clear cache
}
/**
* See org.w3.dom.Node#normalize.
*/
public void
normalize()
{
node.normalize();
}
public Node
getNode()
{
return node;
}
public boolean
isComment() { return false; }
public boolean
isElement()
{
if (node instanceof Element) { return true; } // in case of subclassing
else { return false; }
}
public boolean
isProcessingInstruction() { return false; }
/**
* Return the string value of the attribute <code>key</code> or
* nil.
*
* Only applies where the underlying Node is an Element node, but
* implemented here in XmlNode because not all nodes with
* underlying Element nodes subclass XmlElement, such as the DTD
* declarations like XmlElementDecl.
*/
protected IRubyObject
getAttribute(ThreadContext context, String key)
{
return getAttribute(context.runtime, key);
}
protected IRubyObject
getAttribute(Ruby runtime, String key)
{
String value = getAttribute(key);
return nonEmptyStringOrNil(runtime, value);
}
protected String
getAttribute(String key)
{
if (node.getNodeType() != Node.ELEMENT_NODE) { return null; }
String value = ((Element)node).getAttribute(key);
return value.length() == 0 ? null : value;
}
/**
* This method should be called after a node has been adopted in a new
* document. This method will ensure that the node is renamed with the
* appriopriate NS uri. First the prefix of the node is extracted, then is
* used to lookup the namespace uri in the new document starting at the
* current node and traversing the ancestors. If the namespace uri wasn't
* empty (or null) all children and the node has attributes and/or children
* then the algorithm is recursively applied to the children.
*/
public void
relink_namespace(ThreadContext context)
{
if (!(node instanceof Element)) {
return;
}
Element e = (Element) node;
// disable error checking to prevent lines like the following
// from throwing a `NAMESPACE_ERR' exception:
// Nokogiri::XML::DocumentFragment.parse("<o:div>a</o:div>")
// since the `o' prefix isn't defined anywhere.
e.getOwnerDocument().setStrictErrorChecking(false);
String prefix = e.getPrefix();
String nsURI = e.lookupNamespaceURI(prefix);
this.node = NokogiriHelpers.renameNode(e, nsURI, e.getNodeName());
if (nsURI == null || nsURI.isEmpty()) {
RubyBoolean ns_inherit =
(RubyBoolean)document(context.runtime).getInstanceVariable("@namespace_inheritance");
if (ns_inherit.isTrue()) {
set_namespace(context, ((XmlNode)parent(context)).namespace(context));
}
return;
}
String currentPrefix = e.getParentNode().lookupPrefix(nsURI);
String currentURI = e.getParentNode().lookupNamespaceURI(prefix);
boolean isDefault = e.getParentNode().isDefaultNamespace(nsURI);
// add xmlns attribute if this is a new root node or if the node's
// namespace isn't a default namespace in the new document
if (e.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
// this is the root node, so we must set the namespaces attributes anyway
e.setAttribute(prefix == null ? "xmlns" : "xmlns:" + prefix, nsURI);
} else if (prefix == null) {
// this is a default namespace but isn't the default where this node is being added
if (!isDefault) { e.setAttribute("xmlns", nsURI); }
} else if (!prefix.equals(currentPrefix) || nsURI.equals(currentURI)) {
// this is a prefixed namespace
// but doesn't have the same prefix or the prefix is set to a different URI
e.setAttribute("xmlns:" + prefix, nsURI);
}
if (e.hasAttributes()) {
NamedNodeMap attrs = e.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Attr attr = (Attr) attrs.item(i);
String attrPrefix = attr.getPrefix();
if (attrPrefix == null) {
attrPrefix = NokogiriHelpers.getPrefix(attr.getNodeName());
}
String nodeName = attr.getNodeName();
String nsUri;
if ("xml".equals(attrPrefix)) {
nsUri = "http://www.w3.org/XML/1998/namespace";
} else if ("xmlns".equals(attrPrefix) || nodeName.equals("xmlns")) {
nsUri = "http://www.w3.org/2000/xmlns/";
} else {
nsUri = attr.lookupNamespaceURI(attrPrefix);
}
if (nsUri != null && nsUri.equals(e.getNamespaceURI())) {
nsUri = null;
}
if (!(nsUri == null || "".equals(nsUri) || "http://www.w3.org/XML/1998/namespace".equals(nsUri))) {
// Create a new namespace object and add it to the document namespace cache.
// TODO: why do we need the namespace cache ?
XmlNamespace.createFromAttr(context.runtime, attr);
}
NokogiriHelpers.renameNode(attr, nsUri, nodeName);
}
}
if (this.node.hasChildNodes()) {
relink_namespace(context, getChildren());
}
}
static void
relink_namespace(ThreadContext context, IRubyObject[] nodes)
{
for (int i = 0; i < nodes.length; i++) {
if (nodes[i] instanceof XmlNode) {
((XmlNode) nodes[i]).relink_namespace(context);
}
}
}
// Users might extend XmlNode. This method works for such a case.
public void
accept(ThreadContext context, SaveContextVisitor visitor)
{
visitor.enter(node);
acceptChildren(context, getChildren(), visitor);
visitor.leave(node);
}
void
acceptChildren(ThreadContext context, IRubyObject[] nodes, SaveContextVisitor visitor)
{
if (nodes.length > 0) {
for (int i = 0; i < nodes.length; i++) {
Object item = nodes[i];
if (item instanceof XmlNode) {
((XmlNode) item).accept(context, visitor);
} else if (item instanceof XmlNamespace) {
((XmlNamespace) item).accept(context, visitor);
}
}
}
}
RubyString
doSetName(IRubyObject name)
{
if (name.isNil()) { return this.name = null; }
return this.name = name.convertToString();
}
public void
setDocument(ThreadContext context, XmlDocument doc)
{
this.doc = doc;
setDocumentAndDecorate(context, this, doc);
}
// shared logic with XmlNodeSet
static void
setDocumentAndDecorate(ThreadContext context, RubyObject self, XmlDocument doc)
{
self.setInstanceVariable("@document", doc == null ? context.nil : doc);
if (doc != null) { Helpers.invoke(context, doc, "decorate", self); }
}
public void
setNode(Ruby runtime, Node node)
{
this.node = node;
decorate(runtime);
if (this instanceof XmlAttr) {
((XmlAttr) this).setNamespaceIfNecessary(runtime);
}
}
protected IRubyObject
getNodeName(ThreadContext context)
{
if (name != null) { return name; }
String str = null;
if (node != null) {
str = NokogiriHelpers.getLocalPart(node.getNodeName());
}
if (str == null) { str = ""; }
if (str.startsWith("#")) { str = str.substring(1); } // eliminates '#'
return name = context.runtime.newString(str);
}
/**
* Add a namespace definition to this node. To the underlying
* node, add an attribute of the form
* <code>xmlns:prefix="uri"</code>.
*/
@JRubyMethod(name = {"add_namespace_definition", "add_namespace"})
public IRubyObject
add_namespace_definition(ThreadContext context, IRubyObject prefix, IRubyObject href)
{
String hrefStr, prefixStr = prefix.isNil() ? null : prefix.convertToString().decodeString();
// try to search the namespace first
if (href.isNil()) {
hrefStr = findNamespaceHref(context, prefixStr);
if (hrefStr == null) { return context.nil; }
href = context.runtime.newString(hrefStr);
} else {
hrefStr = rubyStringToString(href.convertToString());
}
NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCache(node);
XmlNamespace cachedNamespace = nsCache.get(prefixStr, hrefStr);
if (cachedNamespace != null) { return cachedNamespace; }
Node namespaceOwner;
if (node.getNodeType() == Node.ELEMENT_NODE) {
namespaceOwner = node;
Element element = (Element) node;
// adds namespace as node's attribute
String qName = prefix.isNil() ? "xmlns" : "xmlns:" + prefixStr;
element.setAttributeNS("http://www.w3.org/2000/xmlns/", qName, hrefStr);
} else if (node.getNodeType() == Node.ATTRIBUTE_NODE) { namespaceOwner = ((Attr) node).getOwnerElement(); }
else { namespaceOwner = node.getParentNode(); }
XmlNamespace ns = XmlNamespace.createImpl(namespaceOwner, prefix, prefixStr, href, hrefStr);
if (node != namespaceOwner) {
node = NokogiriHelpers.renameNode(node, ns.getHref(), ns.getPrefix() + ':' + node.getLocalName());
}
updateNodeNamespaceIfNecessary(ns);
return ns;
}
private void
updateNodeNamespaceIfNecessary(XmlNamespace ns)
{
String oldPrefix = this.node.getPrefix();
/*
* Update if both prefixes are null or equal
*/
boolean update =
(oldPrefix == null && ns.getPrefix() == null) ||
(oldPrefix != null && oldPrefix.equals(ns.getPrefix()));
if (update) {
this.node = NokogiriHelpers.renameNode(this.node, ns.getHref(), this.node.getNodeName());
}
}
@JRubyMethod(name = {"attribute", "attr"})
public IRubyObject
attribute(ThreadContext context, IRubyObject name)
{
NamedNodeMap attrs = this.node.getAttributes();
Node attr = attrs.getNamedItem(rubyStringToString(name));
if (attr == null) { return context.nil; }
return getCachedNodeOrCreate(context.runtime, attr);
}
@JRubyMethod
public IRubyObject
attribute_nodes(ThreadContext context)
{
final Ruby runtime = context.runtime;
NamedNodeMap nodeMap = this.node.getAttributes();
if (nodeMap == null) { return runtime.newEmptyArray(); }
RubyArray attr = runtime.newArray(nodeMap.getLength());
final XmlDocument doc = document(context.runtime);
for (int i = 0; i < nodeMap.getLength(); i++) {
if ((doc instanceof Html4Document) || !NokogiriHelpers.isNamespace(nodeMap.item(i))) {
attr.append(getCachedNodeOrCreate(runtime, nodeMap.item(i)));
}
}
return attr;
}
@JRubyMethod
public IRubyObject
attribute_with_ns(ThreadContext context, IRubyObject name, IRubyObject namespace)
{
String namej = rubyStringToString(name);
String nsj = (namespace.isNil()) ? null : rubyStringToString(namespace);
Node el = this.node.getAttributes().getNamedItemNS(nsj, namej);
if (el == null) { return context.nil; }
return NokogiriHelpers.getCachedNodeOrCreate(context.runtime, el);
}
@JRubyMethod(name = "blank?")
public IRubyObject
blank_p(ThreadContext context)
{
// according to libxml doc,
// a node is blank if if it is a Text or CDATA node consisting of whitespace only
if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
String data = node.getTextContent();
return context.runtime.newBoolean(data == null || isBlank(data));
}
return context.runtime.getFalse();
}
@JRubyMethod
public IRubyObject
child(ThreadContext context)
{
return getCachedNodeOrCreate(context.getRuntime(), node.getFirstChild());
}
@JRubyMethod
public IRubyObject
children(ThreadContext context)
{
final IRubyObject[] nodes = getChildren();
if (nodes.length == 0) {
return XmlNodeSet.newEmptyNodeSet(context, this);
}
return XmlNodeSet.newNodeSet(context.runtime, nodes);
}
IRubyObject[]
getChildren()
{
NodeList nodeList = node.getChildNodes();
if (nodeList.getLength() > 0) {
return nodeListToRubyArray(getRuntime(), nodeList);
}
return IRubyObject.NULL_ARRAY;
}
@JRubyMethod
public IRubyObject
first_element_child(ThreadContext context)
{
List<Node> elementNodes = getElements(node, true);
if (elementNodes.size() == 0) { return context.nil; }
return getCachedNodeOrCreate(context.runtime, elementNodes.get(0));
}
@JRubyMethod
public IRubyObject
last_element_child(ThreadContext context)
{
List<Node> elementNodes = getElements(node, false);
if (elementNodes.size() == 0) { return context.nil; }
return getCachedNodeOrCreate(context.runtime, elementNodes.get(elementNodes.size() - 1));
}
@JRubyMethod(name = {"element_children", "elements"})
public IRubyObject
element_children(ThreadContext context)
{
List<Node> elementNodes = getElements(node, false);
IRubyObject[] array = NokogiriHelpers.nodeListToArray(context.runtime, elementNodes);
return XmlNodeSet.newNodeSet(context.runtime, array, this);
}
private static List<Node>
getElements(Node node, final boolean firstOnly)
{
NodeList children = node.getChildNodes();
if (children.getLength() == 0) {
return Collections.emptyList();
}
ArrayList<Node> elements = firstOnly ? null : new ArrayList<Node>(children.getLength());
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
if (firstOnly) {
return Collections.singletonList(child);
}
elements.add(child);
}
}
return elements;
}
/**
* call-seq:
* compare(other)
*
* Compare this Node to +other+ with respect to their Document
*/
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject
compare(ThreadContext context, IRubyObject other)
{
if (!(other instanceof XmlNode)) {
return context.runtime.newFixnum(-2);
}
Node otherNode = asXmlNode(context, other).node;
// Do not touch this if, if it's not for a good reason.
if (node.getNodeType() == Node.DOCUMENT_NODE ||
otherNode.getNodeType() == Node.DOCUMENT_NODE) {
return context.runtime.newFixnum(1);
}
try {
int res = node.compareDocumentPosition(otherNode);
if ((res & FIRST_PRECEDES_SECOND) == FIRST_PRECEDES_SECOND) {
return context.runtime.newFixnum(-1);
} else if ((res & SECOND_PRECEDES_FIRST) == SECOND_PRECEDES_FIRST) {
return context.runtime.newFixnum(1);
} else if (res == IDENTICAL_ELEMENTS) {
return context.runtime.newFixnum(0);
}
return context.runtime.newFixnum(-2);
} catch (Exception ex) {
return context.runtime.newFixnum(-2);
}
}
/**
* TODO: this is a stub implementation. It's not clear what
* 'in_context' is supposed to do. Also should take
* <code>options</code> into account.
*/
@JRubyMethod(required = 2, visibility = Visibility.PRIVATE)
public IRubyObject
in_context(ThreadContext context, IRubyObject str, IRubyObject options)
{
RubyClass klass;
XmlDomParserContext ctx;
InputStream istream;
final Ruby runtime = context.runtime;
XmlDocument document = document(runtime);
if (document == null) { return context.nil; }
if (document instanceof Html4Document) {
klass = getNokogiriClass(runtime, "Nokogiri::HTML4::Document");
ctx = new HtmlDomParserContext(runtime, options);
((HtmlDomParserContext) ctx).enableDocumentFragment();
ctx.setStringInputSource(context, str, context.nil);
} else {
klass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
ctx = new XmlDomParserContext(runtime, options);
ctx.setStringInputSource(context, str, context.nil);
}
// TODO: for some reason, document.getEncoding() can be null or nil (don't know why)
// run `test_parse_with_unparented_html_text_context_node' few times to see this happen
if (document instanceof Html4Document && !(document.getEncoding() == null || document.getEncoding().isNil())) {
HtmlDomParserContext htmlCtx = (HtmlDomParserContext) ctx;
htmlCtx.setEncoding(document.getEncoding().asJavaString());
}
XmlDocument doc = ctx.parse(context, klass, context.nil);
RubyArray documentErrors = getErrors(document);
RubyArray docErrors = getErrors(doc);
if (checkNewErrors(documentErrors, docErrors)) {
for (int i = 0; i < docErrors.getLength(); i++) {
documentErrors.append(docErrors.entry(i));
}
document.setInstanceVariable("@errors", documentErrors);
return XmlNodeSet.newNodeSet(context.runtime, IRubyObject.NULL_ARRAY, this);
}
// The first child might be document type node (dtd declaration).
// XmlNodeSet to be return should not have dtd decl in its list.
Node first;
if (doc.node.getFirstChild().getNodeType() == Node.DOCUMENT_TYPE_NODE) {
first = doc.node.getFirstChild().getNextSibling();
} else {
first = doc.node.getFirstChild();
}
IRubyObject[] nodes = new IRubyObject[] { NokogiriHelpers.getCachedNodeOrCreate(runtime, first) };
return XmlNodeSet.newNodeSet(context.runtime, nodes, this);
}
private static RubyArray
getErrors(XmlDocument document)
{
IRubyObject obj = document.getInstanceVariable("@errors");
if (obj instanceof RubyArray) { return (RubyArray) obj; }
return RubyArray.newEmptyArray(document.getRuntime());
}
private static boolean
checkNewErrors(RubyArray baseErrors, RubyArray newErrors)
{
int length = ((RubyArray) newErrors.op_diff(baseErrors)).size();
return length > 0;
}
@JRubyMethod(name = {"content", "text", "inner_text"})
public IRubyObject
content(ThreadContext context)
{
return stringOrNil(context.runtime, getContentImpl());
}
public CharSequence
getContentImpl()
{
if (!node.hasChildNodes() && node.getNodeValue() == null &&
(node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE)) {
return null;
}
CharSequence textContent;
if (this instanceof XmlDocument) {
Node node = ((Document) this.node).getDocumentElement();
if (node == null) {
textContent = "";
} else {
Node documentElement = ((Document) this.node).getDocumentElement();
textContent = getTextContentRecursively(new StringBuilder(), documentElement);
}
} else {
textContent = getTextContentRecursively(new StringBuilder(), node);
}
// textContent = NokogiriHelpers.convertEncodingByNKFIfNecessary(context, (XmlDocument) document(context), textContent);
return textContent;
}
private static StringBuilder
getTextContentRecursively(StringBuilder buffer, Node currentNode)
{
CharSequence textContent = currentNode.getNodeValue();
if (textContent != null && NokogiriHelpers.shouldDecode(currentNode)) {
textContent = NokogiriHelpers.decodeJavaString(textContent);
}
if (textContent != null) { buffer.append(textContent); }
NodeList children = currentNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (hasTextContent(child)) { getTextContentRecursively(buffer, child); }
}
return buffer;
}
private static boolean
hasTextContent(Node child)
{
return child.getNodeType() != Node.COMMENT_NODE && child.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE;
}
@JRubyMethod
public final IRubyObject
document(ThreadContext context)
{
return document(context.runtime);
}
XmlDocument
document(final Ruby runtime)
{
return document(runtime, true);
}
XmlDocument
document(final Ruby runtime, boolean create)
{
if (doc == null) {
doc = (XmlDocument) node.getOwnerDocument().getUserData(NokogiriHelpers.CACHED_NODE);
if (doc == null && create) {
doc = (XmlDocument) getCachedNodeOrCreate(runtime, node.getOwnerDocument());
node.getOwnerDocument().setUserData(NokogiriHelpers.CACHED_NODE, doc, null);
}
}
return doc;
}
public IRubyObject
dup()
{
return dup_implementation(getMetaClass().getClassRuntime(), true);
}
@JRubyMethod
public IRubyObject
dup(ThreadContext context)
{
return dup_implementation(context, true);
}
@JRubyMethod
public IRubyObject
dup(ThreadContext context, IRubyObject depth)
{
boolean deep = depth instanceof RubyInteger && RubyFixnum.fix2int(depth) != 0;
return dup_implementation(context, deep);
}
protected final IRubyObject
dup_implementation(ThreadContext context, boolean deep)
{
return dup_implementation(context.runtime, deep);
}
protected IRubyObject
dup_implementation(Ruby runtime, boolean deep)
{
XmlNode clone;
try {
clone = (XmlNode) clone();
} catch (CloneNotSupportedException e) {
throw runtime.newRuntimeError(e.toString());
}
Node newNode = node.cloneNode(deep);
clone.node = newNode;
return clone;
}
public static RubyString
encode_special_chars(ThreadContext context, IRubyObject string)
{
CharSequence str = NokogiriHelpers.encodeJavaString(rubyStringToString(string));
return RubyString.newString(context.runtime, str);
}
/**
* Instance method version of the above static method.
*/
@JRubyMethod(name = "encode_special_chars")
public IRubyObject
i_encode_special_chars(ThreadContext context, IRubyObject string)
{
return encode_special_chars(context, string);
}
/**
* Get the attribute at the given key, <code>key</code>.
* Assumes that this node has attributes (i.e. that key? returned
* true).
*/
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject
get(ThreadContext context, IRubyObject rbkey)
{
if (node instanceof Element) {
if (rbkey == null || rbkey.isNil()) { return context.nil; }
String key = rubyStringToString(rbkey);
Element element = (Element) node;
if (!element.hasAttribute(key)) { return context.nil; }
String value = element.getAttribute(key);
return stringOrNil(context.runtime, value);
}
return context.nil;
}
/**
* Returns the owner document, checking if this node is the
* document, or returns null if there is no owner.
*/
protected Document
getOwnerDocument()
{
if (node.getNodeType() == Node.DOCUMENT_NODE) {
return (Document) node;
} else {
return node.getOwnerDocument();
}
}
@JRubyMethod
public IRubyObject
internal_subset(ThreadContext context)
{
Document document = getOwnerDocument();
if (document == null) {
return context.getRuntime().getNil();
}
XmlDocument xdoc =
(XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
IRubyObject xdtd = xdoc.getInternalSubset(context);
return xdtd;
}
@JRubyMethod
public IRubyObject
create_internal_subset(ThreadContext context,
IRubyObject name,
IRubyObject external_id,
IRubyObject system_id)
{
IRubyObject subset = internal_subset(context);
if (!subset.isNil()) {
throw context.runtime.newRuntimeError("Document already has internal subset");
}
Document document = getOwnerDocument();
if (document == null) {
return context.getRuntime().getNil();
}
XmlDocument xdoc =
(XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
IRubyObject xdtd = xdoc.createInternalSubset(context, name,
external_id, system_id);
return xdtd;
}
@JRubyMethod
public IRubyObject
external_subset(ThreadContext context)
{
Document document = getOwnerDocument();
if (document == null) {
return context.getRuntime().getNil();
}
XmlDocument xdoc =
(XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
IRubyObject xdtd = xdoc.getExternalSubset(context);
return xdtd;
}
@JRubyMethod
public IRubyObject
create_external_subset(ThreadContext context,
IRubyObject name,
IRubyObject external_id,
IRubyObject system_id)
{
IRubyObject subset = external_subset(context);
if (!subset.isNil()) {
throw context.runtime.newRuntimeError("Document already has external subset");
}
Document document = getOwnerDocument();
if (document == null) {
return context.getRuntime().getNil();
}
XmlDocument xdoc = (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
IRubyObject xdtd = xdoc.createExternalSubset(context, name, external_id, system_id);
return xdtd;
}
/**
* Test if this node has an attribute named <code>rbkey</code>.
* Overridden in XmlElement.
*/
@JRubyMethod(name = {"key?", "has_attribute?"})
public IRubyObject
key_p(ThreadContext context, IRubyObject rbkey)
{
if (node instanceof Element) {
String key = rubyStringToString(rbkey);
Element element = (Element) node;
if (element.hasAttribute(key)) {
return context.runtime.getTrue();
} else {
NamedNodeMap namedNodeMap = element.getAttributes();
for (int i = 0; i < namedNodeMap.getLength(); i++) {
Node n = namedNodeMap.item(i);
if (key.equals(n.getLocalName())) {
return context.runtime.getTrue();
}
}
}
return context.runtime.getFalse();
}
return context.nil;
}
@JRubyMethod
public IRubyObject
namespace(ThreadContext context)
{
final XmlDocument doc = document(context.runtime);
if (doc instanceof Html4Document) { return context.nil; }
String namespaceURI = node.getNamespaceURI();
if (namespaceURI == null || namespaceURI.isEmpty()) {
return context.nil;
}
String prefix = node.getPrefix();
NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCache(node);
XmlNamespace namespace = nsCache.get(prefix, namespaceURI);
if (namespace == null || namespace.isEmpty()) {
// if it's not in the cache, create an unowned, uncached namespace and
// return that. XmlReader can't insert namespaces into the cache, so
// this is necessary for XmlReader to work correctly.
namespace = new XmlNamespace(context.runtime, null, prefix, namespaceURI, doc);
}
return namespace;
}
/**
* Return an array of XmlNamespace nodes based on the attributes
* of this node.
*/
@JRubyMethod
public IRubyObject
namespace_definitions(ThreadContext context)
{
// don't use namespace_definitions cache anymore since
// namespaces might be deleted. Reflecting the result of
// namespace removals is complicated, so the cache might not be
// updated.
final XmlDocument doc = document(context.runtime);
if (doc == null) { return context.runtime.newEmptyArray(); }
if (doc instanceof Html4Document) { return context.runtime.newEmptyArray(); }
List<XmlNamespace> namespaces = doc.getNamespaceCache().get(node);
return context.runtime.newArray((List) namespaces);
}
/**
* Return an array of XmlNamespace nodes defined on this node and
* on any ancestor node.
*/
@JRubyMethod
public RubyArray
namespace_scopes(ThreadContext context)
{
final XmlDocument doc = document(context.runtime);
if (doc == null) { return context.runtime.newEmptyArray(); }
if (doc instanceof Html4Document) { return context.runtime.newEmptyArray(); }
Node previousNode;
if (node.getNodeType() == Node.ELEMENT_NODE) {
previousNode = node;
} else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
previousNode = ((Attr)node).getOwnerElement();
} else {
previousNode = findPreviousElement(node);
}
if (previousNode == null) { return context.runtime.newEmptyArray(); }
final RubyArray scoped_namespaces = context.runtime.newArray();
final HashSet<String> prefixes_in_scope = new HashSet<String>(8);
NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCache(previousNode);
for (Node previous = previousNode; previous != null;) {
List<XmlNamespace> namespaces = nsCache.get(previous);
for (XmlNamespace namespace : namespaces) {
if (prefixes_in_scope.contains(namespace.getPrefix())) { continue; }
scoped_namespaces.append(namespace);
prefixes_in_scope.add(namespace.getPrefix());
}
previous = findPreviousElement(previous);
}
return scoped_namespaces;
}
private Node
findPreviousElement(Node n)
{
Node previous = n.getPreviousSibling() == null ? n.getParentNode() : n.getPreviousSibling();
if (previous == null || previous.getNodeType() == Node.DOCUMENT_NODE) { return null; }
if (previous.getNodeType() == Node.ELEMENT_NODE) {
return previous;
} else {
return findPreviousElement(previous);
}
}
@JRubyMethod(name = "namespaced_key?")
public IRubyObject
namespaced_key_p(ThreadContext context, IRubyObject elementLName, IRubyObject namespaceUri)
{
return this.attribute_with_ns(context, elementLName, namespaceUri).isNil() ?
context.runtime.getFalse() : context.runtime.getTrue();
}
protected void
setContent(IRubyObject content)
{
String javaContent = rubyStringToString(content);
node.setTextContent(javaContent);
if (javaContent == null || javaContent.length() == 0) { return; }
if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) { return; }
if (node.getFirstChild() != null) {
node.getFirstChild().setUserData(NokogiriHelpers.ENCODED_STRING, true, null);
}
}
private void
setContent(String content)
{
node.setTextContent(content);
this.content = null; // clear cache
}
@JRubyMethod(name = "native_content=")
public IRubyObject
native_content_set(ThreadContext context, IRubyObject content)
{
setContent(content);
return content;
}
@JRubyMethod
public IRubyObject
lang(ThreadContext context)
{
IRubyObject currentObj = this ;
while (!currentObj.isNil()) {
XmlNode currentNode = asXmlNode(context, currentObj);
IRubyObject lang = currentNode.getAttribute(context.runtime, "xml:lang");
if (!lang.isNil()) { return lang ; }
currentObj = currentNode.parent(context);
}
return context.nil;
}
@JRubyMethod(name = "lang=")
public IRubyObject
set_lang(ThreadContext context, IRubyObject lang)
{
setAttribute(context, "xml:lang", rubyStringToString(lang));
return context.nil ;
}
/**
* @param args {IRubyObject io,
* IRubyObject encoding,
* IRubyObject indentString,
* IRubyObject options}
*/
@JRubyMethod(required = 4, visibility = Visibility.PRIVATE)
public IRubyObject
native_write_to(ThreadContext context, IRubyObject[] args)
{
IRubyObject io = args[0];
IRubyObject encoding = args[1];
IRubyObject indentString = args[2];
IRubyObject options = args[3];
String encString = rubyStringToString(encoding);
SaveContextVisitor visitor =
new SaveContextVisitor(RubyFixnum.fix2int(options), rubyStringToString(indentString), encString, isHtmlDoc(context),
isFragment(), 0);
accept(context, visitor);
final IRubyObject rubyString;
if (NokogiriHelpers.isUTF8(encString)) {
rubyString = convertString(context.runtime, visitor.getInternalBuffer());
} else {
ByteBuffer bytes = convertEncoding(Charset.forName(encString), visitor.getInternalBuffer());
ByteList str = new ByteList(bytes.array(), bytes.arrayOffset(), bytes.remaining());
rubyString = RubyString.newString(context.runtime, str);
}
Helpers.invoke(context, io, "write", rubyString);
return io;
}
private boolean
isHtmlDoc(ThreadContext context)
{
return document(context).getMetaClass().isKindOfModule(getNokogiriClass(context.runtime, "Nokogiri::HTML4::Document"));
}
private boolean
isFragment()
{
if (node instanceof DocumentFragment) { return true; }
if (node.getParentNode() != null && node.getParentNode() instanceof DocumentFragment) { return true; }
return false;
}
@JRubyMethod(name = {"next_sibling", "next"})
public IRubyObject
next_sibling(ThreadContext context)
{
return getCachedNodeOrCreate(context.getRuntime(), node.getNextSibling());
}
@JRubyMethod(name = {"previous_sibling", "previous"})
public IRubyObject
previous_sibling(ThreadContext context)
{
return getCachedNodeOrCreate(context.getRuntime(), node.getPreviousSibling());
}
@JRubyMethod(name = {"node_name", "name"})
public IRubyObject
node_name(ThreadContext context)
{
return getNodeName(context);
}
@JRubyMethod(name = {"node_name=", "name="})
public IRubyObject
node_name_set(ThreadContext context, IRubyObject nodeName)
{
nodeName = doSetName(nodeName);
String newName = nodeName == null ? null : rubyStringToString((RubyString) nodeName);
this.node = NokogiriHelpers.renameNode(node, null, newName);
return this;
}
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject
set(ThreadContext context, IRubyObject rbkey, IRubyObject rbval)
{
if (node instanceof Element) {
setAttribute(context, rubyStringToString(rbkey), rubyStringToString(rbval));
return this;
} else {
return rbval;
}
}
private void
setAttribute(ThreadContext context, String key, String val)
{
Element element = (Element) node;
String uri = null;
int colonIndex = key.indexOf(":");
if (colonIndex > 0) {
String prefix = key.substring(0, colonIndex);
if (prefix.equals("xml")) {
uri = "http://www.w3.org/XML/1998/namespace";
} else if (prefix.equals("xmlns")) {
uri = "http://www.w3.org/2000/xmlns/";
} else {
uri = node.lookupNamespaceURI(prefix);
}
}
if (uri != null) {
element.setAttributeNS(uri, key, val);
} else {
element.setAttribute(key, val);
}
clearXpathContext(node);
}
private String
findNamespaceHref(ThreadContext context, String prefix)
{
XmlNode currentNode = this;
final XmlDocument doc = document(context.runtime);
while (currentNode != doc) {
RubyArray namespaces = currentNode.namespace_scopes(context);
for (int i = 0; i < namespaces.size(); i++) {
XmlNamespace namespace = (XmlNamespace) namespaces.eltInternal(i);
if (namespace.hasPrefix(prefix)) { return namespace.getHref(); }
}
IRubyObject parent = currentNode.parent(context);
if (parent == context.nil) { break; }
currentNode = (XmlNode) parent;
}
return null;
}
@JRubyMethod
public IRubyObject
parent(ThreadContext context)
{
/*
* Check if this node is the root node of the document.
* If so, parent is the document.
*/
if (node.getOwnerDocument() != null &&
node.getOwnerDocument().getDocumentElement() == node) {
return document(context);
}
return getCachedNodeOrCreate(context.runtime, node.getParentNode());
}
@JRubyMethod
public IRubyObject
path(ThreadContext context)
{
return RubyString.newString(context.runtime, NokogiriHelpers.getNodeCompletePath(this.node));
}
@JRubyMethod
public IRubyObject
pointer_id(ThreadContext context)
{
return RubyFixnum.newFixnum(context.runtime, this.node.hashCode());
}
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject
set_namespace(ThreadContext context, IRubyObject namespace)
{
if (namespace.isNil()) {
XmlDocument doc = document(context.runtime);
if (doc != null) {
Node node = this.node;
doc.getNamespaceCache().remove(node);
this.node = NokogiriHelpers.renameNode(node, null, NokogiriHelpers.getLocalPart(node.getNodeName()));
}
} else {
XmlNamespace ns = (XmlNamespace) namespace;
// Assigning node = ...renameNode() or not seems to make no
// difference. Why not? -pmahoney
// It actually makes a great deal of difference. renameNode()
// will operate in place if it can, but sometimes it can't.
// The node you passed in *might* come back as you expect, but
// it might not. It's much safer to throw away the original
// and keep the return value. -mbklein
String new_name = NokogiriHelpers.newQName(ns.getPrefix(), node);
this.node = NokogiriHelpers.renameNode(node, ns.getHref(), new_name);
}
clearXpathContext(getNode());
return this;
}
@JRubyMethod(name = {"unlink", "remove"})
public IRubyObject
unlink(ThreadContext context)
{
final Node parent = node.getParentNode();
if (parent != null) {
parent.removeChild(node);
clearXpathContext(parent);
}
return this;
}
/**
* The C-library simply returns libxml2 magic numbers. Here we
* convert Java Xml nodes to the appropriate constant defined in
* xml/node.rb.
*/
@JRubyMethod(name = {"node_type", "type"})
public IRubyObject
node_type(ThreadContext context)
{
String type;
switch (node.getNodeType()) {
case Node.ELEMENT_NODE:
if (this instanceof XmlElementDecl) {
type = "ELEMENT_DECL";
} else if (this instanceof XmlAttributeDecl) {
type = "ATTRIBUTE_DECL";
} else if (this instanceof XmlEntityDecl) {
type = "ENTITY_DECL";
} else {
type = "ELEMENT_NODE";
}
break;
case Node.ATTRIBUTE_NODE:
type = "ATTRIBUTE_NODE";
break;
case Node.TEXT_NODE:
type = "TEXT_NODE";
break;
case Node.CDATA_SECTION_NODE:
type = "CDATA_SECTION_NODE";
break;
case Node.ENTITY_REFERENCE_NODE:
type = "ENTITY_REF_NODE";
break;
case Node.ENTITY_NODE:
type = "ENTITY_NODE";
break;
case Node.PROCESSING_INSTRUCTION_NODE:
type = "PI_NODE";
break;
case Node.COMMENT_NODE:
type = "COMMENT_NODE";
break;
case Node.DOCUMENT_NODE:
if (this instanceof Html4Document) {
type = "HTML_DOCUMENT_NODE";
} else {
type = "DOCUMENT_NODE";
}
break;
case Node.DOCUMENT_TYPE_NODE:
type = "DOCUMENT_TYPE_NODE";
break;
case Node.DOCUMENT_FRAGMENT_NODE:
type = "DOCUMENT_FRAG_NODE";
break;
case Node.NOTATION_NODE:
type = "NOTATION_NODE";
break;
default:
return context.runtime.newFixnum(0);
}
return getNokogiriClass(context.runtime, "Nokogiri::XML::Node").getConstant(type);
}
/*
* NOTE that the behavior of this function is very difference from the CRuby implementation, see
* the docstring in ext/nokogiri/xml_node.c for details.
*/
@JRubyMethod
public IRubyObject
line(ThreadContext context)
{
Node root = getOwnerDocument();
int[] counter = new int[1];
count(root, counter);
// offset of 2:
// - one because humans start counting at 1 not zero
// - one to account for the XML declaration present in the output
return RubyFixnum.newFixnum(context.runtime, counter[0] + 2);
}
private boolean
count(Node node, int[] counter)
{
if (node == this.node) {
return true;
}
NodeList list = node.getChildNodes();
for (int jchild = 0; jchild < list.getLength(); jchild++) {
Node child = list.item(jchild);
String text = null;
if (child instanceof Text) {
text = ((Text)child).getData();
} else if (child instanceof Comment) {
text = ((Comment)child).getData();
}
if (text != null) {
int textLength = text.length();
for (int jchar = 0; jchar < textLength; jchar++) {
if (text.charAt(jchar) == '\n') {
counter[0] += 1;
}
}
}
if (count(child, counter)) { return true; }
}
return false;
}
@JRubyMethod
public IRubyObject
next_element(ThreadContext context)
{
Node nextNode = node.getNextSibling();
if (nextNode == null) { return context.nil; }
if (nextNode instanceof Element) {
return getCachedNodeOrCreate(context.runtime, nextNode);
}
Node deeper = nextNode.getNextSibling();
if (deeper == null) { return context.nil; }
return getCachedNodeOrCreate(context.runtime, deeper);
}
@JRubyMethod
public IRubyObject
previous_element(ThreadContext context)
{
Node prevNode = node.getPreviousSibling();
if (prevNode == null) { return context.nil; }
if (prevNode instanceof Element) {
return getCachedNodeOrCreate(context.runtime, prevNode);
}
Node shallower = prevNode.getPreviousSibling();
if (shallower == null) { return context.nil; }
return getCachedNodeOrCreate(context.runtime, shallower);
}
protected enum AdoptScheme {
CHILD, PREV_SIBLING, NEXT_SIBLING, REPLACEMENT
}
/**
* Adopt XmlNode <code>other</code> into the document of
* <code>this</code> using the specified scheme.
*/
protected IRubyObject
adoptAs(ThreadContext context, AdoptScheme scheme, IRubyObject other_)
{
final XmlNode other = asXmlNode(context, other_);
// this.doc might be null since this node can be empty node.
if (doc != null) { other.setDocument(context, doc); }
IRubyObject nodeOrTags = other;
Node thisNode = node;
Node otherNode = other.node;
try {
Document prev = otherNode.getOwnerDocument();
Document doc = thisNode.getOwnerDocument();
if (doc == null && thisNode instanceof Document) {
// we are adding the new node to a new empty document
doc = (Document) thisNode;
}
clearXpathContext(prev);
clearXpathContext(doc);
if (doc != null && doc != otherNode.getOwnerDocument()) {
Node ret = doc.adoptNode(otherNode);
if (ret == null) {
throw context.runtime.newRuntimeError("Failed to take ownership of node");
}
// FIXME: this is really a hack, see documentation of fixUserData() for more details.
fixUserData(prev, ret);
otherNode = ret;
}
Node parent = thisNode.getParentNode();
switch (scheme) {
case CHILD:
Node[] children = adoptAsChild(thisNode, otherNode);
if (children.length == 1 && otherNode == children[0]) {
break;
} else {
nodeOrTags = nodeArrayToRubyArray(context.runtime, children);
}
break;
case PREV_SIBLING:
adoptAsPrevSibling(context, parent, thisNode, otherNode);
break;
case NEXT_SIBLING:
adoptAsNextSibling(context, parent, thisNode, otherNode);
break;
case REPLACEMENT:
adoptAsReplacement(context, parent, thisNode, otherNode);
break;
}
} catch (Exception e) {
throw context.runtime.newRuntimeError(e.toString());
}
if (otherNode.getNodeType() == Node.TEXT_NODE) {
coalesceTextNodes(context, other, scheme);
}
if (this instanceof XmlDocument) {
((XmlDocument) this).resetNamespaceCache(context);
}
other.relink_namespace(context);
return nodeOrTags;
}
/**
* This is a hack to fix #839. We should submit a patch to Xerces.
* It looks like CoreDocumentImpl.adoptNode() doesn't copy
* the user data associated with child nodes (recursively).
*/
private static void
fixUserData(Document previous, Node ret)
{
final String key = NokogiriHelpers.ENCODED_STRING;
for (Node child = ret.getFirstChild(); child != null; child = child.getNextSibling()) {
CoreDocumentImpl previousDocument = (CoreDocumentImpl) previous;
child.setUserData(key, previousDocument.getUserData(child, key), null);
fixUserData(previous, child);
}
}
private Node[]
adoptAsChild(final Node parent, Node otherNode)
{
/*
* This is a bit of a hack. C-Nokogiri allows adding a bare text node as the root element.
* Java (and XML spec?) does not. So we wrap the text node in an element.
*/
if (parent.getNodeType() == Node.DOCUMENT_NODE && otherNode.getNodeType() == Node.TEXT_NODE) {
Element e = (Element) parent.getFirstChild();
if (e == null || !e.getNodeName().equals(TEXT_WRAPPER_NAME)) {
e = ((Document) parent).createElement(TEXT_WRAPPER_NAME);
adoptAsChild(parent, e);
}
e.appendChild(otherNode);
otherNode = e;
} else {
parent.appendChild(otherNode);
}
return new Node[] { otherNode };
}
protected void
adoptAsPrevSibling(ThreadContext context,
Node parent,
Node thisNode, Node otherNode)
{
if (parent == null) {
/* I'm not sure what do do here... A node with no
* parent can't exactly have a 'sibling', so we make
* otherNode parentless also. */
if (otherNode.getParentNode() != null) {
otherNode.getParentNode().removeChild(otherNode);
}
return;
}
parent.insertBefore(otherNode, thisNode);
}
protected void
adoptAsNextSibling(ThreadContext context,
Node parent,
Node thisNode, Node otherNode)
{
if (parent == null) {
/* I'm not sure what do do here... A node with no
* parent can't exactly have a 'sibling', so we make
* otherNode parentless also. */
if (otherNode.getParentNode() != null) {
otherNode.getParentNode().removeChild(otherNode);
}
return;
}
Node nextSib = thisNode.getNextSibling();
if (nextSib != null) {
parent.insertBefore(otherNode, nextSib);
} else {
parent.appendChild(otherNode);
}
}
protected void
adoptAsReplacement(ThreadContext context,
Node parentNode,
Node thisNode, Node otherNode)
{
if (parentNode == null) {
/* nothing to replace? */
return;
}
try {
parentNode.replaceChild(otherNode, thisNode);
} catch (Exception e) {
String prefix = "could not replace child: ";
throw context.runtime.newRuntimeError(prefix + e.toString());
}
}
/**
* Add <code>other</code> as a child of <code>this</code>.
*/
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject
add_child_node(ThreadContext context, IRubyObject other)
{
return adoptAs(context, AdoptScheme.CHILD, other);
}
/**
* Replace <code>this</code> with <code>other</code>.
*/
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject
replace_node(ThreadContext context, IRubyObject other)
{
return adoptAs(context, AdoptScheme.REPLACEMENT, other);
}
/**
* Add <code>other</code> as a sibling before <code>this</code>.
*/
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject
add_previous_sibling_node(ThreadContext context, IRubyObject other)
{
return adoptAs(context, AdoptScheme.PREV_SIBLING, other);
}
/**
* Add <code>other</code> as a sibling after <code>this</code>.
*/
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject
add_next_sibling_node(ThreadContext context, IRubyObject other)
{
return adoptAs(context, AdoptScheme.NEXT_SIBLING, other);
}
/**
* call-seq:
* process_xincludes(options)
*
* Loads and substitutes all xinclude elements below the node. The
* parser context will be initialized with +options+.
*
*/
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject
process_xincludes(ThreadContext context, IRubyObject options)
{
XmlDocument xmlDocument = (XmlDocument)document(context);
RubyArray errors = (RubyArray)xmlDocument.getInstanceVariable("@errors");
while (errors.getLength() > 0) {
XmlSyntaxError error = (XmlSyntaxError)errors.shift(context);
if (error.toString().contains("Include operation failed")) {
throw error.toThrowable();
}
}
return this;
}
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject
clear_xpath_context(ThreadContext context)
{
clearXpathContext(getNode());
return context.nil ;
}
@SuppressWarnings("unchecked")
@Override
public Object
toJava(final Class target)
{
if (target == Object.class || Node.class.isAssignableFrom(target)) {
return getNode();
}
return super.toJava(target);
}
}
|