PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/Zend/Translate/Adapter/XmlTm.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 139 lines | 81 code | 15 blank | 43 comment | 6 complexity | b776f860e03da86051028443b2a267e0 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: XmlTm.php 23775 2011-03-01 17:25:24Z ralph $
  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. /**
  26. * @category Zend
  27. * @package Zend_Translate
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Translate_Adapter_XmlTm extends Zend_Translate_Adapter {
  32. // Internal variables
  33. private $_file = false;
  34. private $_cleared = array();
  35. private $_lang = null;
  36. private $_content = null;
  37. private $_tag = null;
  38. private $_data = array();
  39. /**
  40. * Load translation data (XMLTM file reader)
  41. *
  42. * @param string $locale Locale/Language to add data for, identical with locale identifier,
  43. * see Zend_Locale for more information
  44. * @param string $filename XMLTM file to add, full path must be given for access
  45. * @param array $option OPTIONAL Options to use
  46. * @throws Zend_Translation_Exception
  47. * @return array
  48. */
  49. protected function _loadTranslationData($filename, $locale, array $options = array())
  50. {
  51. $this->_data = array();
  52. $this->_lang = $locale;
  53. if (!is_readable($filename)) {
  54. require_once 'Zend/Translate/Exception.php';
  55. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  56. }
  57. $encoding = $this->_findEncoding($filename);
  58. $this->_file = xml_parser_create($encoding);
  59. xml_set_object($this->_file, $this);
  60. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  61. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  62. xml_set_character_data_handler($this->_file, "_contentElement");
  63. if (!xml_parse($this->_file, file_get_contents($filename))) {
  64. $ex = sprintf('XML error: %s at line %d',
  65. xml_error_string(xml_get_error_code($this->_file)),
  66. xml_get_current_line_number($this->_file));
  67. xml_parser_free($this->_file);
  68. require_once 'Zend/Translate/Exception.php';
  69. throw new Zend_Translate_Exception($ex);
  70. }
  71. return $this->_data;
  72. }
  73. private function _startElement($file, $name, $attrib)
  74. {
  75. switch(strtolower($name)) {
  76. case 'tm:tu':
  77. $this->_tag = $attrib['id'];
  78. $this->_content = null;
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. private function _endElement($file, $name)
  85. {
  86. switch (strtolower($name)) {
  87. case 'tm:tu':
  88. if (!empty($this->_tag) and !empty($this->_content) or
  89. (isset($this->_data[$this->_lang][$this->_tag]) === false)) {
  90. $this->_data[$this->_lang][$this->_tag] = $this->_content;
  91. }
  92. $this->_tag = null;
  93. $this->_content = null;
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. private function _contentElement($file, $data)
  100. {
  101. if (($this->_tag !== null)) {
  102. $this->_content .= $data;
  103. }
  104. }
  105. private function _findEncoding($filename)
  106. {
  107. $file = file_get_contents($filename, null, null, 0, 100);
  108. if (strpos($file, "encoding") !== false) {
  109. $encoding = substr($file, strpos($file, "encoding") + 9);
  110. $encoding = substr($encoding, 1, strpos($encoding, $encoding[0], 1) - 1);
  111. return $encoding;
  112. }
  113. return 'UTF-8';
  114. }
  115. /**
  116. * Returns the adapter name
  117. *
  118. * @return string
  119. */
  120. public function toString()
  121. {
  122. return "XmlTm";
  123. }
  124. }