This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
forked from Mac2/libiwparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserBaseC.php
320 lines (260 loc) · 9.87 KB
/
ParserBaseC.php
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet some
* day, and you think this stuff is worth it, you can buy me a beer in return.
* Benjamin Wöster
* ----------------------------------------------------------------------------
*/
/**
* @author Benjamin Wöster <[email protected]>
* @author Mac <[email protected]>
* @package libIwParsers
* @subpackage parsers
*/
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* Base class for parsers
*
* This class provides attributes and methods, needed by many concrete
* parsers. By collecting them in this base class, we reduce redundant
* code.
*/
class ParserBaseC extends ParserFunctionC
{
const ALL_WHITESPACE = 'ALL_WHITESPACE';
const LEADING_WHITESPACE = 'LEADING_WHITESPACE';
const TRAILING_WHITESPACE = 'TRAILING_WHITESPACE';
/**
* @var string identifier of the concrete parser
*/
private $_strIdentifier = '';
/**
* @var string identifier of the concrete parser
*/
private $_strName = '';
/**
* @var string regExp, checking if the concrete parser is able to parse the
* text provided.
*/
private $_reCanParseText = '';
/**
* @var string regExp, finding the begin of the data section
*/
private $_reBeginData = '';
/**
* @var string regExp, finding the end of the data section
*/
private $_reEndData = '';
/**
* @var string text to be parsed
*/
private $_strText = '';
/////////////////////////////////////////////////////////////////////////////
/**
* protected constructor
*
* As this class isn't meant to do anything on its own,
* we protect it from beeing instantiated.
*
* Only deriving classes shall be able to instantiate it.
*/
protected function __construct()
{
}
/////////////////////////////////////////////////////////////////////////////
/**
* Strips useless boiler plate text
*
* Many pages of IceWars contain data, that is simply of no use for us.
* By using _reBeginData and _reEndData regular expressions, we can strip
* down the text to the data sections.
*/
protected function stripTextToData()
{
if ($this->getText() === '') {
return;
}
$reStartData = $this->getRegExpStartData();
$reEndData = $this->getRegExpEndData();
$text = '';
if (!empty($reStartData)) {
$aStart = preg_split($reStartData, $this->getText());
for ($n = 1; $n < count($aStart); $n++) { //! bei Mehrfach-Berichten (Universum, Highscore, etc) alle verarbeiten
if (isset($aStart[$n])) {
if (!empty($reEndData)) {
$aEnd = preg_split($reEndData, $aStart[$n]); //! jeden Bericht einzeln auf ein Ende prüfen
if (isset($aEnd[0])) {
$text .= trim($aEnd[0]);
}
} else {
$text .= $aStart[$n];
}
}
}
$this->setText($text);
}
return;
}
/////////////////////////////////////////////////////////////////////////////
/**
* @see ParserI::canParseText()
*/
public function canParseText($text)
{
$retVal = preg_match($this->getRegExpCanParseText(), $text, $aMatches);
switch ($retVal) {
case 0:
return false;
break;
case $retVal > 0:
$this->setText($text);
return true;
break;
case false:
$e = get_class($this) . '::canParseText - ERROR in preg_match...';
throw new Exception($e);
break;
}
}
/////////////////////////////////////////////////////////////////////////////
/**
* @see ParserI::getIdentifier()
*/
public function getIdentifier()
{
return $this->_strIdentifier;
}
/////////////////////////////////////////////////////////////////////////////
/**
* @see ParserI::getName()
*/
public function getName()
{
return $this->_strName;
}
/////////////////////////////////////////////////////////////////////////////
protected function getRegExpCanParseText()
{
return $this->_reCanParseText;
}
/////////////////////////////////////////////////////////////////////////////
protected function getRegExpStartData()
{
return $this->_reBeginData;
}
/////////////////////////////////////////////////////////////////////////////
protected function getRegExpEndData()
{
return $this->_reEndData;
}
/////////////////////////////////////////////////////////////////////////////
protected function getText()
{
return $this->_strText;
}
/////////////////////////////////////////////////////////////////////////////
protected function setIdentifier($value)
{
$this->_strIdentifier = PropertyValueC::ensureString($value);
}
/////////////////////////////////////////////////////////////////////////////
protected function setName($value)
{
$this->_strName = PropertyValueC::ensureString($value);
}
/////////////////////////////////////////////////////////////////////////////
protected function setRegExpCanParseText($value)
{
$this->_reCanParseText = PropertyValueC::ensureString($value);
}
/////////////////////////////////////////////////////////////////////////////
protected function setRegExpBeginData($value)
{
$this->_reBeginData = PropertyValueC::ensureString($value);
}
/////////////////////////////////////////////////////////////////////////////
protected function setRegExpEndData($value)
{
$this->_reEndData = PropertyValueC::ensureString($value);
}
/////////////////////////////////////////////////////////////////////////////
/**
* @todo I'm not sure about all these utf8 replacements,
* can you modify them to use utf8_encode/utf8_decode?
* Because eclipse/svn/dontknowwhat mixes them up all
* the time, and I don't want to break your tool!
*/
public function setText($value)
{
//replace different line endings by \n
$value = $this->cleanupLineEndings($value);
$replacements = array(
'blubbernde Gallertmasse' => 'Bevölkerung',
'Erdbeermarmelade' => 'Stahl',
'Erdbeerkonfitüre' => 'VV4A',
'Erdbeeren' => 'Eisen',
'Brause' => 'chem. Elemente',
'Vanilleeis' => 'Eis',
'Eismatsch' => 'Wasser',
'Traubenzucker' => 'Energie',
' Kekse ' => ' Credits ',
//! Mac: Workaround um nicht Teile eines Wortes/Gebäudenamens zu ersetzen
'Systrans (Systransporter Klasse 1)' => 'Systrans (Systemtransporter Klasse 1)',
'Lurch (Systransporter Klasse 2)' => 'Lurch (Systemtransporter Klasse 2)',
'Crux (Systransporter Kolonisten)' => 'Crux (Systemtransporter Kolonisten)',
'Pflaumenmus (kleiner Carrier)' => 'Pflaumenmus (Carrier)',
//! Mac: nötig, da Forschung anders heißt als das Schiff
'\\\%' => '\%',
);
$value = str_replace(
array_keys($replacements),
array_values($replacements),
$value
);
//undo magic quotes
if (ini_get("magic_quotes_gpc") == 1) {
$value = stripslashes($value);
}
//set text
$this->_strText = PropertyValueC::ensureString($value);
}
/////////////////////////////////////////////////////////////////////////////
protected function cleanupLineEndings($text)
{
//replace different line endings by \n (linux)
$replacements = array(
chr(13) . chr(10) => chr(10), //windows
chr(13) => chr(10), //mac
);
return str_replace(array_keys($replacements), array_values($replacements), $text);
}
/////////////////////////////////////////////////////////////////////////////
/**
* Strips whitespaces from lines.
*
* @param string $text the text whos lines shall be striped
* @param string $option one of ParserBaseC::XYZ_WHITESPACE constants
*
* @return string the striped text
*/
protected function stripWhitespace($text, $option = ParserBaseC::ALL_WHITESPACE)
{
switch ($option) {
case ParserBaseC::LEADING_WHITESPACE:
$retVal = rtrim($text);
break;
case ParserBaseC::TRAILING_WHITESPACE:
$retVal = ltrim($text);
break;
case ParserBaseC::ALL_WHITESPACE:
default:
$retVal = trim($text);
}
return $retVal;
}
}