xmlutils.php File Reference


Detailed Description

XML helper utilities.

If you need to output elements, never do simple string replacement and then <xsl:value-of disable-output-escaping='yes' ... /> ! It's evil and you will definitely at least once forget to properly escape it. Used these routines instead and then <xsl:copy-of ... />.

Note that code in this file is real WTF, but I was not able to create anything simpler without loosing flexibility. Tell me if you have some good idea how to make this code shorter or more readable. Otherwise you can try to send this code to http://www.thedailywtf.com :-)

Definition in file xmlutils.php.

Go to the source code of this file.

Functions

 regex_splitter ($doc, $text, $regex, $elemcreator)
 Helper function for the xml_convert_any2elem() function.
 str_splitter ($doc, $text, $str, $elemcreator)
 Helper function for the xml_convert_any2elem() function.
 xml_convert_any2elem ($doc, $text, $str, $elemcreator, $splitter, $recurse=FALSE)
 Converts string to an array of DOMNode descendants.
 xml_convert_regex2elem ($doc, $text, $regex, $elemcreator, $recurse=FALSE)
 Shortcut for calling xml_convert_any2elem() with regex_splitter().
 xml_convert_str2elem ($doc, $text, $str, $elemcreator, $recurse=FALSE)
 Shortcut for calling xml_convert_any2elem() with str_splitter().


Function Documentation

regex_splitter doc,
text,
regex,
elemcreator
 

Helper function for the xml_convert_any2elem() function.

It splits the string into an array of DOMText and DOMElement (or other DOMNode descendant) instances.

Parameters:
[in] $doc DOMDocument instance.
[in] $text String which is going to be converted.
[in] $regex Regular expression used for splitting the string. Note that if you need to use rounding brackets inside of the regex you should mark them non-capturing (?:) otherwise strange things will happen.
[in] $elemcreator Instance of ElementCreator class.
Returns:
Array of DOMNodes, there is always at least one DOMText instance.

Definition at line 183 of file xmlutils.php.

Referenced by xml_convert_regex2elem().

