cvs: pear /XML_Parser Parser.php package.xml /XML_Parser/tests .cvsignore 001.phpt 002.phpt 003.phpt 004.phpt 005.phpt test2.xml test3.xml
| From: | Stig Bakken | Date: | Thu, 09 May 2002 12:38:41 +0000 |
| Subject: | cvs: pear /XML_Parser Parser.php package.xml /XML_Parser/tests .cvsignore 001.phpt 002.phpt 003.phpt 004.phpt 005.phpt test2.xml test3.xml | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-3722@lists.php.net to get a copy of this message | ||
ssb Thu May 9 08:38:41 2002 EDT
Added files:
/pear/XML_Parser Parser.php package.xml
/pear/XML_Parser/tests .cvsignore 001.phpt 002.phpt 003.phpt
004.phpt 005.phpt test2.xml test3.xml
Log:
* moving XML_Parser to /pear
Index: pear/XML_Parser/Parser.php +++ pear/XML_Parser/Parser.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Stig Bakken <ssb@fast.no> | // +----------------------------------------------------------------------+ // // $Id: Parser.php,v 1.1 2002/05/09 12:38:41 ssb Exp $ require_once "PEAR.php"; /** * XML Parser class. This is an XML parser based on PHP's "xml" extension, * based on the bundled expat library. * * @author Stig Bakken <ssb@fast.no> * @todo Tests that need to be made: * - error class * - mixing character encodings * - a test using all expat handlers * - options (folding, output charset) * - different parsing modes * * @notes - It requires PHP 4.0.4pl1 or greater * - From revision 1.17, the function names used by the 'func' mode * are in the format "xmltag_$elem", for example: use "xmltag_name" * to handle the <name></name> tags of your xml file. */ class XML_Parser extends PEAR { // {{{ properties /** * @var resource XML parser handle */ var $parser; /** * @var resource File handle if parsing from a file */ var $fp; /** * @var boolean Whether to do case folding */ var $folding = true; /** * @var string Mode of operation, one of "event" or "func" */ var $mode; /** * Mapping from expat handler function to class method. * * @var array */ var $handler = array( "character_data_handler" => "cdataHandler", "default_handler" => "defaultHandler", "processing_instruction_handler" => "piHandler", "unparsed_entitry_decl_handler" => "unparsedHandler", "notation_decl_handler" => "notationHandler", "external_entity_ref_handler" => "entityrefHandler" ); /** @var string source encoding */ var $srcenc; /** @var string target encoding */ var $tgtenc; /* * Use call_user_func when php >= 4.0.7 * @var boolean * @see setMode() */ var $use_call_user_func = true; // }}} // {{{ constructor /** * Creates an XML parser. * * @param string source charset encoding, use NULL (default) to use * whatever the document specifies * @param string how this parser object should work, "event" for * startelement/endelement-type events, "func" * to have it call functions named after elements * * @see xml_parser_create */ function XML_Parser($srcenc = null, $mode = "event", $tgtenc = null) { $this->PEAR('XML_Parser_Error'); if ($srcenc === null) { $xp = @xml_parser_create(); } else { $xp = @xml_parser_create($srcenc); } if (is_resource($xp)) { if ($tgtenc !== null) { if (!@xml_parser_set_option($xp, XML_OPTION_TARGET_ENCODING, $tgtenc)) { return $this->raiseError("invalid target encoding"); } } $this->parser = $xp; $this->setMode($mode); xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, $this->folding); } $this->srcenc = $srcenc; $this->tgtenc = $tgtenc; } // }}} // {{{ setMode() /** * Sets the mode and all handler. * * @param string * @see $handler */ function setMode($mode) { $this->mode = $mode; xml_set_object($this->parser, $this); switch ($mode) { case "func": // use call_user_func() when php >= 4.0.7 // or call_user_method() if not if (version_compare(phpversion(), '4.0.7', 'lt')) { $this->use_call_user_func = false; } else { $this->use_call_user_func = true; } xml_set_element_handler($this->parser, "funcStartHandler", "funcEndHandler"); break; case "event": xml_set_element_handler($this->parser, "startHandler", "endHandler"); break; } foreach ($this->handler as $xml_func => $method) if (method_exists($this, $method)) { $xml_func = "xml_set_" . $xml_func; $xml_func($this->parser, $method); } } // }}} // {{{ setInputFile() /** * Defines * * @param string Filename (full path) * @return resource fopen handle of the given file * @throws XML_Parser_Error * @see setInput(), parse() * @access public */ function setInputFile($file) { $fp = @fopen($file, "rb"); if (is_resource($fp)) { $this->fp = $fp; return $fp; } return $this->raiseError($php_errormsg); } // }}} // {{{ setInput() /** * Sets the file handle to use with parse(). * * @param resource fopen * @access public * @see parse(), setInputFile() */ function setInput($fp) { if (is_resource($fp)) { $this->fp = $fp; return true; } return $this->raiseError("not a file resource"); } // }}} // {{{ parse() /** * Central parsing function. * * @throws XML_Parser_Error * @return boolean true on success * @see parseString() * @access public */ function parse() { if (!is_resource($this->fp)) { return $this->raiseError("no input"); } while ($data = fread($this->fp, 2048)) { $err = $this->parseString($data, feof($this->fp)); if (PEAR::isError($err)) { fclose($this->fp); return $err; } } fclose($this->fp); return true; } // }}} // {{{ parseString() /** * Parses a string. * * @param string XML data * @param boolean ??? * @throws XML_Parser_Error * @return mixed true on success or a string with the xml parser error */ function parseString($data, $eof = false) { if (!xml_parse($this->parser, $data, $eof)) { $err = $this->raiseError($this->parser); xml_parser_free($this->parser); return $err; } return true; } // }}} // {{{ funcStartHandler() function funcStartHandler($xp, $elem, $attribs) { $func = 'xmltag_' . $elem; if (method_exists($this, $func)) { if ($this->use_call_user_func) { call_user_func(array(&$this, $func), $xp, $elem, $attribs); } else { call_user_method($func, $this, $xp, $elem, $attribs); } } } // }}} // {{{ funcEndHandler() function funcEndHandler($xp, $elem) { $func = 'xmltag_' . $elem . '_'; if (method_exists($this, $func)) { if ($this->use_call_user_func) { call_user_func(array(&$this, $func), $xp, $elem); } else { call_user_method($func, $this, $xp, $elem); } } } /** * * @abstract */ function StartHandler($xp, $elem, &$attribs) { return NULL; } /** * * @abstract */ function EndHandler($xp, $elem) { return NULL; } // }}} } class XML_Parser_Error extends PEAR_Error { // {{{ properties var $error_message_prefix = 'XML_Parser: '; // }}} // {{{ constructor() function XML_Parser_Error($msgorparser = 'unknown error', $code = 0, $mode = PEAR_ERROR_RETURN, $level = E_USER_NOTICE) { if (is_resource($msgorparser)) { $code = xml_get_error_code($msgorparser); $msgorparser = sprintf("%s at XML input line %d", xml_error_string($code), xml_get_current_line_number($msgorparser)); } $this->PEAR_Error($msgorparser, $code, $mode, $level); } // }}} } ?> Index: pear/XML_Parser/package.xml +++ pear/XML_Parser/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "package.dtd"> <package version="1.0"> <name>XML_Parser</name> <summary>XML parsing class based on PHP's bundled expat</summary> <description> This is an XML parser based on PHP's built-in xml extension. It supports two basic modes of operation: "func" and "event". In "func" mode, it will look for a function named after each element (xmltag_ELEMENT for start tags and xmltag_ELEMENT_ for end tags), and in "event" mode it uses a set of generic callbacks. </description> <license>PHP License</license> <maintainers> <maintainer> <user>ssb</user> <role>lead</role> <name>Stig Sæther Bakken</name> <email>stig@php.net</email> </maintainer> </maintainers> <release> <state>stable</state> <version>1.0</version> <date>2002-05-09</date> <notes>First independent release</notes> <filelist> <dir name="/" baseinstalldir="XML"> <file role="php">Parser.php</file> </dir> <file role="test">tests/001.phpt</file> <file role="test">tests/002.phpt</file> <file role="test">tests/003.phpt</file> <file role="test">tests/004.phpt</file> <file role="test">tests/005.phpt</file> <file role="test">tests/test2.xml</file> <file role="test">tests/test3.xml</file> </filelist> <deps> <dep type="php" rel="ge" version="4.0.4pl1" /> </deps> </release> </package> Index: pear/XML_Parser/tests/.cvsignore +++ pear/XML_Parser/tests/.cvsignore *.exp *.out *.php phpt.* Index: pear/XML_Parser/tests/001.phpt +++ pear/XML_Parser/tests/001.phpt --TEST-- XML Parser: parse simple string --SKIPIF-- <?php if (!extension_loaded("xml")) echo 'skip'; ?> --FILE-- <?php // // Test for: XML/Parser.php // Parts tested: - parser creation // - some handlers // - parse simple string // require_once "XML/Parser.php"; class __TestParser1 extends XML_Parser { function __TestParser1() { $this->XML_Parser(); } function startHandler($xp, $element, $attribs) { print "<$element"; reset($attribs); while (list($key, $val) = each($attribs)) { $enc = htmlentities($val); print " $key=\"$enc\""; } print ">"; } function endHandler($xp, $element) { print "</$element>\n"; } function cdataHandler($xp, $cdata) { print "<![CDATA[$cdata]]>"; } function defaultHandler($xp, $cdata) { } } error_reporting(1023); print "new __TestParser1 "; var_dump(get_class($o = new __TestParser1())); print "parseString "; var_dump($o->parseString("<?xml version='1.0' ?><root>foo</root>", 1)); ?> --EXPECT-- new __TestParser1 string(13) "__testparser1" parseString <ROOT><![CDATA[foo]]></ROOT> bool(true) Index: pear/XML_Parser/tests/002.phpt +++ pear/XML_Parser/tests/002.phpt --TEST-- XML Parser: parse from file --SKIPIF-- <?php if (!extension_loaded("xml")) echo 'skip'; ?> --FILE-- <?php // -*- C++ -*- // // Test for: XML/Parser.php // Parts tested: - parser creation // - some handlers // - parse from file // chdir (dirname(__FILE__)); require_once "XML/Parser.php"; class __TestParser2 extends XML_Parser { function __TestParser2() { $this->XML_Parser(); } function startHandler($xp, $element, $attribs) { print "<$element"; reset($attribs); while (list($key, $val) = each($attribs)) { $enc = htmlentities($val); print " $key=\"$enc\""; } print ">"; } function endHandler($xp, $element) { print "</$element>\n"; } function cdataHandler($xp, $cdata) { print "<![CDATA[$cdata]]>"; } function defaultHandler($xp, $cdata) { } } print "new __TestParser2 "; var_dump(get_class($o = new __TestParser2())); print "setInputFile "; var_dump($o->setInputFile("test2.xml")); print "parse "; var_dump($o->parse()); ?> --EXPECT-- new __TestParser2 string(13) "__testparser2" setInputFile resource(4) of type (stream) parse <ROOT><![CDATA[foo]]></ROOT> bool(true) Index: pear/XML_Parser/tests/003.phpt +++ pear/XML_Parser/tests/003.phpt --TEST-- XML Parser: parse from file resource --SKIPIF-- <?php if (!extension_loaded("xml")) echo 'skip'; ?> --FILE-- <?php // -*- C++ -*- // // Test for: XML/Parser.php // Parts tested: - parser creation // - some handlers // - parse from file resource // chdir (dirname(__FILE__)); require_once "XML/Parser.php"; class __TestParser3 extends XML_Parser { function __TestParser3() { $this->XML_Parser(); } function startHandler($xp, $element, $attribs) { print "<$element"; reset($attribs); while (list($key, $val) = each($attribs)) { $enc = htmlentities($val); print " $key=\"$enc\""; } print ">"; } function endHandler($xp, $element) { print "</$element>\n"; } function cdataHandler($xp, $cdata) { print "<![CDATA[$cdata]]>"; } function defaultHandler($xp, $cdata) { } } print "new __TestParser3 "; var_dump(get_class($o = new __TestParser3())); print "fopen "; var_dump($fp = fopen("test3.xml", "r")); print "setInput "; var_dump($o->setInput($fp)); print "parse "; var_dump($o->parse()); ?> --EXPECT-- new __TestParser3 string(13) "__testparser3" fopen resource(4) of type (stream) setInput bool(true) parse <ROOT><![CDATA[foo]]></ROOT> bool(true) Index: pear/XML_Parser/tests/004.phpt +++ pear/XML_Parser/tests/004.phpt --TEST-- XML Parser: error class --SKIPIF-- <?php if (!extension_loaded("xml")) echo 'skip'; ?> --FILE-- <?php // -*- C++ -*- // // Test for: XML/Parser.php // Parts tested: - parser error class // require_once "XML/Parser.php"; print "new XML_Parser "; var_dump(get_class($p = new XML_Parser())); $e = $p->parseString("<?xml version='1.0' ?><foo></bar>", true); if (PEAR::isError($e)) { printf("error message: %s\n", $e->getMessage()); } else { print "no error\n"; } ?> --EXPECT-- new XML_Parser string(10) "xml_parser" error message: XML_Parser: mismatched tag at XML input line 1 Index: pear/XML_Parser/tests/005.phpt +++ pear/XML_Parser/tests/005.phpt --TEST-- XML Parser: mixing character encodings --SKIPIF-- <?php if (!extension_loaded("xml")) echo 'skip'; ?> --FILE-- <?php // -*- C++ -*- // // Test for: XML/Parser.php // Parts tested: - mixing character encodings // // This is what we test: // 1 UTF-8 -> ISO-8859-1 // 2 UTF-8 -> US-ASCII // 3 ISO-8859-1 -> UTF-8 // 4 ISO-8859-1 -> US-ASCII // 5 US-ASCII -> UTF-8 // 6 US-ASCII -> ISO-8859-1 // require_once "XML/Parser.php"; class TestEncodings1 extends XML_Parser { var $output = ''; function TestEncodings1($to, $from) { $this->XML_Parser($from, 'event', $to); } function startHandler($xp, $elem, $attribs) { $this->output .= "<$elem>"; } function endHandler($xp, $elem) { $this->output .= "</$elem>"; } function cdataHandler($xp, $data) { $this->output .= $data; } function test($data) { // $this->output = ''; $this->parseString($data, true); return $this->output; } } $xml = "<?xml version='1.0' ?>"; $input = array( "UTF-8" => "<a>abcæøå</a>", "ISO-8859-1" => "<a>abcæøå</a>", "US-ASCII" => "<a>abcaoa</a>" ); $encodings = array_keys($input); foreach ($input as $srcenc => $string) { foreach ($encodings as $tgtenc) { if ($srcenc == $tgtenc) { continue; } print "Testing $srcenc -> $tgtenc: "; $p =& new TestEncodings1($tgtenc, $srcenc); $e = $p->test($input[$srcenc]); if (PEAR::isError($e)) { printf("OOPS: %s\n", $e->getMessage()); } else { var_dump($e); } } } ?> --EXPECT-- Testing UTF-8 -> ISO-8859-1: string(13) "<A>abcæøå</A>" Testing UTF-8 -> US-ASCII: string(13) "<A>abc???</A>" Testing ISO-8859-1 -> UTF-8: string(16) "<A>abcæøå</A>" Testing ISO-8859-1 -> US-ASCII: string(13) "<A>abc???</A>" Testing US-ASCII -> UTF-8: string(13) "<A>abcaoa</A>" Testing US-ASCII -> ISO-8859-1: string(13) "<A>abcaoa</A>" Index: pear/XML_Parser/tests/test2.xml +++ pear/XML_Parser/tests/test2.xml <?xml version='1.0' ?> <root>foo</root> Index: pear/XML_Parser/tests/test3.xml +++ pear/XML_Parser/tests/test3.xml <?xml version='1.0' ?> <root>foo</root>
Index: pear/XML_Parser/Parser.php +++ pear/XML_Parser/Parser.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Stig Bakken <ssb@fast.no> | // +----------------------------------------------------------------------+ // // $Id: Parser.php,v 1.1 2002/05/09 12:38:41 ssb Exp $ require_once "PEAR.php"; /** * XML Parser class. This is an XML parser based on PHP's "xml" extension, * based on the bundled expat library. * * @author Stig Bakken <ssb@fast.no> * @todo Tests that need to be made: * - error class * - mixing character encodings * - a test using all expat handlers * - options (folding, output charset) * - different parsing modes * * @notes - It requires PHP 4.0.4pl1 or greater * - From revision 1.17, the function names used by the 'func' mode * are in the format "xmltag_$elem", for example: use "xmltag_name" * to handle the <name></name> tags of your xml file. */ class XML_Parser extends PEAR { // {{{ properties /** * @var resource XML parser handle */ var $parser; /** * @var resource File handle if parsing from a file */ var $fp; /** * @var boolean Whether to do case folding */ var $folding = true; /** * @var string Mode of operation, one of "event" or "func" */ var $mode; /** * Mapping from expat handler function to class method. * * @var array */ var $handler = array( "character_data_handler" => "cdataHandler", "default_handler" => "defaultHandler", "processing_instruction_handler" => "piHandler", "unparsed_entitry_decl_handler" => "unparsedHandler", "notation_decl_handler" => "notationHandler", "external_entity_ref_handler" => "entityrefHandler" ); /** @var string source encoding */ var $srcenc; /** @var string target encoding */ var $tgtenc; /* * Use call_user_func when php >= 4.0.7 * @var boolean * @see setMode() */ var $use_call_user_func = true; // }}} // {{{ constructor /** * Creates an XML parser. * * @param string source charset encoding, use NULL (default) to use * whatever the document specifies * @param string how this parser object should work, "event" for * startelement/endelement-type events, "func" * to have it call functions named after elements * * @see xml_parser_create */ function XML_Parser($srcenc = null, $mode = "event", $tgtenc = null) { $this->PEAR('XML_Parser_Error'); if ($srcenc === null) { $xp = @xml_parser_create(); } else { $xp = @xml_parser_create($srcenc); } if (is_resource($xp)) { if ($tgtenc !== null) { if (!@xml_parser_set_option($xp, XML_OPTION_TARGET_ENCODING, $tgtenc)) { return $this->raiseError("invalid target encoding"); } } $this->parser = $xp; $this->setMode($mode); xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, $this->folding); } $this->srcenc = $srcenc; $this->tgtenc = $tgtenc; } // }}} // {{{ setMode() /** * Sets the mode and all handler. * * @param string * @see $handler */ function setMode($mode) { $this->mode = $mode; xml_set_object($this->parser, $this); switch ($mode) { case "func": // use call_user_func() when php >= 4.0.7 // or call_user_method() if not if (version_compare(phpversion(), '4.0.7', 'lt')) { $this->use_call_user_func = false; } else { $this->use_call_user_func = true; } xml_set_element_handler($this->parser, "funcStartHandler", "funcEndHandler"); break; case "event": xml_set_element_handler($this->parser, "startHandler", "endHandler"); break; } foreach ($this->handler as $xml_func => $method) if (method_exists($this, $method)) { $xml_func = "xml_set_" . $xml_func; $xml_func($this->parser, $method); } } // }}} // {{{ setInputFile() /** * Defines * * @param string Filename (full path) * @return resource fopen handle of the given file * @throws XML_Parser_Error * @see setInput(), parse() * @access public */ function setInputFile($file) { $fp = @fopen($file, "rb"); if (is_resource($fp)) { $this->fp = $fp; return $fp; } return $this->raiseError($php_errormsg); } // }}} // {{{ setInput() /** * Sets the file handle to use with parse(). * * @param resource fopen * @access public * @see parse(), setInputFile() */ function setInput($fp) { if (is_resource($fp)) { $this->fp = $fp; return true; } return $this->raiseError("not a file resource"); } // }}} // {{{ parse() /** * Central parsing function. * * @throws XML_Parser_Error * @return boolean true on success * @see parseString() * @access public */ function parse() { if (!is_resource($this->fp)) { return $this->raiseError("no input"); } while ($data = fread($this->fp, 2048)) { $err = $this->parseString($data, feof($this->fp)); if (PEAR::isError($err)) { fclose($this->fp); return $err; } } fclose($this->fp); return true; } // }}} // {{{ parseString() /** * Parses a string. * * @param string XML data * @param boolean ??? * @throws XML_Parser_Error * @return mixed true on success or a string with the xml parser error */ function parseString($data, $eof = false) { if (!xml_parse($this->parser, $data, $eof)) { $err = $this->raiseError($this->parser); xml_parser_free($this->parser); return $err; } return true; } // }}} // {{{ funcStartHandler() function funcStartHandler($xp, $elem, $attribs) { $func = 'xmltag_' . $elem; if (method_exists($this, $func)) { if ($this->use_call_user_func) { call_user_func(array(&$this, $func), $xp, $elem, $attribs); } else { call_user_method($func, $this, $xp, $elem, $attribs); } } } // }}} // {{{ funcEndHandler() function funcEndHandler($xp, $elem) { $func = 'xmltag_' . $elem . '_'; if (method_exists($this, $func)) { if ($this->use_call_user_func) { call_user_func(array(&$this, $func), $xp, $elem); } else { call_user_method($func, $this, $xp, $elem); } } } /** * * @abstract */ function StartHandler($xp, $elem, &$attribs) { return NULL; } /** * * @abstract */ function EndHandler($xp, $elem) { return NULL; } // }}} } class XML_Parser_Error extends PEAR_Error { // {{{ properties var $error_message_prefix = 'XML_Parser: '; // }}} // {{{ constructor() function XML_Parser_Error($msgorparser = 'unknown error', $code = 0, $mode = PEAR_ERROR_RETURN, $level = E_USER_NOTICE) { if (is_resource($msgorparser)) { $code = xml_get_error_code($msgorparser); $msgorparser = sprintf("%s at XML input line %d", xml_error_string($code), xml_get_current_line_number($msgorparser)); } $this->PEAR_Error($msgorparser, $code, $mode, $level); } // }}} } ?> Index: pear/XML_Parser/package.xml +++ pear/XML_Parser/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "package.dtd"> <package version="1.0"> <name>XML_Parser</name> <summary>XML parsing class based on PHP's bundled expat</summary> <description> This is an XML parser based on PHP's built-in xml extension. It supports two basic modes of operation: "func" and "event". In "func" mode, it will look for a function named after each element (xmltag_ELEMENT for start tags and xmltag_ELEMENT_ for end tags), and in "event" mode it uses a set of generic callbacks. </description> <license>PHP License</license> <maintainers> <maintainer> <user>ssb</user> <role>lead</role> <name>Stig Sæther Bakken</name> <email>stig@php.net</email> </maintainer> </maintainers> <release> <state>stable</state> <version>1.0</version> <date>2002-05-09</date> <notes>First independent release</notes> <filelist> <dir name="/" baseinstalldir="XML"> <file role="php">Parser.php</file> </dir> <file role="test">tests/001.phpt</file> <file role="test">tests/002.phpt</file> <file role="test">tests/003.phpt</file> <file role="test">tests/004.phpt</file> <file role="test">tests/005.phpt</file> <file role="test">tests/test2.xml</file> <file role="test">tests/test3.xml</file> </filelist> <deps> <dep type="php" rel="ge" version="4.0.4pl1" /> </deps> </release> </package> Index: pear/XML_Parser/tests/.cvsignore +++ pear/XML_Parser/tests/.cvsignore *.exp *.out *.php phpt.* Index: pear/XML_Parser/tests/001.phpt +++ pear/XML_Parser/tests/001.phpt --TEST-- XML Parser: parse simple string --SKIPIF-- <?php if (!extension_loaded("xml")) echo 'skip'; ?> --FILE-- <?php // // Test for: XML/Parser.php // Parts tested: - parser creation // - some handlers // - parse simple string // require_once "XML/Parser.php"; class __TestParser1 extends XML_Parser { function __TestParser1() { $this->XML_Parser(); } function startHandler($xp, $element, $attribs) { print "<$element"; reset($attribs); while (list($key, $val) = each($attribs)) { $enc = htmlentities($val); print " $key=\"$enc\""; } print ">"; } function endHandler($xp, $element) { print "</$element>\n"; } function cdataHandler($xp, $cdata) { print "<![CDATA[$cdata]]>"; } function defaultHandler($xp, $cdata) { } } error_reporting(1023); print "new __TestParser1 "; var_dump(get_class($o = new __TestParser1())); print "parseString "; var_dump($o->parseString("<?xml version='1.0' ?><root>foo</root>", 1)); ?> --EXPECT-- new __TestParser1 string(13) "__testparser1" parseString <ROOT><![CDATA[foo]]></ROOT> bool(true) Index: pear/XML_Parser/tests/002.phpt +++ pear/XML_Parser/tests/002.phpt --TEST-- XML Parser: parse from file --SKIPIF-- <?php if (!extension_loaded("xml")) echo 'skip'; ?> --FILE-- <?php // -*- C++ -*- // // Test for: XML/Parser.php // Parts tested: - parser creation // - some handlers // - parse from file // chdir (dirname(__FILE__)); require_once "XML/Parser.php"; class __TestParser2 extends XML_Parser { function __TestParser2() { $this->XML_Parser(); } function startHandler($xp, $element, $attribs) { print "<$element"; reset($attribs); while (list($key, $val) = each($attribs)) { $enc = htmlentities($val); print " $key=\"$enc\""; } print ">"; } function endHandler($xp, $element) { print "</$element>\n"; } function cdataHandler($xp, $cdata) { print "<![CDATA[$cdata]]>"; } function defaultHandler($xp, $cdata) { } } print "new __TestParser2 "; var_dump(get_class($o = new __TestParser2())); print "setInputFile "; var_dump($o->setInputFile("test2.xml")); print "parse "; var_dump($o->parse()); ?> --EXPECT-- new __TestParser2 string(13) "__testparser2" setInputFile resource(4) of type (stream) parse <ROOT><![CDATA[foo]]></ROOT> bool(true) Index: pear/XML_Parser/tests/003.phpt +++ pear/XML_Parser/tests/003.phpt --TEST-- XML Parser: parse from file resource --SKIPIF-- <?php if (!extension_loaded("xml")) echo 'skip'; ?> --FILE-- <?php // -*- C++ -*- // // Test for: XML/Parser.php // Parts tested: - parser creation // - some handlers // - parse from file resource // chdir (dirname(__FILE__)); require_once "XML/Parser.php"; class __TestParser3 extends XML_Parser { function __TestParser3() { $this->XML_Parser(); } function startHandler($xp, $element, $attribs) { print "<$element"; reset($attribs); while (list($key, $val) = each($attribs)) { $enc = htmlentities($val); print " $key=\"$enc\""; } print ">"; } function endHandler($xp, $element) { print "</$element>\n"; } function cdataHandler($xp, $cdata) { print "<![CDATA[$cdata]]>"; } function defaultHandler($xp, $cdata) { } } print "new __TestParser3 "; var_dump(get_class($o = new __TestParser3())); print "fopen "; var_dump($fp = fopen("test3.xml", "r")); print "setInput "; var_dump($o->setInput($fp)); print "parse "; var_dump($o->parse()); ?> --EXPECT-- new __TestParser3 string(13) "__testparser3" fopen resource(4) of type (stream) setInput bool(true) parse <ROOT><![CDATA[foo]]></ROOT> bool(true) Index: pear/XML_Parser/tests/004.phpt +++ pear/XML_Parser/tests/004.phpt --TEST-- XML Parser: error class --SKIPIF-- <?php if (!extension_loaded("xml")) echo 'skip'; ?> --FILE-- <?php // -*- C++ -*- // // Test for: XML/Parser.php // Parts tested: - parser error class // require_once "XML/Parser.php"; print "new XML_Parser "; var_dump(get_class($p = new XML_Parser())); $e = $p->parseString("<?xml version='1.0' ?><foo></bar>", true); if (PEAR::isError($e)) { printf("error message: %s\n", $e->getMessage()); } else { print "no error\n"; } ?> --EXPECT-- new XML_Parser string(10) "xml_parser" error message: XML_Parser: mismatched tag at XML input line 1 Index: pear/XML_Parser/tests/005.phpt +++ pear/XML_Parser/tests/005.phpt --TEST-- XML Parser: mixing character encodings --SKIPIF-- <?php if (!extension_loaded("xml")) echo 'skip'; ?> --FILE-- <?php // -*- C++ -*- // // Test for: XML/Parser.php // Parts tested: - mixing character encodings // // This is what we test: // 1 UTF-8 -> ISO-8859-1 // 2 UTF-8 -> US-ASCII // 3 ISO-8859-1 -> UTF-8 // 4 ISO-8859-1 -> US-ASCII // 5 US-ASCII -> UTF-8 // 6 US-ASCII -> ISO-8859-1 // require_once "XML/Parser.php"; class TestEncodings1 extends XML_Parser { var $output = ''; function TestEncodings1($to, $from) { $this->XML_Parser($from, 'event', $to); } function startHandler($xp, $elem, $attribs) { $this->output .= "<$elem>"; } function endHandler($xp, $elem) { $this->output .= "</$elem>"; } function cdataHandler($xp, $data) { $this->output .= $data; } function test($data) { // $this->output = ''; $this->parseString($data, true); return $this->output; } } $xml = "<?xml version='1.0' ?>"; $input = array( "UTF-8" => "<a>abcæøå</a>", "ISO-8859-1" => "<a>abcæøå</a>", "US-ASCII" => "<a>abcaoa</a>" ); $encodings = array_keys($input); foreach ($input as $srcenc => $string) { foreach ($encodings as $tgtenc) { if ($srcenc == $tgtenc) { continue; } print "Testing $srcenc -> $tgtenc: "; $p =& new TestEncodings1($tgtenc, $srcenc); $e = $p->test($input[$srcenc]); if (PEAR::isError($e)) { printf("OOPS: %s\n", $e->getMessage()); } else { var_dump($e); } } } ?> --EXPECT-- Testing UTF-8 -> ISO-8859-1: string(13) "<A>abcæøå</A>" Testing UTF-8 -> US-ASCII: string(13) "<A>abc???</A>" Testing ISO-8859-1 -> UTF-8: string(16) "<A>abcæøå</A>" Testing ISO-8859-1 -> US-ASCII: string(13) "<A>abc???</A>" Testing US-ASCII -> UTF-8: string(13) "<A>abcaoa</A>" Testing US-ASCII -> ISO-8859-1: string(13) "<A>abcaoa</A>" Index: pear/XML_Parser/tests/test2.xml +++ pear/XML_Parser/tests/test2.xml <?xml version='1.0' ?> <root>foo</root> Index: pear/XML_Parser/tests/test3.xml +++ pear/XML_Parser/tests/test3.xml <?xml version='1.0' ?> <root>foo</root>