Passed
Push — main ( ddd1a4...b98cb8 )
by Michiel
08:47
created

XmlPropertyTask::getCollapseAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
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
namespace Phing\Task\System;
21
22
use Phing\Exception\BuildException;
23
use Phing\Io\IOException;
24
use Phing\Io\File;
25
use Phing\Io\XmlFileParser;
26
use Phing\Project;
27
use Phing\Task\System\PropertyTask;
28
use Phing\Util\Properties;
29
30
/**
31
 * Task for setting properties from an XML file in buildfiles.
32
 *
33
 * @author  Jonathan Bond-Caron <[email protected]>
34
 * @package phing.tasks.ext
35
 * @since   2.4.0
36
 * @link    http://ant.apache.org/manual/CoreTasks/xmlproperty.html
37
 */
38
class XmlPropertyTask extends PropertyTask
39
{
40
    private $keepRoot = true;
41
    private $collapseAttr = false;
42
    private $delimiter = ',';
43
44
    /**
45
     * Keep the xml root tag as the first value in the property name
46
     *
47
     * @param bool $yesNo
48
     */
49
    public function setKeepRoot(bool $yesNo)
50
    {
51
        $this->keepRoot = $yesNo;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function getKeepRoot()
58
    {
59
        return $this->keepRoot;
60
    }
61
62
    /**
63
     * Treat attributes as nested elements.
64
     *
65
     * @param bool $yesNo
66
     */
67
    public function setCollapseAttributes(bool $yesNo)
68
    {
69
        $this->collapseAttr = $yesNo;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function getCollapseAttributes()
76
    {
77
        return $this->collapseAttr;
78
    }
79
80
    /**
81
     * Delimiter for splitting multiple values.
82
     *
83
     * @param string $d
84
     */
85
    public function setDelimiter($d)
86
    {
87
        $this->delimiter = $d;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getDelimiter()
94
    {
95
        return $this->delimiter;
96
    }
97
98
    /**
99
     * set the property in the project to the value.
100
     * if the task was give a file or env attribute
101
     * here is where it is loaded
102
     */
103
    public function main()
104
    {
105
        if ($this->file === null) {
106
            throw new BuildException("You must specify file to load properties from", $this->getLocation());
107
        }
108
109
        $props = $this->loadFile($this->file);
110
        $this->addProperties($props);
111
    }
112
113
    /**
114
     * load properties from an XML file.
115
     *
116
     * @param  File $file
117
     * @return Properties
118
     *@throws BuildException
119
     */
120
    protected function loadFile(File $file)
121
    {
122
        $this->log("Loading " . $file->getAbsolutePath(), Project::MSG_INFO);
123
        try { // try to load file
124
            if ($file->exists()) {
125
                $parser = new XmlFileParser();
126
                $parser->setCollapseAttr($this->collapseAttr);
127
                $parser->setKeepRoot($this->keepRoot);
128
                $parser->setDelimiter($this->delimiter);
129
130
                $properties = $parser->parseFile($file);
131
132
                return new Properties($properties);
133
            }
134
135
            if ($this->getRequired()) {
136
                throw new BuildException("Could not load required properties file.");
137
            }
138
139
            $this->log(
140
                "Unable to find property file: " . $file->getAbsolutePath() . "... skipped",
141
                Project::MSG_WARN
142
            );
143
        } catch (IOException $ioe) {
144
            throw new BuildException("Could not load properties from file.", $ioe);
145
        }
146
    }
147
}
148