Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: ExpatParser.php 123 2006-09-14 20:19:08Z mrook $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information please see
19
 * <http://phing.info>.
20
 */
21
 
22
require_once 'phing/parser/AbstractSAXParser.php';
23
include_once 'phing/parser/ExpatParseException.php';
24
include_once 'phing/system/io/IOException.php';
25
include_once 'phing/system/io/FileReader.php';
26
 
27
/**
28
 * This class is a wrapper for the PHP's internal expat parser.
29
 *
30
 * It takes an XML file represented by a abstract path name, and starts
31
 * parsing the file and calling the different "trap" methods inherited from
32
 * the AbstractParser class.
33
 *
34
 * Those methods then invoke the represenatative methods in the registered
35
 * handler classes.
36
 *
37
 * @author      Andreas Aderhold <andi@binarycloud.com>
38
 * @copyright © 2001,2002 THYRELL. All rights reserved
39
 * @version   $Revision: 1.8 $ $Date: 2006-09-14 22:19:08 +0200 (Thu, 14 Sep 2006) $
40
 * @access    public
41
 * @package   phing.parser
42
 */
43
 
44
class ExpatParser extends AbstractSAXParser {
45
 
46
    /** @var resource */
47
    private $parser;
48
 
49
    /** @var Reader */
50
    private $reader;
51
 
52
    private $file;
53
 
54
    private $buffer = 4096;
55
 
56
    private $error_string = "";
57
 
58
    private $line = 0;
59
 
60
    /** @var Location Current cursor pos in XML file. */
61
    private $location;
62
 
63
    /**
64
     * Constructs a new ExpatParser object.
65
     *
66
     * The constructor accepts a PhingFile object that represents the filename
67
     * for the file to be parsed. It sets up php's internal expat parser
68
     * and options.
69
     *
70
     * @param Reader $reader  The Reader Object that is to be read from.
71
     * @param string $filename Filename to read.
72
     * @throws Exception if the given argument is not a PhingFile object
73
     */
74
    function __construct(Reader $reader, $filename=null) {
75
 
76
        $this->reader = $reader;
77
        if ($filename !== null) {
78
            $this->file = new PhingFile($filename);
79
        }
80
        $this->parser = xml_parser_create();
81
        $this->buffer = 4096;
82
        $this->location = new Location();
83
        xml_set_object($this->parser, $this);
84
        xml_set_element_handler($this->parser, array($this,"startElement"),array($this,"endElement"));
85
        xml_set_character_data_handler($this->parser, array($this, "characters"));
86
    }
87
 
88
    /**
89
     * Override PHP's parser default settings, created in the constructor.
90
     *
91
     * @param  string  the option to set
92
     * @throws mixed   the value to set
93
     * @return boolean true if the option could be set, otherwise false
94
     * @access public
95
     */
96
    function parserSetOption($opt, $val) {
97
        return xml_parser_set_option($this->parser, $opt, $val);
98
    }
99
 
100
    /**
101
     * Returns the location object of the current parsed element. It describes
102
     * the location of the element within the XML file (line, char)
103
     *
104
     * @return object  the location of the current parser
105
     * @access public
106
     */
107
    function getLocation() {
108
        if ($this->file !== null) {
109
            $path = $this->file->getAbsolutePath();
110
        } else {
111
            $path = $this->reader->getResource();
112
        }
113
        $this->location = new Location($path, xml_get_current_line_number($this->parser), xml_get_current_column_number($this->parser));
114
        return $this->location;
115
    }
116
 
117
    /**
118
     * Starts the parsing process.
119
     *
120
     * @param  string  the option to set
121
     * @return int     1 if the parsing succeeded
122
     * @throws ExpatParseException if something gone wrong during parsing
123
     * @throws IOException if XML file can not be accessed
124
     * @access public
125
     */
126
    function parse() {
127
 
128
        while ( ($data = $this->reader->read()) !== -1 ) {
129
            if (!xml_parse($this->parser, $data, $this->reader->eof())) {
130
                $error = xml_error_string(xml_get_error_code($this->parser));
131
                $e = new ExpatParseException($error, $this->getLocation());
132
                xml_parser_free($this->parser);
133
                throw $e;
134
            }
135
        }
136
        xml_parser_free($this->parser);
137
 
138
        return 1;
139
    }
140
}