[TASK] Move and Namespace classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / Parser / AbstractXmlParser.php
1 <?php
2 namespace TYPO3\CMS\Core\Localization\Parser;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Abstract class for XML based parser.
31 *
32 * @package TYPO3
33 * @subpackage t3lib
34 * @author Dominique Feyer <dfeyer@reelpeek.net>
35 */
36 abstract class AbstractXmlParser implements \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface {
37
38 /**
39 * @var string
40 */
41 protected $sourcePath;
42
43 /**
44 * @var string
45 */
46 protected $languageKey;
47
48 /**
49 * @var string
50 */
51 protected $charset;
52
53 /**
54 * Returns parsed representation of XML file.
55 *
56 * @param string $sourcePath Source file path
57 * @param string $languageKey Language key
58 * @param string $charset File charset
59 * @return array
60 * @throws \TYPO3\CMS\Core\Localization\Exception\FileNotFoundException
61 */
62 public function getParsedData($sourcePath, $languageKey, $charset = '') {
63 $this->sourcePath = $sourcePath;
64 $this->languageKey = $languageKey;
65 $this->charset = $this->getCharset($languageKey, $charset);
66 if ($this->languageKey !== 'default' && $this->languageKey !== 'en') {
67 $this->sourcePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(\TYPO3\CMS\Core\Utility\GeneralUtility::llXmlAutoFileName($this->sourcePath, $this->languageKey));
68 if (!@is_file($this->sourcePath)) {
69 // Global localization is not available, try split localization file
70 $this->sourcePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(\TYPO3\CMS\Core\Utility\GeneralUtility::llXmlAutoFileName($sourcePath, $languageKey, TRUE));
71 }
72 if (!@is_file($this->sourcePath)) {
73 throw new \TYPO3\CMS\Core\Localization\Exception\FileNotFoundException('Localization file does not exist', 1306332397);
74 }
75 }
76 $LOCAL_LANG = array();
77 $LOCAL_LANG[$languageKey] = $this->parseXmlFile();
78 return $LOCAL_LANG;
79 }
80
81 /**
82 * Gets the character set to use.
83 *
84 * @param string $languageKey
85 * @param string $charset
86 * @return string
87 */
88 protected function getCharset($languageKey, $charset = '') {
89 /** @var $csConvObj \TYPO3\CMS\Core\Charset\CharsetConverter */
90 if (is_object($GLOBALS['LANG'])) {
91 $csConvObj = $GLOBALS['LANG']->csConvObj;
92 } elseif (is_object($GLOBALS['TSFE'])) {
93 $csConvObj = $GLOBALS['TSFE']->csConvObj;
94 } else {
95 $csConvObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
96 }
97 if ($charset !== '') {
98 $targetCharset = $csConvObj->parse_charset($charset);
99 } else {
100 $targetCharset = 'utf-8';
101 }
102 return $targetCharset;
103 }
104
105 /**
106 * Loads the current XML file before processing.
107 *
108 * @return array An array representing parsed XML file (structure depends on concrete parser)
109 * @throws \TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException
110 */
111 protected function parseXmlFile() {
112 $rootXmlNode = simplexml_load_file($this->sourcePath, 'SimpleXmlElement', \LIBXML_NOWARNING);
113 if (!isset($rootXmlNode) || $rootXmlNode === FALSE) {
114 throw new \TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException('The path provided does not point to existing and accessible well-formed XML file.', 1278155987);
115 }
116 return $this->doParsingFromRoot($rootXmlNode);
117 }
118
119 /**
120 * Returns array representation of XML data, starting from a root node.
121 *
122 * @param SimpleXMLElement $root A root node
123 * @return array An array representing the parsed XML file
124 */
125 abstract protected function doParsingFromRoot(\SimpleXMLElement $root);
126
127 }
128
129
130 ?>