PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/arc/parsers/ARC2_JSONParser.php

https://github.com/damz/foafssl-drupal
PHP | 151 lines | 116 code | 19 blank | 16 comment | 24 complexity | 1b06bd4c0c9d1f90b116f7d00f1b39cb MD5 | raw file
  1. <?php
  2. /*
  3. homepage: http://arc.semsol.org/
  4. license: http://arc.semsol.org/license
  5. class: ARC2 JSON Parser
  6. author: Benjamin Nowack
  7. version: 2009-02-12 Tweak: "null" is now supported by extractValue
  8. */
  9. ARC2::inc('RDFParser');
  10. class ARC2_JSONParser extends ARC2_RDFParser {
  11. function __construct($a = '', &$caller) {
  12. parent::__construct($a, $caller);
  13. }
  14. function ARC2_JSONParser($a = '', &$caller) {
  15. $this->__construct($a, $caller);
  16. }
  17. function __init() {
  18. parent::__init();
  19. }
  20. /* */
  21. function x($re, $v, $options = 'si') {
  22. while (preg_match('/^\s*(\/\*.*\*\/)(.*)$/Usi', $v, $m)) {/* comment removal */
  23. $v = $m[2];
  24. }
  25. $this->unparsed_code = (strlen($this->unparsed_code) > strlen($v)) ? $v : $this->unparsed_code;
  26. return ARC2::x($re, $v, $options);
  27. }
  28. function parse($path, $data = '') {
  29. $this->state = 0;
  30. /* reader */
  31. if (!$this->v('reader')) {
  32. ARC2::inc('Reader');
  33. $this->reader = & new ARC2_Reader($this->a, $this);
  34. }
  35. $this->reader->setAcceptHeader('Accept: application/json; q=0.9, */*; q=0.1');
  36. $this->reader->activate($path, $data);
  37. $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base;
  38. /* parse */
  39. $doc = '';
  40. while ($d = $this->reader->readStream()) {
  41. $doc .= $d;
  42. }
  43. $this->reader->closeStream();
  44. unset($this->reader);
  45. $doc = preg_replace('/^[^\{]*(.*\})[^\}]*$/is', '\\1', $doc);
  46. $this->unparsed_code = $doc;
  47. list($this->struct, $rest) = $this->extractObject($doc);
  48. return $this->done();
  49. }
  50. /* */
  51. function extractObject($v) {
  52. if (function_exists('json_decode')) return array(json_decode($v, 1), '');
  53. $r = array();
  54. if ($sub_r = $this->x('\{', $v)) {
  55. $v = $sub_r[1];
  56. while ((list($sub_r, $v) = $this->extractEntry($v)) && $sub_r) {
  57. $r[$sub_r['key']] = $sub_r['value'];
  58. }
  59. if ($sub_r = $this->x('\}', $v)) $v = $sub_r[1];
  60. }
  61. elseif ($sub_r = $this->x('\[', $v)) {
  62. $v = $sub_r[1];
  63. while ((list($sub_r, $v) = $this->extractValue($v)) && $sub_r) {
  64. $r[] = $sub_r;
  65. }
  66. if ($sub_r = $this->x('\]', $v)) $v = $sub_r[1];
  67. }
  68. elseif ((list($sub_r, $v) = $this->extractValue($v)) && $sub_r) {
  69. $r = $sub_r;
  70. }
  71. return array($r, $v);
  72. }
  73. function extractEntry($v) {
  74. if ($r = $this->x('\,', $v)) $v = $r[1];
  75. /* k */
  76. if ($r = $this->x('\"([^\"]+)\"\s*\:', $v)) {
  77. $k = $r[1];
  78. $sub_v = $r[2];
  79. if (list($sub_r, $sub_v) = $this->extractObject($sub_v)) {
  80. return array(
  81. array('key' => $k, 'value' => $sub_r),
  82. $sub_v
  83. );
  84. }
  85. }
  86. return array(0, $v);
  87. }
  88. function extractValue($v) {
  89. if ($r = $this->x('\,', $v)) $v = $r[1];
  90. if ($sub_r = $this->x('null', $v)) {
  91. return array(null, $sub_r[1]);
  92. }
  93. if ($sub_r = $this->x('([0-9\.]+)', $v)) {
  94. return array($sub_r[1], $sub_r[2]);
  95. }
  96. if ($sub_r = $this->x('\"', $v)) {
  97. $rest = $sub_r[1];
  98. if (preg_match('/^([^\x5c]*|.*[^\x5c]|.*\x5c{2})\"(.*)$/sU', $rest, $m)) {
  99. return array($m[1], $m[2]);
  100. }
  101. }
  102. return array(0, $v);
  103. }
  104. /* */
  105. function getObject() {
  106. return $this->v('struct', array());
  107. }
  108. function getTriples() {
  109. return $this->v('triples', array());
  110. }
  111. function countTriples() {
  112. return $this->t_count;
  113. }
  114. function addT($s = '', $p = '', $o = '', $s_type = '', $o_type = '', $o_dt = '', $o_lang = '') {
  115. //echo str_replace($this->base, '', "-----\n adding $s / $p / $o\n-----\n");
  116. $t = array('s' => $s, 'p' => $p, 'o' => $o, 's_type' => $s_type, 'o_type' => $o_type, 'o_datatype' => $o_dt, 'o_lang' => $o_lang);
  117. if ($this->skip_dupes) {
  118. $h = md5(serialize($t));
  119. if (!isset($this->added_triples[$h])) {
  120. $this->triples[$this->t_count] = $t;
  121. $this->t_count++;
  122. $this->added_triples[$h] = true;
  123. }
  124. }
  125. else {
  126. $this->triples[$this->t_count] = $t;
  127. $this->t_count++;
  128. }
  129. }
  130. /* */
  131. }