summaryrefslogtreecommitdiffstats
path: root/paste/include/geshi/scripts/get-keywords/lib/class.keywordgetter.php
blob: 8e1f3af7e2e91952c8f316bf222999ea6885b563 (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
 * GeSHi - Generic Syntax Highlighter
 * ----------------------------------
 * 
 * For information on how to use GeSHi, please consult the documentation
 * found in the docs/ directory, or online at http://geshi.org/docs/
 * 
 *  This file is part of GeSHi.
 *
 *  GeSHi is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  GeSHi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GeSHi; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * You can view a copy of the GNU GPL in the COPYING file that comes
 * with GeSHi, in the docs/ directory.
 *
 * @package   scripts
 * @author    Nigel McNie <nigel@geshi.org>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright (C) 2005 Nigel McNie
 * @version   1.1.0
 * 
 */

/** The language to get keywords for is not supported by this tool */
define('LANG_NOT_SUPPORTED', -1);
/** The keyword group asked for is not supported by the language */
define('INVALID_KEYWORD_GROUP', -2);
/** There was a parse error parsing an XML document for keywords */
define('PARSE_ERROR', -3);

/**#@+
 * @access private
 */
/** The strategy object used to get keywords is invalid */
define('INVALID_STRATEGY', -4);
/**#@-*/

/** Class that handles errors */
require_once 'class.keywordgettererror.php';

/** Class that is the root of the keyword getting strategy */
require_once 'class.keywordgetterstrategy.php';

/**
 * The KeywordGetter class. The get-keywords program is a
 * frontend to this class.
 * 
 * @author Nigel McNie
 * @since  0.1.0
 */
class KeywordGetter
{
    /**#@+
     * @access private
     */
    /**
     * The language being used
     * @var string
     */
    var $_language;
    
    /**
     * The keyword strategy to use
     * @var KeywordGetterStrategy
     */
    var $_keywordStrategy;
    /**#@-*/
    
    /**
     * Constructor
     * 
     * @param KeywordGetterStrategy The strategy to use to get keywords
     * @private
     * {@internal Yes, that's right, PRIVATE. Use KeywordGetter::factory
     * to create new KeywordGetters}}
     */
    function KeywordGetter ($kwstrategy)
    {
        if (!is_a($kwstrategy, 'KeywordGetterStrategy')) {
            return new KeywordGetterError(INVALID_STRATEGY, $this->_language);
        }
        $this->_keywordStrategy = $kwstrategy;
    }
    
    /**
     * Creates a new keyword getter based on the incoming language
     * 
     * @param string The language to get keywords for
     * @static
     */
    function &factory ($language)
    {
        if (!file_exists('languages/' . $language . '/class.' . $language . 'keywordgetter.php')) {
            return new KeywordGetterError(LANG_NOT_SUPPORTED, $language);
        }
        $this->_language = $language;
        
        /** Get the requested language */
        require_once 'languages/' . $language . '/class.' . $language . 'keywordgetter.php';
        
        $class = $language . 'KeywordGetterStrategy';
        if (!class_exists($class)) {
            return new KeywordGetterError(LANG_NOT_SUPPORTED, $language);
        }
        
        return new KeywordGetter(new $class);
    }
    
    /**
     * Returns whether the passed object is an error object
     * 
     * @param  mixed   The variable that may be an error object
     * @return boolean Whether the variable is an error object
     * @static
     */
    function isError ($possible_err_object)
    {
        return is_a($possible_err_object, 'KeywordGetterError');
    }
    
    /**
     * Gets the keywords for a language
     * 
     * @param  The keyword group to get keywords for
     * @return array An array of the keywords for this language/keyword group
     */
    function &getKeywords ($keyword_group)
    {
        return $this->_keywordStrategy->getKeywords($keyword_group);
    }
    
    /**
     * Gets valid keyword groups for a language
     * 
     * @return array An array of valid keyword groups for the language
     *               that this KeywordGetter is representing
     */
    function &getValidKeywordGroups ()
    {
        return $this->_keywordStrategy->getValidKeywordGroups();
    }
    
    /**
     * Gets a list of all supported languages
     * 
     * @return array
     * @static
     */
    function &getSupportedLanguages ()
    {
        $files_to_ignore = array('.', '..', 'CVS');
        $supported_languages = array();
        
        $dh = @opendir('languages');
        if (false === $dh) {
            return false;
        }
        
        while (false !== ($file = @readdir($dh))) {
            if (in_array($file, $files_to_ignore)) {
                continue;
            }
            if (file_exists('languages/' . $file . '/class.' . $file . 'keywordgetter.php')) {
                array_push($supported_languages, $file);
            }
        }
        
        return $supported_languages;
    }
}
    
?>