comparison php/score_parser.php @ 2224:760719986df3

Tidy up file locations.
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Thu, 14 Apr 2016 13:54:24 +0100
parents
children d26623bd65e0
comparison
equal deleted inserted replaced
2222:4d1aa94202e3 2224:760719986df3
1 <?php
2 // Value parser for WAET XML
3 // testPage --> axis --> element --> value
4 class nestedObject {
5 function __construct($id) {
6 $this->id = $id;
7 $this->nest = array();
8 $this->type = null;
9 $this->num = 0;
10 }
11 function addNewChild($id) {
12 if ($this->type == null) {
13 $this->type = "nest";
14 }
15 if ($this->type == "nest") {
16 $obj = new nestedObject($id);
17 array_push($this->nest,$obj);
18 $this->num = count($this->nest);
19 return $this->nest[$this->num-1];
20 }
21 return null;
22 }
23 function findChild($checkId) {
24 if ($this->type == "nest"){
25 foreach($this->nest as $child)
26 {
27 if (strcmp($checkId,$child->id) == 0) {
28 return $child;
29 }
30 }
31 }
32 return null;
33 }
34 function addValue($val) {
35 if ($this->type == null) {
36 $this->type = "value";
37 }
38 if ($this->type == "value") {
39 array_push($this->nest,$val);
40 $this->num = count($this->nest);
41 return $this->nest[$this->num-1];
42 }
43 return null;
44 }
45 }
46
47 // Build the root nest object to hold the testPages
48 $root = new nestedObject("root");
49
50 // XML Saves location - assumes it will be saves/
51 $saves = glob("../saves/*.xml");
52 if (is_array($saves))
53 {
54 foreach($saves as $filename) {
55 $xml_string = file_get_contents($filename, FILE_TEXT);
56 $xml_object = simplexml_load_string($xml_string);
57 if ($xml_object == false) {
58 echo "<h1>FATAL</h1> <span>could not parse file ".$filename.": </span>";
59 foreach(libxml_get_errors() as $error) {
60 echo "<br>", $error->message;
61 }
62 } else {
63 // Iterate over each $page node
64 foreach($xml_object->page as $pageInstance)
65 {
66 // Find in the nest
67 $pageInstanceId = $pageInstance['ref'];
68 $page_nest = $root->findChild($pageInstanceId);
69 if ($page_nest == null) {
70 $page_nest = $root->addNewChild($pageInstanceId);
71 }
72
73 // Iterate over each $element node
74 foreach($pageInstance->audioelement as $element) {
75 // Find our specific element tag
76 $elementId = null;
77 if (isset($element['name'])) {
78 $elementId = $element['name'];
79 } else {
80 $elementId = $element['ref'];
81 }
82 $element_nest = $page_nest->findChild($elementId);
83 if ($element_nest == null) {
84 $element_nest = $page_nest->addNewChild($elementId);
85 }
86 // Now get the <value> tags
87 foreach($element->value as $value) {
88 $axis_nest = null;
89 $axisName = "default";
90 if (isset($value['interface-name']))
91 {
92 // Find the axis nest
93 $axisName = $value['interface-name'];
94 }
95
96 $axis_nest = $element_nest->findChild($axisName);
97 if ($axis_nest == null) {
98 $axis_nest = $element_nest->addNewChild($axisName);
99 }
100 // Now push our value
101 $axis_nest->addValue($value);
102 }
103 }
104 }
105 }
106 }
107 // We now have a structure in $root. EXPORT IT
108 switch($_GET['format']) {
109 case "XML":
110 // Convert to XML
111 $doc_root = new SimpleXMLElement('<waetprocess/>');
112 foreach($root->nest as $page) {
113 $doc_page = $doc_root->addChild("page");
114 $doc_page->addAttribute("id",$page->id);
115 foreach($page->nest as $element) {
116 $doc_element = $doc_page->addChild("audioelement");
117 $doc_element->addAttribute("id",$element->id);
118 foreach($element->nest as $axis) {
119 $doc_axis = $doc_element->addChild("interface");
120 $doc_axis->addAttribute("name",$axis->id);
121 foreach($axis->nest as $value) {
122 $doc_axis->addChild("value",$value);
123 }
124 }
125 }
126 }
127 echo $doc_root->asXML();
128 break;
129 case "JSON":
130 // Convert to JSON
131 $doc_root = '{ "pages": [';
132 for ($pageIndex = 0; $pageIndex < $root->num; $pageIndex++)
133 {
134 $page = $root->nest[$pageIndex];
135 $doc_page = '{ "id": "'.$page->id.'", "elements": [';
136 for($elementIndex = 0; $elementIndex < $page->num; $elementIndex++)
137 {
138 $element = $page->nest[$elementIndex];
139 $doc_element = '{ "id": "'.$element->id.'", "axis": [';
140 for($axisIndex = 0; $axisIndex < $element->num; $axisIndex++)
141 {
142 $axis = $element->nest[$axisIndex];
143 $doc_axis = '{ "name": "'.$axis->id.'", "values": [';
144 for ($valueIndex = 0; $valueIndex < $axis->num; $valueIndex++)
145 {
146 $doc_axis = $doc_axis."".strval($axis->nest[$valueIndex]);
147 if ($valueIndex < $axis->num-1) {
148 $doc_axis = $doc_axis.', ';
149 }
150 }
151 $doc_axis = $doc_axis.']}';
152 if ($axisIndex < $element->num-1) {
153 $doc_axis = $doc_axis.', ';
154 }
155 $doc_element = $doc_element.$doc_axis;
156 }
157 $doc_element = $doc_element.']}';
158 if ($elementIndex < $page->num-1) {
159 $doc_element = $doc_element.', ';
160 }
161 $doc_page = $doc_page.$doc_element;
162 }
163 $doc_page = $doc_page.']}';
164 if ($pageIndex < $root->num-1) {
165 $doc_page = $doc_page.', ';
166 }
167 $doc_root = $doc_root.$doc_page;
168 }
169 $doc_root = $doc_root.']}';
170 echo $doc_root;
171 break;
172 case "CSV":
173 // Convert to CSV
174 // CSV Columts: page, axis, element, value
175 $doc_string = "page,element,axis,value"."\r\n";
176 foreach($root->nest as $page){
177 foreach($page->nest as $element) {
178 foreach($element->nest as $axis) {
179 foreach($axis->nest as $value) {
180 $doc_string = $doc_string.$page->id.',';
181 $doc_string = $doc_string.$element->id.',';
182 $doc_string = $doc_string.$axis->id.',';
183 $doc_string = $doc_string.$value;
184 $doc_string = $doc_string."\r\n";
185 }
186 }
187 }
188 }
189 echo $doc_string;
190 }
191 } else {
192 echo "FATAL - No saved XML files discovered";
193 }
194
195 ?>