1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * This class is a wrapper for the PHP's internal expat parser.
22
 *
23
 * It takes an XML file represented by a abstract path name, and starts
24
 * parsing the file and calling the different "trap" methods inherited from
25
 * the AbstractParser class.
26
 *
27
 * Those methods then invoke the represenatative methods in the registered
28
 * handler classes.
29
 *
30
 * @author    Andreas Aderhold <andi@binarycloud.com>
31
 * @copyright 2001,2002 THYRELL. All rights reserved
32
 * @package   phing.parser
33
 */
34
class ExpatParser extends AbstractSAXParser
35
{
36

37
    /**
38
     * @var resource
39
     */
40
    private $parser;
41

42
    /**
43
     * @var Reader
44
     */
45
    private $reader;
46

47
    /**
48
     * @var SplFileObject
49
     */
50
    private $file;
51

52
    private $buffer = 4096;
53

54
    /**
55
     * @var Location Current cursor pos in XML file.
56
     */
57
    private $location;
58

59
    /**
60
     * Constructs a new ExpatParser object.
61
     *
62
     * The constructor accepts a PhingFile object that represents the filename
63
     * for the file to be parsed. It sets up php's internal expat parser
64
     * and options.
65
     *
66
     * @param  Reader $reader The Reader Object that is to be read from.
67
     * @param  string $filename Filename to read.
68
     * @throws Exception if the given argument is not a PhingFile object
69
     */
70 1
    public function __construct(Reader $reader, $filename = null)
71
    {
72 1
        $this->reader = $reader;
73 1
        if ($filename !== null) {
74 0
            $this->file = new SplFileObject($filename);
75
        }
76 1
        $this->parser = xml_parser_create();
77 1
        $this->buffer = 4096;
78 1
        $this->location = new Location();
79 1
        xml_set_object($this->parser, $this);
80 1
        xml_set_element_handler($this->parser, [$this, "startElement"], [$this, "endElement"]);
81 1
        xml_set_character_data_handler($this->parser, [$this, "characters"]);
82
    }
83

84
    /**
85
     * Override PHP's parser default settings, created in the constructor.
86
     *
87
     * @param    $opt
88
     * @param    $val
89
     * @internal param the $string option to set
90
     * @return   boolean true if the option could be set, otherwise false
91
     */
92 1
    public function parserSetOption($opt, $val)
93
    {
94 1
        return xml_parser_set_option($this->parser, $opt, $val);
95
    }
96

97
    /**
98
     * Returns the location object of the current parsed element. It describes
99
     * the location of the element within the XML file (line, char)
100
     *
101
     * @return Location the location of the current parser
102
     */
103 1
    public function getLocation()
104
    {
105 1
        if ($this->file !== null) {
106 0
            $path = $this->file->getRealPath() !== false ? $this->file->getRealPath() : null;
107
        } else {
108 1
            $path = $this->reader->getResource();
109
        }
110 1
        $this->location = new Location(
111 1
            $path,
112 1
            xml_get_current_line_number($this->parser),
113 1
            xml_get_current_column_number(
114 1
                $this->parser
115
            )
116
        );
117

118 1
        return $this->location;
119
    }
120

121
    /**
122
     * Starts the parsing process.
123
     *
124
     * @return int                 1 if the parsing succeeded
125
     * @throws ExpatParseException if something gone wrong during parsing
126
     * @throws IOException         if XML file can not be accessed
127
     */
128 1
    public function parse()
129
    {
130 1
        while (($data = $this->reader->read()) !== -1) {
131 1
            if (!xml_parse($this->parser, $data, $this->reader->eof())) {
132 0
                $error = xml_error_string(xml_get_error_code($this->parser));
133 0
                $e = new ExpatParseException($error, $this->getLocation());
134 0
                xml_parser_free($this->parser);
135 0
                throw $e;
136
            }
137
        }
138 1
        xml_parser_free($this->parser);
139

140 1
        return 1;
141
    }
142
}

Read our documentation on viewing source code .

Loading