00001 <?php
00024 require_once 'Zend/Locale.php';
00025
00027 require_once 'Zend/Translate/Adapter.php';
00028
00029
00036 class Zend_Translate_Adapter_XmlTm extends Zend_Translate_Adapter {
00037
00038 private $_file = false;
00039 private $_cleared = array();
00040 private $_lang = null;
00041 private $_content = null;
00042 private $_tag = null;
00043 private $_data = array();
00044
00054 public function __construct($data, $locale = null, array $options = array())
00055 {
00056 parent::__construct($data, $locale, $options);
00057 }
00058
00059
00070 protected function _loadTranslationData($filename, $locale, array $options = array())
00071 {
00072 $this->_data = array();
00073 $this->_lang = $locale;
00074 if (!is_readable($filename)) {
00075 require_once 'Zend/Translate/Exception.php';
00076 throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
00077 }
00078
00079 $encoding = $this->_findEncoding($filename);
00080 $this->_file = xml_parser_create($encoding);
00081 xml_set_object($this->_file, $this);
00082 xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
00083 xml_set_element_handler($this->_file, "_startElement", "_endElement");
00084 xml_set_character_data_handler($this->_file, "_contentElement");
00085
00086 if (!xml_parse($this->_file, file_get_contents($filename))) {
00087 $ex = sprintf('XML error: %s at line %d',
00088 xml_error_string(xml_get_error_code($this->_file)),
00089 xml_get_current_line_number($this->_file));
00090 xml_parser_free($this->_file);
00091 require_once 'Zend/Translate/Exception.php';
00092 throw new Zend_Translate_Exception($ex);
00093 }
00094
00095 return $this->_data;
00096 }
00097
00098 private function _startElement($file, $name, $attrib)
00099 {
00100 switch(strtolower($name)) {
00101 case 'tm:tu':
00102 $this->_tag = $attrib['id'];
00103 $this->_content = null;
00104 break;
00105 default:
00106 break;
00107 }
00108 }
00109
00110 private function _endElement($file, $name)
00111 {
00112 switch (strtolower($name)) {
00113 case 'tm:tu':
00114 if (!empty($this->_tag) and !empty($this->_content) or
00115 (isset($this->_data[$this->_lang][$this->_tag]) === false)) {
00116 $this->_data[$this->_lang][$this->_tag] = $this->_content;
00117 }
00118 $this->_tag = null;
00119 $this->_content = null;
00120 break;
00121
00122 default:
00123 break;
00124 }
00125 }
00126
00127 private function _contentElement($file, $data)
00128 {
00129 if (($this->_tag !== null)) {
00130 $this->_content .= $data;
00131 }
00132 }
00133
00134 private function _findEncoding($filename)
00135 {
00136 $file = file_get_contents($filename, null, null, 0, 100);
00137 if (strpos($file, "encoding") !== false) {
00138 $encoding = substr($file, strpos($file, "encoding") + 9);
00139 $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
00140 return $encoding;
00141 }
00142 return 'UTF-8';
00143 }
00144
00150 public function toString()
00151 {
00152 return "XmlTm";
00153 }
00154 }