svn: /pear2/Pyrus/trunk/src/Pyrus/ XMLParser.php XMLWriter.php

[email protected] (Helgi Þormar Þorbjörnsson)
Newsgroups php.pear.cvs,php.pear.core
Message-ID <[email protected]>
dufuz                                    Sat, 08 May 2010 01:51:31 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=299133

Log:
Extend the XMLReader and XMLWriter classes instead of instantiating them internally and storing the object.

Changed paths:
    U   pear2/Pyrus/trunk/src/Pyrus/XMLParser.php
    U   pear2/Pyrus/trunk/src/Pyrus/XMLWriter.php
svn-diffs-299133.txt (text/x-diff, 10.9 KB)
Modified: pear2/Pyrus/trunk/src/Pyrus/XMLParser.php
===================================================================
--- pear2/Pyrus/trunk/src/Pyrus/XMLParser.php	2010-05-07 23:23:40 UTC (rev 299132)
+++ pear2/Pyrus/trunk/src/Pyrus/XMLParser.php	2010-05-08 01:51:31 UTC (rev 299133)
@@ -8,6 +8,7 @@
  * @package    PEAR2_Pyrus
  * @subpackage XML
  * @author     Greg Beaver <[email protected]>
+ * @author     Helgi Þormar Þorbjörnsson <[email protected]>
  * @copyright  2010 The PEAR Group
  * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
  * @version    SVN: $Id$
@@ -21,20 +22,15 @@
  * @package    PEAR2_Pyrus
  * @subpackage XML
  * @author     Greg Beaver <[email protected]>
+ * @author     Helgi Þormar Þorbjörnsson <[email protected]>
  * @copyright  2010 The PEAR Group
  * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
  * @link       http://svn.php.net/viewvc/pear2/Pyrus/
  */
 namespace pear2\Pyrus;
