Class: XMLParser

Inherits:
Object
  • Object
show all
Defined in:
ext/xmlparser.c

Constant Summary collapse

Error =
eXMLParserError
PARAM_ENTITY_PARSING_NEVER =
XML_PARAM_ENTITY_PARSING_NEVER
PARAM_ENTITY_PARSING_UNLESS_STANDALONE =
XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE
PARAM_ENTITY_PARSING_ALWAYS =
XML_PARAM_ENTITY_PARSING_ALWAYS

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



1499
1500
1501
1502
1503
# File 'ext/xmlparser.c', line 1499

static VALUE
XMLParser_initialize(VALUE obj)
{
  return Qnil;
}

Class Method Details

.expatVersionObject



2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
# File 'ext/xmlparser.c', line 2012

static VALUE
XMLParser_s_expatVersion(VALUE obj)
{
#if defined(HAVE_EXPAT_H)
  return ENC_(rb_str_new2(XML_ExpatVersion()));
#elif defined(EXPAT_1_2)
  return ENC_(rb_str_new2("1.2"));
#elif defined(NEW_EXPAT)
  return ENC_(rb_str_new2("1.1"));
#else
  return ENC_(rb_str_new2("1.0"));
#endif
}

.getFeatureListObject



2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
# File 'ext/xmlparser.c', line 2123

static VALUE
XMLParser_s_getFeatureList(VALUE obj)
{
  const XML_Feature* list;
  VALUE ret = rb_hash_new();

  list = XML_GetFeatureList();
  while (list && list->feature) {
    rb_hash_aset(ret, FO_(ENC_(rb_str_new2(list->name))), INT2NUM(list->value));
    list++;
  }

  return ret;
}

.new(*args) ⇒ Object

constructor



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
# File 'ext/xmlparser.c', line 1364

static VALUE
XMLParser_new(int argc, VALUE* argv, VALUE klass)
{
  XMLParser* parser;
  VALUE obj;
  VALUE arg1;
  VALUE arg2;
  VALUE arg3;
  int count;
  char* encoding = NULL;
#ifdef NEW_EXPAT
  char* nssep = NULL;
#endif
  char* context = NULL;
  XMLParser* rootparser = NULL;
  VALUE parent = Qnil;

  count = rb_scan_args(argc, argv, "03", &arg1, &arg2, &arg3);
  if (count == 1) {
    /* new(encoding) */
    if (TYPE(arg1) != T_NIL) {
      Check_Type(arg1, T_STRING); /* encoding */
      encoding = RSTRING_PTR(arg1);
    }
  }
  else if (count == 2) {
    /* new(encoding, nschar) */
    /* new(parser, context) */
#ifdef NEW_EXPAT
    if (TYPE(arg1) != T_DATA) {
      if (TYPE(arg1) != T_NIL) {
	Check_Type(arg1, T_STRING); /* encoding */
	encoding = RSTRING_PTR(arg1);
      }
      Check_Type(arg2, T_STRING); /* nschar */
      nssep = RSTRING_PTR(arg2);
    }
    else {
#endif
      Check_Type(arg1, T_DATA); /* parser */
      GET_PARSER(arg1, rootparser);
      if (!NIL_P(arg2)) {
	Check_Type(arg2, T_STRING); /* context */
	context = RSTRING_PTR(arg2);
      }
      parent = arg1;
#ifdef NEW_EXPAT
    }
#endif
  }
  else if (count == 3) {
    /* new(parser, context, encoding) */
    Check_Type(arg1, T_DATA); /* parser */
    GET_PARSER(arg1, rootparser);
    if (!NIL_P(arg2)) {
      Check_Type(arg2, T_STRING); /* context */
      context = RSTRING_PTR(arg2);
    }
    Check_Type(arg3, T_STRING); /* encoding */
    encoding = RSTRING_PTR(arg3);
    parent = arg1;
  }

  /* create object */
  obj = Data_Make_Struct(klass, XMLParser,
			 XMLParser_mark, XMLParser_free, parser);
  /* create parser */
  if (rootparser == NULL) {
#ifdef NEW_EXPAT
    if (nssep == NULL)
      parser->parser = XML_ParserCreate(encoding);
    else
      parser->parser = XML_ParserCreateNS(encoding, nssep[0]);
#else
    parser->parser = XML_ParserCreate(encoding);
#endif
    parser->tainted = 0;
    parser->context = NULL;
  }
  else {
    parser->parser = XML_ExternalEntityParserCreate(rootparser->parser,
    						    context, encoding);
    /* clear all inhrited handlers,
       because handlers should be set in "parse" method  */
    XML_SetElementHandler(parser->parser, NULL, NULL);
    XML_SetCharacterDataHandler(parser->parser, NULL);
    XML_SetProcessingInstructionHandler(parser->parser, NULL);
    XML_SetDefaultHandler(parser->parser, NULL);
    XML_SetUnparsedEntityDeclHandler(parser->parser, NULL);
    XML_SetNotationDeclHandler(parser->parser, NULL);
    XML_SetExternalEntityRefHandler(parser->parser, NULL);
#ifdef NEW_EXPAT
    XML_SetCommentHandler(parser->parser, NULL);
    XML_SetCdataSectionHandler(parser->parser, NULL, NULL);
    XML_SetNamespaceDeclHandler(parser->parser, NULL, NULL);
    XML_SetNotStandaloneHandler(parser->parser, NULL);
#endif
#ifdef HAVE_XML_SETDOCTYPEDECLHANDLER
    XML_SetDoctypeDeclHandler(parser->parser, NULL, NULL);
#endif
#ifdef HAVE_EXPAT_H
    XML_SetElementDeclHandler(parser->parser, NULL);
    XML_SetAttlistDeclHandler(parser->parser, NULL);
    XML_SetXmlDeclHandler(parser->parser, NULL);
    XML_SetEntityDeclHandler(parser->parser, NULL);
#endif
#if 0
    XML_SetExternalParsedEntityDeclHandler(parser->parser, NULL);
    XML_SetInternalParsedEntityDeclHandler(parser->parser, NULL);
#endif
#ifdef HAVE_XML_SETSKIPPEDENTITYHANDLER
    XML_SetSkippedEntityHandler(parser->parser, NULL);
#endif
    if (rootparser->tainted)
      parser->tainted |= 1;
    parser->context = context;
  }
  if (!parser->parser)
    rb_raise(eXMLParserError, "cannot create parser");

  /* setting up internal data */
  XML_SetUserData(parser->parser, (void*)obj);
  parser->iterator = 0;
  parser->defaultCurrent = 0;
#ifdef NEW_EXPAT
  parser->lastAttrs = NULL;
#endif
  parser->parent = parent;
  parser->detectedEncoding = NULL;

  rb_obj_call_init(obj, argc, argv);

  return obj;
}

