summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/types/TokenSource.php
blob: c073ece0b6148ae5fa8a2bfb2f426231d53caf94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/*
 *  $Id: TokenSource.php,v 1.7 2004/03/18 20:44:26 hlellelid Exp $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
*/

require_once 'phing/types/DataType.php';
include_once 'phing/util/StringHelper.php';

/**
 * A parameter is composed of a name, type and value.
 *
 * Example of usage:
 *
 * <replacetokens>
 *   <tokensource classname="phing.filters.util.IniFileTokenReader">
 *     <!-- all params for the TokenReader here -->
 *     <param name="file" value="tokens.ini" />
 *   </tokensource>
 * </replacetokens>
 *
 * or:
 * 
 * <filterreader classname="phing.filters.ReplaceTokens">
 *   <param type="tokensource>
 *     <param name="classname" value="phing.filters.util.IniFileTokenReader" />
 *     <param name="file" value="tokens.ini" />
 *   </param>
 * </filterreader>
 *
 * @author    <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
 * @package   phing.types
 */
class TokenSource extends DataType {

    /**
     * String to hold the path to the TokenReader
     * @var     string
     */
    protected $classname = null;

    /**
     * Array holding parameters for the wrapped TokenReader.
     * @var array
     */
    protected $parameters = array();

    /**
     * Reference to the TokenReader used by this TokenSource
     * @var TokenReader
     */
    protected $reader;

    /**
     * Array with key/value pairs of tokens
     */
    protected $tokens = array();

    /**
     * This method is called to load the sources from the reader
     * into the buffer of the source.
     */
    function load() {
        // Create new Reader
        if ($this->classname === null) {
            throw new BuildException("No Classname given to TokenSource.");
        }
        
        $classname = Phing::import($this->classname);        
        $this->reader = new $classname($this->project);

        // Configure Reader
        $this->configureTokenReader($this->reader);

        // Load Tokens
        try {
            while ($token = $this->reader->readToken()) {
                $this->tokens[] = $token;
            }
        } catch (BuildException $e) {
            $this->log("Error reading TokenSource: " . $e->getMessage(), PROJECT_MSG_WARN);
        } catch (IOException $e) {
            $this->log("Error reading TokenSource: " . $e->getMessage(), PROJECT_MSG_WARN);
        }
    }

    /**
     * This function uses the wrapper to read the tokens and then
     * returns them.
     *
     * @access  public
     */
    function getTokens() {
        if ($this->tokens === null)
            $this->Load();

        return $this->tokens;
    }

    /**
     * Configures a TokenReader with the parameters passed to the
     * TokenSource.
     * @param TokenReader $reader
     */
    private function configureTokenReader(TokenReader $reader) {
        $count = count($this->parameters);
        for ($i = 0; $i < $count; $i++) {
            $method_name = "Set" . $this->parameters[$i]->getName();
            $value = $this->parameters[$i]->getValue();
            $reader->$method_name($value);
        }
    }
    
    /**
     * Set the classname (dot-path) to use for handling token replacement.
     * @param string $c
     */
    function setClassname($c) {
        $this->classname = $c;
    }
    
    /**
     * Returns the qualified classname (dot-path) to use for handling token replacement.
     * @return string
     */
    function getClassname() {
        return $this->classname;
    }

    /**
     * Create nested <param> tag.
     * Uses standard name/value Parameter class.
     * @return Parameter
     */
    function createParam() {
        $num = array_push($this->parameters, new Parameter());
        return $this->parameters[$num-1];
    }
}


?>