Menu

[r55]: / trunk / PhpCheckstyle / src / CheckStyleConfig.php  Maximize  Restore  History

Download this file

293 lines (255 with data), 7.7 kB

  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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/*
* $Id: CheckStyleConfig.php 28215 2005-07-28 02:53:05Z hkodungallur $
*
* Copyright(c) 2004-2005, SpikeSource Inc. All Rights Reserved.
* Licensed under the Open Source License version 2.1
* (See http://www.spikesource.com/license.html)
*/
if (!defined("PHPCHECKSTYLE_HOME_DIR")) {
define("PHPCHECKSTYLE_HOME_DIR", dirname(__FILE__)."/..");
}
/**
* Loads the user specified test configuration
*
* @author Hari Kodungallur <hkodungallur@spikesource.com>
* @version $Revision: $
*/
class CheckStyleConfig {
private $file;
// Array that contains the loaded checks
public $_myConfig = array();
private $_currentTest = false;
private $_currentConfig = false;
private $_xmlParser;
/**
* constructor
*
* @access public
*/
public function CheckStyleConfig() {
$this->file = PHPCHECKSTYLE_HOME_DIR."/config/".CONFIG_FILE;
$this->_xmlParser = xml_parser_create();
xml_set_object($this->_xmlParser, $this);
xml_set_element_handler($this->_xmlParser, "_startElement", "_endElement");
xml_set_character_data_handler($this->_xmlParser, "_gotCdata");
xml_set_default_handler($this->_xmlParser, "_gotCdata");
}
/**
* destructor
*
* @return
* @access public
*/
public function __destruct() {
xml_parser_free($this->_xmlParser);
}
/**
* parses the configuration file and stores the values
*
* @return none
* @access public
*/
public function parse() {
// example from php.net
$fp = fopen($this->file, "r");
if (!$fp) {
die("Could not open XML input file");
}
$data = fread($fp, 4096);
while ($data) {
if (!xml_parse($this->_xmlParser, $data, feof($fp))) {
$msg = sprintf("Warning: XML error: %s at line %d", xml_error_string(xml_get_error_code($this->_xmlParser)), xml_get_current_line_number($this->_xmlParser));
echo $msg;
$this->_myConfig = array();
}
$data = fread($fp, 4096);
}
}
/**
* Return a list of items associed with a test.
*
* @param $test name of the test
* @return array the list of items for this test.
*/
public function getTestItems($test) {
$test = strtolower($test);
return isset($this->_myConfig[$test]['item']) ? $this->_myConfig[$test]['item'] : false;
}
/**
* Return a list of items associed with a configuration.
*
* @param $config name of the config
* @return array the list of items for this config.
*/
public function getConfigItems($config) {
return $this->_myConfig[strtolower($config)];
}
/**
* Return a list of exceptionfor a test.
*
* @param $test name of the test
* @return array the list of exceptions for this test.
*/
public function getTestExceptions($test) {
$test = strtolower($test);
return isset($this->_myConfig[$test]['exception']) ? $this->_myConfig[$test]['exception'] : false;
}
/**
* Return a true if the test exist, false otherwise.
*
* @param $test name of the test
* @return boolean true if test exists.
*/
public function getTest($test) {
$test = strtolower($test);
return (array_key_exists($test, $this->_myConfig));
}
/**
* Return the level of severity of a test.
*
* @param $test name of the test
* @return the level of severity.
*/
public function getTestLevel($test) {
$test = strtolower($test);
$ret = WARNING;
if (array_key_exists($test, $this->_myConfig) && array_key_exists('level', $this->_myConfig[$test])) {
$ret = $this->_myConfig[$test]['level'];
}
if ($ret != ERROR && $ret != IGNORE && $ret != INFO && $ret != WARNING) {
echo "Invalid level for test ".$test." : ".$ret;
$ret = WARNING;
}
return $ret;
}
/**
* Return the regular expression linked to the test.
*
* @param $test name of the test
* @return the regular expression.
*/
public function getTestRegExp($test) {
$test = strtolower($test);
$ret = "";
if (array_key_exists($test, $this->_myConfig) && array_key_exists('regexp', $this->_myConfig[$test])) {
$ret = $this->_myConfig[$test]['regexp'];
}
return $ret;
}
/**
* Return the list of deprecated method and their replacement.
*
* @param $test name of the test
* @return the list of depecated values.
*/
public function getTestDeprecations($test) {
$test = strtolower($test);
$ret = "";
if (array_key_exists($test, $this->_myConfig)) {
$ret = $this->_myConfig[$test];
}
return $ret;
}
/**
* Return the value of a property
*
* @param $test name of the test
* @param $property name of the property
* @return the value.
*/
public function getTestProperty($test, $property) {
$test = strtolower($test);
$property = strtolower($property);
if (array_key_exists($test, $this->_myConfig) && array_key_exists($property, $this->_myConfig[$test])) {
return $this->_myConfig[$test][$property];
} else {
return false;
}
}
/**
* SAX function indicating start of an element
* Store the TEST and PROPERTY values in an array
*
* @param $parser the parser
* @param $elem name of element
* @param $attrs list of attributes of the element
*/
private function _startElement($parser, $elem, $attrs) {
switch ($elem) {
// Case of a configuration property
case 'CONFIG':
$this->_currentConfig = strtolower($attrs['NAME']);
$this->_myConfig[$this->_currentConfig] = array();
break;
// Case of a configuration property item
case 'CONFIGITEM':
$this->_myConfig[$this->_currentConfig][] = $attrs['VALUE'];
break;
// Case of a test rule
case 'TEST':
$this->_currentTest = strtolower($attrs['NAME']);
$this->_myConfig[$this->_currentTest] = array();
if (isset($attrs['LEVEL'])) {
$this->_myConfig[$this->_currentTest]['level'] = $attrs['LEVEL'];
}
if (isset($attrs['REGEXP'])) {
$this->_myConfig[$this->_currentTest]['regexp'] = $attrs['REGEXP'];
}
break;
// Case of a propertie of a rule (name / value)
case 'PROPERTY':
$pname = $attrs['NAME'];
$pval = true;
if (array_key_exists('VALUE', $attrs)) {
$pval = $attrs['VALUE'];
}
$this->_myConfig[$this->_currentTest][strtolower($pname)] = $pval;
break;
// Case of a item of a list of values of a rule
case 'ITEM':
if (isset($attrs['VALUE'])) {
$this->_myConfig[$this->_currentTest]['item'][] = $attrs['VALUE'];
}
break;
// Case of an exception to a rule
case 'EXCEPTION':
if (isset($attrs['VALUE'])) {
$this->_myConfig[$this->_currentTest]['exception'][] = $attrs['VALUE'];
}
break;
// Case of a deprecated function
case 'DEPRECATED':
if (isset($attrs['OLD'])) {
$this->_myConfig[$this->_currentTest][strtolower($attrs['OLD'])]['old'] = $attrs['OLD'];
}
if (isset($attrs['NEW'])) {
$this->_myConfig[$this->_currentTest][strtolower($attrs['OLD'])]['new'] = $attrs['NEW'];
}
if (isset($attrs['VERSION'])) {
$this->_myConfig[$this->_currentTest][strtolower($attrs['OLD'])]['version'] = $attrs['VERSION'];
}
break;
default:
break;
}
}
/**
* SAX function indicating end of element
* Currenlty we dont need to do anything here
*
* @param $parser
* @param $name
*/
private function _endElement($parser, $name) {
}
/**
* SAX function for processing CDATA
* Currenlty we dont need to do anything here
*
* @param $parser
* @param $name
*/
private function _gotCdata($parser, $name) {
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.