/libraries/Zend/Translator/Adapter/XmlTm.php

https://github.com/kiranatama/sagalaya · PHP · 136 lines · 83 code · 12 blank · 41 comment · 6 complexity · a5b76ac446cc2fcdadd80bf562097527 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_Translator
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. namespace Zend\Translator\Adapter;
  21. use Zend\Translator\Adapter\AbstractAdapter,
  22. Zend\Translator,
  23. Zend\Translator\Exception\InvalidArgumentException,
  24. Zend\Translator\Exception\InvalidFileTypeException;
  25. /**
  26. * @category Zend
  27. * @package Zend_Translator
  28. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class XmlTm extends AbstractAdapter
  32. {
  33. // Internal variables
  34. private $_file = false;
  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\Translator\Exception\InvalidArgumentException
  47. * @throws \Zend\Translator\Exception\InvalidFileTypeException
  48. * @return array
  49. */
  50. protected function _loadTranslationData($filename, $locale, array $options = array())
  51. {
  52. $this->_data = array();
  53. $this->_lang = $locale;
  54. if (!is_readable($filename)) {
  55. throw new InvalidArgumentException('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 of file %s',
  65. xml_error_string(xml_get_error_code($this->_file)),
  66. xml_get_current_line_number($this->_file),
  67. $filename);
  68. xml_parser_free($this->_file);
  69. throw new InvalidFileTypeException($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. }