xmloutputter.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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, if null it defaults to stdout ('php://output')
  62. * @param boolean $indent Whether to indent output, default true
  63. */
  64. 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. 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. 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 $attrs Array of element attributes, as
  122. * key-value pairs
  123. * @param string $content string content of the element
  124. *
  125. * @return void
  126. */
  127. function element($tag, $attrs=null, $content=null)
  128. {
  129. $this->elementStart($tag, $attrs);
  130. if (!is_null($content)) {
  131. $this->xw->text($content);
  132. }
  133. $this->elementEnd($tag);
  134. }
  135. function elementNS(array $ns, $tag, $attrs=null, $content=null)
  136. {
  137. $this->elementStartNS($ns, $tag, $attrs);
  138. if (!is_null($content)) {
  139. $this->xw->text($content);
  140. }
  141. $this->elementEnd($tag);
  142. }
  143. /**
  144. * output a start tag for an element
  145. *
  146. * Mostly used for when an element has content that's
  147. * not a simple string.
  148. *
  149. * If $attrs is a string instead of an array, it will be treated
  150. * as the class attribute of the element.
  151. *
  152. * @param string $tag Element type or tagname
  153. * @param array $attrs Array of element attributes
  154. *
  155. * @return void
  156. */
  157. function elementStart($tag, $attrs=null)
  158. {
  159. $this->xw->startElement($tag);
  160. if (is_array($attrs)) {
  161. foreach ($attrs as $name => $value) {
  162. $this->xw->writeAttribute($name, $value);
  163. }
  164. } else if (is_string($attrs)) {
  165. $this->xw->writeAttribute('class', $attrs);
  166. }
  167. }
  168. function elementStartNS(array $ns, $tag, $attrs=null)
  169. {
  170. reset($ns); // array pointer to 0
  171. $uri = key($ns);
  172. $this->xw->startElementNS($ns[$uri], $tag, $uri);
  173. if (is_array($attrs)) {
  174. foreach ($attrs as $name => $value) {
  175. $this->xw->writeAttribute($name, $value);
  176. }
  177. } else if (is_string($attrs)) {
  178. $this->xw->writeAttribute('class', $attrs);
  179. }
  180. }
  181. /**
  182. * output an end tag for an element
  183. *
  184. * Used in conjunction with elementStart(). $tag param
  185. * should match the elementStart() param.
  186. *
  187. * For HTML 4 compatibility, this method will force
  188. * a full end element (</tag>) even if the element is
  189. * empty, except for a handful of exception tagnames.
  190. * This is a hack.
  191. *
  192. * @param string $tag Element type or tagname.
  193. *
  194. * @return void
  195. */
  196. function elementEnd($tag)
  197. {
  198. static $empty_tag = array('base', 'meta', 'link', 'hr',
  199. 'br', 'param', 'img', 'area',
  200. 'input', 'col', 'source');
  201. // XXX: check namespace
  202. if (in_array($tag, $empty_tag)) {
  203. $this->xw->endElement();
  204. } else {
  205. $this->xw->fullEndElement();
  206. }
  207. }
  208. /**
  209. * output plain text
  210. *
  211. * Text will be escaped. If you need it not to be,
  212. * use raw() instead.
  213. *
  214. * @param string $txt Text to output.
  215. *
  216. * @return void
  217. */
  218. function text($txt)
  219. {
  220. $this->xw->text($txt);
  221. }
  222. /**
  223. * output raw xml
  224. *
  225. * This will spit out its argument verbatim -- no escaping is
  226. * done.
  227. *
  228. * @param string $xml XML to output.
  229. *
  230. * @return void
  231. */
  232. function raw($xml)
  233. {
  234. $this->xw->writeRaw($xml);
  235. }
  236. /**
  237. * output a comment
  238. *
  239. * @param string $txt text of the comment
  240. *
  241. * @return void
  242. */
  243. function comment($txt)
  244. {
  245. $this->xw->writeComment($txt);
  246. }
  247. /**
  248. * Flush output buffers
  249. *
  250. * @return void
  251. */
  252. function flush()
  253. {
  254. $this->xw->flush();
  255. }
  256. }