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
/**
21
 * Task for setting properties from an XML file in buildfiles.
22
 *
23
 * @author  Jonathan Bond-Caron <jbondc@openmv.com>
24
 * @package phing.tasks.ext
25
 * @since   2.4.0
26
 * @link    http://ant.apache.org/manual/CoreTasks/xmlproperty.html
27
 */
28
class XmlPropertyTask extends PropertyTask
29
{
30
    private $keepRoot = true;
31
    private $collapseAttr = false;
32
    private $delimiter = ',';
33

34
    /**
35
     * Keep the xml root tag as the first value in the property name
36
     *
37
     * @param bool $yesNo
38
     */
39 0
    public function setKeepRoot(bool $yesNo)
40
    {
41 0
        $this->keepRoot = $yesNo;
42
    }
43

44
    /**
45
     * @return bool
46
     */
47 0
    public function getKeepRoot()
48
    {
49 0
        return $this->keepRoot;
50
    }
51

52
    /**
53
     * Treat attributes as nested elements.
54
     *
55
     * @param bool $yesNo
56
     */
57 0
    public function setCollapseAttributes(bool $yesNo)
58
    {
59 0
        $this->collapseAttr = $yesNo;
60
    }
61

62
    /**
63
     * @return bool
64
     */
65 0
    public function getCollapseAttributes()
66
    {
67 0
        return $this->collapseAttr;
68
    }
69

70
    /**
71
     * Delimiter for splitting multiple values.
72
     *
73
     * @param string $d
74
     */
75 0
    public function setDelimiter($d)
76
    {
77 0
        $this->delimiter = $d;
78
    }
79

80
    /**
81
     * @return string
82
     */
83 0
    public function getDelimiter()
84
    {
85 0
        return $this->delimiter;
86
    }
87

88
    /**
89
     * set the property in the project to the value.
90
     * if the task was give a file or env attribute
91
     * here is where it is loaded
92
     */
93 0
    public function main()
94
    {
95 0
        if ($this->file === null) {
96 0
            throw new BuildException("You must specify file to load properties from", $this->getLocation());
97
        }
98

99 0
        $props = $this->loadFile($this->file);
100 0
        $this->addProperties($props);
101
    }
102

103
    /**
104
     * load properties from an XML file.
105
     *
106
     * @param  PhingFile $file
107
     * @throws BuildException
108
     * @return Properties
109
     */
110 0
    protected function loadFile(PhingFile $file)
111
    {
112 0
        $this->log("Loading " . $file->getAbsolutePath(), Project::MSG_INFO);
113
        try { // try to load file
114 0
            if ($file->exists()) {
115 0
                $parser = new XmlFileParser();
116 0
                $parser->setCollapseAttr($this->collapseAttr);
117 0
                $parser->setKeepRoot($this->keepRoot);
118 0
                $parser->setDelimiter($this->delimiter);
119

120 0
                $properties = $parser->parseFile($file);
121

122 0
                return new Properties($properties);
123
            }
124

125 0
            if ($this->getRequired()) {
126 0
                throw new BuildException("Could not load required properties file.");
127
            }
128

129 0
            $this->log(
130 0
                "Unable to find property file: " . $file->getAbsolutePath() . "... skipped",
131 0
                Project::MSG_WARN
132
            );
133 0
        } catch (IOException $ioe) {
134 0
            throw new BuildException("Could not load properties from file.", $ioe);
135
        }
136
    }
137
}

Read our documentation on viewing source code .

Loading