Instance Method Details

#byteCountObject



1987
1988
1989
1990
1991
1992
1993
1994
# File 'ext/xmlparser.c', line 1987

static VALUE
XMLParser_getCurrentByteCount(VALUE obj)
{
  XMLParser* parser;

  GET_PARSER(obj, parser);
  return INT2FIX(XML_GetCurrentByteCount(parser->parser));
}

#byteIndexObject

byte index method



1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
# File 'ext/xmlparser.c', line 1895

static VALUE
XMLParser_getCurrentByteIndex(VALUE obj)
{
  XMLParser* parser;
  long pos;

  GET_PARSER(obj, parser);
  pos = XML_GetCurrentByteIndex(parser->parser);

  return INT2FIX(pos);
}

#columnObject

column method



1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
# File 'ext/xmlparser.c', line 1882

static VALUE
XMLParser_getCurrentColumnNumber(VALUE obj)
{
  XMLParser* parser;
  int column;

  GET_PARSER(obj, parser);
  column = XML_GetCurrentColumnNumber(parser->parser);

  return INT2FIX(column);
}

#defaultCurrentObject

defaultCurrent method



1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
# File 'ext/xmlparser.c', line 1853

static VALUE
XMLParser_defaultCurrent(VALUE obj)
{
  XMLParser* parser;

  GET_PARSER(obj, parser);
  if (!(parser->iterator)) {
    XML_DefaultCurrent(parser->parser);
  }
  else {
    parser->defaultCurrent = 1;
  }
  return Qnil;
}

#doneObject

done method



1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
# File 'ext/xmlparser.c', line 1839

static VALUE
XMLParser_done(VALUE obj)
{
  XMLParser* parser;

  GET_PARSER(obj, parser);
  if (parser->parser) {
    XML_ParserFree(parser->parser);
    parser->parser = NULL;
  }
  return Qnil;
}

#getBaseObject

get URI base



1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
# File 'ext/xmlparser.c', line 1924

static VALUE
XMLParser_getBase(VALUE obj)
{
  XMLParser* parser;
  const XML_Char* ret;

  GET_PARSER(obj, parser);
  ret = XML_GetBase(parser->parser);
  if (!ret)
    return Qnil;

  return TO_(ENC_(rb_str_new2((char*)ret)));
}

#getIdAttributeObject



2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
# File 'ext/xmlparser.c', line 2076

static VALUE
XMLParser_getIdAttrribute(VALUE obj)
{
  XMLParser* parser;
  int idattr;
  const XML_Char** atts;

  GET_PARSER(obj, parser);
  atts = parser->lastAttrs;
  if (!atts)
    return Qnil;
  idattr = XML_GetIdAttributeIndex(parser->parser);
  if (idattr < 0)
    return Qnil;
  return TO_(ENC_(rb_str_new2((char*)atts[idattr])));
}

