[Phpxd-commits] CVS: phpXD/include/dom DOMWriter.php,NONE,1.1
Status: Beta
Brought to you by:
growbal
From: Growbal K. <gr...@us...> - 2002-03-03 06:23:21
|
Update of /cvsroot/phpxd/phpXD/include/dom In directory usw-pr-cvs1:/tmp/cvs-serv8265/include/dom Added Files: DOMWriter.php Log Message: Add DOMWriter.php sample_big5.xml dwtest.php Modify dom.php for including DOMWriter.php by Growbal --- NEW FILE: DOMWriter.php --- <?php // phpXMLDOM - an XML DOM Implementation // // This Software and all associated files are released unter the // GNU Public License (GPL), see LICENSE for details. // // $Id: DOMWriter.php,v 1.1 2002/03/03 06:23:16 growbal Exp $ /** * Class representing the DOMWriter interface. * * * @package phpXD * @author Growbal Kuo <gr...@ta...> * @author Thomas Dohmke <th...@do...> * @version $Revision: 1.1 $ */ class DOMWriter { /** * An array for saving DOMWriter's names features.<br> * array keys are feature names. array values are feature states(true or false).<br> * * DOM-Level 3 * * @private * @type array */ var $namedFeatures; /** * The character encoding in which the output will be written.<br> * The encoding to use when writing is determined as follows: <br> * <li>If the encoding attribute has been set, that value will be used. <br> * <li>If the encoding attribute is null or empty, but the item to be <br> * written includes an encoding declaration, that value will be used. <br> * <li>If neither of the above provides an encoding name, a default encoding <br> * of "UTF-8" will be used. <br> * DOM-Level 3 * * @public * @type string */ var $encoding; /** * The actual character encoding that was last used by this formatter.<br> * This convenience method allows the encoding that was used <br> * when serializing a document to be directly obtained.<br> * Readonly<br> * * DOM-Level 3 * * @private * @type string */ var $lastEncoding; /** * The error handler that will receive error notifications <br> * during serialization. The node where the error occured is <br> * passed to this error handler, any modification to nodes <br> * from within an error callback should be avoided since this<br> * will result in undefined, implementation dependent behavior.<br> * * DOM-Level 3 * * @public * @type string */ var $errorHandler; /** * The end-of-line sequence of characters to be used in<br> * the XML being written out. The only permitted values are these: <br> * <li> null<br> * <li> CR <br> * <li> CR-LF <br> * <li> LF <br> * * DOM-Level 3 * * @public * @type string */ var $newLine; /** * Constructor of the class.<br> * * @public */ function DOMWriter() { $this->namedFeatures = array( "normalize-characters" => true, "split-cdata-sections" => true, "validation" => false, "expand-entity-references" => false, "whitespace-in-element-content" => true, "discard-default-content" => true, "format-canonical" => false, "format-pretty-print" => false ); $this->encoding = "UTF-8"; $this->$newLine = "\n"; //LF } /** * Returns true if the feature could be successfully set to the specified value.<br> * DOM-Level 3 * * @public * @param $name <code>string</code> * @param $state <code>boolean</code> * @returns void */ function setFeature($name, $state) { if ($this->canSetFeatureName($name)) if ($this->canSetFeature($name, $state)) { $this->namedFeatures[$name] = $state; if ($name=="format-pretty-print") { $this->namedFeatures["format-canonical"] = ! $state; } else if ($name=="format-canonical") { $this->namedFeatures["format-pretty-print"] = ! $state; } } else return NOT_SUPPORTED_ERR; else return NOT_FOUND_ERR; } /** * Returns true if the feature could be successfully set.<br> * * @private * @param $name <code>string</code> * @returns boolean */ function canRecognizeFeatureName($name) { $ret = in_array($name, $this->namedFeatures); return $ret; } /** * Returns true if the feature could be successfully set to the specified value.<br> * DOM-Level 3 * * @public * @param $name <code>string</code> * @param $state <code>boolean</code> * @returns boolean */ function canSetFeature($name, $state) { $canSetState = ($state===true || $state===false); return canRecognizeFeatureName($name) && $canSetState; } /** * Returns the current state of the feature (true or false).<br> * DOM-Level 3 * * @public * @param $name <code>string</code> * @returns boolean */ function getFeature($name) { if ($this->canRecognizeFeatureName($name)) return $this->namedFeatures[$name]; else return NOT_FOUND_ERR; } /** * Write out the specified node in the description of DOMWriter. <br> * * Destination can be in these PHP supported output: * <li> "/home/rasmus/file.xml" * <li> "ftp://user:pas...@ex.../file.xml" * <li> "php://stdout", or "php://stderr" * * On the Windows platform, be careful to escape any backslashes <br> * used in the path to the file, or use forward slashes: * "c:\\data\\file.xml" * * * DOM-Level 3 * * @public * @param $destination <code>string</code> * @param $wnode <code>Node</code> * @returns boolean */ function writeNode($destination, $wnode) { $ok = true; if ($fp = fopen($destination, "w")) { $text = ""; $this->writeToString2($wnode, false, 0, $text); if (! fwrite($fp, $text) >=0 ) $ok = false; fclose($fp); } else $ok = false; return $ok; } /** * Serialize the specified node in the description of DOMWriter.<br> * DOM-Level 3 * * @public * @param $wnode <code>Node</code> * @returns string */ function writeToString($wnode) { $str = ""; $this->writeToString2($wnode, $trim, 0, $str); return $str; } /** * Print indents for pretty format<br> * * * @private * @param $indent <code>int</code> * @returns string */ function printIndent($indent) { $ret = ""; //if ($this->getFeature("format-pretty-print")) { for ($i=0; $i<$indent; $i++) { $ret .= "\t"; } //} return $ret; } /** * Serialize the specified node in the description of DOMWriter.<br> * * * @private * @param $dom <code>Node</code> * @param $trim <code>boolean</code> * @param $indent <code>string</code> * @param &$text <code>string</code> * @returns void */ function writeToString2(&$dom, $trim, $indent, &$text) { $newLine=$this->$newLine; $indentStr = $this->printIndent($indent); if ($dom->nodeType == CDATA_SECTION_NODE) { $text .= $indentStr."<![CDATA["; $text .= $dom->getData(); $text .= $indentStr."]]>".$newLine; } if ($dom->nodeType == COMMENT_NODE) { $text .= $indentStr."<!--"; $text .= $dom->getData(); $text .= $indentStr."-->".$newLine; } if ($dom->nodeType == DOCUMENT_NODE) { $text =""; $encodingDecl = sprintf(' encoding="%s"', $this->encoding); // $SDDecl = ""; $text .= sprintf('<?xml version="1.0"%s?>', $encodingDecl).$newLine; if ($dom->hasChildNodes()) { $this->writeToString2($dom->firstChild, $trim, $indent, $text); } } if ($dom->nodeType == DOCUMENT_FRAGMENT_NODE) { $this->writeToString2($dom->firstChild, $trim, $text); } if ($dom->nodeType == ELEMENT_NODE) { $text .= $indentStr."<".$dom->tagName; if (isset($dom->attributes)) { for ($i = 0; $i < $dom->attributes->getLength(); $i++) { $elem =& $dom->attributes->item($i); if ($elem->specified) { $text .= " ".$elem->getName()."=\"".$elem->getValue()."\""; } } } if ($dom->hasChildNodes()) { $text .= ">".$newLine; $this->writeToString2($dom->firstChild, $trim, $indent+1, $text); $text .= $indentStr."</".$dom->tagName.">".$newLine; } else { $text .= " />".$newLine; } } if ($dom->nodeType == ENTITY_REFERENCE_NODE) { if ($dom->hasChildNodes()) { $this->writeToStrring2($dom->firstChild, $text,$indent, $trim); } } if ($dom->nodeType == PROCESSING_INSTRUCTION_NODE) { $output = $dom->getData(); if ($trim) { $output = str_replace("\t", " ", $output); $output = preg_replace('= =', ' ', $output); $output = str_replace("\n ", "\n", $output); } $text .= $indentStr."<?".$dom->getTarget()." "; $text .= $output; $text .= "?>".$newLine; } if ($dom->nodeType == TEXT_NODE) { $output = trim($dom->getData()); if ($output!="") { //$output = ereg_replace("^[\n\t ](.*)[\n\t ]$", "\\1",$output); $output = preg_replace("/^\s(.*)\s$/", "$1",$output); if ($trim) { $output = str_replace("\t", " ", $output); $output = preg_replace('= =', ' ', $output); $output = str_replace("\n ", "\n", $output); $text .= $indentStr.$output.$newLine; } else { $text .= $indentStr.$output.$newLine; } } } if (isset($dom->nextSibling)) { $this->writeToString2($dom->nextSibling, $trim, $indent, $text); } } } |