Untitled diff

建立於 差異永不過期
兩個文本完全相同
這兩個文本之間沒有差異行數:480 · 差異:0
0 刪除
480 行
0 新增
480 行
<?php
<?php
/**
/**
* PHPTAL templating engine
* PHPTAL templating engine
*
*
* PHP Version 5
* PHP Version 5
*
*
* @category HTML
* @category HTML
* @package PHPTAL
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @version SVN: $Id$
* @version SVN: $Id$
* @link http://phptal.org/
* @link http://phptal.org/
*/
*/


/**
/**
* Simple sax like xml parser for PHPTAL
* Simple sax like xml parser for PHPTAL
* ("Dom" in the class name comes from name of the directory, not mode of operation)
* ("Dom" in the class name comes from name of the directory, not mode of operation)
*
*
* At the time this parser was created, standard PHP libraries were not suitable
* At the time this parser was created, standard PHP libraries were not suitable
* (could not retrieve doctypes, xml declaration, problems with comments and CDATA).
* (could not retrieve doctypes, xml declaration, problems with comments and CDATA).
*
*
* There are still some problems: XML parsers don't care about exact format of enties
* There are still some problems: XML parsers don't care about exact format of enties
* or CDATA sections (PHPTAL tries to preserve them),
* or CDATA sections (PHPTAL tries to preserve them),
* <?php ?> blocks are not allowed in attributes.
* <?php ?> blocks are not allowed in attributes.
*
*
* This parser failed to enforce some XML well-formedness constraints,
* This parser failed to enforce some XML well-formedness constraints,
* and there are ill-formed templates "in the wild" because of this.
* and there are ill-formed templates "in the wild" because of this.
*
*
* @package PHPTAL
* @package PHPTAL
* @subpackage Dom
* @subpackage Dom
* @see PHPTAL_DOM_DocumentBuilder
* @see PHPTAL_DOM_DocumentBuilder
*/
*/
class PHPTAL_Dom_SaxXmlParser
class PHPTAL_Dom_SaxXmlParser
{
{
private $_file;
private $_file;
private $_line;
private $_line;
private $_source;
private $_source;


// available parser states
// available parser states
const ST_ROOT = 0;
const ST_ROOT = 0;
const ST_TEXT = 1;
const ST_TEXT = 1;
const ST_LT = 2;
const ST_LT = 2;
const ST_TAG_NAME = 3;
const ST_TAG_NAME = 3;
const ST_TAG_CLOSE = 4;
const ST_TAG_CLOSE = 4;
const ST_TAG_SINGLE = 5;
const ST_TAG_SINGLE = 5;
const ST_TAG_ATTRIBUTES = 6;
const ST_TAG_ATTRIBUTES = 6;
const ST_TAG_BETWEEN_ATTRIBUTE = 7;
const ST_TAG_BETWEEN_ATTRIBUTE = 7;
const ST_CDATA = 8;
const ST_CDATA = 8;
const ST_COMMENT = 9;
const ST_COMMENT = 9;
const ST_DOCTYPE = 10;
const ST_DOCTYPE = 10;
const ST_XMLDEC = 11;
const ST_XMLDEC = 11;
const ST_PREPROC = 12;
const ST_PREPROC = 12;
const ST_ATTR_KEY = 13;
const ST_ATTR_KEY = 13;
const ST_ATTR_EQ = 14;
const ST_ATTR_EQ = 14;
const ST_ATTR_QUOTE = 15;
const ST_ATTR_QUOTE = 15;
const ST_ATTR_VALUE = 16;
const ST_ATTR_VALUE = 16;


const BOM_STR = "\xef\xbb\xbf";
const BOM_STR = "\xef\xbb\xbf";




static $state_names = array(
static $state_names = array(
self::ST_ROOT => 'root node',
self::ST_ROOT => 'root node',
self::ST_TEXT => 'text',
self::ST_TEXT => 'text',
self::ST_LT => 'start of tag',
self::ST_LT => 'start of tag',
self::ST_TAG_NAME => 'tag name',
self::ST_TAG_NAME => 'tag name',
self::ST_TAG_CLOSE => 'closing tag',
self::ST_TAG_CLOSE => 'closing tag',
self::ST_TAG_SINGLE => 'self-closing tag',
self::ST_TAG_SINGLE => 'self-closing tag',
self::ST_TAG_ATTRIBUTES => 'tag',
self::ST_TAG_ATTRIBUTES => 'tag',
self::ST_TAG_BETWEEN_ATTRIBUTE => 'tag attributes',
self::ST_TAG_BETWEEN_ATTRIBUTE => 'tag attributes',
self::ST_CDATA => 'CDATA',
self::ST_CDATA => 'CDATA',
self::ST_COMMENT => 'comment',
self::ST_COMMENT => 'comment',
self::ST_DOCTYPE => 'doctype',
self::ST_DOCTYPE => 'doctype',
self::ST_XMLDEC => 'XML declaration',
self::ST_XMLDEC => 'XML declaration',
self::ST_PREPROC => 'preprocessor directive',
self::ST_PREPROC => 'preprocessor directive',
self::ST_ATTR_KEY => 'attribute name',
self::ST_ATTR_KEY => 'attribute name',
self::ST_ATTR_EQ => 'attribute value',
self::ST_ATTR_EQ => 'attribute value',
self::ST_ATTR_QUOTE => 'quoted attribute value',
self::ST_ATTR_QUOTE => 'quoted attribute value',
self::ST_ATTR_VALUE => 'unquoted attribute value',
self::ST_ATTR_VALUE => 'unquoted attribute value',
);
);


private $input_encoding;
private $input_encoding;
public function __construct($input_encoding)
public function __construct($input_encoding)
{
{
$this->input_encoding = $input_encoding;
$this->input_encoding = $input_encoding;
$this->_file = "<string>";
$this->_file = "<string>";
}
}


public function parseFile(PHPTAL_Dom_DocumentBuilder $builder, $src)
public function parseFile(PHPTAL_Dom_DocumentBuilder $builder, $src)
{
{
if (!file_exists($src)) {
if (!file_exists($src)) {
throw new PHPTAL_IOException("file $src not found");
throw new PHPTAL_IOException("file $src not found");
}
}
return $this->parseString($builder, file_get_contents($src), $src);
return $this->parseString($builder, file_get_contents($src), $src);
}
}


public function parseString(PHPTAL_Dom_DocumentBuilder $builder, $src, $filename = '<string>')
public function parseString(PHPTAL_Dom_DocumentBuilder $builder, $src, $filename = '<string>')
{
{
try
try
{
{
未偵測到變更。