PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/XMLParser.class.php

https://bitbucket.org/mageecprojects/portail3g
PHP | 584 lines | 500 code | 11 blank | 73 comment | 64 complexity | 6bf8a7c70be2909044dce2345cd67f70 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. if(! defined('XML_PARSER_INCLUDED'))
  3. {
  4. define('XML_PARSER_INCLUDED', 1) ;
  5. /**
  6. * Project: XMLParser: A library for parsing XML feeds
  7. * File: XMLParser.class.php
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * @link http://www.phpinsider.com/php/code/XMLParser/
  24. * @copyright 2004-2005 New Digital Group, Inc.
  25. * @author Monte Ohrt <monte at newdigitalgroup dot com>
  26. * @package XMLParser
  27. * @version 1.0-dev
  28. */
  29. class XMLParser
  30. {
  31. /**
  32. * holds the expat object
  33. *
  34. * @var obj
  35. */
  36. var $xml_obj = null;
  37. /**
  38. * holds the output array
  39. *
  40. * @var array
  41. */
  42. var $output = array();
  43. /**
  44. * the XML file character set
  45. *
  46. * @var array
  47. */
  48. var $char_set = 'ISO-8859-1';
  49. var $parse_error_string = "" ;
  50. var $check_file_path = true ;
  51. public $CaseInsensitive = 1 ;
  52. public $SkipWhiteSpaces = 1 ;
  53. /**#@-*/
  54. /**
  55. * The class constructor.
  56. */
  57. function XMLParser(){ }
  58. /**
  59. * parse the XML file (or URL)
  60. *
  61. * @param string $path the XML file path, or URL
  62. */
  63. function init_output()
  64. {
  65. $this->output = array();
  66. $this->parse_error_string = "" ;
  67. $this->init_parser() ;
  68. }
  69. function init_parser()
  70. {
  71. $this->xml_obj = xml_parser_create($this->char_set);
  72. xml_parser_set_option($this->xml_obj, XML_OPTION_SKIP_WHITE, $this->SkipWhiteSpaces) ;
  73. xml_parser_set_option($this->xml_obj, XML_OPTION_CASE_FOLDING, $this->CaseInsensitive) ;
  74. xml_set_object($this->xml_obj, $this);
  75. xml_set_character_data_handler($this->xml_obj, 'dataHandler');
  76. xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
  77. }
  78. function parse($path)
  79. {
  80. $this->init_output() ;
  81. try
  82. {
  83. if($this->check_file_path && ! file_exists($path)) {
  84. $this->parse_error_string = "Le fichier $path n'existe pas" ;
  85. return false ;
  86. }
  87. if (!($fp = fopen($path, "r"))) {
  88. $this->parse_error_string = "Cannot open XML data file: $path" ;
  89. return false;
  90. }
  91. while (($data = fread($fp, 4096)) != false) {
  92. $this->parse_data($data, feof($fp)) ;
  93. }
  94. }
  95. catch(Exception $ex)
  96. {
  97. $this->parse_error_string = $ex->getMessage() ;
  98. }
  99. return $this->output;
  100. }
  101. function & parse_error()
  102. {
  103. return $this->parse_error_string ;
  104. }
  105. function parse_data($data, $eof=true)
  106. {
  107. if(! xml_parse($this->xml_obj, $data, $eof)) {
  108. $this->parse_error_string = sprintf("XML error: %s at line %d",
  109. xml_error_string(xml_get_error_code($this->xml_obj)),
  110. xml_get_current_line_number($this->xml_obj)
  111. ) ;
  112. xml_parser_free($this->xml_obj);
  113. }
  114. }
  115. function parse_ctn($ctn)
  116. {
  117. $this->init_output() ;
  118. $this->parse_data($ctn, true) ;
  119. return $this->output;
  120. }
  121. /**
  122. * define the start tag handler
  123. *
  124. * @param obj $parser the expat parser object
  125. * @param string $name the XML tag name
  126. * @param array $attribs the XML tag attributes
  127. */
  128. function startHandler($parser, $name, $attribs){
  129. $_content = array('name' => $name);
  130. if(! empty($attribs))
  131. $_content['attrs'] = $attribs;
  132. array_push($this->output, $_content);
  133. }
  134. /**
  135. * define the tag data handler
  136. *
  137. * @param obj $parser the expat parser object
  138. * @param string $data the XML data
  139. */
  140. function dataHandler($parser, $data){
  141. if(! empty($data) || $data === 0 || $data === "") {
  142. $_output_idx = count($this->output) - 1;
  143. if(!isset($this->output[$_output_idx]['content']))
  144. $this->output[$_output_idx]['content'] = trim($data);
  145. else
  146. $this->output[$_output_idx]['content'] .= $data;
  147. }
  148. }
  149. /**
  150. * define the end tag handler
  151. *
  152. * @param obj $parser the expat parser object
  153. * @param string $name the XML tag name
  154. */
  155. function endHandler($parser, $name){
  156. if(count($this->output) > 1) {
  157. $_data = array_pop($this->output);
  158. $_output_idx = count($this->output) - 1;
  159. $this->output[$_output_idx]['child'][] = $_data;
  160. }
  161. }
  162. }
  163. function array_to_xml($data, $rootNodeName = 'data')
  164. {
  165. $ctn = '' ;
  166. // turn off compatibility mode as simple xml throws a wobbly if you don't.
  167. if (ini_get('zend.ze1_compatibility_mode') == 1)
  168. {
  169. ini_set ('zend.ze1_compatibility_mode', 0);
  170. }
  171. if($rootNodeName != '')
  172. {
  173. $ctn .= '<?xml version="1.0"?>' ;
  174. $ctn .= '<'.$rootNodeName.'>' ;
  175. }
  176. if(is_array($data))
  177. {
  178. // loop through the data passed in.
  179. foreach($data as $key => $value)
  180. {
  181. // no numeric keys in our xml please!
  182. if (is_numeric($key))
  183. {
  184. // make string key...
  185. $key = "unknownNode_". (string) $key;
  186. }
  187. // replace anything not alpha numeric
  188. $key = preg_replace('/[^a-z]/i', '', $key);
  189. // if there is another array found recrusively call this function
  190. if (is_array($value))
  191. {
  192. // recrusive call.
  193. $ctn .= array_to_xml($value, '');
  194. }
  195. else
  196. {
  197. // add single node.
  198. $value = htmlentities($value);
  199. $ctn .= '<'.$key.'>'.$value.'</'.$key.'>' ;
  200. }
  201. }
  202. }
  203. if($rootNodeName != '')
  204. {
  205. $ctn .= '</'.$rootNodeName.'>' ;
  206. }
  207. // pass back as string. or simple xml object if you want!
  208. return $ctn ;
  209. }
  210. // Racourcis pour parser
  211. // Parser du contenu XML
  212. function xmldata_parse_content($ctn)
  213. {
  214. $xml = new XMLParser() ;
  215. $result = $xml->parse_ctn($ctn) ;
  216. return $result ;
  217. }
  218. // Parser un fichier XML
  219. function xmldata_parse_file($filepath)
  220. {
  221. $xml = new XMLParser() ;
  222. $result = $xml->parse($filepath) ;
  223. return $result ;
  224. }
  225. function xmldata_parse($filepath)
  226. {
  227. return xmldata_parse_file($filepath) ;
  228. }
  229. // Localisation de noeuds XML
  230. function & xmldata_get_node_by_name(& $node, $node_name="")
  231. {
  232. if($node_name == "")
  233. {
  234. return null ;
  235. }
  236. $node_name = strtoupper($node_name) ;
  237. $current_nodes = array() ;
  238. if(isset($node["child"]))
  239. {
  240. foreach($node["child"] as $i => $node_temp)
  241. {
  242. if($node_temp["name"] == $node_name)
  243. {
  244. $current_nodes[] = $node_temp ;
  245. }
  246. }
  247. }
  248. return $current_nodes ;
  249. }
  250. function & xmldata_get_node_by_attr(& $node, $attrs=array())
  251. {
  252. if(empty($attrs) || empty($node))
  253. {
  254. $res = null ;
  255. return $res ;
  256. }
  257. $current_nodes = array() ;
  258. if(isset($node["child"]))
  259. {
  260. foreach($node["child"] as $i => $node_temp)
  261. {
  262. if(isset($node_temp["attrs"]))
  263. {
  264. $match = 1 ;
  265. foreach($attrs as $n => $v)
  266. {
  267. $n = strtoupper($n) ;
  268. if(! isset($node_temp["attrs"][$n]))
  269. {
  270. $match = 0 ;
  271. break ;
  272. }
  273. if($node_temp["attrs"][$n] != $v)
  274. {
  275. $match = 0 ;
  276. break ;
  277. }
  278. }
  279. if($match)
  280. {
  281. $current_nodes[] = $node_temp ;
  282. }
  283. }
  284. }
  285. }
  286. // print_r($current_nodes) ;
  287. return $current_nodes ;
  288. }
  289. function xmldata_get_node_by_attr_name(& $node, $attr_name="")
  290. {
  291. return xmldata_get_node_by_attr($node, array('NAME' => $attr_name)) ;
  292. }
  293. function xmldata_get_first_node_by_attr_name(& $node, $attr_name="")
  294. {
  295. return xmldata_get_first_node_by_attr($node, array('NAME' => $attr_name)) ;
  296. }
  297. function & xmldata_get_first_node_by_attr(& $node, $attr=array())
  298. {
  299. $current_nodes = xmldata_get_node_by_attr($node, $attr) ;
  300. $current_node = null ;
  301. if($current_nodes)
  302. {
  303. if(count($current_nodes))
  304. {
  305. $current_node = $current_nodes[0] ;
  306. }
  307. }
  308. return $current_node ;
  309. }
  310. function & xmldata_get_first_node_by_name(& $node, $node_name="")
  311. {
  312. $current_nodes = xmldata_get_node_by_name($node, $node_name) ;
  313. $current_node = null ;
  314. if($current_nodes)
  315. {
  316. if(count($current_nodes))
  317. {
  318. $current_node = $current_nodes[0] ;
  319. }
  320. }
  321. return $current_node ;
  322. }
  323. function & xmldata_reach_first_node_by_name(& $node, $node_path=array())
  324. {
  325. $node_temp = $node ;
  326. $result = null ;
  327. for($i=0; $i<count($node_path); $i++)
  328. {
  329. $node_temp = xmldata_get_first_node_by_name($node_temp, $node_path[$i]) ;
  330. //print $node_temp["name"]."<br>" ;
  331. if($node_temp === null)
  332. {
  333. break ;
  334. }
  335. else
  336. {
  337. if($i == count($node_path) - 1)
  338. {
  339. $result = $node_temp ;
  340. }
  341. }
  342. }
  343. return $result ;
  344. }
  345. // Convertion de noeuds XML en tableau
  346. /****
  347. Conversion en Params Array :
  348. ---------------------------------------
  349. <node>
  350. <param name="data1" value="val1"></param>
  351. <param name="data2" value="val2"></param>
  352. </node>
  353. ***/
  354. function & xmldata_node_to_param_array($node)
  355. {
  356. if(empty($node))
  357. {
  358. $res = null ;
  359. return $res ;
  360. }
  361. $result = array() ;
  362. if(isset($node["child"]))
  363. {
  364. foreach($node["child"] as $i => $node_temp)
  365. {
  366. if(isset($node_temp["attrs"]))
  367. {
  368. if(isset($node_temp["attrs"]["NAME"]))
  369. {
  370. $value = (isset($node_temp["attrs"]["VALUE"])) ? $node_temp["attrs"]["VALUE"] : "" ;
  371. $result[$node_temp["attrs"]["NAME"]] = $value ;
  372. }
  373. }
  374. }
  375. }
  376. return $result ;
  377. }
  378. function & xmldata_node_to_defs($node, $child_node_name="")
  379. {
  380. if(empty($node))
  381. {
  382. return null ;
  383. }
  384. $result= array() ;
  385. if(isset($node["child"]))
  386. {
  387. foreach($node["child"] as $i => $node_temp)
  388. {
  389. if(isset($node_temp["attrs"]))
  390. {
  391. if(isset($node_temp["attrs"]["NAME"]))
  392. {
  393. if($child_node_name == "" || strtoupper($child_node_name) == strtoupper($node["name"]))
  394. {
  395. $result[strtoupper($node_temp["attrs"]["NAME"])] = $node_temp["attrs"] ;
  396. }
  397. }
  398. }
  399. }
  400. }
  401. return $result ;
  402. }
  403. function & xmldata_node_to_array($node)
  404. {
  405. if(empty($node))
  406. {
  407. $result = null ;
  408. return $result ;
  409. }
  410. $result = array() ;
  411. if(isset($node["child"]))
  412. {
  413. foreach($node["child"] as $i => $node_temp)
  414. {
  415. $content = (isset($node_temp["content"])) ? $node_temp["content"] : "" ;
  416. $result[$node_temp["name"]] = $content ;
  417. }
  418. }
  419. return $result ;
  420. }
  421. function & xmldata_node_to_list($node)
  422. {
  423. if(empty($node))
  424. {
  425. $result = null ;
  426. return $result ;
  427. }
  428. $result = array() ;
  429. if(isset($node["child"]))
  430. {
  431. foreach($node["child"] as $i => $node_temp)
  432. {
  433. $content = (isset($node_temp["content"])) ? $node_temp["content"] : "" ;
  434. $result[] = $content ;
  435. }
  436. }
  437. return $result ;
  438. }
  439. function & xmldata_node_to_array_of_attrs($node)
  440. {
  441. if(empty($node))
  442. {
  443. $result = null ;
  444. return $result ;
  445. }
  446. $result = array() ;
  447. if(isset($node["child"]))
  448. {
  449. foreach($node["child"] as $i => $node_temp)
  450. {
  451. $attrs = (isset($node_temp["attrs"])) ? $node_temp["attrs"] : "" ;
  452. $result[$node_temp["name"]] = $attrs ;
  453. }
  454. }
  455. return $result ;
  456. }
  457. function & xmldata_node_to_child_param_array($node)
  458. {
  459. if(empty($node))
  460. {
  461. $result = null ;
  462. return $result ;
  463. }
  464. $result = array() ;
  465. if(isset($node["child"]))
  466. {
  467. foreach($node["child"] as $i => $node_temp)
  468. {
  469. if(isset($node_temp["attrs"]))
  470. {
  471. if(isset($node_temp["attrs"]["NAME"]))
  472. {
  473. $result[$node_temp["attrs"]["NAME"]] = $node_temp["attrs"] ;
  474. $result[$node_temp["attrs"]["NODETYPE"]] = $node_temp["name"] ;
  475. $result[$node_temp["attrs"]["NAME"]]["child"] = (isset($node_temp["child"])) ? $node_temp["_child"] : array() ;
  476. $result[$node_temp["attrs"]["NAME"]]["content"] = (isset($node_temp["content"])) ? $node_temp["content"] : array() ;
  477. }
  478. }
  479. }
  480. }
  481. return $result ;
  482. }
  483. // Conversion et formattage XML <-> Chaine de caractĨre
  484. function xmldata_format_attr_value($value)
  485. {
  486. return htmlentities($value) ;
  487. }
  488. function xmldata_format_attr_name($name)
  489. {
  490. $name = preg_replace('/[^a-zA-Z0-9_\:\-]/', '', $name) ;
  491. return $name ;
  492. }
  493. function xmldata_format_content($ctn)
  494. {
  495. return '<![CDATA['.$node["content"].']]>' ;
  496. }
  497. function xmldata_node_to_string(& $node)
  498. {
  499. $ctn = '' ;
  500. if($node)
  501. {
  502. $node_name = (isset($node["name"])) ? $node["name"] : "UNKNOWN_NODE" ;
  503. $ctn .= '<'.$node_name ;
  504. if(isset($node["attrs"]))
  505. {
  506. foreach($node["attrs"] as $key => $val)
  507. {
  508. $ctn .= ' '.xmldata_format_attr_name($key).'="'.xmldata_format_attr_value($val).'"' ;
  509. }
  510. }
  511. $ctn .= '>' ;
  512. if(isset($node["child"]))
  513. {
  514. foreach($node["child"] as $i => $node_temp)
  515. {
  516. $ctn .= xmldata_node_to_string($node_temp) ;
  517. }
  518. }
  519. if(isset($node["content"]))
  520. {
  521. if($node["content"] != "")
  522. {
  523. $ctn .= xmldata_format_content($node["content"]) ;
  524. }
  525. }
  526. $ctn .= '</'.$node_name.'>' ;
  527. }
  528. return $ctn ;
  529. }
  530. function xmldata_param_array_to_string(& $data)
  531. {
  532. $ctn = '' ;
  533. if(is_array($data))
  534. {
  535. foreach($data as $key => $val)
  536. {
  537. $ctn .= '<param name="'.xmldata_format_attr_value($key).'" value="'.xmldata_format_attr_value($value).'" />'."\r\n" ;
  538. }
  539. }
  540. return $ctn ;
  541. }
  542. // Configuration
  543. function xmlconfig_load_array($file_path)
  544. {
  545. $CONFIG_NODE = xmldata_parse_file($file_path) ;
  546. $CONFIG_DATA = array() ;
  547. if(isset($CONFIG_NODE[0]))
  548. {
  549. $CONFIG_DATA = xmldata_node_to_array($CONFIG_NODE[0]) ;
  550. }
  551. else
  552. {
  553. $CONFIG_DATA = null ;
  554. }
  555. return $CONFIG_DATA ;
  556. }
  557. function xmlconfig_load_array_data($data)
  558. {
  559. $CONFIG_NODE = xmldata_parse($data) ;
  560. $CONFIG_DATA = array() ;
  561. if(isset($CONFIG_NODE[0]))
  562. {
  563. $CONFIG_DATA = xmldata_node_to_array($CONFIG_NODE[0]) ;
  564. }
  565. else
  566. {
  567. $CONFIG_DATA = null ;
  568. }
  569. return $CONFIG_DATA ;
  570. }
  571. }
  572. ?>