comparison scripts/score_parser.php @ 0:d2eb0e6ccaaf

initial commit
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 20 Apr 2016 16:26:57 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d2eb0e6ccaaf
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['id'];
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 = $element['id'];
77 $element_nest = $page_nest->findChild($elementId);
78 if ($element_nest == null) {
79 $element_nest = $page_nest->addNewChild($elementId);
80 }
81 // Now get the <value> tags
82 foreach($element->value as $value) {
83 $axis_nest = null;
84 $axisName = "default";
85 if (isset($value['interface-name']))
86 {
87 // Find the axis nest
88 $axisName = $value['interface-name'];
89 }
90
91 $axis_nest = $element_nest->findChild($axisName);
92 if ($axis_nest == null) {
93 $axis_nest = $element_nest->addNewChild($axisName);
94 }
95 // Now push our value
96 $axis_nest->addValue($value);
97 }
98 }
99 }
100 }
101 }
102 // We now have a structure in $root. EXPORT IT
103 switch($_GET['format']) {
104 case "XML":
105 // Convert to XML
106 $doc_root = new SimpleXMLElement('<waetprocess/>');
107 foreach($root->nest as $page) {
108 $doc_page = $doc_root->addChild("page");
109 $doc_page->addAttribute("id",$page->id);
110 foreach($page->nest as $element) {
111 $doc_element = $doc_page->addChild("audioelement");
112 $doc_element->addAttribute("id",$element->id);
113 foreach($element->nest as $axis) {
114 $doc_axis = $doc_element->addChild("interface");
115 $doc_axis->addAttribute("name",$axis->id);
116 foreach($axis->nest as $value) {
117 $doc_axis->addChild("value",$value);
118 }
119 }
120 }
121 }
122 echo $doc_root->asXML();
123 break;
124 case "JSON":
125 // Convert to JSON
126 $doc_root = '{ "pages": [';
127 for ($pageIndex = 0; $pageIndex < $root->num; $pageIndex++)
128 {
129 $page = $root->nest[$pageIndex];
130 $doc_page = '{ "id": "'.$page->id.'", "elements": [';
131 for($elementIndex = 0; $elementIndex < $page->num; $elementIndex++)
132 {
133 $element = $page->nest[$elementIndex];
134 $doc_element = '{ "id": "'.$element->id.'", "axis": [';
135 for($axisIndex = 0; $axisIndex < $element->num; $axisIndex++)
136 {
137 $axis = $element->nest[$axisIndex];
138 $doc_axis = '{ "name": "'.$axis->id.'", "values": [';
139 for ($valueIndex = 0; $valueIndex < $axis->num; $valueIndex++)
140 {
141 $doc_axis = $doc_axis."".strval($axis->nest[$valueIndex]);
142 if ($valueIndex < $axis->num-1) {
143 $doc_axis = $doc_axis.', ';
144 }
145 }
146 $doc_axis = $doc_axis.']}';
147 if ($axisIndex < $element->num-1) {
148 $doc_axis = $doc_axis.', ';
149 }
150 $doc_element = $doc_element.$doc_axis;
151 }
152 $doc_element = $doc_element.']}';
153 if ($elementIndex < $page->num-1) {
154 $doc_element = $doc_element.', ';
155 }
156 $doc_page = $doc_page.$doc_element;
157 }
158 $doc_page = $doc_page.']}';
159 if ($pageIndex < $root->num-1) {
160 $doc_page = $doc_page.', ';
161 }
162 $doc_root = $doc_root.$doc_page;
163 }
164 $doc_root = $doc_root.']}';
165 echo $doc_root;
166 break;
167 case "CSV":
168 // Convert to CSV
169 // CSV Columts: page, axis, element, value
170 $doc_string = "page,element,axis,value"."\r\n";
171 foreach($root->nest as $page){
172 foreach($page->nest as $element) {
173 foreach($element->nest as $axis) {
174 foreach($axis->nest as $value) {
175 $doc_string = $doc_string.$page->id.',';
176 $doc_string = $doc_string.$element->id.',';
177 $doc_string = $doc_string.$axis->id.',';
178 $doc_string = $doc_string.$value;
179 $doc_string = $doc_string."\r\n";
180 }
181 }
182 }
183 }
184 echo $doc_string;
185 }
186 } else {
187 echo "FATAL - No saved XML files discovered";
188 }
189
190 ?>