xmloutputter.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Low-level generator for XML
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Output
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Sarven Capadisli <csarven@status.net>
  26. * @copyright 2008 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. /**
  34. * Low-level generator for XML
  35. *
  36. * This is a thin wrapper around PHP's XMLWriter. The main
  37. * advantage is the element() method, which simplifies outputting
  38. * an element.
  39. *
  40. * @category Output
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Sarven Capadisli <csarven@status.net>
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://status.net/
  46. * @see Action
  47. * @see HTMLOutputter
  48. */
  49. class XMLOutputter
  50. {
  51. /**
  52. * Wrapped XMLWriter object, which does most of the heavy lifting
  53. * for output.
  54. */
  55. public $xw = null;
  56. /**
  57. * Constructor
  58. *
  59. * Initializes the wrapped XMLWriter.
  60. *
  61. * @param string $output URL for outputting, if null it defaults to stdout ('php://output')
  62. * @param boolean $indent Whether to indent output, default true
  63. */
  64. public function __construct($output = null, $indent = null)
  65. {
  66. if (is_null($output)) {
  67. $output = 'php://output';
  68. }
  69. $this->xw = new XMLWriter();
  70. $this->xw->openURI($output);
  71. if (is_null($indent)) {
  72. $indent = common_config('site', 'indent');
  73. }
  74. $this->xw->setIndent($indent);
  75. }
  76. /**
  77. * Start a new XML document
  78. *
  79. * @param string $doc document element
  80. * @param string $public public identifier
  81. * @param string $system system identifier
  82. *
  83. * @return void
  84. */
  85. public function startXML($doc = null, $public = null, $system = null)
  86. {
  87. $this->xw->startDocument('1.0', 'UTF-8');
  88. if ($doc) {
  89. $this->xw->writeDTD($doc, $public, $system);
  90. }
  91. }
  92. /**
  93. * finish an XML document
  94. *
  95. * It's probably a bad idea to continue to use this object
  96. * after calling endXML().
  97. *
  98. * @return void
  99. */
  100. public function endXML()
  101. {
  102. $this->xw->endDocument();
  103. $this->xw->flush();
  104. }
  105. /**
  106. * output an XML element
  107. *
  108. * Utility for outputting an XML element. A convenient wrapper
  109. * for a bunch of longer XMLWriter calls. This is best for
  110. * when an element doesn't have any sub-elements; if that's the
  111. * case, use elementStart() and elementEnd() instead.
  112. *
  113. * The $content element will be escaped for XML. If you need
  114. * raw output, use elementStart() and elementEnd() with a call
  115. * to raw() in the middle.
  116. *
  117. * If $attrs is a string instead of an array, it will be treated
  118. * as the class attribute of the element.
  119. *
  120. * @param string $tag Element type or tagname
  121. * @param array|string|null $attrs Array of element attributes, as key-value pairs
  122. * @param string|null $content string content of the element
  123. *
  124. * @return void
  125. */
  126. public function element(string $tag, $attrs = null, $content = null)
  127. {
  128. $this->elementStart($tag, $attrs);
  129. if (!is_null($content)) {
  130. $this->xw->text(strval($content));
  131. }
  132. $this->elementEnd($tag);
  133. }
  134. /**
  135. * output a start tag for an element
  136. *
  137. * Mostly used for when an element has content that's
  138. * not a simple string.
  139. *
  140. * If $attrs is a string instead of an array, it will be treated
  141. * as the class attribute of the element.
  142. *
  143. * @param string $tag Element type or tagname
  144. * @param array|string|null $attrs Attributes
  145. *
  146. * @return void
  147. */
  148. public function elementStart(string $tag, $attrs = null)
  149. {
  150. $this->xw->startElement($tag);
  151. if (is_array($attrs)) {
  152. foreach ($attrs as $name => $value) {
  153. $this->xw->writeAttribute($name, $value);
  154. }
  155. } elseif (is_string($attrs)) {
  156. $this->xw->writeAttribute('class', $attrs);
  157. }
  158. }
  159. /**
  160. * output an end tag for an element
  161. *
  162. * Used in conjunction with elementStart(). $tag param
  163. * should match the elementStart() param.
  164. *
  165. * For HTML 4 compatibility, this method will force
  166. * a full end element (</tag>) even if the element is
  167. * empty, except for a handful of exception tagnames.
  168. * This is a hack.
  169. *
  170. * @param string $tag Element type or tagname.
  171. *
  172. * @return void
  173. */
  174. public function elementEnd(string $tag)
  175. {
  176. static $empty_tag = ['base', 'meta', 'link', 'hr',
  177. 'br', 'param', 'img', 'area',
  178. 'input', 'col', 'source'];
  179. // XXX: check namespace
  180. if (in_array($tag, $empty_tag)) {
  181. $this->xw->endElement();
  182. } else {
  183. $this->xw->fullEndElement();
  184. }
  185. }
  186. public function elementNS(array $ns, $tag, $attrs = null, $content = null)
  187. {
  188. $this->elementStartNS($ns, $tag, $attrs);
  189. if (!is_null($content)) {
  190. $this->xw->text($content);
  191. }
  192. $this->elementEnd($tag);
  193. }
  194. public function elementStartNS(array $ns, $tag, $attrs = null)
  195. {
  196. reset($ns); // array pointer to 0
  197. $uri = key($ns);
  198. $this->xw->startElementNS($ns[$uri], $tag, $uri);
  199. if (is_array($attrs)) {
  200. foreach ($attrs as $name => $value) {
  201. $this->xw->writeAttribute($name, $value);
  202. }
  203. } elseif (is_string($attrs)) {
  204. $this->xw->writeAttribute('class', $attrs);
  205. }
  206. }
  207. /**
  208. * output plain text
  209. *
  210. * Text will be escaped. If you need it not to be,
  211. * use raw() instead.
  212. *
  213. * @param string $txt Text to output.
  214. *
  215. * @return void
  216. */
  217. public function text($txt)
  218. {
  219. $this->xw->text($txt);
  220. }
  221. /**
  222. * output raw xml
  223. *
  224. * This will spit out its argument verbatim -- no escaping is
  225. * done.
  226. *
  227. * @param string $xml XML to output.
  228. *
  229. * @return void
  230. */
  231. public function raw($xml)
  232. {
  233. $this->xw->writeRaw($xml);
  234. }
  235. /**
  236. * output a comment
  237. *
  238. * @param string $txt text of the comment
  239. *
  240. * @return void
  241. */
  242. public function comment($txt)
  243. {
  244. $this->xw->writeComment($txt);
  245. }
  246. /**
  247. * Flush output buffers
  248. *
  249. * @return void
  250. */
  251. public function flush()
  252. {
  253. $this->xw->flush();
  254. }
  255. }