#getInputContextObject



2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
# File 'ext/xmlparser.c', line 2053

static VALUE
XMLParser_getInputContext(VALUE obj)
{
  XMLParser* parser;
  const char* buffer;
  int offset;
  int size;
  VALUE ret = Qnil;

  GET_PARSER(obj, parser);
  buffer = XML_GetInputContext(parser->parser,
			       &offset,
			       &size);
  if (buffer && size > 0) {
    ret = rb_ary_new3(2,
		      TO_(ENC_(rb_str_new(buffer, size))),
		      INT2FIX(offset));
  }

  return ret;
}

#getSpecifiedAttributesObject



1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
# File 'ext/xmlparser.c', line 1964

static VALUE
XMLParser_getSpecifiedAttributes(VALUE obj)
{
  XMLParser* parser;
  int i, count;
  const XML_Char** atts;
  VALUE attrarray;

  GET_PARSER(obj, parser);
  atts = parser->lastAttrs;
  if (!atts)
    return Qnil;
  count = XML_GetSpecifiedAttributeCount(parser->parser)/2;
  attrarray = rb_ary_new2(count);
  for (i = 0; i < count; i++, atts+=2) {
    const char* key = *atts;
    rb_ary_push(attrarray, TO_(ENC_(rb_str_new2((char*)key))));
  }

  return attrarray;
}

#lineObject

line method



1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
# File 'ext/xmlparser.c', line 1869

static VALUE
XMLParser_getCurrentLineNumber(VALUE obj)
{
  XMLParser* parser;
  int line;

  GET_PARSER(obj, parser);
  line = XML_GetCurrentLineNumber(parser->parser);

  return INT2FIX(line);
}

#parse(*args) ⇒ Object

parse method



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
# File 'ext/xmlparser.c', line 1723

static VALUE
XMLParser_parse(int argc, VALUE* argv, VALUE obj)
{
  XMLParser* parser;
  int ret;
  VALUE str;
  VALUE isFinal;
  int final = 1;
  int count;
  int fromStream = 0;
  ID mid = rb_intern("gets");
  ID linebuf = rb_intern("_linebuf");

  count = rb_scan_args(argc, argv, "02", &str, &isFinal);
  /* If "str" has public "gets" method, it will be considered *stream* */
  if (!rb_obj_is_kind_of(str, rb_cString) &&
      rb_method_boundp(CLASS_OF(str), mid, 1)) {
    fromStream = 1;
  }
  else if (!NIL_P(str)) {
    Check_Type(str, T_STRING);
  }
  if (count >= 2) {
    if (isFinal == Qtrue)
      final = 1;
    else if (isFinal == Qfalse)
      final = 0;
    else
      rb_raise(rb_eTypeError, "not valid value");
  }

  GET_PARSER(obj, parser);

  parser->iterator = rb_block_given_p();

  /* Setup event handlers */
  setup_evnet_handlers(parser, obj);

  /* Parse from stream (probably slightly slow) */
  if (fromStream) {
    VALUE buf;

    if (OBJ_TAINTED(str))
      taintParser(parser);
    do {
      buf = rb_funcall(str, mid, 0);
      if (!NIL_P(buf)) {
	Check_Type(buf, T_STRING);
	if (OBJ_TAINTED(buf))
	  taintParser(parser);
	rb_ivar_set(obj, linebuf, buf); /* protect buf from GC (reasonable?)*/
	ret = XML_Parse(parser->parser,
			RSTRING_PTR(buf), RSTRING_LEN(buf), 0);
      }
      else {
	ret = XML_Parse(parser->parser, NULL, 0, 1);
      }
      if (!ret) {
	int err = XML_GetErrorCode(parser->parser);
	const char* errStr = XML_ErrorString(err);
	rb_raise(eXMLParserError, (char*)errStr);
      }
    } while (!NIL_P(buf));
    return Qnil;
  }

  /* Parse string */
  if (!NIL_P(str)) {
#if defined(HAVE_RUBY_ENCODING_H) && defined(HAVE_XML_PARSERRESET)
    int err;
#endif
    if (OBJ_TAINTED(str))
      taintParser(parser);
    ret = XML_Parse(parser->parser,
		    RSTRING_PTR(str), RSTRING_LEN(str), final);
#if defined(HAVE_RUBY_ENCODING_H) && defined(HAVE_XML_PARSERRESET)
    /* Ruby 1.9.1 Encoding conversion */
    err = XML_GetErrorCode(parser->parser);
    if (final && err == XML_ERROR_UNKNOWN_ENCODING) {
      rb_encoding* enc;
      volatile VALUE encobj;
      volatile VALUE ustr;
      enc = rb_enc_find(parser->detectedEncoding);
      if ((int)ENC_TO_ENCINDEX(enc) != rb_ascii8bit_encindex()) {
        rb_enc_associate(str, enc);
        encobj = rb_enc_from_encoding(enc_xml);
        /* rb_str_encode may raises an exception */
        ustr = rb_str_encode(str, encobj, 0, Qnil);
        if (!NIL_P(ustr)) {
          XML_ParserReset(parser->parser, "utf-8");
          XML_SetUserData(parser->parser, (void*)obj);
          parser->defaultCurrent = 0;
#ifdef NEW_EXPAT
          parser->lastAttrs = NULL;
#endif
          parser->detectedEncoding = NULL;
          setup_evnet_handlers(parser, obj);
          ret = XML_Parse(parser->parser,
                          RSTRING_PTR(ustr), RSTRING_LEN(ustr), final);
        }
      }
    }
#endif
  }
  else
    ret = XML_Parse(parser->parser, NULL, 0, final);
  if (!ret) {
    int err = XML_GetErrorCode(parser->parser);
    const char* errStr = XML_ErrorString(err);
    rb_raise(eXMLParserError, (char*)errStr);
  }

  return Qnil;
}

