TYPO3  7.6
core/Classes/Localization/Parser/AbstractXmlParser.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Localization\Parser;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
26 {
30  protected $sourcePath;
31 
35  protected $languageKey;
36 
40  protected $charset;
41 
52  {
53  $this->sourcePath = $sourcePath;
54  $this->languageKey = $languageKey;
55  $this->charset = $this->getCharset($languageKey, $charset);
56  if ($this->languageKey !== 'default') {
57  $this->sourcePath = GeneralUtility::getFileAbsFileName(GeneralUtility::llXmlAutoFileName($this->sourcePath, $this->languageKey));
58  if (!@is_file($this->sourcePath)) {
59  // Global localization is not available, try split localization file
61  }
62  if (!@is_file($this->sourcePath)) {
63  throw new FileNotFoundException('Localization file does not exist', 1306332397);
64  }
65  }
66  $LOCAL_LANG = array();
67  $LOCAL_LANG[$languageKey] = $this->parseXmlFile();
68  return $LOCAL_LANG;
69  }
70 
78  protected function getCharset($languageKey, $charset = '')
79  {
81  if (is_object($GLOBALS['LANG'])) {
82  $charsetConverter = $GLOBALS['LANG']->csConvObj;
83  } elseif (is_object($GLOBALS['TSFE'])) {
84  $charsetConverter = $GLOBALS['TSFE']->csConvObj;
85  } else {
86  $charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
87  }
88  if ($charset !== '') {
89  $targetCharset = $charsetConverter->parse_charset($charset);
90  } else {
91  $targetCharset = 'utf-8';
92  }
93  return $targetCharset;
94  }
95 
102  protected function parseXmlFile()
103  {
104  $rootXmlNode = simplexml_load_file($this->sourcePath, 'SimpleXMLElement', LIBXML_NOWARNING);
105  if (!isset($rootXmlNode) || $rootXmlNode === false) {
106  throw new InvalidXmlFileException('The path provided does not point to existing and accessible well-formed XML file.', 1278155988);
107  }
108  return $this->doParsingFromRoot($rootXmlNode);
109  }
110 
117  abstract protected function doParsingFromRoot(\SimpleXMLElement $root);
118 }