Splitter Test written. Doesn't pass.
[order_line_extra.git] / hincludes / lib / Xml.class.php
1 <?php
2
3 class Xml {
4         public function __construct() {
5                 ;
6                 ;
7         }
8         public $nodeType;
9         //;
10         //;
11         public $parent;
12         public $_nodeName;
13         public $_nodeValue;
14         public $_attributes;
15         public $_children;
16         public $_parent;
17         public function getNodeName() {
18                 if($this->nodeType != Xml::$Element) {
19                         throw new HException("bad nodeType");
20                 }
21                 return $this->_nodeName;
22         }
23         public function setNodeName($n) {
24                 if($this->nodeType != Xml::$Element) {
25                         throw new HException("bad nodeType");
26                 }
27                 return $this->_nodeName = $n;
28         }
29         public function getNodeValue() {
30                 if($this->nodeType == Xml::$Element || $this->nodeType == Xml::$Document) {
31                         throw new HException("bad nodeType");
32                 }
33                 return $this->_nodeValue;
34         }
35         public function setNodeValue($v) {
36                 if($this->nodeType == Xml::$Element || $this->nodeType == Xml::$Document) {
37                         throw new HException("bad nodeType");
38                 }
39                 return $this->_nodeValue = $v;
40         }
41         public function getParent() {
42                 return $this->_parent;
43         }
44         public function get($att) {
45                 if($this->nodeType != Xml::$Element) {
46                         throw new HException("bad nodeType");
47                 }
48                 return $this->_attributes->get($att);
49         }
50         public function set($att, $value) {
51                 if($this->nodeType != Xml::$Element) {
52                         throw new HException("bad nodeType");
53                 }
54                 $this->_attributes->set($att, htmlspecialchars($value, ENT_COMPAT, "UTF-8"));
55         }
56         public function remove($att) {
57                 if($this->nodeType != Xml::$Element) {
58                         throw new HException("bad nodeType");
59                 }
60                 $this->_attributes->remove($att);
61         }
62         public function exists($att) {
63                 if($this->nodeType != Xml::$Element) {
64                         throw new HException("bad nodeType");
65                 }
66                 return $this->_attributes->exists($att);
67         }
68         public function attributes() {
69                 if($this->nodeType != Xml::$Element) {
70                         throw new HException("bad nodeType");
71                 }
72                 return $this->_attributes->keys();
73         }
74         public function iterator() {
75                 if($this->_children === null) {
76                         throw new HException("bad nodetype");
77                 }
78                 $me = $this;
79                 $it = null;
80                 $it = _hx_anonymous(array("cur" => 0, "x" => $me->_children, "hasNext" => array(new _hx_lambda(array("it" => &$it, "me" => &$me), null, array(), "{
81                         return \$it->cur < _hx_len(\$it->x);
82                 }"), 'execute0'), "next" => array(new _hx_lambda(array("it" => &$it, "me" => &$me), null, array(), "{
83                         return \$it->x[\$it->cur++];
84                 }"), 'execute0')));
85                 return $it;
86         }
87         public function elements() {
88                 if($this->_children === null) {
89                         throw new HException("bad nodetype");
90                 }
91                 $me = $this;
92                 $it = null;
93                 $it = _hx_anonymous(array("cur" => 0, "x" => $me->_children, "hasNext" => array(new _hx_lambda(array("it" => &$it, "me" => &$me), null, array(), "{
94                         \$k = \$it->cur;
95                         \$l = _hx_len(\$it->x);
96                         while(\$k < \$l) {
97                                 if(_hx_array_get(\$it->x, \$k)->nodeType == Xml::\$Element) {
98                                         break;
99                                 }
100                                 \$k += 1;
101                                 ;
102                         }
103                         \$it->cur = \$k;
104                         return \$k < \$l;
105                 }"), 'execute0'), "next" => array(new _hx_lambda(array("it" => &$it, "me" => &$me), null, array(), "{
106                         \$k = \$it->cur;
107                         \$l = _hx_len(\$it->x);
108                         while(\$k < \$l) {
109                                 \$n = \$it->x[\$k];
110                                 \$k += 1;
111                                 if(\$n->nodeType == Xml::\$Element) {
112                                         \$it->cur = \$k;
113                                         return \$n;
114                                 }
115                                 unset(\$n);
116                         }
117                         return null;
118                 }"), 'execute0')));
119                 return $it;
120         }
121         public function elementsNamed($name) {
122                 if($this->_children === null) {
123                         throw new HException("bad nodetype");
124                 }
125                 $me = $this;
126                 $it = null;
127                 $it = _hx_anonymous(array("cur" => 0, "x" => $me->_children, "hasNext" => array(new _hx_lambda(array("it" => &$it, "me" => &$me, "name" => &$name), null, array(), "{
128                         \$k = \$it->cur;
129                         \$l = _hx_len(\$it->x);
130                         while(\$k < \$l) {
131                                 \$n = \$it->x[\$k];
132                                 if(\$n->nodeType == Xml::\$Element && \$n->_nodeName == \$name) {
133                                         break;
134                                 }
135                                 \$k++;
136                                 unset(\$n);
137                         }
138                         \$it->cur = \$k;
139                         return \$k < \$l;
140                 }"), 'execute0'), "next" => array(new _hx_lambda(array("it" => &$it, "me" => &$me, "name" => &$name), null, array(), "{
141                         \$k = \$it->cur;
142                         \$l = _hx_len(\$it->x);
143                         while(\$k < \$l) {
144                                 \$n = \$it->x[\$k];
145                                 \$k++;
146                                 if(\$n->nodeType == Xml::\$Element && \$n->_nodeName == \$name) {
147                                         \$it->cur = \$k;
148                                         return \$n;
149                                 }
150                                 unset(\$n);
151                         }
152                         return null;
153                 }"), 'execute0')));
154                 return $it;
155         }
156         public function firstChild() {
157                 if($this->_children === null) {
158                         throw new HException("bad nodetype");
159                 }
160                 if($this->_children->length === 0) {
161                         return null;
162                 }
163                 return $this->_children[0];
164         }
165         public function firstElement() {
166                 if($this->_children === null) {
167                         throw new HException("bad nodetype");
168                 }
169                 $cur = 0;
170                 $l = $this->_children->length;
171                 while($cur < $l) {
172                         $n = $this->_children[$cur];
173                         if($n->nodeType == Xml::$Element) {
174                                 return $n;
175                         }
176                         $cur++;
177                         unset($n);
178                 }
179                 return null;
180         }
181         public function addChild($x) {
182                 if($this->_children === null) {
183                         throw new HException("bad nodetype");
184                 }
185                 if($x->_parent !== null) {
186                         $x->_parent->_children->remove($x);
187                 }
188                 $x->_parent = $this;
189                 $this->_children->push($x);
190         }
191         public function removeChild($x) {
192                 if($this->_children === null) {
193                         throw new HException("bad nodetype");
194                 }
195                 $b = $this->_children->remove($x);
196                 if($b) {
197                         $x->_parent = null;
198                 }
199                 return $b;
200         }
201         public function insertChild($x, $pos) {
202                 if($this->_children === null) {
203                         throw new HException("bad nodetype");
204                 }
205                 if($x->_parent !== null) {
206                         $x->_parent->_children->remove($x);
207                 }
208                 $x->_parent = $this;
209                 $this->_children->insert($pos, $x);
210         }
211         public function toString() {
212                 if($this->nodeType == Xml::$PCData) {
213                         return $this->_nodeValue;
214                 }
215                 if($this->nodeType == Xml::$CData) {
216                         return ("<![CDATA[" . $this->_nodeValue) . "]]>";
217                 }
218                 if($this->nodeType == Xml::$Comment || $this->nodeType == Xml::$DocType || $this->nodeType == Xml::$Prolog) {
219                         return $this->_nodeValue;
220                 }
221                 $s = "";
222                 if($this->nodeType == Xml::$Element) {
223                         $s .= "<";
224                         $s .= $this->_nodeName;
225                         $»it = $this->_attributes->keys();
226                         while($»it->hasNext()) {
227                         $k = $»it->next();
228                         {
229                                 $s .= " ";
230                                 $s .= $k;
231                                 $s .= "=\"";
232                                 $s .= $this->_attributes->get($k);
233                                 $s .= "\"";
234                                 ;
235                         }
236                         }
237                         if($this->_children->length === 0) {
238                                 $s .= "/>";
239                                 return $s;
240                         }
241                         $s .= ">";
242                 }
243                 $»it2 = $this->iterator();
244                 while($»it2->hasNext()) {
245                 $x = $»it2->next();
246                 $s .= $x->toString();
247                 }
248                 if($this->nodeType == Xml::$Element) {
249                         $s .= "</";
250                         $s .= $this->_nodeName;
251                         $s .= ">";
252                 }
253                 return $s;
254         }
255         public function __call($m, $a) {
256                 if(isset($this->$m) && is_callable($this->$m))
257                         return call_user_func_array($this->$m, $a);
258                 else if(isset($this->»dynamics[$m]) && is_callable($this->»dynamics[$m]))
259                         return call_user_func_array($this->»dynamics[$m], $a);
260                 else if('toString' == $m)
261                         return $this->__toString();
262                 else
263                         throw new HException('Unable to call «'.$m.'»');
264         }
265         static $Element;
266         static $PCData;
267         static $CData;
268         static $Comment;
269         static $DocType;
270         static $Prolog;
271         static $Document;
272         static $build;
273         static function __start_element_handler($parser, $name, $attribs) {
274                 $node = Xml::createElement($name);
275                 while(list($k, $v) = each($attribs)) $node->set($k, $v);
276                 Xml::$build->addChild($node);
277                 Xml::$build = $node;
278         }
279         static function __end_element_handler($parser, $name) {
280                 Xml::$build = Xml::$build->getParent();
281         }
282         static function __character_data_handler($parser, $data) {
283                 if((strlen($data) === 1 && htmlentities($data) != $data) || htmlentities($data) == $data) {
284                         Xml::$build->addChild(Xml::createPCData(htmlentities($data)));
285                 }
286                 else {
287                         Xml::$build->addChild(Xml::createCData($data));
288                 }
289         }
290         static function __default_handler($parser, $data) {
291                 Xml::$build->addChild(Xml::createPCData($data));
292         }
293         static $xmlChecker;
294         static function parse($str) {
295                 Xml::$build = Xml::createDocument();
296                 $xml_parser = xml_parser_create();
297                 xml_set_element_handler($xml_parser, (isset(Xml::$__start_element_handler) ? Xml::$__start_element_handler: array("Xml", "__start_element_handler")), (isset(Xml::$__end_element_handler) ? Xml::$__end_element_handler: array("Xml", "__end_element_handler")));
298                 xml_set_character_data_handler($xml_parser, (isset(Xml::$__character_data_handler) ? Xml::$__character_data_handler: array("Xml", "__character_data_handler")));
299                 xml_set_default_handler($xml_parser, (isset(Xml::$__default_handler) ? Xml::$__default_handler: array("Xml", "__default_handler")));
300                 xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
301                 xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 0);
302                 $isComplete = Xml::$xmlChecker->match($str);
303                 if(!$isComplete) {
304                         $str = ("<doc>" . $str) . "</doc>";
305                 }
306                 if(1 !== xml_parse($xml_parser, $str, true)) {
307                         throw new HException("Xml parse error (" . ((xml_error_string(xml_get_error_code($xml_parser)) . ") line #") . xml_get_current_line_number($xml_parser)));
308                 }
309                 xml_parser_free($xml_parser);
310                 if($isComplete) {
311                         return Xml::$build;
312                 }
313                 else {
314                         Xml::$build = Xml::$build->_children[0];
315                         Xml::$build->_parent = null;
316                         Xml::$build->_nodeName = null;
317                         Xml::$build->nodeType = Xml::$Document;
318                         return Xml::$build;
319                 }
320         }
321         static function createElement($name) {
322                 $r = new Xml();
323                 $r->nodeType = Xml::$Element;
324                 $r->_children = new _hx_array(array());
325                 $r->_attributes = new Hash();
326                 $r->setNodeName($name);
327                 return $r;
328         }
329         static function createPCData($data) {
330                 $r = new Xml();
331                 $r->nodeType = Xml::$PCData;
332                 $r->setNodeValue($data);
333                 return $r;
334         }
335         static function createCData($data) {
336                 $r = new Xml();
337                 $r->nodeType = Xml::$CData;
338                 $r->setNodeValue($data);
339                 return $r;
340         }
341         static function createComment($data) {
342                 $r = new Xml();
343                 $r->nodeType = Xml::$Comment;
344                 $r->setNodeValue($data);
345                 return $r;
346         }
347         static function createDocType($data) {
348                 $r = new Xml();
349                 $r->nodeType = Xml::$DocType;
350                 $r->setNodeValue($data);
351                 return $r;
352         }
353         static function createProlog($data) {
354                 $r = new Xml();
355                 $r->nodeType = Xml::$Prolog;
356                 $r->setNodeValue($data);
357                 return $r;
358         }
359         static function createDocument() {
360                 $r = new Xml();
361                 $r->nodeType = Xml::$Document;
362                 $r->_children = new _hx_array(array());
363                 return $r;
364         }
365         function __toString() { return $this->toString(); }
366 }
367 {
368         Xml::$Element = "element";
369         Xml::$PCData = "pcdata";
370         Xml::$CData = "cdata";
371         Xml::$Comment = "comment";
372         Xml::$DocType = "doctype";
373         Xml::$Prolog = "prolog";
374         Xml::$Document = "document";
375 }
376 Xml::$xmlChecker = new EReg("\\s*(<\\?xml|<!DOCTYPE)", "mi");