3 homepage: http://arc.semsol.org/
4 license: http://arc.semsol.org/license
6 class: ARC2 SPARQL Result XML Parser
7 author: Benjamin Nowack
8 version: 2008-08-28 (Addition: Support for "inserted" and "deleted")
11 ARC2::inc('LegacyXMLParser');
13 class ARC2_SPARQLXMLResultParser extends ARC2_LegacyXMLParser {
15 function __construct($a = '', &$caller) {
16 parent::__construct($a, $caller);
19 function ARC2_SPARQLXMLResultParser($a = '', &$caller) {
20 $this->__construct($a, $caller);
23 function __init() {/* reader */
25 $this->srx = 'http://www.w3.org/2005/sparql-results#';
26 $this->nsp[$this->srx] = 'srx';
27 $this->allowCDataNodes = 0;
37 function getVariables() {
39 foreach ($this->nodes as $node) {
40 if ($node['tag'] == $this->srx . 'variable') {
41 $r[] = $node['a']['name'];
49 $index = $this->getNodeIndex();
50 foreach ($this->nodes as $node) {
51 if ($node['tag'] == $this->srx . 'result') {
53 $row_id = $node['id'];
54 $bindings = isset($index[$row_id])? $index[$row_id] : array();
55 foreach ($bindings as $binding) {
56 $row = array_merge($row, $this->getBinding($binding));
66 function getBinding($node) {
68 $index = $this->getNodeIndex();
69 $var = $node['a']['name'];
70 $term = $index[$node['id']][0];
71 $r[$var . ' type'] = preg_replace('/^uri$/', 'uri', substr($term['tag'], strlen($this->srx)));
72 $r[$var] = ($r[$var . ' type'] == 'bnode') ? '_:' . $term['cdata'] : $term['cdata'];
73 if (isset($term['a']['datatype'])) {
74 $r[$var . ' datatype'] = $term['a']['datatype'];
76 elseif (isset($term['a'][$this->xml . 'lang'])) {
77 $r[$var . ' lang'] = $term['a'][$this->xml . 'lang'];
82 function getBooleanInsertedDeleted() {
83 foreach ($this->nodes as $node) {
84 if ($node['tag'] == $this->srx . 'boolean') {
85 return ($node['cdata'] == 'true') ? array('boolean' => true) : array('boolean' => false);
87 elseif ($node['tag'] == $this->srx . 'inserted') {
88 return array('inserted' => $node['cdata']);
90 elseif ($node['tag'] == $this->srx . 'deleted') {
91 return array('deleted' => $node['cdata']);
93 elseif ($node['tag'] == $this->srx . 'results') {
102 function getStructure() {
103 $r = array('variables' => $this->getVariables(), 'rows' => $this->getRows());
104 /* boolean|inserted|deleted */
105 if ($sub_r = $this->getBooleanInsertedDeleted()) {
106 foreach ($sub_r as $k => $v) {