Changeset 1359

Show
Ignore:
Timestamp:
02/22/08 18:13:10 (7 months ago)
Author:
mikey
Message:

added possiblity to check if a stream writer is finished, i.e it has no open elements left

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/xml/stubAbstractXMLStreamWriter.php

    r1296 r1359  
    11<?php 
    22/** 
    3  * Abstract base class for XML stream writers 
     3 * Abstract base class for XML stream writers. 
    44 * 
    55 * @author      Stephan Schmidt <schst@stubbles.net> 
     
    99 */ 
    1010/** 
    11  * Abstract base class for XML stream writers 
     11 * Abstract base class for XML stream writers. 
    1212 * 
    1313 * @package     stubbles 
     
    3434     */ 
    3535    protected $features = array(); 
     36    /** 
     37     * depth, i.e. amount of opened tags 
     38     * 
     39     * @var  int 
     40     */ 
     41    protected $depth   = 0; 
    3642 
    3743    /** 
     
    6571        return in_array($feature, $this->features); 
    6672    } 
     73 
     74    /** 
     75     * Write an opening tag 
     76     * 
     77     * @param  string  $elementName 
     78     */ 
     79    public function writeStartElement($elementName) 
     80    { 
     81        $this->doWriteStartElement($elementName); 
     82        $this->depth++; 
     83    } 
     84 
     85    /** 
     86     * really writes an opening tag 
     87     * 
     88     * @param  string  $elementName 
     89     */ 
     90    protected abstract function doWriteStartElement($elementName); 
     91 
     92    /** 
     93     * Write an end element 
     94     */ 
     95    public function writeEndElement() 
     96    { 
     97        $this->doWriteEndElement(); 
     98        $this->depth--; 
     99    } 
     100 
     101    /** 
     102     *  really writes an end element 
     103     */ 
     104    protected abstract function doWriteEndElement(); 
     105 
     106    /** 
     107     * checks whether the document is finished meaning no open tags are left 
     108     * 
     109     * @return  bool 
     110     */ 
     111    public function isFinished() 
     112    { 
     113        return 0 === $this->depth; 
     114    } 
    67115} 
    68116?> 
  • trunk/src/main/php/net/stubbles/xml/stubDomXMLStreamWriter.php

    r1301 r1359  
    6363 
    6464    /** 
    65      * Write an opening tag 
     65     * really writes an opening tag 
    6666     * 
    6767     * @param   string            $elementName 
    6868     * @throws  stubXMLException 
    6969     */ 
    70     public function writeStartElement($elementName) 
     70    protected function doWriteStartElement($elementName) 
    7171    { 
    7272        try { 
     
    226226 
    227227    /** 
    228      * Write an end element 
    229      * 
    230      * @throws  stubXMLException 
    231      */ 
    232     public function writeEndElement() 
     228     * really writes an end element 
     229     * 
     230     * @throws  stubXMLException 
     231     */ 
     232    protected function doWriteEndElement() 
    233233    { 
    234234        if (count($this->elementStack) === 0) { 
  • trunk/src/main/php/net/stubbles/xml/stubLibXmlXMLStreamWriter.php

    r1301 r1359  
    6262 
    6363    /** 
    64      * Write an opening tag 
     64     * really writes an opening tag 
    6565     * 
    6666     * @param  string  $elementName 
    6767     */ 
    68     public function writeStartElement($elementName) 
     68    protected function doWriteStartElement($elementName) 
    6969    { 
    7070        $this->writer->startElement($elementName); 
     
    134134 
    135135    /** 
    136      * Write an end element 
     136     * really writes an end element 
    137137     */ 
    138     public function writeEndElement() 
     138    protected function doWriteEndElement() 
    139139    { 
    140140        $this->writer->endElement(); 
  • trunk/src/main/php/net/stubbles/xml/stubXMLStreamWriter.php

    r1301 r1359  
    138138 
    139139    /** 
     140     * checks whether the document is finished meaning no open tags are left 
     141     * 
     142     * @return  bool 
     143     */ 
     144    public function isFinished(); 
     145 
     146    /** 
    140147     * Return the XML as a string 
    141148     * 
  • trunk/src/test/php/net/stubbles/xml/stubDomXMLStreamWriterTestCase.php

    r1271 r1359  
    340340        $writer->writeEndElement(); 
    341341    } 
     342 
     343    /** 
     344     * checks if the finished status is reported properly 
     345     * 
     346     * @test 
     347     */ 
     348    public function isFinished() 
     349    { 
     350        $writer = new stubDomXMLStreamWriter(); 
     351        $this->assertTrue($writer->isFinished()); 
     352        $writer->writeStartElement('root'); 
     353        $this->assertFalse($writer->isFinished()); 
     354        $writer->writeEndElement(); 
     355        $this->assertTrue($writer->isFinished()); 
     356    } 
    342357} 
    343358?> 
  • trunk/src/test/php/net/stubbles/xml/stubLibXmlXMLStreamWriterTestCase.php

    r1271 r1359  
    199199        $this->assertTrue($writer->hasFeature(stubXMLStreamWriter::FEATURE_AS_DOM)); 
    200200        $this->assertFalse($writer->hasFeature(stubXMLStreamWriter::FEATURE_IMPORT_WRITER)); 
    201     }     
     201    } 
     202 
     203    /** 
     204     * checks if the finished status is reported properly 
     205     * 
     206     * @test 
     207     */ 
     208    public function isFinished() 
     209    { 
     210        $writer = new stubLibXmlXMLStreamWriter(); 
     211        $this->assertTrue($writer->isFinished()); 
     212        $writer->writeStartElement('root'); 
     213        $this->assertFalse($writer->isFinished()); 
     214        $writer->writeEndElement(); 
     215        $this->assertTrue($writer->isFinished()); 
     216    } 
    202217} 
    203218?>