xmloutputter.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. var $xw = null;
  56. /**
  57. * Constructor
  58. *
  59. * Initializes the wrapped XMLWriter.
  60. *
  61. * @param string $output URL for outputting, defaults to stdout
  62. * @param boolean $indent Whether to indent output, default true
  63. */
  64. function __construct($output='php://output', $indent=null)
  65. {
  66. $this->xw = new XMLWriter();
  67. $this->xw->openURI($output);
  68. if(is_null($indent)) {
  69. $indent = common_config('site', 'indent');
  70. }
  71. $this->xw->setIndent($indent);
  72. }
  73. /**
  74. * Start a new XML document
  75. *
  76. * @param string $doc document element
  77. * @param string $public public identifier
  78. * @param string $system system identifier
  79. *
  80. * @return void
  81. */
  82. function startXML($doc=null, $public=null, $system=null)
  83. {
  84. $this->xw->startDocument('1.0', 'UTF-8');
  85. if ($doc) {
  86. $this->xw->writeDTD($doc, $public, $system);
  87. }
  88. }
  89. /**
  90. * finish an XML document
  91. *
  92. * It's probably a bad idea to continue to use this object
  93. * after calling endXML().
  94. *
  95. * @return void
  96. */
  97. function endXML()
  98. {
  99. $this->xw->endDocument();
  100. $this->xw->flush();
  101. }
  102. /**
  103. * output an XML element
  104. *
  105. * Utility for outputting an XML element. A convenient wrapper
  106. * for a bunch of longer XMLWriter calls. This is best for
  107. * when an element doesn't have any sub-elements; if that's the
  108. * case, use elementStart() and elementEnd() instead.
  109. *
  110. * The $content element will be escaped for XML. If you need
  111. * raw output, use elementStart() and elementEnd() with a call
  112. * to raw() in the middle.
  113. *
  114. * If $attrs is a string instead of an array, it will be treated
  115. * as the class attribute of the element.
  116. *
  117. * @param string $tag Element type or tagname
  118. * @param array $attrs Array of element attributes, as
  119. * key-value pairs
  120. * @param string $content string content of the element
  121. *
  122. * @return void
  123. */
  124. function element($tag, $attrs=null, $content=null)
  125. {
  126. $this->elementStart($tag, $attrs);
  127. if (!is_null($content)) {
  128. $this->xw->text($content);
  129. }
  130. $this->elementEnd($tag);
  131. }
  132. function elementNS(array $ns, $tag, $attrs=null, $content=null)
  133. {
  134. $this->elementStartNS($ns, $tag, $attrs);
  135. if (!is_null($content)) {
  136. $this->xw->text($content);
  137. }
  138. $this->elementEnd($tag);
  139. }
  140. /**
  141. * output a start tag for an element
  142. *
  143. * Mostly used for when an element has content that's
  144. * not a simple string.
  145. *
  146. * If $attrs is a string instead of an array, it will be treated
  147. * as the class attribute of the element.
  148. *
  149. * @param string $tag Element type or tagname
  150. * @param array $attrs Array of element attributes
  151. *
  152. * @return void
  153. */
  154. function elementStart($tag, $attrs=null)
  155. {
  156. $this->xw->startElement($tag);
  157. if (is_array($attrs)) {
  158. foreach ($attrs as $name => $value) {
  159. $this->xw->writeAttribute($name, $value);
  160. }
  161. } else if (is_string($attrs)) {
  162. $this->xw->writeAttribute('class', $attrs);
  163. }
  164. }
  165. function elementStartNS(array $ns, $tag, $attrs=null)
  166. {
  167. reset($ns); // array pointer to 0
  168. $uri = key($ns);
  169. $this->xw->startElementNS($ns[$uri], $tag, $uri);
  170. if (is_array($attrs)) {
  171. foreach ($attrs as $name => $value) {
  172. $this->xw->writeAttribute($name, $value);
  173. }
  174. } else if (is_string($attrs)) {
  175. $this->xw->writeAttribute('class', $attrs);
  176. }
  177. }
  178. /**
  179. * output an end tag for an element
  180. *
  181. * Used in conjunction with elementStart(). $tag param
  182. * should match the elementStart() param.
  183. *
  184. * For HTML 4 compatibility, this method will force
  185. * a full end element (</tag>) even if the element is
  186. * empty, except for a handful of exception tagnames.
  187. * This is a hack.
  188. *
  189. * @param string $tag Element type or tagname.
  190. *
  191. * @return void
  192. */
  193. function elementEnd($tag)
  194. {
  195. static $empty_tag = array('base', 'meta', 'link', 'hr',
  196. 'br', 'param', 'img', 'area',
  197. 'input', 'col');
  198. // XXX: check namespace
  199. if (in_array($tag, $empty_tag)) {
  200. $this->xw->endElement();
  201. } else {
  202. $this->xw->fullEndElement();
  203. }
  204. }
  205. /**
  206. * output plain text
  207. *
  208. * Text will be escaped. If you need it not to be,
  209. * use raw() instead.
  210. *
  211. * @param string $txt Text to output.
  212. *
  213. * @return void
  214. */
  215. function text($txt)
  216. {
  217. $this->xw->text($txt);
  218. }
  219. /**
  220. * output raw xml
  221. *
  222. * This will spit out its argument verbatim -- no escaping is
  223. * done.
  224. *
  225. * @param string $xml XML to output.
  226. *
  227. * @return void
  228. */
  229. function raw($xml)
  230. {
  231. $this->xw->writeRaw($xml);
  232. }
  233. /**
  234. * output a comment
  235. *
  236. * @param string $txt text of the comment
  237. *
  238. * @return void
  239. */
  240. function comment($txt)
  241. {
  242. $this->xw->writeComment($txt);
  243. }
  244. /**
  245. * Flush output buffers
  246. *
  247. * @return void
  248. */
  249. function flush()
  250. {
  251. $this->xw->flush();
  252. }
  253. }