updates
Brought to you by:
bs_php,
nigelswinson
From: <php...@li...> - 2001-10-22 00:33:06
|
Hey Nigel, just saw you fixed the & bug, that's sweet. I wanted to suggest one other change to the export function that I forgot to mention...I am going to dump some old code I modified here (since you just made changes to export) but you will see what I am talking about. I am attempting to clean up the export of data when a tag has "mixed" children...notice how I added the parameter $useIndent...the point here is if the node has mixed children, it turns off the linefeeding and indenting so that when you read in a file, dump it, read it in, dump it, etc... you don't end up with tons of empty lines in your mixed data....try it out now by putting some <br/> tags inside of your text node and you will see what I am talking about...cruise through this code to see my idea about adding a "mixed" child checker...is is pretty basic what I did and seems to work 100%... Dan here it is ---------------------------------------------------------------------------- function _export($highlight = array(), $currentXpath = '', $level = 0, $output_as_xml = 0, $useIndent = TRUE) { // If you are having difficulty using this function. Then set this to TRUE and // you'll get diagnostic info displayed to the output. $b_debug_this_function = FALSE; if ($b_debug_this_function) { $a_start_time = $this->_beginDebugFunction("_export"); echo "Highlights:\n"; print_r($highlight); echo "Root: $currentXpath\n"; echo "Level: $level\n"; echo "Output As Xml: $output_as_xml\n\n"; } ////////////////////////////////////////////// // Create a string to save the generated XML data. $xml = ''; // Create two strings containing the tags for highlighting a node. static $highlight_start = '<font color="#FF0000"><b>'; static $highlight_end = '</b></font>'; // Check whether a root node is given. if (empty($currentXpath)) { // Set it to the document root. $currentXpath = $this->root; if ($b_debug_this_function) echo "Changing root to $this->root as it is empty\n"; } // Generate a string to be displayed before the tags. $before = ''; // Calculate the amount of whitespaces to display. for ( $i=0; $i<$level; $i++) { //Add a whitespaces to the string. $before .= ' '; } // If there is no node at $currentXpath then we have nothing to export. Quit now if (!isSet($this->nodes[$currentXpath])) { if ($b_debug_this_function) echo "No node at $currentXpath, returning null\n"; // Not completely sure yet if this is fatal, but I think it is. $this->_displayError("When exporting the class, the node at $currentXpath ". "was not found. This is probably due to previous internal corruption."); return ''; } $theNode = &$this->nodes[$currentXpath]; // Check whether the node is selected. $selected = empty($highlight) ? FALSE : in_array($currentXpath, $highlight); $hasChildren = (sizeOf($theNode['children'])>0) ? TRUE : FALSE; $hasText = ($this->_textlength($theNode['text'])) ? TRUE : FALSE; /** added by Dan Allen **/ $isMixed = ($hasText && $hasChildren) ? TRUE : FALSE; // Check whether the node is selected for highlight. if ($selected ) $xml .= $highlight_start; // Now open the tag adding the whitespaces to the XML data. if ($level>0 && $useIndent) $xml .= "\n" . $before; if (empty($theNode['name'])) { // If the node has no name, then ring alarm bells. $this->_displayError("When exporting the class, the node at '{$currentXpath}' ". "was found to have no node name.", __LINE__, FALSE); return ''; } $xml .= ($output_as_xml) ? '<' : '<'; $xml .= htmlspecialchars($theNode['name']); // Check whether there are attributes for this node. if (count($theNode['attributes']) > 0) { if ($b_debug_this_function) echo "Outputing the attributes\n"; // Run through all attributes. $highlighting = FALSE; reset($theNode['attributes']); while (list($key) = each($theNode['attributes'])) { // Check whether this attribute is highlighted. if (is_array($highlight) and in_array($currentXpath.'/attribute::'.$key, $highlight)) { // Add the highlight code to the XML data. $xml .= $highlight_start; $highlighting = TRUE; } // Add the attribute to the XML data. $xml .= ' '.$key.'="'.htmlspecialchars($theNode['attributes'][$key]).'"'; // Check whether this attribute is highlighted. if ($highlighting) { // Add the highlight code to the XML data. $xml .= $highlight_end; $highlighting = FALSE; } } } if (empty($theNode['text'])) { $mergedText = ""; } else { $mergedText = implode('', $theNode['text']); } $useShortEnd = (!$hasChildren && empty($mergedText)); // Check whether the node contains character data or has children. if ($useShortEnd) { // Add the end to the tag. $xml .= ($output_as_xml) ? '/>' : "/>"; } else { // Close the tag. $xml .= ($output_as_xml) ? '>' : '>'; } // Check whether the node is selected. if ($selected ) $xml .= $highlight_end; // Check whether the node has children or not. if (!$hasChildren) { $xml .= $mergedText; } else { // Run through all children in the order they where set. $childSize = sizeOf($theNode['children']); for ($i=0; $i<$childSize; $i++) { if (!empty($theNode['text'][$i])) $xml .= $theNode['text'][$i]; // Generate the full path of the child. $fullchild = $currentXpath.'/'.$theNode['children'][$i]; // Add the child's XML data to the existing data. /** Modified by Dan Allen **/ $xml .= $this->_export(&$highlight, $fullchild, $level + 1, $output_as_xml, !$isMixed); } // Add the text fagment after the chield node if (!empty($theNode['text'][$i])) $xml .= $theNode['text'][$i]; } // Check if we have to set a ending </foo> tag if (! $useShortEnd) { // Add the whitespaces to the XML data, but only if there were kids. /** modified by Dan Allen **/ if ($hasChildren && !$isMixed) { $xml .= "\n".$before; } // Check whether the node is selected. Add the highlight code to the XML data. if ($selected) $xml .= $highlight_start; Add the highlight code to the XML data. // Add the closing tag. $xml .= ($output_as_xml) ? '</' : '</'; $xml .= $theNode['name']; $xml .= ($output_as_xml) ? '>' : '>'; // Check whether the node is selected. Add the highlight code to the XML data. if ($selected) $xml .= $highlight_end; // Add a linebreak. // --sam do we need it?? $xml .= "\n"; } ////////////////////////////////////////////// if ($b_debug_this_function) { $this->_closeDebugFunction($a_start_time, $xml); } // Return the XML data. return $xml; } /** * Returns the total length of all the text nodes * * This method is used to determine if the text of a given node exists, it needs * to sum up all the elements of the array * * @author Daniel Allen <big...@ya...> * @param array $text_array Array, from which the length with be determined * @return int $length Int the total length of the text in the node * @see _export() */ function _textlength($text_array) { $length = 0; foreach($text_array as $text) { $length += strlen($text); } return $length; } ------------------------------------------------------- __________________________________________________ Do You Yahoo!? Make a great connection at Yahoo! Personals. http://personals.yahoo.com |