2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
15 * Class for parsing XML language info and returning a Language Object
17 * @author Joel Kronenberg
20 class LanguageParser {
23 var $parser; // the XML handler
24 var $language_rows = array(); // the language data used for creating the Language Object
25 var $character_data; // tmp variable for storing the data
26 var $element_path; // array of element paths (basically a stack)
29 function LanguageParser() {
30 $this->parser = xml_parser_create('');
32 xml_set_object($this->parser, $this);
33 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); /* conform to W3C specs */
34 xml_set_element_handler($this->parser, 'startElement', 'endElement');
35 xml_set_character_data_handler($this->parser, 'characterData');
39 function parse($xml_data) {
40 $this->element_path = array();
41 $this->language_rows = array();
42 $this->character_data = '';
44 xml_parse($this->parser, $xml_data, TRUE);
48 function getLanguage($row_num) {
49 return new Language($this->language_rows[$row_num]);
53 function getLanguageEditor($row_num) {
54 require_once(TR_INCLUDE_PATH.'classes/Language/LanguageEditor.class.php');
55 return new LanguageEditor($this->language_rows[$row_num]);
59 function startElement($parser, $name, $attributes) {
60 array_push($this->element_path, $name);
62 if ($this->element_path == array('language')) {
63 $this->language_rows[$this->row_num]['language_code'] = $attributes['code'];
68 /* called when an element ends */
69 /* removed the current element from the $path */
70 function endElement($parser, $name) {
71 if ($this->element_path == array('language', 'transformable-version')) {
72 $this->language_rows[$this->row_num]['version'] = trim($this->character_data);
74 } else if ($this->element_path === array('language', 'charset')) {
75 $this->language_rows[$this->row_num]['charset'] = trim($this->character_data);
77 } else if ($this->element_path === array('language', 'reg-exp')) {
78 $this->language_rows[$this->row_num]['reg_exp'] = trim($this->character_data);
80 } else if ($this->element_path === array('language', 'native-name')) {
81 $this->language_rows[$this->row_num]['native_name'] = trim($this->character_data);
83 } else if ($this->element_path === array('language', 'english-name')) {
84 $this->language_rows[$this->row_num]['english_name'] = trim($this->character_data);
86 } else if ($this->element_path === array('language', 'status')) {
87 $this->language_rows[$this->row_num]['status'] = trim($this->character_data);
89 } else if ($this->element_path === array('language')) {
93 array_pop($this->element_path);
94 $this->character_data = '';
98 function characterData($parser, $data){
99 $this->character_data .= $data;
103 function getNumLanguages() {
104 return count($this->language_rows);