Menu

[r312]: / trunk / src / PHPCheckstyle / Config / CheckXMLStyleConfig.php  Maximize  Restore  History

Download this file

200 lines (174 with data), 5.5 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
<?php
namespace PHPCheckstyle\Config;
/**
* Loads the test configuration.
*
* @author Hari Kodungallur <hkodungallur@spikesource.com>
* @SuppressWarnings checkUnusedPrivateFunctions
*/
class CheckXMLStyleConfig extends CheckStyleConfig {
// The configuration file
private $file;
private $currentTest = false;
private $currentConfig = false;
private $xmlParser;
/**
* Constructor.
*
* @param String $configFile
* The path of the config file
*/
public function __construct($configFile) {
// If the path is a valid file we use it as is
if (is_file($configFile)) {
$this->file = $configFile;
} else {
// Otherwise we look in the config directory
$this->file = PHPCHECKSTYLE_HOME_DIR . "/config/" . $configFile;
if (!is_file($this->file)) {
echo "Config file not found : " . $configFile;
exit(0);
}
}
$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.
*/
public function __destruct() {
if ($this->xmlParser !== null) {
xml_parser_free($this->xmlParser);
}
}
/**
* Parses the configuration file and stores the values.
*
* @throws Exception if cannot access file
*/
public function parse() {
$fp = fopen($this->file, "r");
if (!$fp) {
throw new Exception("Could not open XML input file");
}
$data = fread($fp, 4096);
while ($data) {
if (!xml_parse($this->xmlParser, $data, feof($fp))) {
$errorString = xml_error_string(xml_get_error_code($this->xmlParser));
$errorLineNo = xml_get_current_line_number($this->xmlParser);
$msg = sprintf("Warning: XML error: %s at line %d", $errorString, $errorLineNo);
echo $msg;
$this->config = array();
}
$data = fread($fp, 4096);
}
}
/**
* SAX function indicating start of an element
* Store the TEST and PROPERTY values in an array
*
* @param Parser $parser
* the parser
* @param Elem $elem
* name of element
* @param Attributes $attrs
* list of attributes of the element
* @SuppressWarnings cyclomaticComplexity checkUnusedFunctionParameters
*/
private function _startElement($parser, $elem, $attrs) {
switch ($elem) {
// Case of a configuration property
case 'CONFIG':
$this->currentConfig = strtolower($attrs['NAME']);
$this->config[$this->currentConfig] = array();
break;
// Case of a configuration property item
case 'CONFIGITEM':
$this->config[$this->currentConfig][] = $attrs['VALUE'];
break;
// Case of a test rule
case 'TEST':
$this->currentTest = strtolower($attrs['NAME']);
$this->config[$this->currentTest] = array();
if (isset($attrs['LEVEL'])) {
$this->config[$this->currentTest]['level'] = $attrs['LEVEL'];
}
if (isset($attrs['REGEXP'])) {
$this->config[$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->config[$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->config[$this->currentTest]['item'][] = $attrs['VALUE'];
}
break;
// Case of an exception to a rule
case 'EXCEPTION':
if (isset($attrs['VALUE'])) {
$this->config[$this->currentTest]['exception'][] = $attrs['VALUE'];
}
break;
// Case of a deprecated function
case 'DEPRECATED':
if (isset($attrs['OLD'])) {
$this->config[$this->currentTest][strtolower($attrs['OLD'])]['old'] = $attrs['OLD'];
}
if (isset($attrs['NEW'])) {
$this->config[$this->currentTest][strtolower($attrs['OLD'])]['new'] = $attrs['NEW'];
}
if (isset($attrs['VERSION'])) {
$this->config[$this->currentTest][strtolower($attrs['OLD'])]['version'] = $attrs['VERSION'];
}
break;
// Case of an alias function
case 'ALIAS':
if (isset($attrs['OLD'])) {
$this->config[$this->currentTest][strtolower($attrs['OLD'])]['old'] = $attrs['OLD'];
}
if (isset($attrs['NEW'])) {
$this->config[$this->currentTest][strtolower($attrs['OLD'])]['new'] = $attrs['NEW'];
}
break;
// Case of a replacement
case 'REPLACEMENT':
if (isset($attrs['OLD'])) {
$this->config[$this->currentTest][strtolower($attrs['OLD'])]['old'] = $attrs['OLD'];
}
if (isset($attrs['NEW'])) {
$this->config[$this->currentTest][strtolower($attrs['OLD'])]['new'] = $attrs['NEW'];
}
break;
default:
break;
}
}
/**
* SAX function indicating end of element
* Currently we dont need to do anything here
*
* @param Parser $parser
* @param String $name
*/
private function _endElement($parser, $name) {}
/**
* SAX function for processing CDATA
* Currently we dont need to do anything here
*
* @param Parser $parser
* @param String $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.