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