Changeset 849

Show
Ignore:
Timestamp:
08/19/07 17:21:21 (1 year ago)
Author:
schst
Message:

Fixed ticket #73

Files:

Legend:

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

    r835 r849  
    8282     */ 
    8383    public function writeStartElement($elementName) { 
    84         $element = $this->doc->createElement($elementName); 
    85         if (count($this->elementStack) == 0) { 
    86             $this->doc->appendChild($element); 
    87         } else { 
    88             $parent = end($this->elementStack); 
    89             $parent->appendChild($element); 
    90         } 
    91         array_push($this->elementStack, $element); 
     84        try { 
     85            libxml_use_internal_errors(true); 
     86            $element = $this->doc->createElement($elementName); 
     87            if (count($this->elementStack) == 0) { 
     88                $this->doc->appendChild($element); 
     89            } else { 
     90                $parent = end($this->elementStack); 
     91                $parent->appendChild($element); 
     92            } 
     93            array_push($this->elementStack, $element); 
     94            $errors = libxml_get_errors(); 
     95            if (!empty($errors)) { 
     96                libxml_clear_errors(); 
     97                throw new stubXMLException('Error writing start element: ' . $this->convertLibXmlErrorsToString($errors)); 
     98            } 
     99        } catch (DOMException $e) { 
     100            throw new stubXMLException('Error writing start element.', $e); 
     101        } 
    92102    } 
    93103 
     
    95105     * Write a text node 
    96106     * 
     107     * Data has to be encoded even if document encoding is not UTF-8 
     108     * 
    97109     * @param string $data 
     110     * @link  http://php.net/manual/en/function.dom-domdocument-save.php#67952 
    98111     */ 
    99112    public function writeText($data) 
    100113    { 
    101         // data has to be encoded even if document encoding is not UTF-8 
    102         // (see http://php.net/manual/en/function.dom-domdocument-save.php#67952) 
    103         $textNode = $this->doc->createTextNode(utf8_encode($data)); 
    104         $this->addToDom($textNode); 
     114        try { 
     115            libxml_use_internal_errors(true); 
     116            $textNode = $this->doc->createTextNode(utf8_encode($data)); 
     117            $this->addToDom($textNode); 
     118            $errors = libxml_get_errors(); 
     119            if (!empty($errors)) { 
     120                libxml_clear_errors(); 
     121                throw new stubXMLException('Error writing text: ' . $this->convertLibXmlErrorsToString($errors)); 
     122            } 
     123        } catch (DOMException $e) { 
     124            throw new stubXMLException('Error writing text.', $e); 
     125        } 
    105126    } 
    106127 
     
    111132     */ 
    112133    public function writeCData($cdata) { 
    113         $cdataNode = $this->doc->createCDATASection(utf8_encode($cdata)); 
    114         $this->addToDom($cdataNode); 
     134        try { 
     135            libxml_use_internal_errors(true); 
     136            $cdataNode = $this->doc->createCDATASection(utf8_encode($cdata)); 
     137            $this->addToDom($cdataNode); 
     138            $errors = libxml_get_errors(); 
     139            if (!empty($errors)) { 
     140                libxml_clear_errors(); 
     141                throw new stubXMLException('Error writing cdata section: ' . $this->convertLibXmlErrorsToString($errors)); 
     142            } 
     143        } catch (DOMException $e) { 
     144            throw new stubXMLException('Error writing cdata section.', $e); 
     145        } 
    115146    } 
    116147 
     
    121152     */ 
    122153    public function writeComment($comment) { 
    123         $commentNode = $this->doc->createComment(utf8_encode($comment)); 
    124         $this->addToDom($commentNode); 
     154        try { 
     155            libxml_use_internal_errors(true); 
     156            $commentNode = $this->doc->createComment(utf8_encode($comment)); 
     157            $this->addToDom($commentNode); 
     158            $errors = libxml_get_errors(); 
     159            if (!empty($errors)) { 
     160                libxml_clear_errors(); 
     161                throw new stubXMLException('Error writing comment: ' . $this->convertLibXmlErrorsToString($errors)); 
     162            } 
     163        } catch (DOMException $e) { 
     164            throw new stubXMLException('Error writing comment.', $e); 
     165        } 
    125166    } 
    126167 
     
    132173     */ 
    133174    public function writeProcessingInstruction($target, $data = '') { 
    134         $piNode = $this->doc->createProcessingInstruction($target, utf8_encode($data)); 
    135         $this->addToDom($piNode); 
     175        try { 
     176            libxml_use_internal_errors(true); 
     177            $piNode = $this->doc->createProcessingInstruction($target, utf8_encode($data)); 
     178            $this->addToDom($piNode); 
     179            $errors = libxml_get_errors(); 
     180            if (!empty($errors)) { 
     181                libxml_clear_errors(); 
     182                throw new stubXMLException('Error writing processing instruction: ' . $this->convertLibXmlErrorsToString($errors)); 
     183            } 
     184        } catch (DOMException $e) { 
     185            throw new stubXMLException('Error writing processing instruction.', $e); 
     186        } 
    136187    } 
    137188 
     
    164215     */ 
    165216    public function writeAttribute($attributeName, $attributeValue) { 
    166         $currentElement = end($this->elementStack); 
    167         $currentElement->setAttribute($attributeName, utf8_encode($attributeValue)); 
     217        try { 
     218            libxml_use_internal_errors(true); 
     219            $currentElement = end($this->elementStack); 
     220            $currentElement->setAttribute($attributeName, utf8_encode($attributeValue)); 
     221            $errors = libxml_get_errors(); 
     222            if (!empty($errors)) { 
     223                libxml_clear_errors(); 
     224                throw new stubXMLException('Error writing attribute: ' . $this->convertLibXmlErrorsToString($errors)); 
     225            } 
     226        } catch (DOMException $e) { 
     227            throw new stubXMLException('Error writing attribute.', $e); 
     228        } 
    168229    } 
    169230 
     
    172233     */ 
    173234    public function writeEndElement() { 
     235        if (count($this->elementStack) === 0) { 
     236            throw new stubXMLException('No open element available.'); 
     237        } 
    174238        array_pop($this->elementStack); 
    175239    } 
     
    183247     */ 
    184248    public function writeElement($elementName, array $attributes = array(), $cdata = null) { 
    185         $element = $this->doc->createElement($elementName); 
    186         foreach ($attributes as $attName => $attValue) { 
    187             $element->setAttribute($attName, $attValue); 
    188         } 
    189         if (null !== $cdata) { 
    190             $element->appendChild($this->doc->createTextNode($cdata)); 
    191         } 
    192         if (count($this->elementStack) == 0) { 
    193             $this->doc->appendChild($element); 
    194         } else { 
    195             $parent = end($this->elementStack); 
    196             $parent->appendChild($element); 
    197         } 
    198  
     249        try { 
     250            libxml_use_internal_errors(true); 
     251            $element = $this->doc->createElement($elementName); 
     252            foreach ($attributes as $attName => $attValue) { 
     253                $element->setAttribute($attName, $attValue); 
     254            } 
     255            if (null !== $cdata) { 
     256                $element->appendChild($this->doc->createTextNode($cdata)); 
     257            } 
     258            if (count($this->elementStack) == 0) { 
     259                $this->doc->appendChild($element); 
     260            } else { 
     261                $parent = end($this->elementStack); 
     262                $parent->appendChild($element); 
     263            } 
     264            $errors = libxml_get_errors(); 
     265            if (!empty($errors)) { 
     266                libxml_clear_errors(); 
     267                throw new stubXMLException('Error writing element: ' . $this->convertLibXmlErrorsToString($errors)); 
     268            } 
     269        } catch (DOMException $e) { 
     270            throw new stubXMLException('Error writing element.', $e); 
     271        } 
    199272    } 
    200273 
     
    205278     */ 
    206279    public function importStreamWriter(stubXMLStreamWriter $writer) { 
    207         $newNode = $writer->asDOM()->documentElement; 
    208         $newNodeImported = $this->doc->importNode($newNode, true); 
    209         $this->addToDom($newNodeImported); 
     280        try { 
     281            libxml_use_internal_errors(true); 
     282            $newNode = $writer->asDOM()->documentElement; 
     283            $newNodeImported = $this->doc->importNode($newNode, true); 
     284            $this->addToDom($newNodeImported); 
     285            $errors = libxml_get_errors(); 
     286            if (!empty($errors)) { 
     287                libxml_clear_errors(); 
     288                throw new stubXMLException('Error during import: ' . $this->convertLibXmlErrorsToString($errors)); 
     289            } 
     290        } catch (DOMException $e) { 
     291            throw new stubXMLException('Error during import.', $e); 
     292        } 
    210293    } 
    211294