5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
8 * Copyright (c) 2004-2009, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above copyright notice, this list
18 * of conditions and the following disclaimer in the documentation and/or other materials
19 * provided with the distribution.
21 * * Neither the name of the SimplePie Team nor the names of its contributors may be used
22 * to endorse or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
26 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
28 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * @copyright 2004-2010 Ryan Parman, Geoffrey Sneddon, Ryan McCue
39 * @author Geoffrey Sneddon
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @todo phpDoc comments
48 * Parses the XML Declaration
52 class SimplePie_XML_Declaration_Parser
68 var $encoding = 'UTF-8';
76 var $standalone = false;
79 * Current state of the state machine
84 var $state = 'before_version_name';
95 * Input data length (to avoid calling strlen() everytime this is needed)
100 var $data_length = 0;
103 * Current position of the pointer
111 * Create an instance of the class with the input data
114 * @param string $data Input data
116 public function __construct($data)
119 $this->data_length
= strlen($this->data
);
123 * Parse the input data
126 * @return bool true on success, false on failure
128 public function parse()
130 while ($this->state
&& $this->state
!== 'emit' && $this->has_data())
132 $state = $this->state
;
136 if ($this->state
=== 'emit')
143 $this->encoding
= '';
144 $this->standalone
= '';
150 * Check whether there is data beyond the pointer
153 * @return bool true if there is further data, false if not
155 public function has_data()
157 return (bool) ($this->position
< $this->data_length
);
161 * Advance past any whitespace
163 * @return int Number of whitespace characters passed
165 public function skip_whitespace()
167 $whitespace = strspn($this->data
, "\x09\x0A\x0D\x20", $this->position
);
168 $this->position +
= $whitespace;
175 public function get_value()
177 $quote = substr($this->data
, $this->position
, 1);
178 if ($quote === '"' || $quote === "'")
181 $len = strcspn($this->data
, $quote, $this->position
);
182 if ($this->has_data())
184 $value = substr($this->data
, $this->position
, $len);
185 $this->position +
= $len +
1;
192 public function before_version_name()
194 if ($this->skip_whitespace())
196 $this->state
= 'version_name';
200 $this->state
= false;
204 public function version_name()
206 if (substr($this->data
, $this->position
, 7) === 'version')
208 $this->position +
= 7;
209 $this->skip_whitespace();
210 $this->state
= 'version_equals';
214 $this->state
= false;
218 public function version_equals()
220 if (substr($this->data
, $this->position
, 1) === '=')
223 $this->skip_whitespace();
224 $this->state
= 'version_value';
228 $this->state
= false;
232 public function version_value()
234 if ($this->version
= $this->get_value())
236 $this->skip_whitespace();
237 if ($this->has_data())
239 $this->state
= 'encoding_name';
243 $this->state
= 'emit';
248 $this->state
= false;
252 public function encoding_name()
254 if (substr($this->data
, $this->position
, 8) === 'encoding')
256 $this->position +
= 8;
257 $this->skip_whitespace();
258 $this->state
= 'encoding_equals';
262 $this->state
= 'standalone_name';
266 public function encoding_equals()
268 if (substr($this->data
, $this->position
, 1) === '=')
271 $this->skip_whitespace();
272 $this->state
= 'encoding_value';
276 $this->state
= false;
280 public function encoding_value()
282 if ($this->encoding
= $this->get_value())
284 $this->skip_whitespace();
285 if ($this->has_data())
287 $this->state
= 'standalone_name';
291 $this->state
= 'emit';
296 $this->state
= false;
300 public function standalone_name()
302 if (substr($this->data
, $this->position
, 10) === 'standalone')
304 $this->position +
= 10;
305 $this->skip_whitespace();
306 $this->state
= 'standalone_equals';
310 $this->state
= false;
314 public function standalone_equals()
316 if (substr($this->data
, $this->position
, 1) === '=')
319 $this->skip_whitespace();
320 $this->state
= 'standalone_value';
324 $this->state
= false;
328 public function standalone_value()
330 if ($standalone = $this->get_value())
335 $this->standalone
= true;
339 $this->standalone
= false;
343 $this->state
= false;
347 $this->skip_whitespace();
348 if ($this->has_data())
350 $this->state
= false;
354 $this->state
= 'emit';
359 $this->state
= false;