0
0

XMLWriter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. if (!defined('DATE_W3C')) {
  3. define('DATE_W3C', 'Y-m-d\TH:i:sP');
  4. }
  5. if (!defined('DEBUGMODE_ENABLED')) {
  6. define('DEBUGMODE_ENABLED', false);
  7. }
  8. /**
  9. * PHPExcel_Shared_XMLWriter
  10. *
  11. * Copyright (c) 2006 - 2015 PHPExcel
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * @category PHPExcel
  28. * @package PHPExcel_Shared
  29. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  30. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  31. * @version ##VERSION##, ##DATE##
  32. */
  33. class PHPExcel_Shared_XMLWriter extends XMLWriter
  34. {
  35. /** Temporary storage method */
  36. const STORAGE_MEMORY = 1;
  37. const STORAGE_DISK = 2;
  38. /**
  39. * Temporary filename
  40. *
  41. * @var string
  42. */
  43. private $tempFileName = '';
  44. /**
  45. * Create a new PHPExcel_Shared_XMLWriter instance
  46. *
  47. * @param int $pTemporaryStorage Temporary storage location
  48. * @param string $pTemporaryStorageFolder Temporary storage folder
  49. */
  50. public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = null)
  51. {
  52. // Open temporary storage
  53. if ($pTemporaryStorage == self::STORAGE_MEMORY) {
  54. $this->openMemory();
  55. } else {
  56. // Create temporary filename
  57. if ($pTemporaryStorageFolder === null) {
  58. $pTemporaryStorageFolder = PHPExcel_Shared_File::sys_get_temp_dir();
  59. }
  60. $this->tempFileName = @tempnam($pTemporaryStorageFolder, 'xml');
  61. // Open storage
  62. if ($this->openUri($this->tempFileName) === false) {
  63. // Fallback to memory...
  64. $this->openMemory();
  65. }
  66. }
  67. // Set default values
  68. if (DEBUGMODE_ENABLED) {
  69. $this->setIndent(true);
  70. }
  71. }
  72. /**
  73. * Destructor
  74. */
  75. public function __destruct()
  76. {
  77. // Unlink temporary files
  78. if ($this->tempFileName != '') {
  79. @unlink($this->tempFileName);
  80. }
  81. }
  82. /**
  83. * Get written data
  84. *
  85. * @return $data
  86. */
  87. public function getData()
  88. {
  89. if ($this->tempFileName == '') {
  90. return $this->outputMemory(true);
  91. } else {
  92. $this->flush();
  93. return file_get_contents($this->tempFileName);
  94. }
  95. }
  96. /**
  97. * Fallback method for writeRaw, introduced in PHP 5.2
  98. *
  99. * @param string $text
  100. * @return string
  101. */
  102. public function writeRawData($text)
  103. {
  104. if (is_array($text)) {
  105. $text = implode("\n", $text);
  106. }
  107. if (method_exists($this, 'writeRaw')) {
  108. return $this->writeRaw(htmlspecialchars($text));
  109. }
  110. return $this->text($text);
  111. }
  112. }