You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.9 KiB

  1. <?php
  2. /*
  3. This file is part of ActiveLink PHP XML Package (www.active-link.com).
  4. Copyright (c) 2002-2004 by Zurab Davitiani
  5. You can contact the author of this software via E-mail at
  6. hattrick@mailcan.com
  7. ActiveLink PHP XML Package is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as published by
  9. the Free Software Foundation; either version 2.1 of the License, or
  10. (at your option) any later version.
  11. ActiveLink PHP XML Package is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with ActiveLink PHP XML Package; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /*
  20. * requires XML, Tag and File classes
  21. */
  22. import("org.active-link.xml.XML");
  23. import("org.active-link.sys.File");
  24. import("org.active-link.xml.Tag");
  25. /**
  26. * XMLDocument class provides a document class for XML
  27. * @class XMLDocument
  28. * @package org.active-link.xml
  29. * @author Zurab Davitiani
  30. * @version 0.4.0
  31. * @extends File
  32. * @requires File, XML, Tag
  33. * @see XML
  34. */
  35. class XMLDocument extends File {
  36. // protected variables
  37. var $xml;
  38. var $tag;
  39. /**
  40. * If filename is set and fileOpenMode is one of the modes that allows file to be read then file is opened and its contents parsed
  41. * If filename is set and fileOpenMode is something other than above the appropriate file is opened/created
  42. * If filename is not set then no files are opened/parsed/created and object contains default values
  43. * @method XMLDocument
  44. * @param optional string filename
  45. * @param optional string fileOpenMode
  46. */
  47. function XMLDocument($filename = "", $fileOpenMode = "r") {
  48. $success = $this->File($filename, $fileOpenMode);
  49. $this->tag = new Tag();
  50. $this->tag->tagStartOpen = "<?";
  51. $this->tag->tagClose = "?>";
  52. if($this->connected && ($this->fileOpenMode == $this->fileOpenModeRead || $this->fileOpenMode == $this->fileOpenModeReadWrite)) {
  53. $fileContents = $this->getContents();
  54. $this->close();
  55. $this->parseFromString($fileContents);
  56. }
  57. else {
  58. $this->setDefaultXMLTag();
  59. $this->xml = new XML();
  60. }
  61. return $success;
  62. }
  63. /**
  64. * Returns the XML object containing actual XML tree; in PHP 4 make sure to use =& to get a reference instead of a copy
  65. * @method getXML
  66. * @returns object of type XML containing actual XML tree
  67. */
  68. function getXML() {
  69. return $this->xml;
  70. }
  71. /**
  72. * Returns the XML string of a complete XML document
  73. * @method getXMLString
  74. * @returns string containing contents of XML document
  75. */
  76. function getXMLString() {
  77. $xmlString = $this->tag->getTagString();
  78. $xmlString .= "\n\n";
  79. $xmlString .= $this->xml->getXMLString(0);
  80. return $xmlString;
  81. }
  82. /**
  83. * Parses XML document from supplied string, also called from constructor when parsing file contents
  84. * @method parseFromString
  85. * @param string XMLDocString
  86. * @returns none
  87. */
  88. function parseFromString($XMLDocString) {
  89. $tagPos = $this->tag->setTagFromString($XMLDocString);
  90. if($tagPos === false) {
  91. $tagPos = array(0 => 0, 1 => 0);
  92. $this->setDefaultXMLTag();
  93. }
  94. $xmlContents = trim(substr($XMLDocString, $tagPos[1]));
  95. $this->xml = new XML($xmlContents);
  96. }
  97. /**
  98. * Saves document contents to a supplied filename
  99. * @method save
  100. * @param string filename
  101. * @returns true if successful, false otherwise
  102. */
  103. function save($filename) {
  104. $success = $this->open($filename, $this->fileOpenModeWrite);
  105. if($success) {
  106. $bytesWritten = $this->write($this->getXMLString());
  107. if($bytesWritten <= 0)
  108. $success = false;
  109. $this->close();
  110. }
  111. return $success;
  112. }
  113. /**
  114. * (Re)sets XML version/encoding to default values
  115. * @method setDefaultXMLTag
  116. * @returns none
  117. */
  118. function setDefaultXMLTag() {
  119. $this->tag->setTagName("xml");
  120. $this->tag->setAttribute("version", "1.0");
  121. $this->tag->setAttribute("encoding", "UTF-8");
  122. }
  123. /**
  124. * Sets encoding of the XML document
  125. * @method setEncoding
  126. * @param string encoding
  127. * @returns none
  128. */
  129. function setEncoding($encoding) {
  130. $this->tag->setAttribute("encoding", $encoding);
  131. }
  132. /**
  133. * Sets version of the XML document
  134. * @method setVersion
  135. * @param string version
  136. * @returns none
  137. */
  138. function setVersion($version) {
  139. $this->tag->setAttribute("version", $version);
  140. }
  141. /**
  142. * Sets XML object of the XMLDocument, sets/changes/updates XML content to the supplied XML tree, uses reference no copy is created
  143. * @method setXML
  144. * @param object xml
  145. * @returns true if successful, false otherwise
  146. */
  147. function setXML(&$xml) {
  148. $success = false;
  149. if(gettype($xml) == "object" && strtolower(get_class($xml)) == "xml") {
  150. $this->xml = &$xml;
  151. $success = true;
  152. }
  153. return $success;
  154. }
  155. }