1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
<?php
/**
* Creates XML documents.
*
* PHPDoc uses this helper class to generate xml documents. It's
* not much what this class can do but it provides some simple
* functions to handle attributes and hides file handling tasks.
*
* @author Ulf Wendel <ulf.wendel@phpdoc.de>
* @version 1.0
*/
class PhpdocXMLWriter extends PhpdocObject {
/**
* Generated XML document.
*
* @var string $xml
*/
var $xml = "";
/**
* PHPDoc Warning object
*
* @var object PhpdocWarning
*/
var $warn;
/**
* Filehandler used for IO operations
*
* @var object PhpdocFilehandler
* @see PhpdocXMLWriter
*/
var $fileHandler;
/**
* Creates a new PhpdocFileHandler
*
* @see $filehandler
*/
function PhpdocXMLWriter() {
$this->fileHandler = new PhpdocFileHandler;
} // end constructor
/**
* Clears the internal xml data buffer so that a new document can be passed to the object.
* @access public
*/
function free() {
$this->xml = "";
} // end func free
/**
* Adds xml to the generated xml.
*
* @param string xml to append
* @access public
*/
function addXML($xml) {
$this->xml.= $xml;
} // end func addXML
/**
* Saves the xml to the specified file.
*
* @param string Name of the target file
* @access public
*/
function export($filename) {
return $this->fileHandler->createFile($filename, $this->xml);
} // end func export
/**
* Adds an open (or single) xml tag to the generated xml.
*
* Use this function to add new elements/tags to the xml document.
* The tagname and all attributenames will be converted to lowercase.
*
* @param string elementname (tagname)
* @param string value of the container: <name>value
* @param array Array of attributes: $attribs[n][type] = boolean|cdata, $attribs[n][value] = value
* @param boolean Flag indication that you want an empty tag like <name/>.
* @access public
* @see endElement()
*/
function startElement($name, $value="", $attribs="", $close = false) {
$xml = "<".strtolower($name);
if (is_array($attribs)) {
reset($attribs);
while (list($attrib, $data)=each($attribs)) {
$attrib = strtolower($attrib);
$type = strtolower($data["type"]);
switch($type) {
case "boolean":
$xml.= sprintf(' %s="%s"', $attrib, ($data["value"]) ? "true" : "false");
break;
case "cdata":
$xml.= sprintf(' %s="%s"', $attrib, $this->xmlencode($data["value"]) );
break;
}
}
}
if ($close) {
$xml.= "/>";
} else {
$xml.= ">";
if (""!=$value)
$xml.= $this->xmlencode($value);
}
$this->xml.= $xml;
} // end func startElement
/**
* Adds a closing xml tag to the generated xml document.
*
* @param string Elementname (tagname)
* @access public
* @see startElement()
*/
function endElement($name) {
$this->xml.= sprintf("</%s>", strtolower($name) );
} // end func endElement
/**
* Adds a complete xml container to the generated xml document.
*
* @param string Elementname (tagname)
* @param string Value
* @param array Attributes
* @access public
* @see startElement(), endElement()
*/
function addElement($name, $value="", $attribs="") {
if (""==$value) {
$this->startElement($name, $value, $attribs, true);
} else {
$this->startElement($name, $value, $attribs, false);
$this->endElement($name);
}
} // end func addElement
/**
* Encodes XML values.
* @param string $value
* @return string $value
*/
function xmlencode($value) {
# return preg_replace( array("@<@", "@>@", "@'@", '@"@', "@&@", "@".LINEBREAK."@", "@\n@", "@\r@"), array("<", ">", "'", """, "&", '
', '
', '
'), $value);
return utf8_encode(preg_replace( array("@<@", "@>@", "@'@", '@"@', "@&@", "@".LINEBREAK."@", "@\n@", "@\r@"), array("<", ">", "'", """, "&", '
', '
', '
'), $value));
} // end func xmlencode
} // end class PhpdocXMLWriter
?>
|