root/trunk/src/main/php/net/stubbles/xml/stubLibXmlXMLStreamWriter.php

Revision 1359, 4.4 kB (checked in by mikey, 3 months ago)

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

Line 
1 <?php
2 /**
3  * XML Stream Writer based on libxml
4  *
5  * @author      Stephan Schmidt <schst@stubbles.net>
6  * @package     stubbles
7  * @subpackage  xml
8  */
9 stubClassLoader::load('net::stubbles::lang::exceptions::stubMethodNotSupportedException',
10                       'net::stubbles::xml::stubXMLStreamWriter',
11                       'net::stubbles::xml::stubAbstractXMLStreamWriter'
12 );
13 /**
14  * XML Stream Writer based on libxml
15  *
16  * @package     stubbles
17  * @subpackage  xml
18  */
19 class stubLibXmlXMLStreamWriter extends stubAbstractXMLStreamWriter implements stubXMLStreamWriter
20 {
21     /**
22      * List of supported features
23      *
24      * @var  array
25      */
26     protected $features = array(stubXMLStreamWriter::FEATURE_AS_DOM);
27     /**
28      * Writer
29      *
30      * @var  XMLWriter
31      */
32     protected $writer;
33
34     /**
35      * Create a new writer
36      *
37      * @param  string  $xmlVersion
38      * @param  string  $encoding
39      */
40     public function __construct($xmlVersion = '1.0', $encoding = 'UTF-8')
41     {
42         $this->xmlVersion = $xmlVersion;
43         $this->encoding   = $encoding;
44         $this->writer     = new XMLWriter();
45         $this->writer->openMemory();
46         $this->writer->startDocument($xmlVersion, $encoding);
47         $this->writer->setIndent(false);
48     }
49
50     /**
51      * Clear all data, that has been written
52      */
53     public function clear()
54     {
55         unset($this->writer);
56
57         $this->writer = new XMLWriter();
58         $this->writer->openMemory();
59         $this->writer->startDocument($this->xmlVersion, $this->encoding);
60         $this->writer->setIndent(false);
61     }
62
63     /**
64      * really writes an opening tag
65      *
66      * @param  string  $elementName
67      */
68     protected function doWriteStartElement($elementName)
69     {
70         $this->writer->startElement($elementName);
71     }
72
73     /**
74      * Write a text node
75      *
76      * @param  string  $data
77      */
78     public function writeText($data)
79     {
80         $this->writer->text($data);
81     }
82
83     /**
84      * Write a cdata section
85      *
86      * @param  string  $cdata
87      */
88     public function writeCData($cdata)
89     {
90         $this->writer->writeCdata($cdata);
91     }
92
93     /**
94      * Write a comment
95      *
96      * @param  string  $comment
97      */
98     public function writeComment($comment)
99     {
100         $this->writer->writeComment($comment);
101     }
102
103     /**
104      * Write a processing instruction
105      *
106      * @param  string  $target
107      * @param  string  $data
108      */
109     public function writeProcessingInstruction($target, $data = '')
110     {
111         $this->writer->writePi($target, $data);
112     }
113
114     /**
115      * Write an xml fragment
116      *
117      * @param  string  $fragment
118      */
119     public function writeXmlFragment($fragment)
120     {
121         $this->writer->writeRaw($fragment);
122     }
123
124     /**
125      * Write an attribute
126      *
127      * @param  string  $attributeName
128      * @param  string  $attributeValue
129      */
130     public function writeAttribute($attributeName, $attributeValue)
131     {
132         $this->writer->writeAttribute($attributeName, $attributeValue);
133     }
134
135     /**
136      * really writes an end element
137      */
138     protected function doWriteEndElement()
139     {
140         $this->writer->endElement();
141     }
142
143     /**
144      * Write a full element
145      *
146      * @param  string  $elementName
147      * @param  array   $attributes
148      * @param  string  $cdata
149      */
150     public function writeElement($elementName, array $attributes = array(), $cdata = null)
151     {
152         $this->writeStartElement($elementName);
153         foreach ($attributes as $attName => $attValue) {
154             $this->writeAttribute($attName, $attValue);
155         }
156         if (null !== $cdata) {
157             $this->writeText($cdata);
158         }
159         $this->writeEndElement();
160     }
161
162     /**
163      * Import another stream
164      *
165      * @param   stubXMLStreamWriter              $writer
166      * @throws  stubMethodNotSupportedException
167      */
168     public function importStreamWriter(stubXMLStreamWriter $writer)
169     {
170         throw new stubMethodNotSupportedException('Can not import another stream writer.');
171     }
172
173     /**
174      * Return the XML as a DOM
175      *
176      * @return  DOMDocument
177      */
178     public function asDom()
179     {
180         $doc = new DOMDocument();
181         $doc->loadXML($this->writer->outputMemory());
182         return $doc;
183     }
184
185     /**
186      * Return the XML as a string
187      *
188      * @return  string
189      */
190     public function asXML()
191     {
192         return rtrim($this->writer->outputMemory());
193     }
194 }
195 ?>
Note: See TracBrowser for help on using the browser.