summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/types/AbstractFileSet.php
blob: 60ca4d04f804c2439181ba2526f256c0e50af1fc (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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<?php
/*
 *  $Id: b7d8ccab6ed556e74626d880dcc09be20ea8bd58 $
 *
 * 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/PhingFile.php';
include_once 'phing/types/DataType.php';
include_once 'phing/types/PatternSet.php';
include_once 'phing/types/selectors/BaseSelector.php';
include_once 'phing/types/selectors/SelectorContainer.php';
include_once 'phing/types/selectors/BaseSelectorContainer.php';

// Load all of the selectors (not really necessary but
// helps reveal parse errors right away)

include_once 'phing/types/selectors/AndSelector.php';
include_once 'phing/types/selectors/ContainsSelector.php';
include_once 'phing/types/selectors/ContainsRegexpSelector.php';
include_once 'phing/types/selectors/DateSelector.php';
include_once 'phing/types/selectors/DependSelector.php';
include_once 'phing/types/selectors/DepthSelector.php';
include_once 'phing/types/selectors/ExtendSelector.php';
include_once 'phing/types/selectors/FilenameSelector.php';
include_once 'phing/types/selectors/MajoritySelector.php';
include_once 'phing/types/selectors/NoneSelector.php';
include_once 'phing/types/selectors/NotSelector.php';
include_once 'phing/types/selectors/OrSelector.php';
include_once 'phing/types/selectors/PresentSelector.php';
include_once 'phing/types/selectors/SizeSelector.php';
include_once 'phing/types/selectors/TypeSelector.php';

include_once 'phing/util/DirectoryScanner.php';

/**
 * The FileSet class provides methods and properties for accessing
 * and managing filesets. It extends ProjectComponent and thus inherits
 * all methods and properties (not explicitly declared). See ProjectComponent
 * for further detail.
 *
 * TODO:
 *   - merge this with patternsets: FileSet extends PatternSet !!!
 *     requires additional mods to the parsing algo
 *         [HL] .... not sure if that really makes so much sense.  I think
 *            that perhaps they should use common utility class if there really
 *            is that much shared functionality
 *
 * @author    Andreas Aderhold <andi@binarycloud.com>
 * @author    Hans Lellelid <hans@xmpl.org>
 * @version    $Id$
 * @see        ProjectComponent
 * @package    phing.types
 */
class AbstractFileSet extends DataType implements SelectorContainer {
    
    // These vars are public for cloning purposes
    
    /**
     * @var boolean
     */
    public $useDefaultExcludes = true;
    
    /** 
     * Whether to expand/dereference symbolic links, default is false
     * @var boolean
     */
    protected $expandSymbolicLinks = false;
    
    /**
     * @var PatternSet
     */
    public $defaultPatterns;
    
    public $additionalPatterns = array();
    public $dir;
    public $isCaseSensitive = true;    
    public $selectors = array();
    
    function __construct($fileset = null) {
        if ($fileset !== null && ($fileset instanceof FileSet)) {
            $this->dir = $fileset->dir;
            $this->defaultPatterns = $fileset->defaultPatterns;
            $this->additionalPatterns = $fileset->additionalPatterns;
            $this->useDefaultExcludes = $fileset->useDefaultExcludes;
            $this->isCaseSensitive = $fileset->isCaseSensitive;
            $this->selectors = $fileset->selectors;
        }
        $this->defaultPatterns = new PatternSet();
    }
    
    /** 
     * Sets whether to expand/dereference symbolic links, default is false
     * @var boolean
     */
    function setExpandSymbolicLinks($expandSymbolicLinks)
    {
        $this->expandSymbolicLinks = $expandSymbolicLinks;
    }

    /**
    * 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 ((isset($this->dir) && !is_null($this->dir)) || $this->defaultPatterns->hasPatterns()) {
            throw $this->tooManyAttributes();
        }
        if (!empty($this->additionalPatterns)) {
            throw $this->noChildrenAllowed();
        }
        if (!empty($this->selectors)) {
            throw $this->noChildrenAllowed();
        }
        parent::setRefid($r);
    }


    function setDir($dir) {
        if ($this->isReference()) {
            throw $this->tooManyAttributes();
        }
        if ($dir instanceof PhingFile) {
            $dir = $dir->getPath();
        }
        $this->dir = new PhingFile((string) $dir);
    }


    function getDir(Project $p) {
        if ($this->isReference()) {
            return $this->getRef($p)->getDir($p);
        }
        return $this->dir;
    }


    function createPatternSet() {
        if ($this->isReference()) {
            throw $this->noChildrenAllowed();
        }
        $num = array_push($this->additionalPatterns, new PatternSet());
        return $this->additionalPatterns[$num-1];
    }

    /**
    * add a name entry on the include list
    */
    function createInclude() {
        if ($this->isReference()) {
            throw $this->noChildrenAllowed();
        }
        return $this->defaultPatterns->createInclude();
    }

    /**
     * add a name entry on the include files list
     */
    function createIncludesFile() {
        if ($this->isReference()) {
            throw $this->noChildrenAllowed();
        }
        return $this->defaultPatterns->createIncludesFile();
    }

    /**
     * add a name entry on the exclude list
     */
    function createExclude() {
        if ($this->isReference()) {
            throw $this->noChildrenAllowed();
        }
        return $this->defaultPatterns->createExclude();
    }

    /**
     * add a name entry on the include files list
     */
    function createExcludesFile() {
        if ($this->isReference()) {
            throw $this->noChildrenAllowed();
            return;
        }
        return $this->defaultPatterns->createExcludesFile();
    }

    /**
     * Sets the set of include patterns. Patterns may be separated by a comma
     * or a space.
     */
    function setIncludes($includes) {
        if ($this->isReference()) {
            throw $this->tooManyAttributes();
        }
        $this->defaultPatterns->setIncludes($includes);
    }

    /**
     * Sets the set of exclude patterns. Patterns may be separated by a comma
     * or a space.
     */
    function setExcludes($excludes) {
        if ($this->isReference()) {
            throw $this->tooManyAttributes();
        }
        $this->defaultPatterns->setExcludes($excludes);
    }

    /**
     * Sets the name of the file containing the includes patterns.
     *
     * @param $incl The file to fetch the include patterns from.
     * @throws BE
     */
    function setIncludesfile($incl) {
        if ($this->isReference()) {
            throw $this->tooManyAttributes();
        }
        $this->defaultPatterns->setIncludesfile($incl);
    }

    /**
     * Sets the name of the file containing the includes patterns.
     *
     * @param $excl The file to fetch the exclude patterns from.
     * @throws BE
     */
    function setExcludesfile($excl) {
        if ($this->isReference()) {
            throw $this->tooManyAttributes();
        }
        $this->defaultPatterns->setExcludesfile($excl);
    }

    /**
     * Sets whether default exclusions should be used or not.
     *
     * @param $useDefaultExcludes "true"|"on"|"yes" when default exclusions
     *                           should be used, "false"|"off"|"no" when they
     *                           shouldn't be used.
     */
    function setDefaultexcludes($useDefaultExcludes) {
        if ($this->isReference()) {
            throw $this->tooManyAttributes();
        }
        $this->useDefaultExcludes = $useDefaultExcludes;
    }

    /**
     * Sets case sensitivity of the file system
     */
    function setCaseSensitive($isCaseSensitive) {
        $this->isCaseSensitive = $isCaseSensitive;
    }

    /** returns a reference to the dirscanner object belonging to this fileset */
    function getDirectoryScanner(Project $p) {
        if ($this->isReference()) {
            $o = $this->getRef($p);
            return $o->getDirectoryScanner($p);
        }

        if ($this->dir === null) {
            throw new BuildException("No directory specified for fileset.");
        }
        if (!$this->dir->exists()) {
            throw new BuildException("Directory ".$this->dir->getAbsolutePath()." not found.");
        }
        if (!$this->dir->isLink() || !$this->expandSymbolicLinks) {
            if (!$this->dir->isDirectory()) {
                throw new BuildException($this->dir->getAbsolutePath()." is not a directory.");
            }
        }
        $ds = new DirectoryScanner();
        $ds->setExpandSymbolicLinks($this->expandSymbolicLinks);
        $this->setupDirectoryScanner($ds, $p);
        $ds->scan();
        return $ds;
    }

    /** feed dirscanner with infos defined by this fileset */
    protected function setupDirectoryScanner(DirectoryScanner $ds, Project $p) {
        if ($ds === null) {
            throw new Exception("DirectoryScanner cannot be null");
        }
        // FIXME - pass dir directly wehn dirscanner supports File
        $ds->setBasedir($this->dir->getPath());
        
        foreach($this->additionalPatterns as $addPattern) {
            $this->defaultPatterns->append($addPattern, $p);
        }              

        $ds->setIncludes($this->defaultPatterns->getIncludePatterns($p));
        $ds->setExcludes($this->defaultPatterns->getExcludePatterns($p));

        $p->log("FileSet: Setup file scanner in dir " . $this->dir->__toString() . " with " . $this->defaultPatterns->toString(), Project::MSG_DEBUG);
        
        if ($ds instanceof SelectorScanner) {
            $ds->setSelectors($this->getSelectors($p));
        }
        
        if ($this->useDefaultExcludes) {
            $ds->addDefaultExcludes();
        }
        $ds->setCaseSensitive($this->isCaseSensitive);
    }


    /**
     * Performs the check for circular references and returns the
     * referenced FileSet.
     */
    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 FileSet)) {
            $msg = $this->ref->getRefId()." doesn't denote a fileset";
            throw new BuildException($msg);
        } else {
            return $o;
        }
    }
    
    // SelectorContainer methods

    /**
     * Indicates whether there are any selectors here.
     *
     * @return boolean Whether any selectors are in this container
     */
    public function hasSelectors() {
        if ($this->isReference() && $this->getProject() !== null) {
            return $this->getRef($this->getProject())->hasSelectors();
        }
        return !empty($this->selectors);
    }

    /**
     * Indicates whether there are any patterns here.
     *
     * @return boolean Whether any patterns are in this container.
     */
    public function hasPatterns() {
    
        if ($this->isReference() && $this->getProject() !== null) {
            return $this->getRef($this->getProject())->hasPatterns();            
        }

        if ($this->defaultPatterns->hasPatterns($this->getProject())) {
            return true;
        }

        for($i=0,$size=count($this->additionalPatterns); $i < $size; $i++) {
            $ps = $this->additionalPatterns[$i];
            if ($ps->hasPatterns($this->getProject())) {
                return true;
            }
        }

        return false;
    }
    
    /**
     * Gives the count of the number of selectors in this container
     *
     * @return int The number of selectors in this container
     */
    public function selectorCount() {
        if ($this->isReference() && $this->getProject() !== null) {
            try {
                return $this->getRef($this->getProject())->selectorCount();
            } catch (Exception $e) {
                throw $e;
            }
        }
        return count($this->selectors);
    }

    /**
     * Returns the set of selectors as an array.
     *
     * @return an array of selectors in this container
     */
    public function getSelectors(Project $p) {
        if ($this->isReference()) {
            return $this->getRef($p)->getSelectors($p);            
        } else {
            // *copy* selectors
            $result = array();
            for($i=0,$size=count($this->selectors); $i < $size; $i++) {
                $result[] = clone $this->selectors[$i];
            }
            return $result;
        }
    }

    /**
     * Returns an array for accessing the set of selectors.
     *
     * @return array The array of selectors
     */
    public function selectorElements() {
        if ($this->isReference() && $this->getProject() !== null) {
            return $this->getRef($this->getProject())->selectorElements();            
        }
        return $this->selectors;
    }

    /**
     * Add a new selector into this container.
     *
     * @param selector the new selector to add
     */
    public function appendSelector(FileSelector $selector) {
        if ($this->isReference()) {
            throw $this->noChildrenAllowed();
        }
        $this->selectors[] = $selector;
    }    

    /* Methods below all add specific selectors */

    /**
     * add a "Select" selector entry on the selector list
     */
    public function createSelector() {
        $o = new SelectSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add an "And" selector entry on the selector list
     */
    public function createAnd() {
        $o = new AndSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add an "Or" selector entry on the selector list
     */
    public function createOr() {
        $o = new OrSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a "Not" selector entry on the selector list
     */
    public function createNot() {
        $o = new NotSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a "None" selector entry on the selector list
     */
    public function createNone() {
        $o = new NoneSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a majority selector entry on the selector list
     */
    public function createMajority() {
        $o = new MajoritySelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a selector date entry on the selector list
     */
    public function createDate() {
        $o = new DateSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a selector size entry on the selector list
     */
    public function createSize() {
        $o = new SizeSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a selector filename entry on the selector list
     */
    public function createFilename() {
        $o = new FilenameSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add an extended selector entry on the selector list
     */
    public function createCustom() {
        $o = new ExtendSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a contains selector entry on the selector list
     */
    public function createContains() {
        $o = new ContainsSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a contains selector entry on the selector list
     */
    public function createContainsRegexp() {
        $o = new ContainsRegexpSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a present selector entry on the selector list
     */
    public function createPresent() {
        $o = new PresentSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a depth selector entry on the selector list
     */
    public function createDepth() {
        $o = new DepthSelector();
        $this->appendSelector($o);
        return $o;
    }

    /**
     * add a depends selector entry on the selector list
     */
    public function createDepend() {        
        $o = new DependSelector();
        $this->appendSelector($o);
        return $o;
    }
    
    /**
     * add a type selector entry on the selector list
     */
    public function createType() {
        $o = new TypeSelector();
        $this->appendSelector($o);
        return $o;
    }
}