Passed
Push — main ( e354fe...c764f7 )
by Michiel
17:39
created

src/Phing/Task/System/XmlPropertyTask.php (1 issue)

Severity
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
        if ($props) {
0 ignored issues
show
$props is of type Phing\Util\Properties, thus it always evaluated to true.
Loading history...
107 1
            $this->addProperties($props);
108
        }
109
    }
110
111
    /**
112
     * load properties from an XML file.
113
     *
114
     * @throws BuildException
115
     *
116
     * @return Properties
117
     */
118 1
    protected function loadFile(File $file)
119
    {
120 1
        $this->log('Loading ' . $file->getAbsolutePath(), Project::MSG_INFO);
121
122
        try { // try to load file
123 1
            if ($file->exists()) {
124 1
                $parser = new XmlFileParser();
125 1
                $parser->setCollapseAttr($this->collapseAttr);
126 1
                $parser->setKeepRoot($this->keepRoot);
127 1
                $parser->setDelimiter($this->delimiter);
128
129 1
                $properties = $parser->parseFile($file);
130
131 1
                return new Properties($properties);
132
            }
133
134
            if ($this->getRequired()) {
135
                throw new BuildException('Could not load required properties file.');
136
            }
137
138
            $this->log(
139
                'Unable to find property file: ' . $file->getAbsolutePath() . '... skipped',
140
                Project::MSG_WARN
141
            );
142
        } catch (IOException $ioe) {
143
            throw new BuildException('Could not load properties from file.', $ioe);
144
        }
145
146
        return null;
147
    }
148
}
149