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
|
|
* Applies Xinclude parsing to incoming text.
|
22
|
|
*
|
23
|
|
* Uses PHP DOM XML support
|
24
|
|
*
|
25
|
|
* @author Bill Karwin <bill@karwin.com>
|
26
|
|
* @see FilterReader
|
27
|
|
* @package phing.filters
|
28
|
|
*/
|
29
|
|
class XincludeFilter extends BaseParamFilterReader implements ChainableReader
|
30
|
|
{
|
31
|
|
/** @var PhingFile */
|
32
|
|
private $basedir = null;
|
33
|
|
|
34
|
|
/**
|
35
|
|
* @var bool
|
36
|
|
*/
|
37
|
|
private $processed = false;
|
38
|
|
|
39
|
|
/**
|
40
|
|
* Whether to resolve entities.
|
41
|
|
*
|
42
|
|
* @var bool
|
43
|
|
*
|
44
|
|
* @since 2.4
|
45
|
|
*/
|
46
|
|
private $resolveExternals = false;
|
47
|
|
|
48
|
|
/**
|
49
|
|
* Whether to resolve entities.
|
50
|
|
*
|
51
|
|
* @param $resolveExternals
|
52
|
|
*
|
53
|
|
* @since 2.4
|
54
|
|
*/
|
55
|
0
|
public function setResolveExternals(bool $resolveExternals)
|
56
|
|
{
|
57
|
0
|
$this->resolveExternals = $resolveExternals;
|
58
|
|
}
|
59
|
|
|
60
|
|
/**
|
61
|
|
* @return bool
|
62
|
|
*
|
63
|
|
* @since 2.4
|
64
|
|
*/
|
65
|
0
|
public function getResolveExternals()
|
66
|
|
{
|
67
|
0
|
return $this->resolveExternals;
|
68
|
|
}
|
69
|
|
|
70
|
|
/**
|
71
|
|
* @param PhingFile $dir
|
72
|
|
*/
|
73
|
0
|
public function setBasedir(PhingFile $dir)
|
74
|
|
{
|
75
|
0
|
$this->basedir = $dir;
|
76
|
|
}
|
77
|
|
|
78
|
|
/**
|
79
|
|
* @return PhingFile
|
80
|
|
*/
|
81
|
0
|
public function getBasedir()
|
82
|
|
{
|
83
|
0
|
return $this->basedir;
|
84
|
|
}
|
85
|
|
|
86
|
|
/**
|
87
|
|
* Reads stream, applies XSLT and returns resulting stream.
|
88
|
|
*
|
89
|
|
* @param int $len
|
90
|
|
* @return string transformed buffer.
|
91
|
|
* @throws IOException
|
92
|
|
* @throws BuildException
|
93
|
|
*/
|
94
|
0
|
public function read($len = null)
|
95
|
|
{
|
96
|
0
|
if (!class_exists('DOMDocument')) {
|
97
|
0
|
throw new BuildException("Could not find the DOMDocument class. Make sure PHP has been compiled/configured to support DOM XML.");
|
98
|
|
}
|
99
|
|
|
100
|
0
|
if ($this->processed === true) {
|
101
|
0
|
return -1; // EOF
|
102
|
|
}
|
103
|
|
|
104
|
|
// Read XML
|
105
|
0
|
$_xml = null;
|
106
|
0
|
while (($data = $this->in->read($len)) !== -1) {
|
107
|
0
|
$_xml .= $data;
|
108
|
|
}
|
109
|
|
|
110
|
0
|
if ($_xml === null) { // EOF?
|
111
|
0
|
return -1;
|
112
|
|
}
|
113
|
|
|
114
|
0
|
if (empty($_xml)) {
|
115
|
0
|
$this->log("XML file is empty!", Project::MSG_WARN);
|
116
|
|
|
117
|
0
|
return '';
|
118
|
|
}
|
119
|
|
|
120
|
0
|
$this->log("Transforming XML " . $this->in->getResource() . " using Xinclude ", Project::MSG_VERBOSE);
|
121
|
|
|
122
|
0
|
$out = '';
|
123
|
|
try {
|
124
|
0
|
$out = $this->process($_xml);
|
125
|
0
|
$this->processed = true;
|
126
|
0
|
} catch (IOException $e) {
|
127
|
0
|
throw new BuildException($e);
|
128
|
|
}
|
129
|
|
|
130
|
0
|
return $out;
|
131
|
|
}
|
132
|
|
|
133
|
|
/**
|
134
|
|
* Try to process the Xinclude transformation
|
135
|
|
*
|
136
|
|
* @param string XML to process.
|
137
|
|
*
|
138
|
|
* @return string
|
139
|
|
*/
|
140
|
0
|
protected function process($xml)
|
141
|
|
{
|
142
|
0
|
if ($this->basedir) {
|
143
|
0
|
$cwd = getcwd();
|
144
|
0
|
chdir($this->basedir);
|
145
|
|
}
|
146
|
|
|
147
|
|
// Create and setup document.
|
148
|
0
|
$xmlDom = new DOMDocument();
|
149
|
0
|
$xmlDom->resolveExternals = $this->resolveExternals;
|
150
|
|
|
151
|
0
|
$xmlDom->loadXML($xml);
|
152
|
|
|
153
|
0
|
$xmlDom->xinclude();
|
154
|
|
|
155
|
0
|
if ($this->basedir) {
|
156
|
0
|
chdir($cwd);
|
157
|
|
}
|
158
|
|
|
159
|
0
|
return $xmlDom->saveXML();
|
160
|
|
}
|
161
|
|
|
162
|
|
/**
|
163
|
|
* Creates a new XincludeFilter using the passed in
|
164
|
|
* Reader for instantiation.
|
165
|
|
*
|
166
|
|
* @param Reader A Reader object providing the underlying stream.
|
167
|
|
* Must not be <code>null</code>.
|
168
|
|
*
|
169
|
|
* @return XincludeFilter A new filter based on this configuration, but filtering
|
170
|
|
* the specified reader
|
171
|
|
*/
|
172
|
0
|
public function chain(Reader $reader): Reader
|
173
|
|
{
|
174
|
0
|
$newFilter = new self($reader);
|
175
|
0
|
$newFilter->setProject($this->getProject());
|
176
|
0
|
$newFilter->setBasedir($this->getBasedir());
|
177
|
|
|
178
|
0
|
return $newFilter;
|
179
|
|
}
|
180
|
|
}
|