XMLWriter.php 3.5 KB

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