00183                                                              {
00184     $text_parts = preg_split($regex, $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
00185     $result = array();
00186     foreach ($text_parts as $text_part):
00187       if (preg_match($regex, $text_part)):
00188         $result[] = $elemcreator->create_element($doc, $text_part);
00189       else:
00190         $result[] = $doc->createTextNode($text_part);
00191       endif;
00192     endforeach;
00193     return $result;
00194   }

str_splitter doc,
text,
str,
elemcreator
 

Helper function for the xml_convert_any2elem() function.

It splits the string into an array of DOMText and DOMElement (or other DOMNode descendant) instances.

Parameters:
[in] $doc DOMDocument instance.
[in] $text String which is going to be converted.
[in] $str Separator.
[in] $elemcreator Instance of ElementCreator class.
Returns:
Array of DOMNodes, there is always at least one DOMText instance.

Definition at line 150 of file xmlutils.php.

Referenced by xml_convert_str2elem().

00150                                                          {
00151     $text_parts = explode($str, $text);
00152     $first = TRUE;
00153     $result = array();
00154     foreach ($text_parts as $text_part):
00155       if ($first):
00156         $first = FALSE;
00157       else:
00158         $result[] = $elemcreator->create_element($doc);
00159       endif;
00160       if(strlen($text_part) > 0):
00161         $result[] = $doc->createTextNode($text_part);
00162       endif;
00163     endforeach;
00164     return $result;
00165   }

xml_convert_any2elem doc,
text,
str,
elemcreator,
splitter,
recurse = FALSE
 

Converts string to an array of DOMNode descendants.

Parameters:
[in] $doc DOMDocument instance.
[in] $text String which is going to be converted. It can be also array of DOMNode instances (so you can call this function on a value returned by another call to this function).
[in] $str Anything (e.g. plain string or regex) which will be used for splitting the text. This value will be passed to the splitter function.
[in] $elemcreator Instance of ElementCreator class or string which will be wrapped into a ElementCreator instance (the string will be used as an elements name).
[in] $splitter Function which will be used to split the string. See str_splitter() and regex_splitter().
[in] $recurse If is set to true than also text nodes which are children of existing elements are processed. Defaults to false.
Returns:
Array of DOMNodes, there is always at least one DOMText instance.

Definition at line 218 of file xmlutils.php.

Referenced by xml_convert_regex2elem(), and xml_convert_str2elem().

00218                                                                                               {
00219     if (is_string($elemcreator)):
00220       $elemcreator = new ElementCreator($elemcreator);
00221     endif;
00222 
00223     if (is_array($text)):
00224       $result = array();
00225       foreach ($text as $part):
00226         $result = array_merge($result, xml_convert_any2elem($doc, $part, $str, $elemcreator, $splitter, $recurse));
00227       endforeach;
00228       return $result;
00229     endif;
00230     
00231     if ($text instanceof DomText):
00232       return xml_convert_any2elem($doc, $text->wholeText, $str, $elemcreator, $splitter, $recurse);
00233     endif;
00234 
00235     if ($text instanceof DomElement):
00236 
00237       // recursion means that we are converting also content of already
00238       // created elements
00239       // we will convert childnodes, result of their conversion put into
00240       // an array and then replace childnodes with it
00241       if ($recurse):
00242         $result = array();
00243         foreach ($text->childNodes as $childNode):
00244 
00245           // text nodes convert
00246           if ($childNode instanceof DomText):
00247             $result = array_merge($result, xml_convert_any2elem($doc, $childNode, $str, $elemcreator, $splitter, $recurse));
00248 
00249           // element nodes recurse
00250           elseif ($childNode instanceof DomElement):
00251             xml_convert_any2elem($doc, $childNode, $str, $elemcreator, $splitter, $recurse);
00252             $result[] = $childNode;
00253             
00254           // other nodes (e.g. attributes) copy
00255           else:
00256             assert($childNode instanceof DomNode);
00257             $result[] = $childNode;
00258           endif;
00259 
00260         endforeach;
00261         
00262         // now remove existing childnodes ...
00263         $node_list = clone $text->childNodes;
00264         foreach ($node_list as $childNode):
00265           $text->removeChild($childNode);
00266         endforeach;
00267         
00268         // ... and add new ones
00269         foreach ($result as $newChild):
00270           assert($newChild instanceof DomNode);
00271           $text->appendChild($newChild);
00272         endforeach;
00273         
00274       endif;
00275 
00276       // we have to return ourselves for a case that DomElement
00277       // is in the array. see the "if (is_array($text))" part to know why
00278       // is this needed
00279       return array($text);
00280     endif;
00281     
00282     assert(is_string($text));
00283 
00284     if (strlen($text) == 0):
00285       return array($doc->createTextNode(''));
00286     endif;
00287 
00288     return $splitter($doc, $text, $str, $elemcreator);
00289   }

xml_convert_regex2elem doc,
text,
regex,
elemcreator,
recurse = FALSE
 

Shortcut for calling xml_convert_any2elem() with regex_splitter().

Definition at line 301 of file xmlutils.php.

References regex_splitter(), and xml_convert_any2elem().

Referenced by link2aXML().

00301                                                                                        {
00302     return xml_convert_any2elem($doc, $text, $regex, $elemcreator, 'regex_splitter', $recurse);
00303   }

xml_convert_str2elem doc,
text,
str,
elemcreator,
recurse = FALSE
 

Shortcut for calling xml_convert_any2elem() with str_splitter().

Definition at line 294 of file xmlutils.php.

References str_splitter(), and xml_convert_any2elem().

Referenced by email(), and nl2brXML().

00294                                                                                    {
00295     return xml_convert_any2elem($doc, $text, $str, $elemcreator, 'str_splitter', $recurse);
00296   }


Generated on Sat Sep 24 01:26:42 2005 for Easy PHP Framework by  doxygen 1.4.2