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
|
|
* <p>
|
22
|
|
* Sort a file before and/or after the file.
|
23
|
|
* </p>
|
24
|
|
*
|
25
|
|
* <p>
|
26
|
|
* Examples:
|
27
|
|
* </p>
|
28
|
|
*
|
29
|
|
* <pre>
|
30
|
|
* <copy todir="build">
|
31
|
|
* <fileset dir="input" includes="*.txt"/>
|
32
|
|
* <filterchain>
|
33
|
|
* <sortfilter/>
|
34
|
|
* </filterchain>
|
35
|
|
* </copy>
|
36
|
|
* </pre>
|
37
|
|
*
|
38
|
|
* <p>
|
39
|
|
* Sort all files <code>*.txt</code> from <i>src</i> location and copy
|
40
|
|
* them into <i>build</i> location. The lines of each file are sorted
|
41
|
|
* in ascendant order comparing the lines.
|
42
|
|
* </p>
|
43
|
|
*
|
44
|
|
* <pre>
|
45
|
|
* <copy todir="build">
|
46
|
|
* <fileset dir="input" includes="*.txt"/>
|
47
|
|
* <filterchain>
|
48
|
|
* <sortfilter reverse="true"/>
|
49
|
|
* </filterchain>
|
50
|
|
* </copy>
|
51
|
|
* </pre>
|
52
|
|
*
|
53
|
|
* <p>
|
54
|
|
* Sort all files <code>*.txt</code> from <i>src</i> location into reverse
|
55
|
|
* order and copy them into <i>build</i> location. If reverse parameter has
|
56
|
|
* value <code>true</code> (default value), then the output line of the files
|
57
|
|
* will be in ascendant order.
|
58
|
|
* </p>
|
59
|
|
*
|
60
|
|
* @author Siad.ardroumli <siad.ardroumli@gmail.com>
|
61
|
|
*
|
62
|
|
* @see BaseParamFilterReader
|
63
|
|
*
|
64
|
|
* @package phing.filters
|
65
|
|
*/
|
66
|
|
class SortFilter extends BaseParamFilterReader implements ChainableReader
|
67
|
|
{
|
68
|
|
/**
|
69
|
|
* Parameter name for reverse order.
|
70
|
|
*/
|
71
|
|
private static $REVERSE_KEY = "reverse";
|
72
|
|
|
73
|
|
/**
|
74
|
|
* Controls if the sorting process will be in ascendant/descendant order. If
|
75
|
|
* If has value <code>true</code>, then the line of the file will be
|
76
|
|
* sorted on descendant order. Default value: <code>false</code>. It will
|
77
|
|
* be considered only if <code>comparator</code> is <code>null</code>.
|
78
|
|
*/
|
79
|
|
private $reverse;
|
80
|
|
|
81
|
|
/**
|
82
|
|
* Stores the lines to be sorted.
|
83
|
|
*/
|
84
|
|
private $lines;
|
85
|
|
|
86
|
|
/**
|
87
|
|
* Creates a new filtered reader.
|
88
|
|
*
|
89
|
|
* @param Reader $in
|
90
|
|
* A Reader object providing the underlying stream. Must not be
|
91
|
|
* <code>null</code>.
|
92
|
|
*/
|
93
|
1
|
public function __construct(Reader $in = null)
|
94
|
|
{
|
95
|
1
|
parent::__construct($in);
|
96
|
|
}
|
97
|
|
|
98
|
|
/**
|
99
|
|
* Returns the next character in the filtered stream. If the desired number
|
100
|
|
* of lines have already been read, the resulting stream is effectively at
|
101
|
|
* an end. Otherwise, the next character from the underlying stream is read
|
102
|
|
* and returned.
|
103
|
|
*
|
104
|
|
* @param int $len
|
105
|
|
* @return string the next character in the resulting stream, or -1 if the end of
|
106
|
|
* the resulting stream has been reached
|
107
|
|
* @throws BuildException
|
108
|
|
* @throws IOException
|
109
|
|
* if the underlying stream throws an IOException during
|
110
|
|
* reading
|
111
|
|
*/
|
112
|
1
|
public function read($len = null)
|
113
|
|
{
|
114
|
1
|
if (!$this->getInitialized()) {
|
115
|
0
|
$this->initialize();
|
116
|
0
|
$this->setInitialized(true);
|
117
|
|
}
|
118
|
|
|
119
|
1
|
$buffer = $this->in->read($len);
|
120
|
|
|
121
|
1
|
if ($buffer === -1) {
|
122
|
1
|
return -1;
|
123
|
|
}
|
124
|
|
|
125
|
1
|
$this->lines = explode("\n", $buffer);
|
126
|
|
|
127
|
1
|
$this->sort();
|
128
|
|
|
129
|
1
|
$filtered_buffer = implode("\n", $this->lines);
|
130
|
|
|
131
|
1
|
return $filtered_buffer;
|
132
|
|
}
|
133
|
|
|
134
|
|
/**
|
135
|
|
* Creates a new SortReader using the passed in Reader for instantiation.
|
136
|
|
*
|
137
|
|
* @param Reader $reader
|
138
|
|
* A Reader object providing the underlying stream. Must not be
|
139
|
|
* <code>null</code>.
|
140
|
|
*
|
141
|
|
* @return SortFilter a new filter based on this configuration, but filtering the
|
142
|
|
* specified reader
|
143
|
|
*/
|
144
|
1
|
public function chain(Reader $reader): Reader
|
145
|
|
{
|
146
|
1
|
$newFilter = new SortFilter($reader);
|
147
|
1
|
$newFilter->setReverse($this->isReverse());
|
148
|
1
|
$newFilter->setInitialized(true);
|
149
|
1
|
return $newFilter;
|
150
|
|
}
|
151
|
|
|
152
|
|
/**
|
153
|
|
* Returns <code>true</code> if the sorting process will be in reverse
|
154
|
|
* order, otherwise the sorting process will be in ascendant order.
|
155
|
|
*
|
156
|
|
* @return boolean <code>true</code> if the sorting process will be in reverse
|
157
|
|
* order, otherwise the sorting process will be in ascendant order.
|
158
|
|
*/
|
159
|
1
|
public function isReverse()
|
160
|
|
{
|
161
|
1
|
return $this->reverse;
|
162
|
|
}
|
163
|
|
|
164
|
|
/**
|
165
|
|
* Sets the sorting process will be in ascendant (<code>reverse=false</code>)
|
166
|
|
* or to descendant (<code>reverse=true</code>).
|
167
|
|
*
|
168
|
|
* @param boolean $reverse
|
169
|
|
* Boolean representing reverse ordering process.
|
170
|
|
*/
|
171
|
1
|
public function setReverse($reverse)
|
172
|
|
{
|
173
|
1
|
$this->reverse = $reverse;
|
174
|
|
}
|
175
|
|
|
176
|
|
/**
|
177
|
|
* Scans the parameters list
|
178
|
|
*/
|
179
|
0
|
private function initialize()
|
180
|
|
{
|
181
|
|
// get parameters
|
182
|
0
|
$params = $this->getParameters();
|
183
|
|
|
184
|
0
|
foreach ($params as $param) {
|
185
|
0
|
$paramName = $param->getName();
|
186
|
0
|
if (self::$REVERSE_KEY === $paramName) {
|
187
|
0
|
$this->setReverse(StringHelper::booleanValue($param->getValue()));
|
188
|
0
|
continue;
|
189
|
|
}
|
190
|
|
}
|
191
|
|
}
|
192
|
|
|
193
|
|
/**
|
194
|
|
* Sorts the read lines (<code>$this->lines</code>) according to the sorting
|
195
|
|
* criteria defined by the user.
|
196
|
|
*/
|
197
|
1
|
private function sort()
|
198
|
|
{
|
199
|
1
|
if ($this->reverse) {
|
200
|
1
|
rsort($this->lines);
|
201
|
|
} else {
|
202
|
0
|
sort($this->lines);
|
203
|
|
}
|
204
|
|
}
|
205
|
|
}
|