PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/xmloutputter.php

https://github.com/Br3nda/laconica
PHP | 242 lines | 68 code | 22 blank | 152 comment | 8 complexity | 03fe5af1d27351a11d528491c85faa10 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /**
  3. * Laconica, 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 Laconica
  24. * @author Evan Prodromou <evan@controlyourself.ca>
  25. * @author Sarven Capadisli <csarven@controlyourself.ca>
  26. * @copyright 2008 Control Yourself, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://laconi.ca/
  29. */
  30. if (!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 Laconica
  42. * @author Evan Prodromou <evan@controlyourself.ca>
  43. * @author Sarven Capadisli <csarven@controlyourself.ca>
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://laconi.ca/
  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=true)
  65. {
  66. $this->xw = new XMLWriter();
  67. $this->xw->openURI($output);
  68. $this->xw->setIndent($indent);
  69. }
  70. /**
  71. * Start a new XML document
  72. *
  73. * @param string $doc document element
  74. * @param string $public public identifier
  75. * @param string $system system identifier
  76. *
  77. * @return void
  78. */
  79. function startXML($doc=null, $public=null, $system=null)
  80. {
  81. $this->xw->startDocument('1.0', 'UTF-8');
  82. if ($doc) {
  83. $this->xw->writeDTD($doc, $public, $system);
  84. }
  85. }
  86. /**
  87. * finish an XML document
  88. *
  89. * It's probably a bad idea to continue to use this object
  90. * after calling endXML().
  91. *
  92. * @return void
  93. */
  94. function endXML()
  95. {
  96. $this->xw->endDocument();
  97. $this->xw->flush();
  98. }
  99. /**
  100. * output an XML element
  101. *
  102. * Utility for outputting an XML element. A convenient wrapper
  103. * for a bunch of longer XMLWriter calls. This is best for
  104. * when an element doesn't have any sub-elements; if that's the
  105. * case, use elementStart() and elementEnd() instead.
  106. *
  107. * The $content element will be escaped for XML. If you need
  108. * raw output, use elementStart() and elementEnd() with a call
  109. * to raw() in the middle.
  110. *
  111. * If $attrs is a string instead of an array, it will be treated
  112. * as the class attribute of the element.
  113. *
  114. * @param string $tag Element type or tagname
  115. * @param array $attrs Array of element attributes, as
  116. * key-value pairs
  117. * @param string $content string content of the element
  118. *
  119. * @return void
  120. */
  121. function element($tag, $attrs=null, $content=null)
  122. {
  123. $this->elementStart($tag, $attrs);
  124. if (!is_null($content)) {
  125. $this->xw->text($content);
  126. }
  127. $this->elementEnd($tag);
  128. }
  129. /**
  130. * output a start tag for an element
  131. *
  132. * Mostly used for when an element has content that's
  133. * not a simple string.
  134. *
  135. * If $attrs is a string instead of an array, it will be treated
  136. * as the class attribute of the element.
  137. *
  138. * @param string $tag Element type or tagname
  139. * @param array $attrs Array of element attributes
  140. *
  141. * @return void
  142. */
  143. function elementStart($tag, $attrs=null)
  144. {
  145. $this->xw->startElement($tag);
  146. if (is_array($attrs)) {
  147. foreach ($attrs as $name => $value) {
  148. $this->xw->writeAttribute($name, $value);
  149. }
  150. } else if (is_string($attrs)) {
  151. $this->xw->writeAttribute('class', $attrs);
  152. }
  153. }
  154. /**
  155. * output an end tag for an element
  156. *
  157. * Used in conjunction with elementStart(). $tag param
  158. * should match the elementStart() param.
  159. *
  160. * For HTML 4 compatibility, this method will force
  161. * a full end element (</tag>) even if the element is
  162. * empty, except for a handful of exception tagnames.
  163. * This is a hack.
  164. *
  165. * @param string $tag Element type or tagname.
  166. *
  167. * @return void
  168. */
  169. function elementEnd($tag)
  170. {
  171. static $empty_tag = array('base', 'meta', 'link', 'hr',
  172. 'br', 'param', 'img', 'area',
  173. 'input', 'col');
  174. // XXX: check namespace
  175. if (in_array($tag, $empty_tag)) {
  176. $this->xw->endElement();
  177. } else {
  178. $this->xw->fullEndElement();
  179. }
  180. }
  181. /**
  182. * output plain text
  183. *
  184. * Text will be escaped. If you need it not to be,
  185. * use raw() instead.
  186. *
  187. * @param string $txt Text to output.
  188. *
  189. * @return void
  190. */
  191. function text($txt)
  192. {
  193. $this->xw->text($txt);
  194. }
  195. /**
  196. * output raw xml
  197. *
  198. * This will spit out its argument verbatim -- no escaping is
  199. * done.
  200. *
  201. * @param string $xml XML to output.
  202. *
  203. * @return void
  204. */
  205. function raw($xml)
  206. {
  207. $this->xw->writeRaw($xml);
  208. }
  209. /**
  210. * output a comment
  211. *
  212. * @param string $txt text of the comment
  213. *
  214. * @return void
  215. */
  216. function comment($txt)
  217. {
  218. $this->xw->writeComment($txt);
  219. }
  220. }