-use \XMLReader, \DOMDocument;
-class XMLParser
+use \DOMDocument;
+class XMLParser extends \XMLReader
 {
-    protected $reader;
-    function __construct()
-    {
-        $this->reader = new XMLReader;
-    }
-
     /**
      * Parse a string containing XML
      *
@@ -45,7 +41,7 @@
      */
     function parseString($string, $schema = false)
     {
-        $this->reader->XML($string);
+        $this->XML($string);
         return $this->_parse($string, $schema, false);
     }

@@ -73,7 +69,7 @@
      */
     function parse($file, $schema = false)
     {
-        if (@$this->reader->open($file) === false) {
+        if (@$this->open($file) === false) {
             throw new XMLParser\Exception('Cannot open ' . $file . ' for parsing');
         }

@@ -107,7 +103,8 @@
         }

         if (is_array($arr) && isset($arr[$name]) && is_array($arr[$name]) &&
-              isset($arr[$name][0])) {
+            isset($arr[$name][0])
+        ) {
             // tag exists as a sibling
             $where = count($arr[$name]);
             if (!isset($arr[$name][$where])) {
@@ -172,7 +169,7 @@
         libxml_use_internal_errors(true);
         libxml_clear_errors();
         $arr = $this->_recursiveParse();
-        $this->reader->close();
+        $this->close();
         $causes = array();
         foreach (libxml_get_errors() as $error) {
             $causes[] = new XMLParser\Exception("Line " .
@@ -217,39 +214,39 @@

     private function _recursiveParse($arr = array())
     {
-        while (@$this->reader->read()) {
-            $depth = $this->reader->depth;
-            if ($this->reader->nodeType == XMLReader::ELEMENT) {
-                $tag = $this->reader->name;
+        while (@$this->read()) {
+            $depth = $this->depth;
+            if ($this->nodeType == self::ELEMENT) {
+                $tag = $this->name;

                 $attribs = array();
-                if ($this->reader->isEmptyElement) {
-                    if ($this->reader->hasAttributes) {
-                        $attr = $this->reader->moveToFirstAttribute();
+                if ($this->isEmptyElement) {
+                    if ($this->hasAttributes) {
+                        $attr = $this->moveToFirstAttribute();
                         while ($attr) {
-                            $attribs[$this->reader->name] = $this->reader->value;
-                            $attr = $this->reader->moveToNextAttribute();
+                            $attribs[$this->name] = $this->value;
+                            $attr = $this->moveToNextAttribute();
                         }

-                        $depth = $this->reader->depth;
+                        $depth = $this->depth;
                         $arr = $this->mergeTag($arr, '', $attribs, $tag, $depth);
                         continue;
                     }

-                    $depth = $this->reader->depth;
+                    $depth = $this->depth;
                     $arr = $this->mergeTag($arr, '', array(), $tag, $depth);
                     continue;
                 }

-                if ($this->reader->hasAttributes) {
-                    $attr = $this->reader->moveToFirstAttribute();
+                if ($this->hasAttributes) {
+                    $attr = $this->moveToFirstAttribute();
                     while ($attr) {
-                        $attribs[$this->reader->name] = $this->reader->value;
-                        $attr = $this->reader->moveToNextAttribute();
+                        $attribs[$this->name] = $this->value;
+                        $attr = $this->moveToNextAttribute();
                     }
                 }

-                $depth = $this->reader->depth;
+                $depth = $this->depth;
                 $arr = $this->mergeTag($arr, '', $attribs, $tag, $depth);
                 if (is_array($arr[$tag]) && isset($arr[$tag][0])) {
                     // seek to last sibling
@@ -262,14 +259,12 @@
                 continue;
             }

-            if ($this->reader->nodeType == XMLReader::END_ELEMENT) {
+            if ($this->nodeType == self::END_ELEMENT) {
                 return $arr;
             }

-            if ($this->reader->nodeType == XMLReader::TEXT ||
-                $this->reader->nodeType == XMLReader::CDATA
-            ) {
-                $arr = $this->mergeValue($arr, $this->reader->value);
+            if ($this->nodeType == self::TEXT || $this->nodeType == self::CDATA) {
+                $arr = $this->mergeValue($arr, $this->value);
             }
         }


Modified: pear2/Pyrus/trunk/src/Pyrus/XMLWriter.php
===================================================================
--- pear2/Pyrus/trunk/src/Pyrus/XMLWriter.php	2010-05-07 23:23:40 UTC (rev 299132)
+++ pear2/Pyrus/trunk/src/Pyrus/XMLWriter.php	2010-05-08 01:51:31 UTC (rev 299133)
@@ -7,6 +7,7 @@
  * @category  PEAR2
  * @package   PEAR2_Pyrus
  * @author    Greg Beaver <[email protected]>
+ * @author    Helgi Þormar Þorbjörnsson <[email protected]>
  * @copyright 2010 The PEAR Group
  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
  * @version   SVN: $Id$
@@ -20,12 +21,13 @@
  * @package    PEAR2_Pyrus
  * @subpackage XML
  * @author     Greg Beaver <[email protected]>
+ * @author     Helgi Þormar Þorbjörnsson <[email protected]>
  * @copyright  2010 The PEAR Group
  * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
  * @link       http://svn.php.net/viewvc/pear2/Pyrus/
  */
 namespace pear2\Pyrus;
-class XMLWriter
+class XMLWriter extends \XMLWriter
 {
     private $_array;
     private $_state;
@@ -33,7 +35,6 @@
     /**
      * @var XMLWriter
      */
-    private $_writer;
     private $_iter;
     private $_tagStack;
     private $_namespaces;
@@ -54,12 +55,11 @@
     function __construct(array $array)
     {
         if (count($array) != 1) {
-            throw new XMLWriter\Exception('Cannot serialize array to' .
-                'XML, array must have exactly 1 element');
+            throw new XMLWriter\Exception('Cannot serialize array to XML, ' .
+                'array must have exactly 1 element');
         }

-        $this->_array  = $array;
-        $this->_writer = new \XMLWriter;
+        $this->_array = $array;
     }

     /**
@@ -69,13 +69,13 @@
      */
     function __toString()
     {
-        $this->_writer->openMemory();
+        $this->openMemory();
         return $this->_serialize();
     }

     function toFile($file)
     {
-        $this->_writer->openUri($file);
+        $this->openUri($file);
         return $this->_serialize();
     }

@@ -107,7 +107,7 @@
             $this->_popState();
         } elseif ($this->_type == 'Tag') {
             $this->_popState();
-            $this->_writer->endElement();
+            $this->endElement();
         }

         return false;
@@ -124,22 +124,22 @@
         if (isset($element) && !isset($this->_namespaces[$ns])) {
             if (is_string($values)) {
                 if (strlen($values)) {
-                    $this->_writer->writeElementNs($ns, $element, $this->_namespaces[$ns], $values);
+                    $this->writeElementNs($ns, $element, $this->_namespaces[$ns], $values);
                 } else {
-                    $this->_writer->writeElementNs($ns, $element, $this->_namespaces[$ns]);
+                    $this->writeElementNs($ns, $element, $this->_namespaces[$ns]);
                 }
             } else {
-                $this->_writer->startElementNs($ns, $element, $this->_namespaces[$ns]);
+                $this->startElementNs($ns, $element, $this->_namespaces[$ns]);
             }
         } else {
             if (is_string($values) || is_int($values) || is_bool($values)) {
                 if (strlen($values)) {
-                    $this->_writer->writeElement($key, $values);
+                    $this->writeElement($key, $values);
                 } else {
-                    $this->_writer->writeElement($key);
+                    $this->writeElement($key);
                 }
             } else {
-                $this->_writer->startElement($key);
+                $this->startElement($key);
             }
         }
     }
@@ -181,8 +181,8 @@
                 }
             } else {
                 if (is_string($values)) {
-                    $this->_writer->text($values);
-                    $this->_writer->endElement();
+                    $this->text($values);
+                    $this->endElement();
                 }
             }
         }
@@ -210,12 +210,12 @@
                     $this->_namespaces[$attr] = $values;
                 }

-                $this->_writer->writeAttribute($key, $values);
+                $this->writeAttribute($key, $values);
             } else {
-                $this->_writer->writeAttributeNS($ns, $attr, $values, $values);
+                $this->writeAttributeNS($ns, $attr, $values, $values);
             }
         } else { // default namespace
-            $this->_writer->writeAttribute($key, $values);
+            $this->writeAttribute($key, $values);
         }

         // cycle to next key
@@ -244,9 +244,9 @@
      */
     private function _serialize()
     {
-        $this->_writer->setIndent(true);
-        $this->_writer->setIndentString(' ');
-        $this->_writer->startDocument('1.0', 'UTF-8');
+        $this->setIndent(true);
+        $this->setIndentString(' ');
+        $this->startDocument('1.0', 'UTF-8');
         $this->_namespaces    = array();
         $this->_tagStack      = array();
         $this->_state         = array();
@@ -282,7 +282,7 @@
             $lastdepth = $depth;
             if ($this->_type !== 'Attribs') {
                 if ($key === '_content') {
-                    $this->_writer->text($values);
+                    $this->text($values);
                     continue;
                 }

@@ -305,7 +305,7 @@
             $lastdepth--;
         }

-        $this->_writer->endDocument();
-        return $this->_writer->flush();
+        $this->endDocument();
+        return $this->flush();
     }
 }
\ No newline at end of file
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.