Ticket #68: XincludeFilter.php

File XincludeFilter.php, 3.9 KB (added by bkarwin, 5 years ago)

Xinclude filter class.

Line 
1<?php
2
3/*
4 *  $Id: XincludeFilter.php,v 1.16 2005/12/07 20:05:01 hlellelid Exp $
5 *
6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 *
18 * This software consists of voluntary contributions made by many individuals
19 * and is licensed under the LGPL. For more information please see
20 * <http://phing.info>.
21 */
22
23include_once 'phing/filters/BaseParamFilterReader.php';
24include_once 'phing/filters/ChainableReader.php';
25
26/**
27 * Applies Xinclude parsing to incoming text.
28 *
29 * Uses PHP DOM XML support
30 *
31 * @author    Bill Karwin <bill@karwin.com>
32 * @version   $Revision: 1.16 $
33 * @see       FilterReader
34 * @package   phing.filters
35 */
36class XincludeFilter extends BaseParamFilterReader implements ChainableReader {
37
38    private $basedir = null;
39
40    public function setBasedir(PhingFile $dir)
41    {
42        $this->basedir = $dir;
43    }
44
45    public function getBasedir()
46    {
47        return $this->basedir;
48    }
49
50    /**
51     * Reads stream, applies XSLT and returns resulting stream.
52     * @return string transformed buffer.
53     * @throws BuildException - if XSLT support missing, if error in xslt processing
54     */
55    function read($len = null) {
56       
57        if (!class_exists('DomDocument')) {
58            throw new BuildException("Could not find the DomDocument class. Make sure PHP has been compiled/configured to support DOM XML.");
59        }
60       
61        if ($this->processed === true) {
62            return -1; // EOF
63        }
64       
65        // Read XML
66        $_xml = null;
67        while ( ($data = $this->in->read($len)) !== -1 )
68            $_xml .= $data;
69
70        if ($_xml === null ) { // EOF?
71            return -1;
72        }
73
74        if (empty($_xml)) {
75            $this->log("XML file is empty!", PROJECT_MSG_WARN);
76            return '';
77        }
78       
79        $this->log("Transforming XML " . $this->in->getResource() . " using Xinclude ", PROJECT_MSG_VERBOSE);
80       
81        $out = '';
82        try {
83            $out = $this->process($_xml);
84            $this->processed = true;
85        } catch (IOException $e) {           
86            throw new BuildException($e);
87        }
88
89        return $out;
90    }
91
92    /**
93     * Try to process the Xinclude transformation
94     *
95     * @param   string  XML to process.
96     *
97     * @throws BuildException   On errors
98     */
99    protected function process($xml) {   
100               
101        if ($this->basedir) {
102            $cwd = getcwd();
103            chdir($this->basedir);
104        }
105
106        $xmlDom = new DomDocument();
107        $xmlDom->loadXML($xml);
108       
109        $xmlDom->xinclude();
110
111        if ($this->basedir) {
112            chdir($cwd);
113        }
114
115        return $xmlDom->saveXML();
116    }   
117
118    /**
119     * Creates a new XincludeFilter using the passed in
120     * Reader for instantiation.
121     *
122     * @param Reader A Reader object providing the underlying stream.
123     *               Must not be <code>null</code>.
124     *
125     * @return Reader A new filter based on this configuration, but filtering
126     *         the specified reader
127     */
128    function chain(Reader $reader) {
129        $newFilter = new XincludeFilter($reader);
130        $newFilter->setProject($this->getProject());
131        $newFilter->setBasedir($this->getBasedir());
132        return $newFilter;
133    }
134
135}
136
137?>