#reset(*args) ⇒ Object



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
# File 'ext/xmlparser.c', line 1506

static VALUE
XMLParser_reset(int argc, VALUE* argv, VALUE obj)
{
  XMLParser* parser;
  VALUE vencoding = Qnil;
  char* encoding = NULL;
  int count;

  count = rb_scan_args(argc, argv, "01", &vencoding);

  GET_PARSER(obj, parser);
  if (count > 0 && TYPE(vencoding) != T_NIL) {
    Check_Type(vencoding, T_STRING);
    encoding = RSTRING_PTR(vencoding);
  }
  XML_ParserReset(parser->parser, encoding);
  /* setting up internal data */
  XML_SetUserData(parser->parser, (void*)obj);
  parser->iterator = 0;
  parser->defaultCurrent = 0;
#ifdef NEW_EXPAT
  parser->lastAttrs = NULL;
#endif
  parser->tainted = 0;
  parser->detectedEncoding = NULL;

  return obj;
}

#setBase(base) ⇒ Object

set URI base



1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
# File 'ext/xmlparser.c', line 1908

static VALUE
XMLParser_setBase(VALUE obj, VALUE base)
{
  XMLParser* parser;
  int ret;

  Check_Type(base, T_STRING);
  GET_PARSER(obj, parser);
  if (OBJ_TAINTED(base))
    taintParser(parser);
  ret = XML_SetBase(parser->parser, RSTRING_PTR(base));

  return INT2FIX(ret);
}

#setParamEntityParsing(parsing) ⇒ Object



1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
# File 'ext/xmlparser.c', line 1998

static VALUE
XMLParser_setParamEntityParsing(VALUE obj, VALUE parsing)
{
  XMLParser* parser;
  int ret;

  Check_Type(parsing, T_FIXNUM);
  GET_PARSER(obj, parser);
  ret = XML_SetParamEntityParsing(parser->parser, FIX2INT(parsing));

  return INT2FIX(ret);
}

#setReturnNSTriplet(do_nst) ⇒ Object



2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
# File 'ext/xmlparser.c', line 2027

static VALUE
XMLParser_setReturnNSTriplet(VALUE obj, VALUE do_nst)
{
  XMLParser* parser;
  int nst;

  GET_PARSER(obj, parser);
  switch (TYPE(do_nst)) {
  case T_TRUE:
    nst = 1;
    break;
  case T_FALSE:
    nst = 0;
    break;
  case T_FIXNUM:
    nst = FIX2INT(do_nst);
    break;
  default:
    rb_raise(rb_eTypeError, "not valid value");
  }
  XML_SetReturnNSTriplet(parser->parser, nst);

  return Qnil;
}

#useForeignDTD(useDTD) ⇒ Object



2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
# File 'ext/xmlparser.c', line 2095

static VALUE
XMLParser_useForeignDTD(VALUE obj, VALUE useDTD)
{
  XMLParser* parser;
  int dtd;
  int ret;

  GET_PARSER(obj, parser);
  switch (TYPE(useDTD)) {
  case T_TRUE:
    dtd = 1;
    break;
  case T_FALSE:
    dtd = 0;
    break;
  case T_FIXNUM:
    dtd = FIX2INT(useDTD);
    break;
  default:
    rb_raise(rb_eTypeError, "not valid value");
  }
  ret = XML_UseForeignDTD(parser->parser, dtd);

  return INT2FIX(ret);
}