Source for file xml-defs.php

Documentation is available at xml-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: xml-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Basic definitions pertaining to XML processing. */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package xml *//** The XML tag class. This class encapsulates the functionality of
  27. * an XML tag. It can accept child classes, usually of the same class,
  28. * which will be rendered as child XML elements. A whole XML hierarchy
  29. * can thus be represented in a single xmltag object.
  30. * @package xml
  31. */
  32. class xmltag {
  33. /** Name of the XML tag */
  34.  
  35. var $tagname;
  36. /** Value of this XML tag */
  37.  
  38. var $value;
  39. /** Array of tag attributes */
  40.  
  41. var $attributes = array();
  42. /** Array of child tags of this tag */
  43.  
  44. var $childtags = array();
  45. /** If "cdata", we use CDATA to encode the value string */
  46.  
  47. var $encode_with = "cdata";
  48. // .....................................................................
  49. /** Constructor
  50. * @param $name Name of this XML tag
  51. * @param $value Optional value for this XML tag
  52. */
  53. function xmltag($name, $value="", $encode_with="cdata") {
  54. $this->tagname = $name;
  55. $this->encode_with = $encode_with;
  56. if ($value != "") {
  57. $this->value = $value;
  58. }
  59. } // xmltag
  60. // .....................................................................
  61. /** Set a tag attribute key=value pair.
  62. * @param $name Name of the tag attribute as in name="value"
  63. * @param $value Value of the tag attribute
  64. */
  65. function setattribute($name, $value) {
  66. $this->attributes[$name] = $value;
  67. } // setattribute
  68. // .....................................................................
  69. /** Add a child tag.
  70. * @param $tag XMl tag object to add to this tag as a child
  71. */
  72. function childtag($tag) {
  73. if (is_object($tag)) {
  74. $this->childtags[] = $tag;
  75. }
  76. } // childtag
  77. // .....................................................................
  78. /** Render the tag as XML.
  79. * @return string The XML for this tag
  80. */
  81. function render($indent=0) {
  82. $s = "";
  83. $margin = str_repeat(" ", $indent);
  84. $s .= "$margin<$this->tagname";
  85. if (isset($this->attributes) && count($this->attributes) > 0) {
  86. foreach ($this->attributes as $name => $value) {
  87. $s .= " $name=\"$value\"";
  88. }
  89. }
  90. if (isset($this->value)) {
  91. $tagval = ($this->encode_with == "cdata") ? xmldata($this->value) : $this->value;
  92. $s .= ">$tagval</" . $this->tagname . ">\n";
  93. }
  94. else {
  95. if (count($this->childtags) > 0) {
  96. $s .= ">\n";
  97. foreach ($this->childtags as $child) {
  98. $s .= $child->render($indent + 2);
  99. }
  100. $s .= "$margin</" . $this->tagname . ">\n";
  101. }
  102. else {
  103. // A simple singleton tag..
  104. $s .= "/>\n";
  105. }
  106. }
  107. return $s;
  108. } // render
  109.  
  110. } // xmltag class
  111. // ----------------------------------------------------------------------
  112.  
  113. /**
  114. * Class comprising the functionality of an XML parser. You should
  115. * build specific parsers by inheriting this class and building your
  116. * custom handler methods to handle the XML content.
  117. * @package xml
  118. */
  119. class xmlparser {
  120. /** Parser object */
  121.  
  122. var $parser;
  123. /** Error message string if any */
  124.  
  125. var $error_message = "";
  126. /** True if XML parsed correctly */
  127.  
  128. var $valid_xml = false;
  129. // .....................................................................
  130. /** Construct a new parser. */
  131.  
  132. function xmlparser() {
  133. $this->parser = xml_parser_create();
  134. xml_set_object($this->parser, &$this);
  135. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
  136. xml_set_element_handler($this->parser, "tag_open", "tag_close");
  137. xml_set_character_data_handler($this->parser, "cdata");
  138. } // xmlparser
  139. // .....................................................................
  140. /**
  141. * Parse the given XML document.
  142. * @param string $xml The content of the XML document to parse.
  143. * @return boolean True if the XML was parsed as valid
  144. */
  145. function parse($xml) {
  146. $this->error_message = "";
  147. $this->valid_xml = false;
  148. $xml = trim($xml);
  149. if ($xml != "") {
  150. $this->valid_xml = xml_parse($this->parser, $xml);
  151. if (!$this->valid_xml) {
  152. $errmsg = xml_error_string(xml_get_error_code($this->parser));
  153. $errline = xml_get_current_line_number($this->parser);
  154. $this->error_message = "XML error: $errmsg at line $errline\n";
  155. }
  156. }
  157. return $this->valid_xml;
  158. } // parse
  159. // .....................................................................
  160. /** Method invoked when a tag is opened */
  161.  
  162. function tag_open($parser, $tag, $attributes) {
  163. }
  164. // .....................................................................
  165. /** Method invoked when character data is available */
  166.  
  167. function cdata($parser, $cdata) {
  168. }
  169. // .....................................................................
  170. /** Method invoked when a tag is closed */
  171.  
  172. function tag_close($parser, $tag) {
  173. }
  174.  
  175. } // xmlparser class
  176. // ----------------------------------------------------------------------
  177.  
  178. /**
  179. * Returns a value for XML compliant data transfer. If the
  180. * value has any of the chars: "<", ">" or "&" in it, then
  181. * we enclose it in a CDATA construct.
  182. * @param string $val The value to return as an XML data value
  183. * @return string The XML-compliant data value
  184. */
  185. function xmldata($val) {
  186. if (is_bool($val)) {
  187. $val = ($val ? "true" : "false");
  188. }
  189. elseif (preg_match("/[<&>]/", $val)) {
  190. $val = "<![CDATA[$val]]>";
  191. }
  192. return $val;
  193. } // xmldata
  194. // ----------------------------------------------------------------------
  195.  
  196. /**
  197. * Returns the XML header string.
  198. * @param string $version The XML version to declare in the header.
  199. * @param string $encoding The XML encoding standard to declare.
  200. * @return string The <?xml...> header string.
  201. */
  202. function xmlheader($version="1.0", $encoding="utf-8") {
  203. return "<?xml version=\"$version\" encoding=\"$encoding\"?>\n";
  204. } // xmlheader
  205. // ----------------------------------------------------------------------
  206.  
  207. /**
  208. * Processes an assumed XML string to a format where it can be safely
  209. * viewed as a debugging dump in a browser et al.
  210. * @param string $xml The XML string to process
  211. * @return string The safe-to-display XML debugging representation
  212. */
  213. function xmldump($xml) {
  214. $xml = trim($xml);
  215. if ($xml != "") {
  216. $xml = preg_replace("/(<\!\[CDATA\[(.*)\]\]>)/", "\\2", $xml);
  217. $xml = htmlspecialchars($xml);
  218. }
  219. return $xml;
  220. } // xmldump
  221. // ----------------------------------------------------------------------
  222.  
  223. ?>

Documentation generated by phpDocumentor 1.3.0RC3