summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/types/PatternSet.php
blob: 2963ab9ec1810fe0633fd4adf06b6435f2534204 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
/*
 *  $Id: PatternSet.php,v 1.8 2005/05/26 13:10:53 mrook Exp $
 *
 * 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/system/io/FileReader.php';
include_once 'phing/types/DataType.php';

/**
 * The patternset storage component. Carries all necessary data and methods
 * for the patternset stuff.
 *
 * @author   Andreas Aderhold, andi@binarycloud.com
 * @version  $Revision: 1.8 $
 * @package  phing.types
 */
class PatternSet extends DataType {

    private $includeList = array();
    private $excludeList = array();
    private $includesFileList = array();
    private $excludesFileList = array();

    /**
     * Makes this instance in effect a reference to another PatternSet
     * instance.
     * You must not set another attribute or nest elements inside
     * this element if you make it a reference.
     */
    function setRefid(Reference $r) {
        if (!empty($this->includeList) || !empty($this->excludeList)) {
            throw $this->tooManyAttributes();
        }
        parent::setRefid($r);
    }


    /**
    * Add a name entry on the include list
    *
    * @returns PatternSetNameEntry Reference to object
    * @throws  BuildException
    */
    function createInclude() {
        if ($this->isReference()) {
            throw $this->noChildrenAllowed();
        }
        return $this->addPatternToList($this->includeList);
    }


    /**
    * Add a name entry on the include files list
    *
    * @returns PatternSetNameEntry Reference to object
    * @throws  BuildException
    */
    function createIncludesFile() {
        if ($this->isReference()) {
            throw $this->noChildrenAllowed();
        }
        return $this->addPatternToList($this->includesFileList);
    }

    /**
    * Add a name entry on the exclude list
    *
    * @returns PatternSetNameEntry Reference to object
    * @throws  BuildException
    */
    function createExclude() {
        if ($this->isReference()) {
            throw $this->noChildrenAllowed();
        }
        return $this->addPatternToList($this->excludeList);
    }

    /**
     * add a name entry on the exclude files list
    *
    * @returns PatternSetNameEntry Reference to object
    * @throws  BuildException
     */

    function createExcludesFile() {
        if ($this->isReference()) {
            throw $this->noChildrenAllowed();
            return;
        }
        return $this->addPatternToList($this->excludesFileList);
    }


    /**
     * Sets the set of include patterns. Patterns may be separated by a comma
     * or a space.
     *
     * @param   string the string containing the include patterns
     * @returns void
     * @throws  BuildException
     */
    function setIncludes($includes) {
        if ($this->isReference()) {
            throw $this->tooManyAttributes();
        }
        if ($includes !== null && strlen($includes) > 0) {
            $tok = strtok($includes, ", ");
            while ($tok !== false) {
                $o = $this->createInclude();
                $o->setName($tok);
                $tok = strtok(", ");
            }
        }
    }


    /**
     * Sets the set of exclude patterns. Patterns may be separated by a comma
     * or a space.
     *
     * @param string the string containing the exclude patterns
    * @returns void
    * @throws  BuildException
     */

    function setExcludes($excludes) {
        if ($this->isReference()) {
            throw $this->tooManyAttributes();
        }
        if ($excludes !== null && strlen($excludes) > 0) {
            $tok = strtok($excludes, ", ");
            while ($tok !== false) {
                $o = $this->createExclude();
                $o->setName($tok);
                $tok = strtok(", ");
            }
        }
    }

    /**
     * add a name entry to the given list
     *
     * @param array List onto which the nameentry should be added
     * @returns PatternSetNameEntry  Reference to the created PsetNameEntry instance
     */
    private function addPatternToList(&$list) {
        $num = array_push($list, new PatternSetNameEntry());
        return $list[$num-1];
    }

    /**
     * Sets the name of the file containing the includes patterns.
     *
     * @param includesFile The file to fetch the include patterns from.
     */
    function setIncludesFile($includesFile) {
        if ($this->isReference()) {
            throw $this->tooManyAttributes();
        }
        if ($includesFile instanceof File) {
            $includesFile = $includesFile->getPath();
        }
        $o = $this->createIncludesFile();
        $o->setName($includesFile);
    }

    /**
     * Sets the name of the file containing the excludes patterns.
     *
     * @param excludesFile The file to fetch the exclude patterns from.
     */
    function setExcludesFile($excludesFile) {
        if ($this->isReference()) {
            throw $this->tooManyAttributes();
        }
        if ($excludesFile instanceof File) {
            $excludesFile = $excludesFile->getPath();
        }        
        $o = $this->createExcludesFile();
        $o->setName($excludesFile);
    }


    /**
     *  Reads path matching patterns from a file and adds them to the
     *  includes or excludes list
     */
    private function readPatterns(PhingFile $patternfile, &$patternlist, Project $p) {
        $patternReader = null;
        try {
            // Get a FileReader
            $patternReader = new BufferedReader(new FileReader($patternfile)); 
        
            // Create one NameEntry in the appropriate pattern list for each 
            // line in the file.
            $line = $patternReader->readLine();
            while ($line !== null) {
                if (!empty($line)) {
                    $line = $p->replaceProperties($line);
                    $this->addPatternToList($patternlist)->setName($line);
                }
                $line = $patternReader->readLine();
            }
            
        } catch (IOException $ioe)  {
            $msg = "An error occured while reading from pattern file: " . $patternfile->__toString(); 
            if($patternReader) $patternReader->close();            
            throw new BuildException($msg, $ioe);
        } 
        
        $patternReader->close();                
    }


