XmlTm.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Translate
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id$
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Locale */
  22. #require_once 'Zend/Locale.php';
  23. /** Zend_Translate_Adapter */
  24. #require_once 'Zend/Translate/Adapter.php';
  25. /** @see Zend_Xml_Security */
  26. #require_once 'Zend/Xml/Security.php';
  27. /** @See Zend_Xml_Exception */
  28. #require_once 'Zend/Xml/Exception.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Translate
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Translate_Adapter_XmlTm extends Zend_Translate_Adapter {
  36. // Internal variables
  37. private $_file = false;
  38. private $_cleared = array();
  39. private $_lang = null;
  40. private $_content = null;
  41. private $_tag = null;
  42. private $_data = array();
  43. /**
  44. * Load translation data (XMLTM file reader)
  45. *
  46. * @param string $locale Locale/Language to add data for, identical with locale identifier,
  47. * see Zend_Locale for more information
  48. * @param string $filename XMLTM file to add, full path must be given for access
  49. * @param array $option OPTIONAL Options to use
  50. * @throws Zend_Translation_Exception
  51. * @return array
  52. */
  53. protected function _loadTranslationData($filename, $locale, array $options = array())
  54. {
  55. $this->_data = array();
  56. $this->_lang = $locale;
  57. if (!is_readable($filename)) {
  58. #require_once 'Zend/Translate/Exception.php';
  59. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  60. }
  61. $encoding = $this->_findEncoding($filename);
  62. $this->_file = xml_parser_create($encoding);
  63. xml_set_object($this->_file, $this);
  64. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  65. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  66. xml_set_character_data_handler($this->_file, "_contentElement");
  67. try {
  68. Zend_Xml_Security::scanFile($filename);
  69. } catch (Zend_Xml_Exception $e) {
  70. #require_once 'Zend/Translate/Exception.php';
  71. throw new Zend_Translate_Exception(
  72. $e->getMessage()
  73. );
  74. }
  75. if (!xml_parse($this->_file, file_get_contents($filename))) {
  76. $ex = sprintf('XML error: %s at line %d of file %s',
  77. xml_error_string(xml_get_error_code($this->_file)),
  78. xml_get_current_line_number($this->_file),
  79. $filename);
  80. xml_parser_free($this->_file);
  81. #require_once 'Zend/Translate/Exception.php';
  82. throw new Zend_Translate_Exception($ex);
  83. }
  84. return $this->_data;
  85. }
  86. private function _startElement($file, $name, $attrib)
  87. {
  88. switch(strtolower($name)) {
  89. case 'tm:tu':
  90. $this->_tag = $attrib['id'];
  91. $this->_content = null;
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. private function _endElement($file, $name)
  98. {
  99. switch (strtolower($name)) {
  100. case 'tm:tu':
  101. if (!empty($this->_tag) and !empty($this->_content) or
  102. (isset($this->_data[$this->_lang][$this->_tag]) === false)) {
  103. $this->_data[$this->_lang][$this->_tag] = $this->_content;
  104. }
  105. $this->_tag = null;
  106. $this->_content = null;
  107. break;
  108. default:
  109. break;
  110. }
  111. }
  112. private function _contentElement($file, $data)
  113. {
  114. if (($this->_tag !== null)) {
  115. $this->_content .= $data;
  116. }
  117. }
  118. private function _findEncoding($filename)
  119. {
  120. $file = file_get_contents($filename, null, null, 0, 100);
  121. if (strpos($file, "encoding") !== false) {
  122. $encoding = substr($file, strpos($file, "encoding") + 9);
  123. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  124. return $encoding;
  125. }
  126. return 'UTF-8';
  127. }
  128. /**
  129. * Returns the adapter name
  130. *
  131. * @return string
  132. */
  133. public function toString()
  134. {
  135. return "XmlTm";
  136. }
  137. }