summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/filters/util/ChainReaderHelper.php
blob: c465d0a16e76e1fd7929f05738cebed077ecb069 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
 *  $Id: c1709ddb9da44ce62fbe072c61d29dba4814e3f6 $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
*/

include_once 'phing/Project.php';
include_once 'phing/filters/BaseFilterReader.php';
include_once 'phing/types/PhingFilterReader.php';
include_once 'phing/types/FilterChain.php';
include_once 'phing/types/Parameter.php';
include_once 'phing/util/FileUtils.php';
include_once 'phing/util/StringHelper.php';
include_once 'phing/filters/ChainableReader.php';

/**
 * Process a FilterReader chain.
 *
 * Here, the interesting method is 'getAssembledReader'.
 * The purpose of this one is to create a simple Reader object which
 * apply all filters on another primary Reader object. 
 *
 * For example : In copyFile (phing.util.FileUtils) the primary Reader
 * is a FileReader object (more accuratly, a BufferedReader) previously 
 * setted for the source file to copy. So, consider this filterchain :
 *        
 *     <filterchain>
 *        <stripphpcomments />
 *        <linecontains>
 *            <contains value="foo">
 *        </linecontains>
 *      <tabtospaces tablength="8" />
 *    </filterchain>
 *
 *    getAssembledReader will return a Reader object wich read on each
 *    of these filters. Something like this : ('->' = 'which read data from') :
 *
 *  [TABTOSPACES] -> [LINECONTAINS] -> [STRIPPHPCOMMENTS] -> [FILEREADER]
 *                                                         (primary reader)
 *
 *  So, getAssembledReader will return the TABTOSPACES Reader object. Then
 *  each read done with this Reader object will follow this path.
 *
 *    Hope this explanation is clear :)
 *
 * TODO: Implement the classPath feature.
 *
 * @author    <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
 * @version   $Id$
 * @access    public
 * @package   phing.filters.util
*/
class ChainReaderHelper {
    
    /** Primary reader to wich the reader chain is to be attached */
    private $primaryReader = null;
    
    /** The site of the buffer to be used. */
    private $bufferSize = 8192;
    
    /** Chain of filters */
    private $filterChains = array();
    
    /** The Phing project */
    private $project;

    /*
     * Sets the primary reader
    */
    function setPrimaryReader(Reader $reader) {
        $this->primaryReader = $reader;
    }

    /*
     * Set the project to work with
    */
    function setProject(Project $project) {
        $this->project = $project;
    }

    /*
     * Get the project
    */
    function getProject() {
        return $this->project;
    }

    /*
     * Sets the buffer size to be used.  Defaults to 8192,
     * if this method is not invoked.
    */
    function setBufferSize($size) {
        $this->bufferSize = $size;
    }

    /*
     * Sets the collection of filter reader sets
    */
    function setFilterChains(&$fchain) {
        $this->filterChains = &$fchain;
    }

    /*
     * Assemble the reader
    */
    function getAssembledReader() {
    
        $instream = $this->primaryReader;
        $filterReadersCount = count($this->filterChains);
        $finalFilters = array();

        // Collect all filter readers of all filter chains used ...
        for($i = 0 ; $i<$filterReadersCount ; $i++) {
            $filterchain = &$this->filterChains[$i];
            $filterReaders = $filterchain->getFilterReaders();
            $readerCount = count($filterReaders);
            for($j = 0 ; $j<$readerCount ; $j++) {
                $finalFilters[] = $filterReaders[$j];
            }
        }

        // ... then chain the filter readers.
        $filtersCount = count($finalFilters);
        if ( $filtersCount > 0 ) {
            for($i = 0 ; $i<$filtersCount ; $i++) {
                $filter = $finalFilters[$i];
                
                if ( $filter instanceof PhingFilterReader ) {
                
                    // This filter reader is an external class.
                    $className = $filter->getClassName();
                    $classpath = $filter->getClasspath();
                    $project   = $filter->getProject();
                    
                    if ( $className !== null ) {
                        $cls = Phing::import($className, $classpath);
                        $impl = new $cls();                        
                    }

                    if ( !($impl instanceof FilterReader) ) {
                        throw new Exception($className." does not extend phing.system.io.FilterReader");
                    }
                    
                    $impl->setReader($instream); // chain
                    $impl->setProject($this->getProject()); // what about $project above ?

                    if ( $impl instanceof Parameterizable ) {
                        $impl->setParameters($filter->getParams());
                    }
                    
                    $instream = $impl; // now that it's been chained
                                                            
                } elseif (($filter instanceof ChainableReader) && ($filter instanceof Reader)) {                   
                    if ( $this->getProject() !== null && ($filter instanceof BaseFilterReader) ) {
                        $filter->setProject($this->getProject());
                    }                    
                    $instream = $filter->chain($instream);
                } else {
                    throw new Exception("Cannot chain invalid filter: " . get_class($filter));
                }
            }
        }

        return $instream;
    }    

}