    /** Adds the patterns of the other instance to this set. */
    function append($other, $p) {
        if ($this->isReference()) {
            throw new BuildException("Cannot append to a reference");
        }

        $incl = $other->getIncludePatterns($p);
        if ($incl !== null) {
            foreach($incl as $incl_name) {
                $o = $this->createInclude();
                $o->setName($incl_name);
            }
        }

        $excl = $other->getExcludePatterns($p);
        if ($excl !== null) {
            foreach($excl as $excl_name) {
                $o = $this->createExclude();
                $o->setName($excl_name);
            }
        }
    }

    /** Returns the filtered include patterns. */
    function getIncludePatterns(Project $p) {
        if ($this->isReference()) {
            $o = $this->getRef($p);
            return $o->getIncludePatterns($p);
        } else {
            $this->readFiles($p);
            return $this->makeArray($this->includeList, $p);
        }
    }

    /** Returns the filtered exclude patterns. */
    function getExcludePatterns(Project $p) {
        if ($this->isReference()) {
            $o = $this->getRef($p);
            return $o->getExcludePatterns($p);
        } else {
            $this->readFiles($p);
            return $this->makeArray($this->excludeList, $p);
        }
    }

    /** helper for FileSet. */
    function hasPatterns() {
        return (boolean) count($this->includesFileList) > 0 || count($this->excludesFileList) > 0
               || count($this->includeList) > 0 || count($this->excludeList) > 0;
    }

    /**
     * Performs the check for circular references and returns the
     * referenced PatternSet.
     */
    function getRef(Project $p) {
        if (!$this->checked) {
            $stk = array();
            array_push($stk, $this);
            $this->dieOnCircularReference($stk, $p);
        }
        $o = $this->ref->getReferencedObject($p);
        if (!($o instanceof PatternSet)) {
            $msg = $this->ref->getRefId()." doesn't denote a patternset";
            throw new BuildException($msg);
        } else {
            return $o;
        }
    }

    /** Convert a array of PatternSetNameEntry elements into an array of Strings. */
    private function makeArray(&$list, Project $p) {

        if (count($list) === 0) {
            return null;
        }

        $tmpNames = array();
        foreach($list as $ne) {
            $pattern = (string) $ne->evalName($p);
            if ($pattern !== null && strlen($pattern) > 0) {
                array_push($tmpNames, $pattern);
            }
        }
        return $tmpNames;
    }

    /** Read includesfile or excludesfile if not already done so. */
    private function readFiles(Project $p) {
        if (!empty($this->includesFileList)) {
            foreach($this->includesFileList as $ne) {
                $fileName = (string) $ne->evalName($p);
                if ($fileName !== null) {
                    $inclFile = $p->resolveFile($fileName);
                    if (!$inclFile->exists()) {
                        throw new BuildException("Includesfile ".$inclFile->getAbsolutePath()." not found.");
                    }
                    $this->readPatterns($inclFile, $this->includeList, $p);
                }
            }
            $this->includesFileList = array();
        }

        if (!empty($this->excludesFileList)) {
            foreach($this->excludesFileList as $ne) {               
                $fileName = (string) $ne->evalName($p);
                if ($fileName !== null) {
                    $exclFile = $p->resolveFile($fileName);
                    if (!$exclFile->exists()) {
                        throw new BuildException("Excludesfile ".$exclFile->getAbsolutePath()." not found.");
                        return;
                    }
                    $this->readPatterns($exclFile, $this->excludeList, $p);
                }
            }
            $this->excludesFileList = array();
        }
    }


    function toString() {
        
        // We can't compile includeList into array because, toString() does
        // not know about project:
        //
        // $includes = $this->makeArray($this->includeList, $this->project);
        // $excludes = $this->makeArray($this->excludeList, $this->project);
            
        if (empty($this->includeList)) {
            $includes = "empty";
        } else {
            $includes = "";
            foreach($this->includeList as $ne) {
                $includes .= $ne->toString() . ",";
            }
            $includes = rtrim($includes, ",");
        }
        
        if (empty($this->excludeList)) {
            $excludes = "empty";
        } else {
            $excludes = "";
            foreach($this->excludeList as $ne) {
                $excludes .= $ne->toString() . ",";
            }
            $excludes = rtrim($excludes, ",");
        }
               
        return "patternSet{ includes: $includes  excludes: $excludes }";
    }
}


/*
 * Note, this class here should become a nested class to
 * PatternSet (PatternSet:NameEntry) as it is only needed
 * internally.
 * This is not possible with php 4.x right now so we place
 * this class (against good style) in this file.
 */

class PatternSetNameEntry {

    private $name       = null;
    private $ifCond     = null;
    private $unlessCond = null;

    function setName($name) {
        $this->name = (string) $name;
    }


    function setIf($cond) {
        $this->ifCond = (string) $cond;
    }


    function setUnless($cond) {
        $this->unlessCond = (string) $cond;
    }


    function getName() {
        return $this->name;
    }


    function evalName($project) {
        return $this->valid($project) ? $this->name : null;
    }


    function valid($project) {
        if ($this->ifCond !== null && $project->getProperty($this->ifCond) === null) {
            return false;
        } else if ($this->unlessCond !== null && $project->getProperty($this->unlessCond) !== null) {
            return false;
        }
        return true;
    }


    function toString() {
        $buf = $this->name;
        if (($this->ifCond !== null) || ($this->unlessCond !== null)) {
            $buf .= ":";
            $connector = "";

            if ($this->ifCond !== null) {
                $buf .= "if->{$this->ifCond}";
                $connector = ";";
            }
            if ($this->unlessCond !== null) {
                $buf .= "$connector unless->{$this->unlessCond}";
            }
        }
        return $buf;
    }
}