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
 * Text element points to a file or contains text.
22
 *
23
 * @package phing.tasks.system.AppendTask
24
 */
25
class TextElement extends ProjectComponent
26
{
27
    public $value = "";
28
    public $trimLeading = false;
29
    public $trim = false;
30
    public $filtering = true;
31
    public $encoding = null;
32

33
    /**
34
     * whether to filter the text in this element
35
     * or not.
36
     *
37
     * @param filtering true if the text should be filtered.
38
     *                  the default value is true.
39
     */
40 1
    public function setFiltering($filtering)
41
    {
42 1
        $this->filtering = $filtering;
43
    }
44

45
    /**
46
     * The encoding of the text element
47
     *
48
     * @param string $encoding the name of the charset used to encode
49
     */
50 0
    public function setEncoding($encoding)
51
    {
52 0
        $this->encoding = $encoding;
53
    }
54

55
    /**
56
     * set the text using a file
57
     *
58
     * @param  PhingFile $file the file to use
59
     * @throws BuildException if the file does not exist, or cannot be
60
     *                        read
61
     */
62 1
    public function setFile(PhingFile $file)
63
    {
64
        // non-existing files are not allowed
65 1
        if (!$file->exists()) {
66 0
            throw new BuildException("File " . $file . " does not exist.");
67
        }
68

69 1
        $reader = null;
70
        try {
71 1
            if ($this->encoding == null) {
72 1
                $reader = new BufferedReader(new FileReader($file));
73
            } else {
74 0
                $reader = new BufferedReader(
75 0
                    new InputStreamReader(new FileInputStream($file))
76
                );
77
            }
78 1
            $this->value = $reader->read();
79 0
        } catch (IOException $ex) {
80 0
            $reader->close();
81 0
            throw new BuildException($ex);
82
        }
83 1
        $reader->close();
84
    }
85

86
    /**
87
     * set the text using inline
88
     *
89
     * @param string $value the text to place inline
90
     */
91 1
    public function addText($value)
92
    {
93 1
        $this->value .= $this->getProject()->replaceProperties($value);
94
    }
95

96
    /**
97
     * s:^\s*:: on each line of input
98
     *
99
     * @param bool $trimLeading if true do the trim
100
     */
101 0
    public function setTrimLeading($trimLeading)
102
    {
103 0
        $this->trimLeading = $trimLeading;
104
    }
105

106
    /**
107
     * whether to call text.trim()
108
     *
109
     * @param bool $trim if true trim the text
110
     */
111 1
    public function setTrim($trim)
112
    {
113 1
        $this->trim = $trim;
114
    }
115

116
    /**
117
     * @return string the text, after possible trimming
118
     */
119 1
    public function getValue()
120
    {
121 1
        if ($this->value == null) {
122 0
            $this->value = "";
123
        }
124 1
        if (trim($this->value) === '') {
125 0
            $this->value = "";
126
        }
127 1
        if ($this->trimLeading) {
128 0
            $current = str_split($this->value);
129 0
            $b = '';
130 0
            $startOfLine = true;
131 0
            $pos = 0;
132 0
            while ($pos < count($current)) {
133 0
                $ch = $current[$pos++];
134 0
                if ($startOfLine) {
135 0
                    if ($ch == ' ' || $ch == "\t") {
136 0
                        continue;
137
                    }
138 0
                    $startOfLine = false;
139
                }
140 0
                $b .= $ch;
141 0
                if ($ch == "\n" || $ch == "\r") {
142 0
                    $startOfLine = true;
143
                }
144
            }
145 0
            $this->value = $b;
146
        }
147 1
        if ($this->trim) {
148 1
            $this->value = trim($this->value);
149
        }
150 1
        return $this->value;
151
    }
152
}

Read our documentation on viewing source code .

Loading