Passed
Push — main ( 66554c...a9ed76 )
by Michiel
16:48
created

XmlPropertyTask::setKeepRoot()   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 1
crap 2
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Task\System;
22
23
use Phing\Exception\BuildException;
24
use Phing\Io\File;
25
use Phing\Io\IOException;
26
use Phing\Io\XmlFileParser;
27
use Phing\Project;
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
 *
35
 * @since   2.4.0
36
 * @see    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
    public function setKeepRoot(bool $yesNo)
48
    {
49
        $this->keepRoot = $yesNo;
50
    }
51
52
    /**
53
     * @return bool
54
     */
55
    public function getKeepRoot()
56
    {
57
        return $this->keepRoot;
58
    }
59
60
    /**
61
     * Treat attributes as nested elements.
62
     */
63
    public function setCollapseAttributes(bool $yesNo)
64
    {
65
        $this->collapseAttr = $yesNo;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    public function getCollapseAttributes()
72
    {
73
        return $this->collapseAttr;
74
    }
75
76
    /**
77
     * Delimiter for splitting multiple values.
78
     *
79
     * @param string $d
80
     */
81
    public function setDelimiter($d)
82
    {
83
        $this->delimiter = $d;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getDelimiter()
90
    {
91
        return $this->delimiter;
92
    }
93
94
    /**
95
     * set the property in the project to the value.
96
     * if the task was give a file or env attribute
97
     * here is where it is loaded.
98
     */
99 1
    public function main()
100
    {
101 1
        if (null === $this->file) {
102
            throw new BuildException('You must specify file to load properties from', $this->getLocation());
103
        }
104
105 1
        $props = $this->loadFile($this->file);
106 1
        $this->addProperties($props);
107
    }
108
109
    /**
110
     * load properties from an XML file.
111
     *
112
     * @throws BuildException
113
     *
114
     * @return Properties
115
     */
116 1
    protected function loadFile(File $file)
117
    {
118 1
        $this->log('Loading ' . $file->getAbsolutePath(), Project::MSG_INFO);
119
120
        try { // try to load file
121 1
            if ($file->exists()) {
122 1
                $parser = new XmlFileParser();
123 1
                $parser->setCollapseAttr($this->collapseAttr);
124 1
                $parser->setKeepRoot($this->keepRoot);
125 1
                $parser->setDelimiter($this->delimiter);
126
127 1
                $properties = $parser->parseFile($file);
128
129 1
                return new Properties($properties);
130
            }
131
132
            if ($this->getRequired()) {
133
                throw new BuildException('Could not load required properties file.');
134
            }
135
136
            $this->log(
137
                'Unable to find property file: ' . $file->getAbsolutePath() . '... skipped',
138
                Project::MSG_WARN
139
            );
140
        } catch (IOException $ioe) {
141
            throw new BuildException('Could not load properties from file.', $ioe);
142
        }
143
144
        return null;
145
    }
146
}
147