c78d8e16d199f5242b943478bcbcbece5a7c4e12
[acontent.git] / docs / include / classes / Language / LanguageParser.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
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 /************************************************************************/
12
13 /**
14 * LanguageParser
15 * Class for parsing XML language info and returning a Language Object
16 * @access       public
17 * @author       Joel Kronenberg
18 * @package      Language
19 */
20 class LanguageParser {
21
22         // all private
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)
27         var $row_num;
28
29         function LanguageParser() {
30                 $this->parser = xml_parser_create(''); 
31
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');
36         }
37
38         // public
39         function parse($xml_data) {
40                 $this->element_path   = array();
41                 $this->language_rows  = array();
42                 $this->character_data = '';
43                 $this->row_num        = 0;
44                 xml_parse($this->parser, $xml_data, TRUE);
45         }
46
47         // public
48         function getLanguage($row_num) {
49                 return new Language($this->language_rows[$row_num]);
50         }
51
52         // public
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]);
56         }
57
58         // private
59         function startElement($parser, $name, $attributes) {
60                 array_push($this->element_path, $name);
61
62                 if ($this->element_path == array('language')) {
63                         $this->language_rows[$this->row_num]['language_code'] = $attributes['code'];
64                 }
65    }
66
67         // private
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);
73
74                 } else if ($this->element_path === array('language', 'charset')) {
75                         $this->language_rows[$this->row_num]['charset'] = trim($this->character_data);
76
77                 } else if ($this->element_path === array('language', 'reg-exp')) {
78                         $this->language_rows[$this->row_num]['reg_exp'] = trim($this->character_data);
79
80                 } else if ($this->element_path === array('language', 'native-name')) {
81                         $this->language_rows[$this->row_num]['native_name'] = trim($this->character_data);
82
83                 } else if ($this->element_path === array('language', 'english-name')) {
84                         $this->language_rows[$this->row_num]['english_name'] = trim($this->character_data);
85
86                 } else if ($this->element_path === array('language', 'status')) {
87                         $this->language_rows[$this->row_num]['status'] = trim($this->character_data);
88
89                 } else if ($this->element_path === array('language')) {
90                         $this->row_num++;
91                 }
92
93                 array_pop($this->element_path);
94                 $this->character_data = '';
95         }
96
97         // private
98         function characterData($parser, $data){
99                 $this->character_data .= $data;
100         }
101
102         // public
103         function getNumLanguages() {
104                 return count($this->language_rows);
105         }
106 }
107
108
109
110 ?>