source: Dev/branches/jQueryUI/server/rdfapi/syntax/SparqlResultParser.php @ 249

Last change on this file since 249 was 249, checked in by hendrikvanantwerpen, 13 years ago

This one's for Subversion, because it's so close...

First widget (stripped down sequencer).
Seperated client and server code in two direcotry trees.

File size: 3.8 KB
Line 
1<?php
2// ----------------------------------------------------------------------------------
3// Class: SparqlResultParser
4// ----------------------------------------------------------------------------------
5
6/**
7* Parses an xml document in SPARQL Result XML Format.
8*
9* @version  $Id: SparqlResultParser.php 442 2007-06-01 16:19:26Z cax $
10* @author   Tobias Gauß <tobias.gauss@web.de>
11*
12* @package sparql
13*/
14
15class SparqlResultParser extends Object {
16
17
18        var $arrOutput = array();
19        var $resParser;
20        var $strXmlData;
21        var $counter = -1;
22        var $mode = -1;
23        var $varname;
24        var $dtype;
25        var $lang;
26        var $varlist;
27        var $bncounter = 0;
28        var $current_literal;   
29       
30       
31/**
32* Main function of SparqlResultParser
33*
34* @param String  $strInputXML  input document.
35*/
36        function parse($strInputXML) {
37
38                $this->resParser = xml_parser_create ();
39
40                xml_set_object($this->resParser,$this);
41                xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
42
43                xml_set_character_data_handler($this->resParser, "tagData");
44
45                $this->strXmlData = xml_parse($this->resParser,$strInputXML );
46                if(!$this->strXmlData) {
47                        echo "unable to parse result<br>".
48                        "server returned: <br>"
49                        .$strInputXML;
50                       
51                        return "false";
52               
53                }
54
55                xml_parser_free($this->resParser);
56
57                return $this->arrOutput;
58        }
59       
60// private parser functions from here //       
61        function tagOpen($parser, $name, $attrs) {
62                //  $tag=array("name"=>$name,"attrs"=>$attrs);
63                //  array_push($this->arrOutput,$tag);
64                if(strtolower($name)=="variable"){
65                        if(isset($attrs['name']))
66                                $this->varlist[]=$attrs['name'];
67                        if(isset($attrs['NAME']))
68                                $this->varlist[]=$attrs['NAME'];
69                }
70               
71                if(strtolower($name)=="result"){
72                        $this->counter++;
73                        if($this->counter > -1){
74                                foreach($this->varlist as $value){
75                                        if(!isset($this->arrOutput[$this->counter][$value]))
76                                                $this->arrOutput[$this->counter]["?".$value]='';
77                                }       
78                        }
79                       
80                }
81                if(strtolower($name)=="boolean"){
82                        $this->counter++;
83                        $this->mode = 3;
84                }
85                if(strtolower($name)=="binding"){
86                        $this->varname = null;
87                        $this->dtype   = null;
88                        $this->lang    = null;
89                       
90                        if(isset($attrs['name'])){
91                                $this->varname = "?".$attrs['name'];
92                        }else{
93                                $this->varname = "?".$attrs['NAME'];
94                        }
95                }
96                if(strtolower($name)=="uri"){
97                        $this->mode = 0;
98                }
99                if(strtolower($name)=="literal"){
100                        $this->mode = 1;
101                        $this->current_literal = "";
102                        if(isset($attrs['datatype'])){
103                                $this->dtype = $attrs['datatype'];
104                        }else if(isset($attrs['DATATYPE'])){
105                                $this->dtype = $attrs['DATATYPE'];
106                        }
107                        if(isset($attrs['xml:lang'])){
108                                $this->lang = $attrs['datatype'];
109                        }else if(isset($attrs['XML:lang'])){
110                                $this->lang = $attrs['XML:lang'];
111                        }else if(isset($attrs['XML:LANG'])){
112                                $this->lang = $attrs['XML:LANG'];
113                        }else if(isset($attrs['xml:LANG'])){
114                                $this->lang = $attrs['xml:LANG'];
115                        }
116                }
117                if(strtolower($name)=="bnode"){
118                        $this->mode = 2;
119                }
120        }
121
122        function tagData($parser, $tagData) {
123                switch($this->mode){
124                        case 0 :
125                        $this->arrOutput[$this->counter][$this->varname] = new Resource($tagData);
126                        $this->mode = -1;
127                        break;
128                        case 1:
129                        $this->current_literal .= $tagData;
130                        break;
131                        case 2:
132                        if($tagData=="/"){
133                                $bn = "bNode".$this->bncounter;
134                                $this->bncounter++;
135                        }else{
136                                $bn = $tagData;
137                        }
138                        $this->arrOutput[$this->counter][$this->varname] = new BlankNode($bn);
139                        $this->mode = -1;
140                        break;
141                        case 3:
142                        $this->arrOutput = $tagData;
143                        $this->mode = -1;
144                        break;
145
146                }
147               
148        }
149
150        function tagClosed($parser, $name) {
151                if ($this->mode == 1) {
152                        $lit = new Literal($this->current_literal);
153                        if($this->lang)
154                                $lit->setLanguage($this->lang);
155                        if($this->dtype)
156                                $lit->setDatatype($this->dtype);
157                        $this->arrOutput[$this->counter][$this->varname] = $lit ;
158                        $this->mode = -1;
159                }               
160        }
161}
162
163
164?>
Note: See TracBrowser for help on using the repository browser.