PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/good/ext/xml/tests/bug32001.php

https://github.com/tstarling/hiphop-php
PHP | 156 lines | 136 code | 19 blank | 1 comment | 14 complexity | 76d4a0afd04b4575c33b83d73c1ae76b MD5 | raw file
  1. <?php
  2. class testcase {
  3. private $encoding;
  4. private $bom;
  5. private $prologue;
  6. private $tags;
  7. private $chunk_size;
  8. function testcase($enc, $chunk_size = 0, $bom = 0, $omit_prologue = 0) {
  9. $this->encoding = $enc;
  10. $this->chunk_size = $chunk_size;
  11. $this->bom = $bom;
  12. $this->prologue = !$omit_prologue;
  13. $this->tags = array();
  14. }
  15. function start_element($parser, $name, $attrs) {
  16. $attrs = array_map('bin2hex', $attrs);
  17. $this->tags[] = bin2hex($name).": ".implode(', ', $attrs);
  18. }
  19. function end_element($parser, $name) {
  20. }
  21. function run() {
  22. $data = '';
  23. if ($this->prologue) {
  24. $canonical_name = preg_replace('/BE|LE/i', '', $this->encoding);
  25. $data .= "<?xml version=\"1.0\" encoding=\"$canonical_name\" ?>\n";
  26. }
  27. $data .= <<<HERE
  28. <テスト:テスト1 xmlns:テスト="http://www.example.com/テスト/" テスト="テスト">
  29. <テスト:テスト2 テスト="テスト">
  30. <テスト:テスト3>
  31. test!
  32. </テスト:テスト3>
  33. </テスト:テスト2>
  34. </テスト:テスト1>
  35. HERE;
  36. $data = iconv("UTF-8", $this->encoding, $data);
  37. if ($this->bom) {
  38. switch (strtoupper($this->encoding)) {
  39. case 'UTF-8':
  40. case 'UTF8':
  41. $data = "\xef\xbb\xbf".$data;
  42. break;
  43. case 'UTF-16':
  44. case 'UTF16':
  45. case 'UTF-16BE':
  46. case 'UTF16BE':
  47. case 'UCS-2':
  48. case 'UCS2':
  49. case 'UCS-2BE':
  50. case 'UCS2BE':
  51. $data = "\xfe\xff".$data;
  52. break;
  53. case 'UTF-16LE':
  54. case 'UTF16LE':
  55. case 'UCS-2LE':
  56. case 'UCS2LE':
  57. $data = "\xff\xfe".$data;
  58. break;
  59. case 'UTF-32':
  60. case 'UTF32':
  61. case 'UTF-32BE':
  62. case 'UTF32BE':
  63. case 'UCS-4':
  64. case 'UCS4':
  65. case 'UCS-4BE':
  66. case 'UCS4BE':
  67. $data = "\x00\x00\xfe\xff".$data;
  68. break;
  69. case 'UTF-32LE':
  70. case 'UTF32LE':
  71. case 'UCS-4LE':
  72. case 'UCS4LE':
  73. $data = "\xff\xfe\x00\x00".$data;
  74. break;
  75. }
  76. }
  77. $parser = xml_parser_create(NULL);
  78. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  79. xml_set_element_handler($parser, "start_element", "end_element");
  80. xml_set_object($parser, $this);
  81. if ($this->chunk_size == 0) {
  82. $success = @xml_parse($parser, $data, true);
  83. } else {
  84. for ($offset = 0; $offset < strlen($data);
  85. $offset += $this->chunk_size) {
  86. $success = @xml_parse($parser, substr($data, $offset, $this->chunk_size), false);
  87. if (!$success) {
  88. break;
  89. }
  90. }
  91. if ($success) {
  92. $success = @xml_parse($parser, "", true);
  93. }
  94. }
  95. echo "Encoding: $this->encoding\n";
  96. echo "XML Prologue: ".($this->prologue ? 'present': 'not present'), "\n";
  97. echo "Chunk size: ".($this->chunk_size ? "$this->chunk_size byte(s)\n": "all data at once\n");
  98. echo "BOM: ".($this->bom ? 'prepended': 'not prepended'), "\n";
  99. if ($success) {
  100. var_dump($this->tags);
  101. } else {
  102. echo "[Error] ", xml_error_string(xml_get_error_code($parser)), "\n";
  103. }
  104. }
  105. }
  106. $suite = array(
  107. new testcase("UTF-8", 0, 0, 0),
  108. new testcase("UTF-8", 0, 0, 1),
  109. new testcase("UTF-8", 0, 1, 0),
  110. new testcase("UTF-8", 0, 1, 1),
  111. new testcase("UTF-16BE", 0, 0, 0),
  112. new testcase("UTF-16BE", 0, 1, 0),
  113. new testcase("UTF-16BE", 0, 1, 1),
  114. new testcase("UTF-16LE", 0, 0, 0),
  115. new testcase("UTF-16LE", 0, 1, 0),
  116. new testcase("UTF-16LE", 0, 1, 1),
  117. new testcase("UTF-8", 1, 0, 0),
  118. new testcase("UTF-8", 1, 0, 1),
  119. new testcase("UTF-8", 1, 1, 0),
  120. new testcase("UTF-8", 1, 1, 1),
  121. new testcase("UTF-16BE", 1, 0, 0),
  122. new testcase("UTF-16BE", 1, 1, 0),
  123. new testcase("UTF-16BE", 1, 1, 1),
  124. new testcase("UTF-16LE", 1, 0, 0),
  125. new testcase("UTF-16LE", 1, 1, 0),
  126. new testcase("UTF-16LE", 1, 1, 1),
  127. );
  128. if (XML_SAX_IMPL == 'libxml') {
  129. echo "libxml2 Version => " . LIBXML_DOTTED_VERSION. "\n";
  130. } else {
  131. echo "libxml2 Version => NONE\n";
  132. }
  133. foreach ($suite as $testcase) {
  134. $testcase->run();
  135. }
  136. // vim600: sts=4 sw=4 ts=4 encoding=UTF-8
  137. ?>