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