<?php
/*
homepage: http://arc.semsol.org/
license:  http://arc.semsol.org/license

class:    ARC2 SPARQL Result XML Parser
author:   Benjamin Nowack
version:  2007-09-05
*/

ARC2::inc('LegacyXMLParser');

class ARC2_SPARQLXMLResultParser extends ARC2_LegacyXMLParser {

  function __construct($a = '', &$caller) {
    parent::__construct($a, $caller);
  }
  
  function ARC2_SPARQLXMLResultParser($a = '', &$caller) {
    $this->__construct($a, $caller);
  }

  function __init() {/* reader */
    parent::__init();
    $this->srx = 'http://www.w3.org/2005/sparql-results#';
    $this->nsp[$this->srx] = 'srx';
    $this->allowCDataNodes = 0;
  }
  
  /*  */
  
  function done() {
  }
  
  /*  */
  
  function getVariables() {
    $r = array();
    foreach ($this->nodes as $node) {
      if ($node['tag'] == $this->srx . 'variable') {
        $r[] = $node['a']['name'];
      }
    }
    return $r;
  }
  
  function getRows() {
    $r = array();
    $index = $this->getNodeIndex();
    foreach ($this->nodes as $node) {
      if ($node['tag'] == $this->srx . 'result') {
        $row = array();
        $row_id = $node['id'];
        $bindings = isset($index[$row_id])? $index[$row_id] : array();
        foreach ($bindings as $binding) {
          $row = array_merge($row, $this->getBinding($binding));
        }
        if ($row) {
          $r[] = $row;
        }
      }
    }
    return $r;
  }

  function getBinding($node) {
    $r = array();
    $index = $this->getNodeIndex();
    $var = $node['a']['name'];
    $term = $index[$node['id']][0];
    $r[$var . ' type'] = preg_replace('/^uri$/', 'iri', substr($term['tag'], strlen($this->srx)));
    $r[$var] = ($r[$var . ' type'] == 'bnode') ? '_:' . $term['cdata'] : $term['cdata'];
    if (isset($term['a']['datatype'])) {
      $r[$var . ' dt'] = $term['a']['datatype'];
    }
    elseif (isset($term['a'][$this->xml . 'lang'])) {
      $r[$var . ' lang'] = $term['a'][$this->xml . 'lang'];
    }
    return $r;
  }

  function getBoolean() {
    foreach ($this->nodes as $node) {
      if ($node['tag'] == $this->srx . 'boolean') {
        return ($node['cdata'] == 'true') ? true : false;
      }
    }
    return '';
  }

  /*  */
  
  function getStructure() {
    $r = array('variables' => $this->getVariables(), 'rows' => $this->getRows());
    $bool = $this->getBoolean();
    if ($bool !== '') {
      $r['boolean'] = $bool;
    }
    return $r;
  }

  /*  */

  
}
